Hacking ESP8285 4 Way relay controller
How to implement your own firmware on the Itead 4 Channel ESP8285 based Relay board ( 7V to 32V-4 Jog Switch)
Since I found no how to in the internet how to flash an own firmware into this nice and cheap Itead device I decided to wrote one. There are some specials to notice:
- This module is based on the ESP8285 (NOT ESP8266 – Take care when selecting board in Arduino ide)
- There are no plugs where you can easily attach your FTDI programmer. You will have to connect directly to the PSF-B85 board that holds the esp. (For more information about this board please refer to https://www.itead.cc/wiki/PSF-B85
- Please note that the show pin reference is meant as a view from below the board which is some kind of confusing. So please follow the following picture how to connect your ftdi.
- This how to does not include how to use the Arduino IDE and how to use a FTDI programmer. Please google this. There is much documentation.
- Actually it can be ordered at bang good. Here: http://www.banggood.com/SONOFF-USB-5V-Or-DC-7V-32V-DIY-4-Channel-Jog-Inching-Self-locking-WIFI-Wireless-Smart-Home-Switch-p-1157460.html
Flash firmware
- At first you will have to connect you FTDI programmer directly to the board. The following picture shows how to connect the 4 FTDI cables:
- You will only need to connect GND, VCC, RX, TX and GPIO 0. Please keep in mind that you have to crossover RX and TX. So connect RX to TX and TX to RF. You can skip GPIO 15 and GPIO 2
- To get the ESP into bootloader mode you have to connect GPIO0 to GND and the power up the device. Afterwards you can download the firmware with Arduino IDE or ESP tool. Afterwards you have to disconnect GPIO0 to let the device start firmware instead of bootloader
Using the board peripherals
- Relay 1-4 is connected to PIN 12, 5, 4, 15 (example for relay 1 in Arduino sketch use: pinMode(12, OUTPUT); digitalWrite(12, LOW);)
- The LED is connected to PIN 13 (example for relay 1 in Arduino sketch use: pinMode(13, OUTPUT); digitalWrite(13, LOW);)
- The button right next to the led is connected to PIN 14 (example for relay 1 in Arduino sketch use: pinMode(14, INPUT); int value = digitalRead(14);)
- The second button cannot be accessed directly
Hints
- This PCBA comes with some kind of mode switch that can be selected with button2 (the one right next to the micro usb connector). If your relay does not react to your commands press this button to change the mode and afterwards do not change it again.
- This PCBA need more current than most of the usb ports or FTDI programmers can offer. Therefore you might have to additionally connect the USB power supply.
I’m not the one to give up quick, so I started to try things out. For the sake of your time, I have created the complete pinout for you. If you want to refer to the original datasheet be my guest.
ESP8285 pinout
https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01-300x238.jpg 300w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01-768x609.jpg 768w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01-640x507.jpg 640w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01-980x777.jpg 980w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01-50x40.jpg 50w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-01.jpg 1362w" sizes="(max-width: 640px) 100vw, 640px" style="box-sizing: border-box; border: 0px; max-width: 100%; height: auto; text-align: center; clear: both; display: block; margin: 6px auto 21px;">
The pinout itself on the pictures was confusing so I took the liberty of redesigning it.
Having the ESP8285 pinout is one thing, but mapping this to an existing board is another. While the AFE Firmware I’m going to use have most of the mapping done, knowing what’s what is crucial when it comes to writing a custom code. This is the board and the connections:
https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-00-300x188.jpg 300w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-00-768x482.jpg 768w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-00-640x402.jpg 640w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-00-980x615.jpg 980w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-00-50x31.jpg 50w" sizes="(max-width: 640px) 100vw, 640px" style="box-sizing: border-box; border: 0px; max-width: 100%; height: auto; text-align: center; clear: both; display: block; margin: 6px auto 21px;">
To flash a ready bin file or your own Arduino IDE code, you have to back up, wipe the flash and upload the new bin file or the Arduino code. The procedure is described in detail here.
I’m going to cover the AFE Firmware, which is pretty easy to use, if you want to push your own code, just open the Arduino IDE and flash it using the ESP8285 module in the board manager.
Before you get irritated, the ESP8285 GeekCreit 4 Way relay controller has a function button which drives the board modes. There is only one mode in which the board will function properly so circle the mode couple of times and try it out.
https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02-300x278.jpg 300w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02-768x713.jpg 768w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02-640x594.jpg 640w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02-980x909.jpg 980w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02-50x46.jpg 50w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-02.jpg 1164w" sizes="(max-width: 640px) 100vw, 640px" style="box-sizing: border-box; border: 0px; max-width: 100%; height: auto; text-align: center; clear: both; display: block; margin: 6px auto 21px;">
AFF Firmware on ESP8285 GeekCreit 4 Way relay controller
If you are feeling lazy, Adrian Czabanowski created the firmware for this (and other) controllers. You can find his firmware on the website www.smartnydom.pl but a lot of it is in Polish.
Fortunately, you have me, and I’m here to make it easy – and download the Firmware for your board with up to 4 relays (T4). There is a version for ESP8266 and ESP8285 so make sure you get the right one. Flash the firmware (rename it to something simple) so you don’t have to type all that in the PowerShell.
Once the firmware is flashed, hold the pairing button for about 10 sec. You will see the board’s LED blinking fast. The ESP8285 GeekGerit 4 Way relay controller will go into the AP mode. Check the SSIDs on your phone and you will see the AFE Device – connect to it and note the gateway IP – usually, it’s something like:
192.168.5.1
And connect to it. Don’t bother with setting up everything on your mobile, unless you don’t have a PC around. Just update the Network details and finish configuration. Once this is done, the ESP8285 GeekCreit 4 Way relay controller will get a new IP assigned by your router.
https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-03-300x187.jpg 300w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-03-768x478.jpg 768w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-03-640x398.jpg 640w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-03-980x610.jpg 980w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-03-50x31.jpg 50w" sizes="(max-width: 640px) 100vw, 640px" style="box-sizing: border-box; border: 0px; max-width: 100%; height: auto; text-align: center; clear: both; display: block; margin: 6px auto 21px;">
Load the device page and enter settings again. From here you will be able to configure the device to your needs. Select the number of relays (4 – GPIO12,04,05,15), switches (1 – GPIO14) and LEDs (1 – GPIO13)
Enable the MQTT and HTTP protocols and check the general configuration. Make notes of anything you don’t think you can remember as we will need this to use the MQTT and HTTP protocols.
MQTT and HTTP
There are 2 protocols that can be used to interact with the ESP8285 GeekCreit 4 Way relay controller. Both have their own advantages and I will show you how to link these to a NodeRED server. If you want to learn more about MQTT and HTTP in NodeRED I have a fantastic tutorial for you.
MQTT
The ESP8285 GeekCreit 4 Way relay controller comes with 4 relays that we can toggle. The MQTT broker allows you to specify the topic for the device, but to control each of the relays we have to modify that topic more.
Let’s say I use the topic name /esp8285/ this means that to control the relays, I will have to add the name of each relay (from the config page) to the topic.
/esp8285/switch1/ /esp8285/switch2/ /esp8285/switch3/ /esp8285/switch4/
To issue the commands I have to modify the topic further:
As you can see, this is fairly straight forward, and the control of each relay is done by modifying the topic and setting a correct payload.
HTTP requests
Another way of controlling the ESP8285 GeekCreit 4 Way relay controller is through the HTTP requests. Most of the time, you will be composing the URL which has embedded commands that will be issued to the board.
To build a valid URL you will need:
https://IP_Address/?device=relay&name=RelayName&command=command
Make sure to reserve the static IP address. The fields in my example are as follow:
command = on/off/get/toggle
RelayName = switch1/switch2/switch3/switch4
The responses given are sent in a JSON format. If you not sure how to handle JSON I have the tutorial explaining all you need to know here.
Here are a couple of JSON samples:
{ „device”:”ESP8285”, „name”:”switch1”, „command”:”on”, „value”:”on”, „status”:”success” } { „device”:”ESP8285”, „name”:”switch1”, „command”:”get”, „value”:”off”, „status”:”success” } { „command”:”reboot”, „status”:”success” }
Lastly, to control the MQTT, HTTP and other APIs links look like:
https://IP_Address/?device=DeviceName&name=ApiName&command=command
Where Apis names are: mqtt, http, domoticz.
Conclusion
https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-06-300x177.jpg 300w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-06-768x453.jpg 768w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-06-640x377.jpg 640w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-06-980x577.jpg 980w, https://notenoughtech.com/wp-content/uploads/2018/08/ESP8285-06-50x29.jpg 50w" sizes="(max-width: 640px) 100vw, 640px" style="box-sizing: border-box; border: 0px; max-width: 100%; height: auto; text-align: center; clear: both; display: block; margin: 6px auto 21px;">
The AFE Firmware is a cool solution if you don’t want to code the HTTP and MQTT protocols yourself. It’s robust enough to link it with the Alexa and Google Home skills. I’d love to see support for HTTPS and other integrations, but for now, this is a quick and easy way to integrate the ESP8285 and ESP8266 switches into your NodeRED environment. I wrote a very basic NodeRED flow to control and display the status of the relays on the dashboard. You can grab these files from below.