In the previous article, we discovered the Sonoff 4CH Pro, a control box that allows to control by WiFi and by radio 433MHz up to 4 relays. In this new tutorial, we will see how to install the Tasmota alternative firmware developed by Theo Arends.
The Sonoff 4CH Pro can be installed in a control cabinet on a DIN rail. The EWeLink home application developed by Itead (the manufacturer of Sonoff products) allows you to control all devices in the range from a smartphone or tablet and create small scenarios. Everything goes through the Itead servers. This allows you to access your devices even outside your home when you have a 3G/4G connection (which is the case in 99% of the time, I hope!). This solution may disturb some users. First, EWeLink is totally closed. Can not create a bridge to a 100% DIY home automation server. On the other hand, you have no control over your personal data. This is a very sensitive issue right now. Here is how to install the Open Source Tasmota firmware from Theo Arends. All the steps in this tutorial can be applied to all Sonoff modules, including a technical comparison on the price comparator.
Introducing the Tasmota firmware for Sonoff / Itead products
The Tasmota firmware is developed and maintained by Theo Arends (its GitHub page and personal site). It is designed on a principle similar to the Homie firmware. It adds a configuration interface accessible via a web browser from any computer, tablet or smartphone on the same local network. It then uses MQTT messaging to communicate with a home automation server, an online server, a mobile application iOS, Android …
The Tasmota firmware will therefore subscribe to an MQTT broker (read this article for more information). We can send him orders. Essentially “active” or “disable” relay X.
In return the firmware Tasmota will publish (send messages) to the broker MQTT:
- The relay has been activated (or not)
- It may send measures:
- Temperature, humidity if a probe is connected (there are pins still available on the modules, cool)
- Power consumption for the Sonoff POW module
The advantage of this firmware is that it is totally dedicated to Sonoff devices. As it is specialized, there is very little configuration to do. You will be able to install the same firmware on all your Sonoff home automation devices (if you are a fan of the brand of course!).
We will return in more detail to the features proposed in a future article. Now let’s turn to a practical case and see how to install this firmware on the ESP8285 that equips the Sonoff 4CH Pro.
Before going further
Electrical Hazard Warnings and Warranty
Before I start, it seems important to me to recall some safety rules. First, you will modify an electrical appliance. These modifications will void any manufacturer’s warranty. On the other hand, this tutorial can be applied to all Sonoff modules. With the exception of the 4CH Pro, they are powered with a voltage of 220V.
Can this tutorial be applied to other Sonoff devices?
Yes, the method presented in this tutorial can be applied to almost the entire range. The main difference is the setting of the module in flash mode. You will find on the WiKi the procedures to follow for the other models of the brand:
- Sonoff Basic
- Sonoff Dual
- Sonoff Pow
- Sonoff RF and Slampher
- Sonoff RF Bridge 433
- Sonoff Touch
- Sonoff 4CH
- Sonoff B1
- HuaFan Smart Socket
The following table summarizes the main specifications of the Itead Swich Smart Home Sonoff switches
What software do you need? esptool.py, PlatformIO
It is possible to install the Tasmota firmware from most development software:
- IDE Arduino
- PlatformIO on the GitHub ATOM Editor
- Visual Studio Code with the PlatformIO plugin. This is the solution that I advise you. Visual Studio Code is much lighter and more stable than Atom
- esptool.py, the python script of the Espressive SDK
Installation from the Arduino IDE is not the easiest method. It is necessary to modify the IDE by adding the support of the module ESP8285, to install libraries … As this tutorial is addressed first and foremost to the beginners, I propose you to opt for PlatformIO which takes care of everything. Follow this article to install PlatformIO on your PC or Mac. Once installed, it will be possible to update it over-the-air using the SonOTA python script or by downloading the firmware directly from the configuration interface using a web browser.
Finally, you will need to install esptool.py tools from the Espressif SDK ESP8266. Follow this tutorial for macOS (also works for Linux) and this one for Windows. The esptool.py script will allow to save the old firmware, and to erase the flash memory before installing the new one.
Preparing the Sonoff 4CH Pro
Like all models in the Sonoff range, we will have to solder a connector on the circuit. Start by opening the module (note, there is no warranty cancellation label, Itead leaves everyone free to change the firmware).
The connector must be soldered just next to the ESP8285 module (below the LEDs). Here I opted for a male hearder (easier to cut out), choose according to your Jumpers. To put the module in flash mode, it will be necessary to connect the pin GPIO0 to the GND to the ignition. I advise you to add a small female connector on a GND pin of the circuit. For example, on the unpopulated connector located above the S6 switch.
Before each firmware download, connect the GPIO0 pin to the GND with a jumper and then turn on the module (either by connecting the VCC pin of the FTDI cable, or with a 5-24VDC power supply). Just wait 1 or 2 seconds before releasing the jumper. If the blue LED indicates a WiFi activity, it means that the module is not in flash mode. We must start again.
It’s ready !
Connect the FTDI cable
The FTDI cable will allow us to communicate with the ESP8285 / ESP8266 via the serial port. Nothing special except that the pins are reversed on some models. Depending on your FTDI cable and the Sonoff module, you may have to fumble a bit. For the tutorial, I used this Adafruit cable.
Obviously, I started by plugging the connector of my cable to the serial port freshly welded on the 4CVH pro. And there, nothing at all! Do not panic, just find the pins RX and TX on the cable (google is our friends) and use 3 jumpers (GND, RX, TX) to make a manual connection. Normally, we have to reverse the jumpers between RX and TX but this is not the case on most Sonoff products because there is a misregistration on a PCB. Try first without reversing. If that does not work, reverse RX / TX.
Put the Sonoff 4CH pro in flash mode (programming mode)
Now place the jumper on the GPIO0 pin of the ESP8285 and the GND pin. Pin GPIO0 is the second pin of the module. Turn on the module using the 5-24VDC power supply. No LEDs (the blue one in particular) should light up. The module is in flash mode! Everything is ready to install the new firmware.
Find the Sonoff 4CH Pro COM Port
On Windows, connect the FTDI cable. Wait for the drivers to install if it’s the first time. Open PowerShell and execute the mode command. The COM port on which Sonoff is connected is listed. If it does not, go to the control panel to verify that the drivers for your FTDI cable are correctly installed
On macOS or Linux, open the Terminal and run the ls /dev/tty.usb* command. In return you should have COM port in the form
/dev/tty.usbserial-A1048DR2
Back up the original firmware and erase the memory
Open PowerShell or a Terminal and run the following command replacing the COM port to back up the original firmware on your computer.
esptool.py --port PORT_COM read_flash 0x00000 0x100000 image1M.bin
The dump (copy) of the flash memory is saved in the image1M.bin file in the executable directory of the esptool.py script.
Now, run this command to completely erase the old firmware from the flash memory of the ESP8285. Modify the COM port before running the command.
esptool.py --port PORT_COM erase_flash
To restore the old firmware, run this command by changing the COM port.
esptool.py --port PORT_COM write_flash -fs 1MB -fm doubt 0x0 image1M.bin
Download the firmware from GitHub
Go to GitHub to download the project repository.
Install the Tasmota firmware on the Sonoff 4CH Pro
Launch PlatformIO and open the project from the File -> Open Folder menu. Specify the project GitHub repository path. Open the user_config.h file and look for the STA_SSID1 key. Replace the WiFi login and password. You can specify a second alternative network.
Then open the platformio.ini configuration file at the root of the project and uncomment the target env_default = sonoff. Go to the PlatformIO menu and compile the firmware with the Build option. PlatformIO takes care of everything!
Once finished, start the installation from the PlatformIO menu and upload. At the end of the installation, the reboot module. The blue led flashes the time to connect to the WiFI network and then lights up continuously. Bravo! You have completed the installation. Return to the menu and open the Serial Monitor.
Choose the COM port on which the Sonoff 4CH Pro is connected.
Wait for PowerShell or the Terminal to launch. Unplug and reconnect the Sonoff to obtain the Tosmata firmware boot log. What interests us here is the IP address that was assigned to Sonoff 4CH pro.
Configure the module
Open a web browser and enter the IP address retrieved from the serial monitor. Here is the new configuration interface.
The first thing to do is to indicate the module used. By default, the Sonoff Basic is selected. Go to the Configuration page and then Configure Module. In the list, choose Sonoff 4CH Pro (# 23) and then Save. The reboot module.
You can now control relays from the Web interface but also from the 4 command buttons that are always available. The different modes are also supported by the firmware (Self-locking, Interlock and Inching presented in the previous article). To control relays independently, configure the selectors as follows:
- S6: out of 1
- K5: all about 1
- K6: all of 0
Inclusion of Sonoff 4CH Pro in Domoticz
The Tasmota firmware makes perfect sense when you have a home automation server.
Prepare the MQTT Broker
All exchanges with Domoticz will be via an MQTT broker. Start by following this tutorial to install it on your system.
Adding an MQTT gateway
Now that you have an MQTT broker, we will connect Domoticz. It will thus be able to receive messages from the firmware: change of state of the relays, acknowledgment of receipt of an order, measurement record (temperature, humidity, level, etc.). It will also be used to send commands to Sonoff modules manually or according to a scenario.
Go to settings then Hardware. In the list of types, choose MQTT Gateway Client with Lan Interface and fill in the various parameters
- Give a name. For example Mosquitto
- Remote address: the IP address of the broker MQTT or localhost if it is installed on the same machine
- Port: default 1883
- Login and Password if access to the broker is protected. Leave empty otherwise.
- Publish Topic: do not change anything.
- Finish by clicking Add
Adding a virtual device
Now that we can receive and send messages, we must create a virtual hardware to which will be attached buttons and possibly measuring instruments. In the Type list, choose Dummy, and give a name. Finally add.
Create control buttons for relays
You can now create as many virtual buttons as you have relay on your Sonoff. 4 in the case of Sonoff 4CH Pro. To create a virtual button, click Create Virtual Sensors in the Dummy Device Type column just created.
In the dialog box that appears, choose Switch and give the desired name.
Then go to the Devices (Settings -> Devices) to find the 4 virtual buttons that will allow to control the 4 relays. What interests us here are the Idx attributed by Domoticz. Here from 14 to 17.
Configuring the MQTT and IDX Domoticz broker on the Sonoff 4CH pro
Log in to the Sonoff administration interface and go to the Configuration -> Configure Domoticz page. Report the IDXs found previously. End with Save. The reboot module. As you can see, everything is already ready on the Tasmota firmware to manage other sensors (Temperature, Temp + Humidity, Barometer, energy + power Sonoff POW, brightness Sonoff SC, counter).
After the restart, go to the Configuration page and then Configure MQTT. Fill in the Broker connection settings:
- Host: the IP address of the broker
- Port: 1883 by default
- User and Password if access to the broker and controlled by a password
- You can leave the other parameters with the default values.
- Save. The Sonoff reboot.
Pilot the Sonoff 4CH Pro from Domoticz
From Domoticz, go to the Switches tab. You will find the 4 command buttons created previously. Press a button. If all is correct, you will hear it “snapping” and the corresponding LED will turn it on to indicate that it has been activated.
Here. In the next tutorials we will see how to include Sonoff modules on other home automation servers (Jeedom, Home Assistant) or a dashboard for connected objects (Node-RED …).
Sources used:
I am not the author of (all) the tips proposed in this tutorial. We can thank Blair Thompson who has already contributed to the Sonoff S20 Hack.
The following table summarizes the main specifications of the Itead Swich Smart Home Sonoff WiFi switches.
- Get Started with HC-SR04, measure distance by ultrasound. Arduino code example
- ESP32, GPIO pins and associated functions. I/O, PWM, RTC, I2C, SPI, ADC, DAC
- ESP32-CAM pinout and equipments. ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera …
- ESP32-CAM. Which model to choose? ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera …
- M5Stack Atomic GPS. ESP32 TinyGPS++ tracker, GPX export on SD card, visualization on Google Maps or VSCode
Gorąco polecam ten temat sprawdzony działa w 100% bardzo szybciutki konfig sonoffa i wszystko gra i idealnie współpracuje z domoticz ( Montaż anten alarmów kamer http://www.ewerasat.pl ) jeśli nie dajesz rady zapraszam do kontaktu
when loading the new software, does amazon echo/google home still work?
Hello Chaun. No, after the update, the functions amazon and google home will no longer work. They are supported by the Itead firmware which is replaced by this update.
Once this firmware is applied, can you change the status of a relay via an http command, and retrieve the status of the relays via http, as you can, with, say, diymall’s 4/8 channel relay module?
Hello Michael. The tasmota and espurna firmwares both use MQTT messaging to communicate with a home automation server. There is no entry point to drive Sonoff modules with HTTP requests. To do this, you should use the ESP Easy firmware https://diyprojects.io/hack-connected-sonoff-s20-plug-esp-easy-rules/#.WnXu3JOdXJw . See you soon
has any of you found a way to work with i2c devices connected to the Sonoff Pro? I cannot find a way to do it…
Noob Question
I purchased my first 4ch Pro (R2) on amazon. I’m attempting to flash Tasmota to integrate with Domoticz to control my door strikes. This will replace my old X10 relays. I think that I have everything figured out, except my 4ch Pro (R2) does not match any online pictures, so I’m not sure where GPIO0 is located. Unlike all of the online pictures, where the board reads, “sonoff_4ch_pro V1.1 2017-4-26”, my board reads “sonoff_4ch_pro V1.0 2017-11-10.” I also have a long bar for my 433mhz antenna, not the black coil of wire. The ESP8285 is also soldered directly to the board, it’s not on the little riser board. Is this just because I have the R2 model? Can I just locate GPIO0 on the chip specs and continue?
Thanks
Hi Allen. Yes I saw last week that the new version R2. I looked briefly and it seems to me that the big difference is in the box. Before the terminal block was accessible from the outside. Now it is covered by the box. It must be to comply with the CE or FCC standard, I have not gone further. In my opinion, the tutorial remains valid for the R2 model. Tell me if I’m wrong. Have a good day
Thanks. I will give it a shot then. This is my first one. I will post any helpful comments. The terminal block is still accessible . The flap above it is hinged and can be pulled up for access to the block. Here are some pictures of the new board.
Thank you very much Allen. Difficult the see the difference. We need to read the documentation to find more.
I tested, and this relay appears to be working. 1) There is a little solder pad to the bottom right of the ESP8285, near R50. It gives easy access to GPIO0. 2) S6 has to be set to Interlocking, not Locking/Inching to enter programming mode for the chip. If it’s set the wrong way, two red leds come on. 3) The relays can not be controlled from the web interface, but everything else appears to work. 4) The TX and RX are labeled correctly on this version, so reverse them when you connect your cable. Feel free to let me know if you have any questions, and thanks for the great tutorial. I couldn’t have done it without.
Thank you very much Allen for all his information. They will be very useful to everyone. I’m glad the tutorial got you started.
Hey,
First of all, thank you for the great tutorial.
But I have one problem, Relai 4 is not working correctly. It turns on and shortly after that back off. All the dip Switches on the Sonoff 4ch Pro are correct. Relai 1-3 are working perfectly with Domoticz. Relai 4 will work when I remove the virtual switch from Domoticz. Any ideas how I can fix that?
Thanks in advanced.
Hello Miles and thank you very much. Are you sure you rite the correct Domoticz IDX in the configuration ?
In Europe the Sonoff Basic and Sonoff 4 Channel Pro Release 2 are available on Amazon with Tasmota flashed already. Basic: https://amzn.to/2BzxsHk and 4 Channel: https://amzn.to/2X35Nr9
Thank you very much for info Julian
Hi, I m trying to figure out if this
https://imgur.com/gallery/3ifUdCl
would be achievable with sonoff 4ch pro. ie being able to open and close multiple contacts concurrently for certain number of seconds