Simple WiFi relay board (2): A 4 channel DIY WiFi relay board with ESP8266-01

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


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:

If you are using a different relay board that does not have it’s inputs pulled high in rest and you are using the ‘old’ ESP8266 module, then you need to add 10k pull-ups on GPIO 0 and GPIO2. Not necessary if you are using the ESP8266-01S.

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

Finally, put it in a nice enclosure

4 Channel relay housing
4 Channel relay housing

Software

MQTT
For theMQTT  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’). (with ‘southbound being a command and northbound being the status). 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).

Also, the device will not boot if you happen to pull pin 1 (Tx) down.
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.

WebServer

If you do not want to use MQTT, then a webserver might be what you are looking for. I adapted a program from Rui Santos (Original can be found here). to be suitable for use on an ESP8266-01.

Another possibility is to put Tasmota on it, or control it via Firebase.

Cost:

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

8 thoughts on “Simple WiFi relay board (2): A 4 channel DIY WiFi relay board with ESP8266-01”

  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

  3. Thanks for the wonderful article. I have exactly the same components like you have. The only problem is whenever I switch on the power for relay, the 4 channels getting powered for a second. How to prevent that?.

    1. is that just when you only power the board, or when you power up the ESP8266 as well?
      As the board is an active LOW, an activation is fairly easy to prevent, by pulling the inputs up.
      Depending on which ESP8266 you are using. The ESP8266-01S has pullups on GPIO0 and GPIO2, but in fact a classic ESP8266-01 should have pullups added there too.
      So in theory, that should be enough to stop at least 2 channels from activation at switch on. you could add pullups on the other two channels as well.
      Now I said ‘in theory’ because for instance the Rx pin, may send data during startup, even before you tell it in software that it should behave like a regular I/O pin.
      Also during startup, internal impedance on the pins may briefly allow current to flow through the optocoupler.
      I would suggest to add at least pullups on the Rx and Tx pins. If the problem is also with the GPIO0 and GPIO2 pins (and mind you, these already should have pullups, either on the board or added by you), you could see what the effect will be if you make that a 4k7 resistor.
      Another problem is that if you have JDVcc and Vcc connected and feed with 5Volt, the 3V3 from the ESP is only marginally capable of switching the relays OFF. You may want to read my 16 relay board article to know why. It is Thanks to the LED that it will work, but it is marginal.
      You could try to add a transistor or FET per channel, or a 4 channel voltage level adapter, but once you go that way, you might as well, just have 4 transistors or FETS drive 4 regular bare relays.
      Truthfully, this just isnt an ideal board, though the 16 channel one that I discuss in an other article is even worse 🙂

      1. Thank you. Am a newbie to electronics, could you please explain what is pullup?. you mean add a resistor? if will be great if you could share a schematic. Am using ESP01s exactly the same you have shown in the above article.

      2. A pullup is a resistor between the signal pin and the Vcc. That is too simple to make a schematic for.
        But the point of my article was that you do not need a pull up as that is what the relay board is providing. You will find the connection schematic in the article.
        Now if you want to use an ESP8266-01 for other things, you need to add the pull ups yourself. BUT….. if you use an ESP8266-01S… that one already has pull ups on its board

Leave a comment

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