#21: Six Sonoff Secrets

Posted on January 4th, 2017

Customise your Sonoff home automation controllers with a memory upgrade, weatherproof housing, control switches, sensors, and custom firmware.

ITEAD keep releasing new Sonoff models so this isn’t a complete list, but some of the models mentioned in the video include:

Storage

winbond-25q32fvThe flash memory chip in Sonoffs is 8Mbit, which is only 1MByte. Then if you want to do OTA (Over The Air) updates you need to limit your program size to less than half the available memory so that a new program can be uploaded alongside the old one. And if you use a SPIFFS (SPI Flash File System) to store non-volatile data outside your program, you lose even more memory.

You can replace the flash memory chip with a Winbond 25Q32FV in SOP-8 package, which is a 32Mbit (4MByte) chip. You can buy them on eBay for about US$3 for a pack of 10.

The original flash memory on the Sonoff is a Winbond 25Q08FV, which is the 8MBit (1MByte) version of the same chip.

Thanks to Pete Scargill for this idea! You can see more on his original blog post: “32Mb ESP01 and Sonoff

Safety

Sure, you can just cut a power lead and screw it into a Sonoff, but it’s probably a bad idea. You need to consider how your Sonoff will be used, including physical protection (stop little fingers reaching the terminals!) and liquid protection from spills. You also need to make sure there is strain relief on the cables to prevent them being pulled out, and possibly exposing live mains connections. You can do it cheaply using a plastic project box, and with some cable ties around the cables just inside the box so they can’t be pulled out. The result can be a very neat setup that won’t look like a dodgy DIY cable, and should be safe for general use.

sonoff-powerboard

You can go even further and use an IP-rated (Ingress Protection) case and cable glands, to make your Sonoff waterproof and physically very strong.

sonoff-ip56-case

In this video I gave a very simple explanation of the two-digit IP codes. There are also extensions to the code for other attributes. You can find more information and tables showing the specific meaning of the numbers at en.wikipedia.org/wiki/IP_Code.

Switches

Internet control is fun, but usually you also want some way to manually turn the output on or off without using your phone. You can modify a Sonoff to connect an external button across the pins of the built-in button, allowing you to toggle the output by pressing the button manually. The built-in button is connected to GPIO0, so when the button is pressed it pulls GPIO0 to GND. This is used during power-up to put the Sonoff into bootloader mode, and can also be used to toggle the output or do other actions.

sonoff-gpio0-terminals

Alternatively, you can connect an external button between GND and GPIO14 so that your software running on the Sonoff can detect when it has been pressed. Some firmware, including Theo Arends’s TASMOTA, supports this out of the box.

GPIO14 is exposed on the internal header used to upgrade the software on a standard Sonoff. There is already a pull-up resistor on GPIO14, so you don’t need any other parts. Just connect a button across the GPIO14 and GND pins, and you’re done!

sonoff-header

Even better, TASMOTA has an option to support an external switch instead of an external button. The difference is that with a button, you want the output to change state each time you press and release the button. This means the firmware needs to treat both the button press and release (cycling from HIGH to LOW to HIGH) as a single event. But a switch just changes state (goes from HIGH to LOW, or goes from LOW to HIGH) and stays there because it latches in place. So your software needs to treat each level change as a separate event, and toggle the output. Once again, TASMOTA supports this out of the box.

Sensors

As explained in the section about switches, the regular Sonoff exposes GPIO14 on the internal header that is used for flashing new firmware. The same header also provides GND and 3.3V connections, and the GPIO14 pin is provided with a pull-up resistor. This makes it super easy to connect anything that needs a single digital pin, such as an external switch / button or a one-wire sensor.

sonoff-th-sensor-pinoutTo make it even easier, the Sonoff TH10A and TH16A both feature a 2.5mm 4-way (TRRS) socket that is intended for connecting external sensors. The socket provides 3.3V, GND, and GPIO14: the same I/O pin exposed on the internal header of a normal Sonoff. That means you don’t even need to modify the board, you can simply plug in your sensor or switch externally.

sonoff-th-with-cable-800

The 2.5mm socket also has a 4th connection, but there are parts missing inside the Sonoff so it’s left unused. You can fix this easily by putting a solder blob across two pads on the Sonoff PCB, and optionally installing a pull-up resistor.

In the video I said that there is a 10k pull-up from GPIO14 to 3.3V. However, I didn’t notice that there are actually 2 pull-ups in parallel, so the effective pull-up on GPIO14 is 5k! You can still use a 10k pull-up on GPIO4 if you like, or you can use a 4.7k resistor if you want them to be about the same. 4.7k is common for I2C, but 10k is generally fine too.

sonoff-th-bottom-breakouts

Software

There are many alternative firmware projects for the Sonoff. Personally I love Theo Arends’s firmware, but there are many others that may suit you better.

To learn about how to install new firmware, see SuperHouseTV Episode #17: Home automation control with Sonoff, Arduino, MQTT, and OpenHAB.

If you have any other suggestions please let me know in the comments below, and I’ll update this list.

Sites

Because the Sonoff uses an ESP8266 microcontroller, there are a huge number of sites with information that’s relevant to the Sonoff. These include:

18 responses to “#21: Six Sonoff Secrets”

  1. Jean-Christophe Duperron says:

    Woah! I extremely like your post!
    All the information’s and ideas that was missing from somewhere else!
    Continue your great work!

    And I’m exited to work on ways to embed toggle switch in firmware with states. You have an starting exemple?

  2. PaulG says:

    Great video! Very instructive and your enthusiasm is infectious.
    Question though, re: housing in IP rated boxes (nice explanation btw).

    How much of a negative effect does that housing have on WiFi range of the ESP8266?

    nb Using Chrome 55. no input boxes appear for this form.

    • Jonathan says:

      Thanks Paul 🙂

      That’s a really interesting question about the box reducing WiFi range. My initial reaction was “no, of course not, plastic doesn’t absorb WiFi signals” but then I stopped to think about it a bit more and realised that it *may* have some effect. Industrial cases like the one I used are usually very thick and heavy, much thicker than the plastic on a simple consumer product, and depending on the specific plastic it may contain material that attenuates a 2.4GHz signal. Not likely, but possible. One interesting idea I’ve heard is that if you want check if a specific material will attenuate a WiFi signal, try putting it in a microwave oven for a few seconds and see if it gets hot. If it does, it contains material that absorbs the same frequencies as 802.11b/g etc use. If it doesn’t get hot, the radiation is passing straight through unimpeded. I’ve never tried that myself but I’m tempted to put a box in the microwave just to see what happens. IMPORTANT: make sure there are no captive nuts or other metal parts in the box before you microwave it!

  3. Rolf Marthin Nilsen says:

    Have you taken a look at “Homie” ?

    Homie is a lightweight MQTT convention for the IoT.

    https://github.com/marvinroger/homie

    This is a cool framework. there is also a OTA managment server for it at : https://github.com/jpmens/homie-ota/

    • Jonathan says:

      Yes, I’ve looked at Homie in the past, and I quite like it. I think it’s important to have some sort of structure to MQTT topics, otherwise things get out of control very quickly. When I started using MQTT about 5 years ago I did the usual thing of using random topics out of convenience. It’s easy to do that, but before long you won’t remember what is being published where. I keep a spreadsheet of topics but even that can end up out of sync with reality. Since I’ve been using Theo Arends’s firmware on Sonoffs I’ve been inclined to use his topic structure, just because it’s how the firmware works and it’s easier than maintaining a forked version of his code. His structure is different to Homie: personally I think the Homie structure is better than Theo’s structure because it slices the topics in a different way, with everything oriented around devices rather than message types, but that’s totally a personal opinion. Both ways are “right” if they make sense, and Theo’s structure makes enough sense to me that I’m very happy to use it.

  4. Can you kindly post details of the waterproof box you used?

  5. carlos says:

    Great video, Jonathan. Please keep them coming.

    I found these Windbond chips on ebay but the lower most number listed on the chip does not match your image. Will this matter?

    Here is the link: http://www.ebay.com/itm/10Pcs-WINBOND-W25Q32FVSSIG-W25Q32FVSIG-25Q32FVSIG-SOP-8-IC-Chips-FLASH-BS1-/262478524681?hash=item3d1cf07109:g:LmMAAOSw9eVXWwLa

    Thanks,

    C

    • Jonathan says:

      Yes, those are fine. The part number is correct: the bottom number is usually a batch code or date code in year-week format, so that’s 2016 week 8.

  6. Grant says:

    Just wanted to say thanks for a quality post. You are so right, safety is forgotten all to often. Thanks for demonstrating how safety is part of quality.

  7. Dwalt says:

    I am confused by two topics in this episode, first you suggest and demonstrate replacing the flash chip to allow OTA. Later, you cover TASMOTA which incorporates OTA using the existing Sonoff memory. Did i miss something along the way? Is the existing memory sufficient for TASMOTA version of OTA or does it require a replacement chip?

    • Jonathan says:

      TASMOTA fits in the factory-supplied flash memory, so there’s no need to upgrade if you use it. However, many other people have run out of space on the Sonoff when writing their own firmware. If you use TASMOTA, you don’t have to bother with the memory upgrade 🙂

  8. Ed Darby says:

    Great video as ever Jonathan 🙂

    I was running a Domoticz setup before I found out about the Sonoff (via you)

    The ESP Easy firmware works really well with the Sonoff and Domoticz has a built in MQTT broker.

    http://www.letscontrolit.com/wiki/index.php/ESPEasy

  9. Mick W says:

    Love your videos Jon!
    Do you know if these devices are approved for install into homes in Australia ? I have a couple Belkin products but they are quite expensive. Getting the sparky to put some of these in would be a lot cheaper and very customisable.

    Cheers mate,
    Mick.

  10. Bogdan says:

    Thank you! Really nice video
    Will really like to see your opinion on the Sonoff Touch too

  11. Jon Archer says:

    Hi Jon,

    Always look forward to your vids, especially the MQTT/ESP related ones. I had bought a Sonoff a while ago to tinker with but haven’t really done much with it. I got quite excited when I saw the touch on this video and nearly bought it. I say nearly because when read the blurb i noticed a neutral wire is required and unfortunately, as with most standard wired light switches, there are no neutral feeds anywhere near any of my switches.

    Thanks again for all your content!

    Cheers
    Jon

  12. JT says:

    Help needed:

    I’ve setup a homekit fan accessory using a raspberry pi, HAP-NodeJS, MQTT and a sonoff.

    It works fine but the one problem i’ve got with it is the status on the home app not showing correctly. When I turn the fan accessory ON the status in the home app my iOS device is correct and shows it’s ON.

    BUT if I leave it on for a while then go back into the home app is shows it’s OFF even though the accessory is still ON???

    I switch it ON then OFF again and it turns OFF.

    I used this code here for the .js file.

    https://github.com/jat80/ESP8266/blob/master/Home%20Automation/Part%208/relayLight_accessory.js

    But modified a few lines to make it look like a fan accessory.
    I don’t really have any experience with javascript (or coding in general for that matter) so don’t know if there is something else in the code that could be the problem with the status not showing correctly.

    Any help would be appreciated.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *