Hacking a Sonoff Touch Panel to Work With MQTT and Homeassistant

9,527

30

40

Published

Introduction: Hacking a Sonoff Touch Panel to Work With MQTT and Homeassistant

About: All round geek, love tinkering with microcontrollers. pcs. and just making stuff

There are some great hackable devices in the SONOFF range such as the original SONOFF switches, the Slampher and the SONOFF Luxury wall plate which is the subject of this instructable.

These devices are based around the ESP8266 or ESP8285 WifFi modules and are easily hackable.

I'll only cover here how to convert a wall plate to run with MQTT and HomeAsisstant, I wont cover hooking it up to the mains, mains bites bad if you mess things up, and I don't want to be responsible for you injuring, killing or worse still pishing off your wife as you just burnt down the house...

One thing to note, is that if you wish to use these devices in the UK, I'm not sure they comply to CE or UK safety laws.

You should also note that you will need a neutral for the devices to work, most light fittings in the UK will only have live and the switch line.

Step 1: What You're Going to Need

  • SONOFF Luxury Wall plate.
  • FTDI or and Arduino Uno with a DIL Atmega chip.
  • 5 Jumper leads suitable for your FTDI or Arduino to connect to your newly adapted panel.
  • A few centimetres of wire wrap or other such wire.
  • USB Lead to hook your Arduino UNO or FTDI up to your PC.
  • Soldering Iron, Solder.
  • 5 Way right angle header.
  • Screw driver.
  • Needle nosed pliers.
  • Magnifying glass/helping hands.

Arduino IDE installed and ready to go Arduino.

Overview of Arduino install for ESP8266 by TrakerJ.

The following libraries will also be needed to be installed in your Arduino IDE

WifiManager by tzapu PubSubClient by Nick O'Leary

Installing Arduino Libraries by MertArduino

Step 2: Step 2: Dismantling the SONOFF

Simply insert a bladed screw driver into the tab as can be seen in the image above. Give the screw driver a twist and it will soon pop apart. Gentle though, don't want to break the glass panel so early in the game.

Step 3: Step 3: Removing the ESP Module Board

You can now remove the board we are interested in. It has a 2x2 header at the top of the board and a small double sided sticky pad at the bottom.


Carefully lift the board up using your screwdriver if need be, to work the board away for the sticky pad. Be gentle, we don't want to break the board or bend the pins on the 2x2 header

Step 4: Modify the Board

Now we need to break out the soldering iron.

While the iron is heating we need to prepare our 5 way header.

With a pair of long nose pliers, carefully push the 5th pin further down into the plastic header thereby lengthening the solder pin.

Once this is as long as can be, gently bend the lead out in line with the header plastic. You can see this better in the image above.

Now insert the 5 way header into the PCB and solder the 4 pins in place on the reverse of the board.

We now need to connect GPIO0 on the ESP8285 to the 5th pin of our header.

Using a magnifying glass and a steady hand, solder the short length of wire to GPIO0 as can be seen in the second image.

Once cooled, carefully dress the wire over the board and take it up to pin 5 of the header and again with your magnifying glass and a steady hand solder the wire on to pin 5.

You can apply a dab of superglue or hot melt glue to the wire to stop it wiggling around and breaking your solder joints.

Step 5: Hook It Up to Your FTDI/UNO

I use an UNO instead of an FTDI, only reason being there's always one on my desk and its quicker to pop the MCU out and use that than hunt through my component drawers for an FTDI, yes I know lazy...

Any way we now need 5 Dupont wires, I'm using Female to Male

Header Colour Arduino

1 green GND

2 brown TX

3 orange RX

4 red 3.3V

5 yellow GND (This is the bent pin)

Check your connections over, paying attention to Pin 4, as this must only be connected to 3.3V, connecting to 5V will more than likely kill the ESP module.

Once you're happy with the connections you can go ahead and hook up the USB lead.

Step 6: Run Some Tests

Start the Arduino editor and make sure it is connected to the correct serial port, if using the UNO it will be labelled along the lines of Port X (Arduino/Genuino Uno) where X is the actual port number.


Change the board to "Generic ESP8285 Module"

Download the Test_Sketch.ino file above, and save it to a folder called Test_Sketch

Open this in your Arduino IDE and Upload it to your touch plate.

Unplug Pin 5, this only needs to be connected while you are uploading your sketch

Open the Serial monitor.

Once you see the WiFi Led light on your touch plate, each time you touch the touch sensor it should light and you should see a message in the Serial Monitor indicating the touch sensor is activated.

If you are unable to upload the sketch try swapping pins 2 and 3, (TX/RX pins on the UNO or FTDI)

Step 7: The Last Sketch

Download the Live_sketch.ino above, save it to a folder Live_sketch

Load it into your Arduino IDE and edit the following lines..

Line 26:

const PROGMEM char* MQTT_CLIENT_ID = "Study Test Switch";

Change "Study Test Switch" to something suitable for your installation.

Line 27:

const PROGMEM char* MQTT_USER = "MQTT User";

Change "MQTT User" to the user name used to connect to you MQTT server and

Line 28:

const PROGMEM char* MQTT_PASSWORD = "MQTT Password";

Change "MQTT Password" to your MQTT servers password

Lines 29 and 30:

const char* MQTT_LIGHT_STATE_TOPIC = "study/swtest/status"; const char*

const char* MQTT_LIGHT_COMMAND_TOPIC = "study/swtest/switch";

Set the state and command topics inline with your naming conventions

Line 70:

WiFi.hostByName("your_mgtt.server.co.uk", MQTT_SERVER_IP);

Change "your_mqtt.server.co.uk" to the fully qualified domain name of your MQTT server.

Give your changes the once over to make sure they are correct then upload the sketch.

Step 8: Connect the Wall Plate to You WLan

After a couple of minutes open up your phones list of Wireless networks

Search for the network called "TaTaTatooouchMe" and join it.

You will now be asked to authenticate which will produce a page as in the first image

Click ConfigureWiFi and You can now select your WLan, enter the passkey and click save.

After 10-15 seconds you device will be connected to you WLan

Check you device has reverted back to you WLan and the network "TaTaTatooouchMe" has disappeared

If not repeat the steps again

Step 9: Add the Touch Plate to Home Assistant

I separate all of my devices into separate files which I then include in the main configuration.yaml.

Please add this device to wherever you keep yours.

- platform: mqtt

name: "Main Light"

state_topic: "study/swtest/status"

command_topic: "study/swtest/switch"

payload_on: "ON"

payload_off: "OFF"

optimistic: false

You can alter the name to whatever you like, but you must make sure state_topic and command_topic match what you entered in the Live_sketch text on lines 29 and 30.

Run a test to make sure everything is ok with your new entry

/usr/local/bin/hass -c ./ --script check_config

If all appears ok restart hass

service hass-daemon restart

You should now see the new light switch in the home assistant gui.

Check you can toggle the switch from within Home assistant and vice-versa from the switch to home assistant.

Re-assemble your SONOFF and my work here is done.

Share

Recommendations

  • Microcontroller Contest

    Microcontroller Contest
  • Casting Contest

    Casting Contest
  • Woodworking Contest

    Woodworking Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

No need to solder on the pin header. It's possible to just hold male Dupont leads in place, releasing the GPIO ground lead after connecting to USB on your computer.

7116860933823990481.jpg

2 Questions

0

I've never had issues while running my units off of PC usb supply, but have definately seen issues before with a wall PSU. Glad you are making progress and thanks for letting me know. If possible could you please post the pin number used for the additional channels. Others have asked and in the past and it could be of use going forward.

Cheers

0

I'm using the 2Gang version of the Sonoff T1 and I'm seeing the buttons randomly turning on/off every few seconds. I have modified it a little - I had no use for the power switching - so I'm only using the top board with the 8285 and the touch switches and powering via the 5V/GND connectors that would have come from the switch/transformer logic board via a USB 5V/1A wall wart. In theory this should look the same as the original, but with no power relay logic. I've turned off as many things as I can (inc. MQTT) in the hope of stopping this behaviour, but no luck. Any advice welcome!

0

It's not rebooting. The working theory is that USB power (either wall wart or from computer port) is not clean enough and the original power source had filtering prior to providing the 5V to the Wifi/Microcontroller daughter card. Plugging it into a crappy stickytaped set of batteries in series to provide a clean source of power reduced the rebooting - i.e. it didn't happen for >20m but then I saw some transient changes when I sat down at the desk where it was sitting. So there are still issues but it's a power/noise issue. The console of the sonoff showed that the 8285 was not rebooting (it comes up with messages on startup) and the ongoing messages were identical to those produced when the touch pad is actually touched, i.e. it is prefixed with APP:.

Thanks for responding.

1 more answer

0

Hi Ray,

Sounds to me like the 8285 is rebooting. Have you run it while still hooked up to the PC with the serial window open? To start with I would first just upload a simple piece of code to just turn the led's on and off in turn. Then add in the WiFi code and see how it runs.

Please let me know how you get on...

40 Comments

GrahamR33, I really wouldn't recommend this, I've tried it in the past and managed to break botah a SonOff original and a SonOff panel.

Hello, did anybody trie to determine which gpio is controling other relays in 3 gang swich?

3 replies

The easiest thing to do would to write a simple sketch that loops through the gpio pins and toggle them on and off, will displaying the pin number in the serial output window. When you hear a relay operating make a note of the pin number in the out put window and use it to adjust the full sketch to include the additional relays

Cheers

wow, thanks. Now just wait for the next 10 switches and upgrade the whole house :)

You got the additional pins sussed then? could you share here in case others need to know?

Have you checked you have Neutrals available for all of your switches?, I only had three I could use, the rest I've had to do with 833Mhz RF units that don't need the neutral

Thanks, glad it was of use

All fine now, my arduino libraries needed an update ... =) Thx anyway! Great tutorial!

1 reply

Ahh well done, I was just breaking out a Linux laptop to try it on there as I wrote the ible with windows in mind.

Glad you got it working:D

Hi Ape, thanks for your tutorial!

I'm having difficulty flashing the esp with the test_sketch. My Sonoff Touch has the PSF-A chip like yours. I have hooked it up via FTDI232. The GPIO 0 is pulled to ground. In Arduino these are my settings:

Generic ESP8285 Module

80Mhz CPU Frequency

Flash Size 1M(64K SPIFFS),

Upload speed 115200

Port /dev/cu.usbserial...

Programmer USBtinyISP

So far so good, but if I connect and try to upload the sketch I get the following error messages:

Uploading 228400 bytes from /var/folders/x7/3k_46nxj60x0t3bh66bbjg280000gn/T/arduino_build_954407/Test_Sketch.ino.bin to flash at 0x00000000
warning: espcomm_send_command: didn't receive command response
warning: espcomm_send_command(FLASH_DOWNLOAD_DATA) failed
warning: espcomm_send_command: wrong direction/command: 0x01 0x03, expected 0x01 0x04
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

From my point of view RX and TX are connected correctly. If I swap them I get the error:

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

I have tried different upload speeds with no luck. Power issues can be ruled out as well, I have tried flashing with external power supply (sometimes had issues with ESP8266 not getting enough power via USB).


Any Idea what the errors want to tell me? Help is much appreciated!

thanks for the tutorial. Works well with openhab

1 reply

Thanks, good to hear for sure it works with openhab

Cheers

The code is not working because off some reason the switch keeps turning on and off by itself.

1 reply

I have the code working on 4 switches in my house and they are all running fine.

If you hook the switch back up to an FTDI and open the serial monitor it may help you debug you issue

Hello

After a failed first attempt I have finally been able to flash my device, so thank you for your help.

would it be possible to connect the sonoff touch to OPENHAB instead of Homeassistant using the above method and if so how would you go about this?

Many Thanks

2 replies

Ahh glad you managed to get it sorted in the end :D

I think OpenHAB supports MQTT so I can't why not but as to how its done I don't know I'm afraid as I couldn't get on with it.

I'm sure you'll be able to find some help in their forums though?

Cheers

Thank you once again for your help look forward to any of your future guides

Hello

Need your help i am trying to upload the Test Sketch to my Sonoff touch however not matter what i do i always receive the same error message:

espcomm_sync failed

espcomm_open failed

espcomm_upload_mem failed

could you please tell me what i am doing wrong ?

thanks

2 replies

Hi, I know I replied to this question though I have no idea where it's gone so here goes again...

You need to check you are connected to the right port, easy way, plug in your FTDI, open the ports menu option, check the ports available. Close the menu, disconnect your FTDI, re-open the ports menu and which ever one is missing is the port to select when you want to upload.

Have you selected the correct board? The Sonoff touch use the ESP8285, from the tools menu, click Boards and scroll down to Generic ESP8285 Module.

Check your TX/RX connections, just swap them round and try uploading again, I nearly always get these arse about face :P

Make sure GPIO0 is pulled low when you power up otherwise the module wont be in flash mode and you wont be able to upload.

Hope this one sticks about this time, Cheers

Hello thanks for your response. I have tried all the above and still no joy. Am now thinking I may have damaged the board at some point. Thanks for your help anyway