Simple WiFi relay board: a 4 channel DIY WiFi relay board (2)

Driving a 4 channel relay board with ESP8266-01 through MQTT


In a previous post I discussed making a WiFi relay inspired by the trouble  Ralph Bacon had with a single relayboard from Aliexpress. Given the fact that the Sonoff SV is relatively cheap (don’t forget the shipping cost though), and the WiFi relay at AliExpress even cheaper, it is a nickle and dime question whether it is wise to DIY such a project yourself. However, if you already have an unused ESP8266-01 and a relay lying around, basically all you need are some DuPont cables to connect the two (as shown in my previous post).

It becomes more interesting to use all 4 pins of an idle lying ESP8266-01 and add a 4 channel relay board.
The ESP8266-01 however is 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 whereas the remaining two pins 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).

The relayboard I had in mind is a bit peculiar. I discussed the full workings of this board in an earlier post.

For now however suffice to say the inputs need an active LOW to  close the relays, while in rest, the inputs are HIGH.
That is in fact quite handy when using an ESP8266-01, as two of the 4 pins, GPIO0 and GPIO2, need to be pulled high on start-up, something this circuit actually does. It is necessary though 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 full board
The ESP8266-01 adapter board

The program

For the program I followed the structure that is used by computourist with regard to the use of MQTT messages.
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)
So in order to control the relays from e.g. OpenHAB, this is what you add to your itemsfile:

Switch rel1 "WiFi relay 1 [%s]" (GF_Corridor) { mqtt="[mosquitto:home/sb/node01/dev17:command:*:default]" }
Switch rel2 "WiFi relay 2 [%s]" (GF_Corridor) { mqtt="[mosquitto:home/sb/node01/dev18:command:*:default]" }
Switch rel3 "WiFi relay 3 [%s]" (GF_Corridor) { mqtt="[mosquitto:home/sb/node01/dev19:command:*:default]" }
Switch rel4 "WiFi relay 4 [%s]" (GF_Corridor) { mqtt="[mosquitto:home/sb/node01/dev16:command:*:default]" }

this will render the following menu:

The program can be downloaded here. FYI, I trimmed down an existing, bigger program of mine. 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

Advertisements

4 thoughts on “Simple WiFi relay board: a 4 channel DIY WiFi relay board (2)”

  1. Very nice indeed!

    I have a two relay board like this here too for a project. Here too, optocoupler LED is wired to Vcc, so it needs active low. That is one of the things I do like about, errrm, “that other software”: you can set pins to inverted relay. Oh and to inverted PWM too. The latter is really nice for driving LEDs using a LM2596S based buck converter: enabled is active low.

    As for MQTT topics, I have standardized on Theo’s methodology: x/y/z, where x is cmnd (to make the device so something), stat (for the device to respond/confirm a state change) and tele for telemetry. y is the device name, and z is the action to perform, i.e. POWER1 (first relay). It suits me well and I have adopted it to my other projects such as mailbox, weather station , etc. Not saying it is better at all. It’s just good to use a consistent system, whatever the system is.

    1. Thanks Jeroen, the active low boards definitely do have their place in projects. Initially I disfavoured them, but for projects with the ESP8266 their High pulled entrance comes in handy.

      I try to settle for one MQTT system too and I kinda like the one from computourist in spite of some drawbacks but indeed, it is what one is used to or has a liking to. s long as you have a system it is fine I guess

  2. I just wanted at add that I just finished that 2 channel, Wemost D1 Mini based lookalike, which also includes two opto-isolated inputs. I would like to share two observations:
    1. Don’t make my mistake and use pin D8 (GPIO15) an output t the relay board. It needs to be pulled down at boot and the relay board pulls it up.
    2. Even while the ESP is running at 3.3 volt and the relay board is on the 5 volt, this runs fine. Even while there is still 1.7 volts over the relay board input when the output is high, that does not fire the relay. The 1.4 volt forward voltage of the opto-coupler plus the 0.6 volt if the series diode is (barely) enough to make that work.

    1. Thanks Jeroen, I am pretty sure I left a detailed reply earlier, but seems gone, ok just trying again:
      1) Very true, it needs to be kept low. That can be handy though for standard transistor driven relays with open basis that you want to be pulled LOW (inactive) at start up
      2) Correct, in a separate article, I have discussed the board. Not alone is the 5Volt fed optocoupler circuit safe for the 3V3 V GPIO pin, but in fact it needs to be fed with 5Volt for it to be able to switch off the relay

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.