Driving a 4 Channel Relay Board With ESP8266-01 and MQTT and OpenHAB

Introduction: Driving a 4 Channel Relay Board With ESP8266-01 and MQTT and OpenHAB

I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit ...

A popular relay board that is available at the Chinese webstores is a 4 channel opto isolated relay module.
It is fairly easy to turn that into a WiFi controlled 4 channel relay with an ESP8266. Sure, a Lolin or Wemos board will do, but as we only need to control 4 channels, the simple ESP8266-01 suffices.

Both the ESP8266-01 and the relay board have their peculiarities, that in this case complement eachother. Let me explain:
The relay board I have in mind uses 4 opto isolators to control the the relay signals. I discussed the full workings of this board in in another post. For now however suffice to say the inputs need an active LOW to close the relays, while in rest, the inputs are HIGH.

The ESP-01is a bit particular with the pins it has as two of the 4 I/O pins are the pins that need to be pulled HIGH on startup. usually that's being done with 10k resistors, but here those can be omitted because pulling the GPIO pins is exactly what the relay board does

The remaining two pins of the ESP8266-01 are the UART. Those UART pins can be used as GPIO, but the problem is that they do not know that, they need to be told that they are no longer UART but in fact GPIO. We do that with a special pinMode command (more about that later).

Though the ESP8266-01 needs 3.3Volt, it is necessary to feed the board, including the optocoupler with 5Volt. The connections between the ESP8266-01 and the relay board are made as follows: A reminder: the relayboard needs 5Volt and the ESP-01 needs 3.3Volt. You could use an LDO like the AMS1117 3.3 to drop the 5Volt to 3.3 however,do not connect the two Vcc's directly. Wait, let me emphasize that: DO NOT CONNECT THE Vcc OF THE ESP TO THE VCC OF THE RELAY BORD, JUST DONT!!!!

The program

For the program I followed the MQTT structure that is used by computourist . The idea behind that is that commands going from MQTT broker to the node are called 'southbound' ('sb'), while the ones going to the broker (so usually the ' state') are called 'northbound' ('nb'). The specific functions in the node are addressed as numbered devices. This has advantages and disadvantages. The disadvantage being that you get codes like: "home/sb/node01/dev17" rather than something like: "home/cmd/wifirelay/relay1". Also the handling of the incoming MQTT is bound to a specified length, so altering it needs to be done with some consideration. The advantages are that the handling of the code is easier and in fact extending the code with more functions is fairly easy. What happens in fact is that once a subscribed MQTT code comes in, it is stripped to a its last two digits. These digits define the function it fulfills. The Payload can be "ON", "OFF", "READ", or a number, depending on the function chosen. The "READ" payload reads and returns the state of the specific function ('device') that is being read. Some 'devices' -as for instance the IP number or the MAC address, can only be read and not 'set'.

The full list of devices is as follows:

  • 00 uptime: read uptime in minutes
  • 01 interval: read/set transmission interval for push messages
  • 02 RSSI: read WiFi signal strength
  • 03 version: read software version
  • 05 ACK: read/set acknowledge message after a 'set' request
  • 10 IP: Read IP address
  • 11 SSID: Read SSID
  • 12 PW: Read Password
  • 16 read/set relay1 output
  • 17 read set relay2 output
  • 18 read set relay3 output
  • 19 read set relay4 output
  • 92 error: tx only: device not supported
  • 91 error: tx only: syntax error
  • 99 wakeup: tx only: first message sent on node startup

As I mentioned earlier, the UART pins need to be told that they should behave like GPIO pins. That can be done with the statement:

pinMode(1,FUNCTION_3);

pinMode(3,FUNCTION_3);

However, this will not work when there are any hardware serial statements left (such as Serial.print, Serial.begin).

In order to control the relays from e.g. OpenHAB, this is what you add to your itemsfile: Switch rel1 "WiFi relay 1 [%s]" <relay> (GF_Corridor) { mqtt="<[mosquitto:home/nb/node01/dev17:state:default],>[mosquitto:home/sb/node01/dev17:command:*:default]" } Switch rel2 "WiFi relay 2 [%s]" <relay> (GF_Corridor) { mqtt="<[mosquitto:home/nb/node01/dev18:state:default],>[mosquitto:home/sb/node01/dev18:command:*:default]" } Switch rel3 "WiFi relay 3 [%s]" <relay> (GF_Corridor) { mqtt="<[mosquitto:home/nb/node01/dev19:state:default],>[mosquitto:home/sb/node01/dev19:command:*:default]" } Switch rel4 "WiFi relay 4 [%s]" <relay> (GF_Corridor) { mqtt="<[mosquitto:home/nb/node01/dev16:state:default],>[mosquitto:home/sb/node01/dev16:command:*:default]" }

The program can be downloaded here. FYI, I used a program that was rather extensive and cut that down to size. I left a bit of code here and there that might not be of immediate use in this project, but may come in handy if you want to use the code on a Wemos. Cost:

  • relayboard 1.80 euro
  • ESP8266-01 1.45 euro
  • 5 and 3.3Volt module 0.40ct

If you are using a different relay board that does not have it's inputs pulled high in rest, then you need to add 10k pull-ups on GPIO 0 and GPIO2

The connections between the ESP8266-01 and the relayboard can be mde with Dupont cable (do not connct the Vcc's) or on a small PCB as shown below

Although there are various MQTT controlled relay boards of 1, 2,4 and even 8 channels available, the least of which is the 4channel SonOff, these come at a price. Though the 1channel versions are relatively cheap the 4 and 8 channel wifi relays are significantly more expensive.
Another 'problem' is that they usually have proprietary software and require a phone app to control them. reflashing them is not always straightforward as sometimes -eventhough the familiair ESP8266 is used- other chips might be involved. If you are interested in getting a cheap 1 channel WiFi relay, you may want to check the trouble Ralph Bacon had with a single relayboard from Aliexpress, or you may check my earlier DIY version of a single channel wifi relay.

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest

    Discussions