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.

myopenhab.org

The way openHAB have implemented their Alexa integration is via their cloud hosted service https://myopenhab.org/ 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 it.cloud connector
  4. Lastly we need to check configuration Configuration>Services>IO>openHAB Cloud.cloud connector config

Now the cloud connector is installed we can create our account over at https://myopenhab.org/.

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 https://myopenhab.org/.
  6. Annoyingly I needed to reboot in order for my device to show up.

You can now remotely access your openHAB UI.

Items

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]" }

Alexa

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 myopenhab.org 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 ones.study 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 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 https://github.com/arendst/Sonoff-Tasmota to modify our Sonoff.

The Sonoff-Tasmota project has a great guide in their Wiki: https://github.com/arendst/Sonoff-Tasmota/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 https://github.com/esp8266/Arduino. All we need to do is follow the instructions in the project readme.md. Start the Arduino IDE.
  2. Open File > Preferences.
  3. Add http://arduino.esp8266.com/stable/package_esp8266com_index.json 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 https://github.com/arendst/Sonoff-Tasmota.
  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 off.my 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.

sonoff

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:
      #[broker].url=tcp://localhost:1883

      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:
      mybroker.url=tcp://localhost:1883
      mybroker.user=openhabian
      mybroker.pwd=mypassword
      mybroker.retain=false
      mybroker.async=false
    4. Check the logs at http://openhabianpi:9001/ for a couple of lines looking like this:
      2017-12-16 21:16:48.377 [INFO ] [penhab.io.transport.mqtt.MqttService] - 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.