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
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 …).
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