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.

 

Advertisement

3 thoughts on “DIY Home Automation with openHAB 2 – Part 3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s