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.