Looking for detailed description for hacking a sonoff

  • Contest Winner

    One of the prices of the contest is a Sonoff. I really love the size of it. I've tried it and it works great.

    What I'm not fond if, is the fact that the state and the logic are being stored on a website. I just like to keep things like that in my home.

    I saw some articles that you can mod the sonoff to an mqtt device. But I've never worked with the ESP so I'm looking for a detailed step by step article to modifiy the sonoff.

    B.t.w. nice gadget for a really low price 😉

  • Admin

    Couldn't your just upload a regular MySensors gateway sketch to it? Controlling whatever pin they use for the relay?

  • This article shows you how to connect an FTDI to the sonoff


    From there, I think this tutorial will get you going


    leading well into this project on Github, which, I think, is what you were referring to for MQTT on the Sonoff (all of this, of course, removes the original cloud functionality)


  • And then there is this:


    Would get you going pretty quickly (the FTDI explanation is a little more clear that the earlier link with the picture)

    I haven't yet made a MySensors node with an ESP, but it's doable


  • @TheoL I have just uploaded ESPEasy R78 stable firmware. It works really well on existing WiFi with Domoticz. The biggest advantage of it as a remote relay is that it reports its status to the virtual sensor in Domoticz. This is something z-wave devices do and something I've been missing with MySensors.

    @hek I think the memory is only 1Mb (Wemos D1 mini - 4Mb) and I am not sure it would be enough for the GW (with signing and other stuff).

    Price wise though this cannot beaten ($7 for the RF + ESP8266 version)

  • How could we make this a MySensors node?

  • @Mark-Swift I do not think you can.
    IMHO one needs to (a) upgrade EEPROM. This is doable but very tricky the flash chip is in a place not easily accessible by a soldering iron
    (b) given that it is based on ESP, connect nrf24l01+ to it. Please see below how FTDI is connected - I think the same way you can connect Arduino Pro mini. I do not think you can connect nrf24l01+ directly as the GPIO number is limited (c) come up with a sketch for ESP8266 as a node.


    In my view this is just not worth the effort.

  • Admin


    Couldn't you just run the MySensors ESP gateway on it (presenting it as a binary switch)?

  • @hek two issues with your proposal:

    1. ESP-12 which is used for MySensors ESP GW has got more GPIOs and 4Mb memory. Sonoff has a limited number of GPIOs and 1Mb memory marginally better than ESP-01. This can be upgraded, but not easy. I can do SMT, but here is a challenge. See below


    1. connect nrf24l01+ - I did not figure out how to do that given that GPIOs are limited. Sure Arduino can be connected, but it is not the same

  • A better photo for your understanding.
    I know some people upgraded the chip, but the board got really messy. I do not think I can stick my fan into it - too many plastic components which will start melting.


  • Admin

    I mean skip the NRF2401.. just run it as a standalone gw/node.

  • @hek in this case it is doable. Not sure if 1mb Is enough though

  • Hardware Contributor

    I hacked a sonoff dual:

    It should be easy to upload a wifi gateway sketch. I made an MQTT integration.

  • Thanks for the great replies.

    I'm not currently using MQTT, I'm running the Ethernet gateway via a W5100. Can I use the ESP boards, i.e this or the nodemcu without the need for an NRF24?

  • @Mark-Swift how would you intend to use Sonoff with MySensors? Do you have the ESP GW?

    I am using several Sonoffs with Domoticz via a JSON script. https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's

  • So - I agree re: Sonoffs - we've written our own software at the tech blog and though not quite step by step it is easy enough to use and ROMS are available to save you having to get into compiling code. We use Node-Red and MQTT as the base of everything - so you have a number of ESP sensors and relay controllers etc all talking to a Raspberry Pi - or something much cheaper like an Orange Pi Zero running Armbian for example - and basically you're sending simple MQTT messages around the house - nothing has to go outside unless of course you want remote control at a distance. Modifying Sonoffs is easy - I'm sitting in front of some now updating them with our software - just in the process of figuring our which port is used for the second relay in the DUAL product.

  • @alexsh1 Replace the FLASH in the Sonoff - easier in the latest models as they've put the FLASH chip on the back (Sonoff TH16 etc) - on the originals it was jammed in next to the programming button and a pain to replace.

  • @peterscargill Peter, I am stuck with the previous model (WiFi + 433) and changing FLASH is literally impossible even with a small tip. Using a hot fan is not an option as it would melt down all plastic including the button.

  • Here is a working example of using Sonoff with MySensors and the mqtt gateway.
    No need to replace any flash etc...

  • @Efflon we were talking about a different software whereby more flash was required
    Besides 1mb flash is not much in the esp8566 world

  • So I have been debating on getting some of these sonoffs. I am assuming the WiFi gateway in the build section of the site is what I would need to build? Is there a better option for using these with my Vera Plus?

  • @dbemowsk no, the wifi gateway above is to connect nrf24l01+ via wifi. For Sonoff I just reflashed it with easyesp and it works via my normal router with Domoticz. You can have the MQTT setup with Sonoff - thats what @peterscargill did

  • @alexsh1 said in Looking for detailed description for hacking a sonoff:

    For Sonoff I just reflashed it with easyesp and it works via my normal router with Domoticz. You can have the MQTT setup with Sonoff - thats what @peterscargill did

    Will it work with Vera? I purchased one of these recently from WalMart which also runs on an esp8266 module. I wonder if I can run run the easyesp firmware on that one also?
    alt text

  • @dbemowsk Unfortunately, I am not familiar with Vera. Can you send http or json commands from Vera? If yes, it will work. On RPi this is easy to implement.

    Generally speaking you can flash many devices with easyesp if you find how to connect FTDI. I have ESP-01 and ESP-12 running easyesp. Again I am not familiar with the device you posted (the most important question is does it have FTDI or can you identify it by soldering corresponding pins?) and suggest to ask the same question on the easyesp forum.

  • @alexsh1 I think all the information I need to flash it is in this post that I found on Hackaday. It is a bit different than the sonoffs in that it does not have an unpopulated header port for programming, so you have to solder directly to the module to get it programmed, but what the heck, I'll give it a shot.

    As far a s controlling it from Domoticz, I am assuming that you just created a dummy switch that you used URLs for the on and off commands, correct? I will look into how to do that from Vera. Before I got my Vera (a month ago) I was using Domoticz.

  • @dbemowsk yes, with Domoticz it is very straight forward, but you have to setup the Sonoff as well.
    Check this out- http://www.letscontrolit.com/wiki/index.php/Tutorial_Domoticz_Switch

  • @alexsh1 Excellent information. I may try flashing the module that I have today and see if I can get it to work.

  • I have a standard ethernet gateway (NRF) and my controller is the home automation system 'Homey'. I presume I cannot connect these directly to my controller?

  • @Mark-Swift the problem with home automation in general is a verity of controllers 😉 I'm not familiar with Homey, but if it can handle MQTT, Sonoff will work with it.

    A quick google found this:


    you are participating in this thread 😉

    PS I have a standard Ethernet GW too. My controller Domoticz can handle JSON/http requests which makes it easy to handle devices like Sonoff connected to my home wifi and not requiring any further GW. Obviously, the setup can be more complicated with MQTT GW and Sonoff MQTT firmware

  • Please checkout my hack of the Sonoff

    Apart from turning the relay on/off it offers the following features

    The device is managed via a set of commands published from any MQTT client. Statuses are published by the device. Subscribe to them on your MQTT client.
    SSID/Password management - The local SPIFSS file system is used to store an array of WiFI credentials.
    Extra GPIO On/Off. There is an 'extra' GPIO available (next to the Ground pin on the UART breakout on the Sonoff.


  • @mgaman Interesting code but I have a hard time understanding how you set ssid + passwd , through mqtt with your hard coded wifi user pass?

  • @Efflon
    Did you read the PDF file? There I explain the methodology
    From an MQTT client I publish commands which the Sonoff subscribes to.
    These commands allow me to add to or delete from an array of SSID/Password pairs held in a file in the ESP8266 SPIFSS file system.
    On power up I scan the local networks for SSID's and search the local file for matching SSID's.
    If a match is found, I use it for connecting to the local WiFI network.
    Does that help?

  • @mgaman Didn't read the pdf, but thanks for the explanation. Nice trick to make installation easier.

  • Hello @mgaman ,

    Nice modified version. Because I can't flash it for now, can you tell me wether it's possible to use json commands together with MQTT features?
    Let me explain : json requests sent from my home automation software are more reliable than MQTT, as they get acknowledgement via the HTTP response, so the sender immediately knows wether the device got the order or not. This is very important to me, as it may be necessary to send orders more than once if the sonoff device is far away from the wifi router. Maybe I'm not clear, your opinion will be appreciated.

  • @Foune What automation software are you using? I have a Vera Plus controller and use a firmware called ESP Easy with my sonoffs. The Vera plugin that I use sends an HTTP request something like this:,12,1

    That is to tell the sonoff to use GPIO pin 12 which is my relay, and send it a 1 or ON command. The sonoff sends an immediate JSON response like this:

    "log": "GPIO 12 Set to 1",
    "plugin": 1,
    "pin": 12,
    "mode": "output",
    "state": 1

    This tells me that GPIO pin 12 was successfully set to a 1 state.

    The Vera plugin will occasionally check the up status of the node by sending a ping and checking the response. On the Vera controller, the device will either show Online or Offline.

  • Thanks for your detailed answer. If your sonoff device is used in a scenario and the order is not received by it, your vera won't do anything to solve that. I'm looking for a synchronous protocol acknowledgement between the sender and the receiver so that I won't need to code a scheduled task to check whether the command was executed. I use eventghost together with domoticz : domoticz when its plugins are OK, eventghost if I want a fully customizable management, like for my 433 devices which are sometimes lazy to trigger.

  • @Foune said in Looking for detailed description for hacking a sonoff:

    If your sonoff device is used in a scenario and the order is not received by it, your vera won't do anything to solve that.

    Correct, but that is coding that needs to be done on the controller side and has little to do with the device. If the device receives the request, it will send the JSON response back.

    I'm looking for a synchronous protocol acknowledgement between the sender and the receiver so that I won't need to code a scheduled task to check whether the command was executed

    As I said, that needs to be coded on the controller side. If the sonoff doesn't receive the request, how is it going to send anything back saying that it didn't receive it. In the example I gave above, it shows the JSON acknowledgement that the command was received. The only thing you can do is write into the code that if the response is not received within some specified period of time, then re-send the command.

    Your 433 devices as you say are sometimes lazy to trigger. They may not be triggering because they did not properly receive the command sent by the controller.

  • @dbemowsk All right, so we all agree about the superiority of the json over the MQTT in term of synchronous acknowledgement. This is why I'm asking to mgaman whether I can send commands to the sonoff using json, having in the same time the MQTT features : status report, command, and his new wifi features. I'm sorry if it is a native feature, having read the official doc It seems that chosing one protocol disable the others.

    EDIT : I have my answer, I read the source code, there is only the MQTT feature on mgaman's version. I wish I had this wifi MQTT management in addition to the original espeasy code.

  • @Foune said in Looking for detailed description for hacking a sonoff:

    All right, so we all agree about the superiority of the json over the MQTT in term of synchronous acknowledgement.

    I never said that. I was merely commenting on how my setup works using the ESP Easy firmware which does send a return JSON response. I have never used MQTT, so I don't know if it sends a return response to a command or not. If it does, then I don't see any difference other than how you would process that response on the controller end.

    Also, with ESP Easy, I don't as you say "send commands to the sonoff using json". I send an HTTP request and receive the response in JSON format. If @mgaman has a way with MQTT to retrieve the status of a node, e.g. ON or OFF, then on your controller side code, you could send your ON/OFF command, and then right after send a status request command and check the response from that. Depending on the response, you could re-send the ON/OFF command if necessary.

  • Hello!
    I have several sonoff RF, which I bought recently. They are with the original firmware. At this point I control the sonoff manually or through the amazon echo dot. I wanted to control the sonoff also through Homeseer which as far as I know supports HTTP / JSON request and MQTT if needed, via plugin. My question is this: it is possible to keep the voice command via amazon echo?

  • @Antoliveira I don't know if there is an Amazon Echo plugin for Homseer, but if there is, you could convert them to work with homseer and run them with the echo via Homseer. This is what I do with my Vera controller. I have never used the original firmware. I didn't even know that worked with the echo, but my guess is that if you changed firmware that that would not work the same any more. The firmware I use on the Sonoffs that I have is called ESP Easy. Controlling a device is as simple as an HTTP request, and the firmware provides a JSON response. The commands are like this:

    HTTP://<SONOFF_IP>/control?cmd=GPIO,12,1 //this will turn on the device
    HTTP://<SONOFF_IP>/control?cmd=GPIO,12,0 //this will turn off the device

    I have a plugin on my Vera controller that controls the Sonoffs, and then I have another plugin that allows my echo to control my Vera enabled devices. The echo will not control the Sonoffs directly though.

  • @dbemowsk
    Thanks for the answer
    That's exactly what I want to do.
    The homeseer has a plugin for the amazon echo, the only difference being that instead of commanding the sonoff directly (eg: "Alexa turn off Kitchen lights" I will need to say "Alexa tell Homeseer to turn off kitchen lights"
    The most negative point will be that without the computer on which the homeseer runs, I will never be able to command the sonoffs by voice ....
    Do you know if ESP Easy will work correctly on sonoff RF?

  • @Antoliveira My understanding of the Sonoff RF devices is that they are the same ans the regular Sonoff devices, with the only difference being that it includes a 433MHz RF receiver. The 433MHz receiver lets you control the device with an RF remote or other 433MHz controller. From what I read on it, the Sonoff RF still uses the same ESP8266 chip and is set up pretty much the same, with the RF receiver triggering the ESP8266 through GPIO 0 which is the same GPIO port that the on-board button is connected to. Because it still uses the ESP8266 chip, the ESP Easy firmware will still work. The only thing is that the 433MHz control will not work by just installing ESP Easy alone. ESP Easy is very configurable in how it functions, so you will have to basically tell it how to use the 433MHz functionality. It is the same as configuring the on-board button to work with it.

    Here is some information from the LetsControlIt forum that gives some information.

    Here to is a link to my website blog post where I show how to configure the Sonoff to work with the Vera controller, but this section of the post explains how to configure ESP Easy to use the button as a local control switch. This should also work with the 433Mhz receiver.

  • @dbemowsk
    Relevant and good quality information.
    I fully understand what has to be done.
    Now I have no excuse to at least test a drive ...

Log in to reply

Suggested Topics

  • 4
  • 1
  • 5
  • 7
  • 1
  • 19
  • 20
  • 4