Hack Sonoff 4CH Pro with firmware MQTT Tasmota, inclusion in Domoticz

Table of Contents

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.

Warnings. You must disconnect the power supply and the relays before opening the case and installing a new firmware. Sonoff products are subject to change without notice from the manufacturer. All the steps in this tutorial have been verified. Technical changes may prevent the installation of a new firmware. Projects DIY can not be held responsible for accidents, injuries or deterioration of equipment. All features offered by Sonoff products may not be supported by alternative firmware. Verify that the desired functionality is supported by the firmware before proceeding with the installation. A backup and restore procedure is indicated in the tutorial without any guarantee of operation. Close the box before powering on the module.

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:

The following table summarizes the main specifications of the Itead Swich Smart Home Sonoff switches

Technical specifications are subject to change by manufacturers at any time without notice. Modules not CE / FC certified are reserved for the Chinese market.

No posts for this criteria.

What software do you need? esptool.py, PlatformIO

It is possible to install the Tasmota firmware from most development software:

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

sonoff 4ch pro pcb serial connector unpopulated

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.

sonoff 4ch pro pcb serial connector soldered

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.

ftdi cable pinout TTL5VT usb connectors

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.

Here I used a 12V power supply. For other modules, also connect the VCC pin of the FTDI cable to power the module. Warning. Do not power 220V modules during this operation. It’s dangerous!

sonoff 4ch pro ftdi serial converter

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.

sonoff 4ch pro esp8285 flash mode

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
Warning. The ESP8285 must be in flash mode to perform the operations described here. You must reset the module to flash mode after each execution of an esptool command.

Download the firmware from GitHub

Go to GitHub to download the project repository.

github sonoff tasmota firmware

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.

sonoff tasmota configure wifi

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!

sonoff tasmota platformio build

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.

 sonoff tasmota platformio serial monitor

Choose the COM port on which the Sonoff 4CH Pro is connected.

sonoff tasmota platformio serial monitor com3

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.

sonoff tasmota platformio ip address

Configure the module

Open a web browser and enter the IP address retrieved from the serial monitor. Here is the new configuration interface.

sonoff tasmota firmware main

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.

 sonoff tasmota configuration sonoff tasmota 4ch pro select

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

sonoff 4ch pro tasmota firmware esp8285 installed

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

domoticz mosquitto mqtt device

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.

sonoff 4ch pro domoticz dummy device

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.

sonoff 4ch pro domoticz dummy device

In the dialog box that appears, choose Switch and give the desired name.

sonoff 4ch pro create virtual instrument switch

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.

sonoff 4ch pro idx switch

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

sonoff 4ch idx tasmota configuration domoticz

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.

sonoff 4ch pro tasmota mqtt configuration

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.

sonoff 4ch domoticz dasboard

sonoff 4ch pro domoticz tasmota firmware mqtt

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.

Technical specifications that may be changed by manufacturers at any time without notice. Uncertified CE/FCC modules are reserved for the Chinese market.

No posts for this criteria.
Click to rate this post!
[Total: 1 Average: 5]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
  1. Reply
    Montaż Anten 511-591-717 3 December 2017 at 6 h 16 min

    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

  2. when loading the new software, does amazon echo/google home still work?

  3. 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?

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

  5. 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?

    • 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

  6. 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.

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

  8. Hi, I m trying to figure out if this


    would be achievable with sonoff 4ch pro. ie being able to open and close multiple contacts concurrently for certain number of seconds

Leave a Reply

Read more
DIY Projects
DIY Projects
%d bloggers like this: