Using the Sonoff 4 Channel with Tasmota Firmware

Last time we looked at using Amazon Alexa to control our smart home lights, with a Sonoff basic and the Tasmota firmware. Sonoff also make other devices, and in this post we’ll be looking at the 4 channel “smart switch” to switch 4 separate mains devices.

You will need

Firmware Update

As we did with the basic the 1st step is to flash our devise with the Tasmota firmware.4ch open.JPG

  1. Step 1 is to remove the 4 screws holding the cover on.
  2. Fortunately the manufacturer has done us a favour here and already soldered header pins on the programming interface. Note the TX and RX labels are the wrong way round.4ch header.JPG
  3. Our programming cable, we made earlier should work fine to connect up the programmer.4ch programmer.JPG
  4. If you remember from last time, you will know that we need to put the Sonoff into program mode. Hold down the button marked FW/IO0 whilst we plug the programmer into the USB port on our PC.4ch buttons.JPG
  5. Next open Tools > Board: and select Generic ESP8285 Module.
  6. Then check the other tools options are set correctly:
    • Upload Using: Serial
    • CPU Frequency: 80MHz
    • Flash Size: 1M (no SPIFFS)
    • Upload Speed: 115200
    • Port: the COM port your programmer is connected to.
      esp8285 tools menu
  7. Finally click upload, to verify and load the new firmware.

Final Configuration

  1. Check your router to identify the IP address it has given the Sonoff.
  2. Open the webpage at this address.
  3. First thing we need to change is the module type. Open Configuration > Configure Module and change Module Type to 07 Sonoff 4CH.sonoff 4chan
  4. Next open Configuration > Configure MQTT and change Topic to identify your device.
  5. Lastly if you didn’t enter the details of your MQTT broker in the config file, now is the time.

Feel free to try accessing your device over MQTT. The commands are the same as the Sonoff Basic, but with the addition of a number to identify the channel e.g. cmnd/sonoff-myswitch/POWER1.

Finally we add the device to openHAB

  1. Open the sonoff items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and add the following lines:
    //My Switch
    Switch My_Switch_1 "Some Lights"  (Lights) ["Lighting"]
     { mqtt=">[mybroker:cmnd/sonoff-myswitch/power1:command:*:default],
     <[mybroker:stat/sonoff-myswitch/POWER1:state:default]" } 
    Switch My_Switch_2 "Some Things" ["Switchable"] 
     { mqtt=">[mybroker:cmnd/sonoff-myswitch/power2:command:*:default],
     <[mybroker:stat/sonoff-myswitch/POWER2:state:default]" } 
    Switch My_Switch_3 "Some Other Lights"  (Lights) ["Lighting"] 
     { mqtt=">[mybroker:cmnd/sonoff-myswitch/power3:command:*:default],
     <[mybroker:stat/sonoff-myswitch/POWER3:state:default]" } 
    Switch My_Switch_4 "Not Used"  
    { mqtt=">[mybroker:cmnd/sonoff-myswitch/power4:command:*:default],
     <[mybroker:stat/sonoff-myswitch/POWER4:state:default]" }
  2. Open your sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap, and add the following lines:
    Switch item=My_Switch_1
    Switch item=My_Switch_2
    Switch item=My_Switch_3

That’s it. We’re done.

Don’t forget to use meaningful descriptions for your devices, so you can control it with your Echo.



Using Alexa and openHAB 2 to Control Your Home

Today we’re going to have some fun and try using Amazon Alexa to control our light.

You will need:


This update is going to be quite simple as openHAB has done pretty much all the work for us. There is also a comprehensive guide here.

The way openHAB have implemented their Alexa integration is via their cloud hosted service this is great for us as most of the hard work is done. Some people may not like the fact we’re using an online service, but hey why are you using Alexa?

First we need to install the openHAB Cloud Connector

  1. So back to the Paper UI at http://openhabianpi:8080.
  2. Navigate to Add-ons in the left hand menu, then the MISC tab.
  3. Find openHAB Cloud Connector in the list and install connector
  4. Lastly we need to check configuration Configuration>Services>IO>openHAB connector config

Now the cloud connector is installed we can create our account over at

To register we’re going to need a couple of bits of information the UUID and Secret.myopenhab register Fortunately they are easy to find.

  1. To get the UUID ssh into your openHABian machine.
  2. Execute the following command cat /var/lib/openhab2/uuid.
  3. Copy the string of characters shown.
  4. Next get the secret in a similar way cat /var/lib/openhab2/openhabcloud/secret.
  5. You can now register for your account at
  6. Annoyingly I needed to reboot in order for my device to show up.

You can now remotely access your openHAB UI.


The Alexa integration uses a tagging syntax to identify which items it can control.

We need to edit our items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and add the Lighting tag:

Switch My_Light "My Light" <light> (gLight) ["Lighting"]
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }


Lastly in you Alexa app navigate to Skills and search for openHAB, and tap Enable. You will be prompted to enter the username and password you just used to create your account, and authorise the Alexa to use the service.

Alexa will spend a few minutes searching for devices then you’re good to go.Alexa Items.PNG

Try saying “Alexa turn on my light”.

Alexa Groups

One last tip. The Alexa app will allow you to create smart home groups. These can be used to associate a number of devices with a specific Echo device.

Whats the point? Well lets create a group for the room we’re in, in my case the study. Then add the Echo Dot located in my study to the group, and also our light.

Now we can control all the lights in a room together. Even better the Echo knows which group it’s part of so can control the lights without you having to define which group.PNG

Try saying “Alexa turn the lights on”.

Lastly if you change Alexa’s wake word to Computer, you can say “Computer turn the lights on”. It’s almost we’re living Star Trek!

DIY Home Automation with openHAB 2 – Part 4

This is my first blog of 2018 and it’s a real struggle getting back into things. So let’s start the year gently by taking a look at some of the configuration we did last time.

openHAB Items

Last time we created an items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and added the following lines:

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

Lets examine what this actually does.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

Switch – defines an item of type switch, in our case a light.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

My_Light – is the unique nae of our item. I suggest you devise a naming convention to make locating items easier.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

“My Light” – is the textual label for our item. This is used by UIs and, as we’ll find out later, by voice control interfaces like Amazon Alexa.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

<light> – tells openHAB which icon set to use in the UI. Icon sets can use the item state to alter which icon is displayed, you may have noticed a lit or unlit bulb displayed, depending on whether the Sonoff is on or off.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

(gLight) – tells us that this item is part of the group gLight. Groups are used to access collections of items together, in our case all the lights, but it could also be all the items in the living room. Items can be members of multiple groups, and also inherit group membership from their parents. I suggest you give some thought to how you intend to structure your smart home, I may publish a blog on this at some point.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

{…} – lastly the statements in the curly braces are the binding. In our case these are an MQTT command to toggle the state of our lamp and and MQTT status update so we can see whether it’s on or not.

Hopefully the above has given you some insight into what we did at the end of the last blog. There is loads more you can do with items in openHAB, take a look here for the complete guide.

Other Items

What other things can we do with items?

WiFi Signal Strength – RSSI

By adding the following to the items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, we can view the signal quality between the Sonoff and your wifi access point. Note the use of the JSONPath binding to transform the returned JSON into a number.

Number My_Light_RSSI "My Light: RSSI [%d %%]" <qualityofservice> (RSSI)
 { mqtt="<[mybroker:tele/sonoff-mylamp/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Adding this to your sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap,

Text item=My_Light_RSSI label="My Lamp Signal"

Will result in something like this, in the basic UI http://openhabianpi:8080. It may take a few minutes to get a value, as state updates are sent periodically.


Firmware Version

Display the current firmware version.


String  My_Light_Version "My Light: version [%s]" (Version)String  My_Light_Version "My Light: version [%s]" (Version)
 { mqtt="<[mybroker:stat/sonoff-mylamp/STATUS2:state:JSONPATH($.StatusFWR.Version)" }


Text item=My_Light_Version label="My Light Version"

Unfortunately going to the basic UI http://openhabianpi:8080 will not display  any value yet. This is because status2 messages are only sent on request.

So we also need to prompt our Sonoff to send a satus2 message.


Switch My_Light_Version_Query "My Light: version query" (Version)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/status:command:*:2]" }


Switch item=My_Light_Version_Query label="My Light Version Query"

Clicking this switch will now prompt the Sonoff to return a status2 message, from which we can retrieve the firmware version.



Something we may need to do, is see which of our devices are offline. This is handled by the LWT last will and testament message.


Switch My_Light_Reachable "My Light: reachable" (Reachable)
 { mqtt="<[mybroker:tele/sonoff-mylamp/LWT:state:MAP(]" }


Switch item=My_Light_Reachable label="My Lamp Reachable"

You may notice the problem, the LWT message payload “Online” or “Offline” is not understood by the Switch item type. The way we get round this is to create a map to convert the text.

Create a map file: \\OPENHABIANPI\openHAB-share\openhab2-conf\transform\, and add:


Now you can display which of your Sonoff devices are connected, their signal strength and firmware version.


DIY Home Automation with openHAB 2 – Part 3

Hacking the Sonoff hardware…

If you just found this, start with my earlier blogs here.

To get the Sonoff module to work with our openHAB installation, we need to change the firmware to something that can talk MQTT. Fortunately someone has done the hard work for us. We will be using the Sonoff-Tasmota project to modify our Sonoff.

The Sonoff-Tasmota project has a great guide in their Wiki:

You will need:

  1. A Sonoff Wi-Fi smart switch, available here.
  2. A USB to TTL serial adaptor module, like this.
  3. A bunch of female to female jumper wires, like these.
  4. Some header pins, like these.
  5. Also, a soldering iron, solder, a couple of scraps of plastic and some super glue.

Install the Arduino IDE on your PC

  1. As recommended in the project Wiki we will be installing a standalone version of the IDE. Download the Windows ZIP file for non admin install from here.
  2. Unzip the folders to your desired location. Important: do not run the IDE yet!
  3. Create an empty folder called portable.

Next we’re going to add support for the ESP8266 chip used in our Sonoff

  1. Again someone has done the hard work for us All we need to do is follow the instructions in the project Start the Arduino IDE.
  2. Open File > Preferences.
  3. Add to the Additional Board Manager URLs field.arduino prefs
  4. Open Tools > Boards: > Board Manager.
  5. Locate esp8266 in the list and install.ESP826 board manager

Now it’s time to get the new firmware

  1. Download (or clone, whatever you prefer) the firmware from Github
  2. Copy file eagle.flash.1m0.ld from arduino\version 2.3.0\tools\sdk\ld to IDE Arduino directory portable\packages\esp8266\hardware\esp8266\2.3.0\tools\sdk\ld.
  3. Replace file boards.txt from arduino\version 2.3.0 to IDE Arduino directory portable\packages\esp8266\hardware\esp8266\2.3.0
  4. Restart the Arduino IDE.
  5. Check for Tools > Flash Size: > 1M (no SPIFFS).
  6. Copy contents of Sonoff-Tasmota/lib directory to portable\sketchbook\libraries.
  7. Restart the Arduino IDE.
  8. Open File > Sketchbook > sonoff.

Time to solder!

  1. Start by prying the top off the Sonoff module. A fingernail worked for me.sonoff open
  2. Next we need to add header pins, so we can connect the programmer. They go in the 4 holes closest to the push button, and are soldered in place from the reverse. Don’t worry about the 5th hole, we aren’t using it in this.sonoff header pins
  3. Next we make a cable to connect the programmer to the header pins on the Sonoff module.
    • 3.3v -> 3.3v/VCC
    • GND -> GND
    • TX -> RX
    • RX -> TXIMG_2991IMG_2992
  4. Push the sockets over the correct pins on both devices and glue a small piece of scrap plastic over each set of sockets to hold them in the correct configuration.Plug construction

Important: never connect the programmer whilst the module is connected to the mains supply!

Loading the new firmware

  1. In the Arduino IDE open the sonoff sketch.
  2. Locate the user_config.h tab.
  3. Edit the lines relating to wifi, to connect to your wifi router.
    #define STA_SSID1 "mySSID"
    #define STA_PASS1 "mywifipassword"
  4. I’d also recommend you enter the connection details for your MQTT host, as this will save having to enter it for each module through the web UI. I needed to use the IP address of my host rather than the host name, I’m assuming this is because there is no DNS resolver in the firmware, but haven’t investigated further.
    #define MQTT_HOST "192.168.x.x"
    #define MQTT_PORT 1883
    #define MQTT_USER "openhabian"
    #define MQTT_PASS "mypassword"
  5. Open Tools > Board: and select Generic ESP8266 Module.
  6. Set other tools options as:
    • Flash Mode: DOUT
    • Flash Frequency: 40MHz
    • Upload Using: Serial
    • CPU Frequency: 80MHz
    • Flash Size: 1M (no SPIFFS)
    • Debug Port: Disabled
    • Debug Level: None
    • Reset Method: ck
    • Upload Speed: 115200
    • arduino tools menu
  7. Starting with programmer, PC and Sonoff disconnected.
  8. Connect the Sonoff to the programmer, using the cable you just made.
  9. Connect the programmer to the mini USB cable.
  10. Hold down the push button on the Sonoff and keep holding.
  11. Insert the USB A connector into your PC, connecting the programmer to the PC.
  12. After 1 second release the push button.
  13. You can now select the COM port your programmer is on, in the tools menu.arduino com port
  14. Finally click upload, to verify and load the new firmware.

Final configuration

  1. Check your router to identify the IP address it has given the Sonoff.
  2. Open the webpage at this address. You will see a screen something like this:sonoff home
  3. Open Configuration > Configure MQTT and change Topic to identify your device. In my case sonoff-mylamp.sonoff mqtt
  4. Also, if you didn’t enter the details of your MQTT broker in the config file, now is the time.
  5. Now back to MQTT. I’ve switched to using MQTT.fx as my client, and although not without a few UI bugs I’ve found it a vast improvement. Download it here.
  6. Enter you connection details, open the Subscribe tab and click Scan. You should see topics for your new Sonoff appear shortly.
  7. Subscribe to tele/sonoff-mylamp/LWT by double clicking, you should immediately see a message Online. LWT stands for last will and testament and will tell you the current status of your device.mqtt sonoff
  8. Let’s test it out. Open the Publish tab and enter the topic cmnd/sonoff-mylamp/POWER, the message TOGGLE, and click publish.
  9. The green LED on your Sonoff should switch on.
  10. Going back to the Subscribe tab, there should be a whole load of new topics visible. Lets subscribe to these too. sonoff topics
  11. Return to the Publish tab and publish your power command again.
  12. The LED should go off and back on the Subscribe tab you should see a series of messages starting with the power command you sent and ending with a status update.sonoff messages

Finally we add the device to openHAB

  1. Create a new items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and add the following lines:
    Switch My_Light "My Light" <light> (gLight)
     { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
     <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }
  2. Open your sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap, and add the following line:
    Switch item=My_Light label="My Lamp"
  3. Now if we open the dashboard http://openhabianpi:8080 and select Basic UI we should see a new switch on our site. Clicking this will turn the LED on and lamp
  4. Congratulations! You are now ready to disconnect your Sonoff from your PC, reassemble it in it’s case and start using it.

Next time we’ll look at adding some more features in openHAB 2 and even over the air updates for your Sonoffs.


DIY Home Automation with openHAB 2 – Part 2

The plan is to make use of these cheep switch modules from Sonoff to operate some lamps in our living room. We’re going to replace the stock firmware and use openHAB’s build in support for MQTT to control them.


Configure openHab 2 to use MQTT

It is beyond the scope of this article to explain MQTT, suffice to say to it is a lightweight message protocol specifically designed for use in IoT applications.

First we need to install an MQTT broker somewhere. The same Raspberry Pi running our openHAB 2 is an ideal choice. Fortunately the openHABian configuration tool can install and configure Mosquitto for you.

  1. SSH to openhabianpi.
  2. Run the openHABian configuration tool.
    sudo openhabian-config
  3. Open menu Option 20 Optional Components, then Option 23 Mosquitto. Don’t forget to set a password.

Next we are going to install the MQTT binding in openHAB 2.

  1. Open the openHAB 2 dashboard http://openhabianpi:8080.
  2. Select the Paper UI, this is used for configuring openHAB 2.
  3. Navigate to Add-ons in the left hand menu, then the BINDINGS tab.
  4. Find the MQTT Binding in the list and install it.MQTT Binding
  5. Also, the MQTT Action on the ACTIONS tab.MQTT Action
  6. Finally, the JSONPath and Map Transformations on the TRANSFORMATIONS tab.JSON Path

Now we are going to configure the MQTT service. Your openHABian configure a number of Samba shares to make this easier.

    1. Open the text file \\OPENHABIANPI\openHAB-share\openhab2-conf\services\mqtt.cfg, I use NotePad++ for this.
    2. Uncomment the line:

      Delete the # symbol and replace [broker] with the name you want to give your broker.

    3. We also need to set the user, pwd, retain, and async parameters. Ending up with something looking like this:
    4. Check the logs at http://openhabianpi:9001/ for a couple of lines looking like this:
      2017-12-16 21:16:48.377 [INFO ] [] - MQTT Service initialization completed.
      2017-12-16 21:16:48.379 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mybroker'

Next let’s see if it works.

    1. Create an items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\default.items, and add the following lines:
    2. Switch mySwitch {mqtt=">[mqbroker:myhouse/office/light:command:ON:ON],>[mqbroker:myhouse/office/light:command:OFF:OFF]"}
      Switch myLamp "lamp [%s]" {mqtt="<[mqbroker:myhouse/office/light:command:default]"}

      Create a sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap, and add the following lines:

      sitemap default label="My first sitemap"
      	Switch item=mySwitch label="My Switch"
      	Switch item=myLamp label="My Lamp"
    3. In Paper UI, navigate to Services and select Basic UI.
    4. Finally we enter the name of out sitemap default, and save.basic UI config

Now if we open the dashboard http://openhabianpi:8080 and select Basic UI we should see our site:basic UI

Try toggling the switch, you can see the connected lamp turn on (after a refresh).

Looking in the logs http://openhabianpi:9001/ you can see the events:

2017-12-16 23:28:06.093 [ItemCommandEvent          ] - Item 'mySwitch' received command OFF
2017-12-16 23:28:06.099 [ItemStateChangedEvent     ] - mySwitch changed from ON to OFF
2017-12-16 23:28:06.108 [ItemCommandEvent          ] - Item 'myLamp' received command OFF
2017-12-16 23:28:06.113 [ItemStateChangedEvent     ] - myLamp changed from ON to OFF

To debug further, you can install an MQTT client, like the Chrome plugin MQTTlens. Once connected to your broker, you can subscribe to the topic we used myhouse/office/light and watch the messages as they are sent. mqtt lens

Next time we’ll be configuring some actual hardware.

DIY Home Automation with openHAB 2

Installing openHABian on your Raspberry Pi

The goal here is to install openHAB  2 and get it up and running.

You will need:

  1. Raspberry Pi 3 (mine is a model B Rev 1.2)
  2. MicroSD card

I followed this excellent guide.

  1. Downloaded v1.3 openHAbian image from here.
  2. Formatt your MicroSD card, I used SD Memory Card Formatter 4.
  3. Burn the downloaded image to your MicroSD card, I used Etcher.
  4. Insert the MicroSD card into your Raspberry Pi and power on.
  5. Wait 15 mins or so.
  6. Then connect the Ethernet port to your network and identify the assigned IP address.
  7. You can now connect to the SSH console using either the IP address or the local DNS name openhabianpi. Logging in with username openhabian and password openhabian, you should see something like this:SSH load
  8. First we need to configure the WiFi and get rid of the Ethernet cable. Fortunately OpenHABian has an excellent configuration tool.

    sudo openhabian-config

    The WiFi connection can be configured under Option 30 System Settings.
    config tool

  9. Now before we go any further, let’s get everything up to date. First use option 01 to update the configuration tool.
  10. Next use option 02 to update the installed packages.
  11. Once everything is up to date, again go into system settings and set the system locale and timezone.
  12. Finally change the default passwords.
  13. Next we need to setup the UI. Open a web browser and navigate to http://openhabianpi:8080.
  14. Select Standard setup.

That’s it we’re done. We now have a working installation of OpenHAB.

You can connect to the OpenHAB dashboard at http://openhabianpi:8080.openHAB dash

Any problems check the log viewer at http://openhabianpi:9001/.

Take a look at the tutorials here to start using openHAB 2.

Read part 2 here, getting MQTT working.