Local Control of Sonoff Switches
April 26th, 2017

Occasionally it is convenient to control devices with a smart phone or tablet,or even a desktop computer. But, very quickly, one tires of having to use a device to light a lamp. And what about visitors? Must they be give a a course on the use of a home automation software to turn a bulb on?

This post is devoted to the manual control of two bedside lamps, each connected to an ITEAD Sonoff wireless switch. The general idea may be of interest to others, the practical details will not necessarily be usable everywhere.

Table of Content

  1. Manual Control of Sonoff Switches
  2. An Example
  3. How To
  4. Modifying the Sonoff-MQTT-OTA-Arduino Settings
  5. Warning
  6. Project

  1. Manual Control of Sonoff Switches
  2. There is the push-button on the face of the Sonoff switch. However it is tiny, almost invisible, and not very easily manipulated. On some switches, I have to use a finger nail to activate the switch because it does not protrude far enough. In this regard, X10 modules were good because a lamp could be turned on or off in the normal way with its normal switch even if it is connected to an X10 module. In addition, it is used for other purposes. The operating mode of the switch could easily be changed using that switch (see Button usage). Consequently, it is often necessary to provide a different way of turning on a Sonoff switch manually.

    Fortunately, an ESP8266 pin, GPIO14, is available on the header used to program it and the Theo Arends' software can use it. It is therefore relatively easy to add a switch to manually control a Sonoff in a more convenient way. The figure below shows where to connect an external switch to the Sonoff:

    TASMOTA supports a momentary push button as well as a conventional wall switch.

  3. An Example
  4. To control two bedside lamps, there are two small cases on each side of the bed, each attached to the back of a dresser serving as a bedside table. Each case contains two push-buttons to control each of the two lamps. This simple and effective arrangement obtained a better WAF rating than the less reliable X10 devices it replaced. The photograph, larger than life, shows that the case is actually a surface mount telephone jack that I modified.

    Four of these jacks were used, two to hold the switches as illustrated above and two others attached to the Sonoff switches. A standard four-wire telephone cable with RJ11 connectors at the ends connects each case to another glued to the Sonoff switch. These two glued telephone jacks are connected by a slightly longer than 2 metres CAT-5 cable which is concealed under the bed and the dressers. I used this type of cable simply because I did not have a four-conductor telephone cable of adequate length.

    B4 and B5 are the fourth and fifth pins of the Sonoff connector (see the first image above). As can be seen, the switch grounds (both pin B4) are connected together and with the four push buttons. Then each B5, connected to the free I/O pin of the ESP8266, is connected in parallel to one of the push buttons of each of the two housings.

  5. How To
  6. The phone jacks (note) are smaller than all others I have seen before. This small size proved to be an asset. The following photograph shows the parts used to make a case with two switches. In addition to the open telephone jack, there are two 12×12mm momentary push buttons with caps (look for "tactile push button switch 12mm" in AliExpress or EBay).

    These items are in the lid of a large plastic jar for mixed nuts. It is the source of the two pieces of black plastic that can be seen in the photograph below.

    With a piece of black plastic cut by hand and rounded with sandpaper, I close the unused opening for the telephone extension wire. In the photograph, this piece has not yet been cut to the right length. The pins of the push buttons have been splayed out to reduce the height. I even lightly sanded the bottom of the push buttons again to reduce their height. Here are the details of fabrication.

    All that remains are the electrical connections.

    A small electrical wire (dark gray here that represents the ground conductor) is soldered to one of the pins of each of the two push buttons and then attached to one of the terminals of the terminal block of the telephone jack. The other connection of each push button is soldered to a wire (blue and white here) that are attached to other terminals.

    Push the cover back on the telephone jack and press the buttons a few times so that everything is placed as it should.

    In the next photo you can see the last step. The connection blocks of the two other telephone jacks are already connected with a cable that is fixed with hot glue. The RJ11 connector has been removed from the jack to make it possible to clamp it to the side of the Sonoff case while it is hot glued to it. Note how the Sonoff case is pierced to allow the two wires connected to pins B4 and B5 of the programming header to pass through the hole.

    When the glue has set, put the RJ11 connector in place and connect its wires along with the two wires from the Sonoff and those from the CAT5 cable using the connection block of the telephone jack.

  7. Modifying the Sonoff-MQTT-OTA-Arduino Settings
  8. It would have been better to enable use of the free GPIO pin in Theo Arends firmware. In the latest and last version (version 3.2.13) this means removing the '//' commenting out the USE_WALL_SWITCH define in line 157 of user_config.h which is part of the sonoff.ino Arduino sketch. Of course, this is done in the Arduino IDE. At the same time the SWITCH_MODE in the previous line should be changed to PUSHBUTTON.

    Since I anticipate making similar changes in other Sonoff switches, I also altered the over the air (OTA) URL in line 42. I will be saving the binary image file of this modified firmware in the HTTP server under the name .sw.ino.bin.

    In the end, these are the changes to user_config.h

    ... 42| #define OTA_URL "" PROJECT ".sw.ino.bin" //**MD** [OtaUrl] ... 152| /*-------------------------------------------------------------------------------------------*153| * Wall switch and HC-SR501 154| \*-------------------------------------------------------------------------------------------*/ 155| #define SWITCH_PIN 14 // GPIO 14 = Standard wall switch to Gnd (Sonoff_TH10A(16A), Sonoff SV) 156| #define SWITCH_MODE PUSHBUTTON //**MD** [SwitchMode] TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON or PUSHBUTTON_INV (the wall switch state) 157| #define USE_WALL_SWITCH //**MD** Enable the use of a standard wall switch to control the relay

    Once the modified binary file was saved, I could have flashed it onto the two Sonoff switches. Instead, I used over the air upgrading as described in a previous post Over the Air Sonoff Flashing.

  9. Warning
  10. I was greatly disappointed during the first tests! While it worked very well on the bench, the lights in the room lit up or closed capriciously. It seemed that the inputs of the ESP8266 floated rather than being pulled towards Vcc by internal resistances. I reopened everything to put 10k ohms resistors between the Vcc (B1) pin and the GPIO14 pin (B5). Unfortunately, this did not bring about any improvement.

    After a moment's thought, the solution became evident. I had given each new Sonoff the same Domoticz index (idx) number previously given to the X10 module it replaced. Foolishly, I had hoped to avoid rewriting timer schedules and some scripts in Domoticz. But those scripts allowed Mochad/Heyu to control the lamps. It was an interaction between these old scripts and the manual switches that gave a final result that seemed random. In the end, I had to get rid of these virtual devices and I removed the unneeded scripts. Rewriting the time schedules and resetting the notification parameters took a lot less time than I wasted trying to locate the problem.

    The lesson is that it is much better to create a new virtual device in the home automation software rather than try to reuse the old one when replacing one physical device with another of a different nature.

  11. Project
  12. The next post will show how I use an X10 wireless controller, the Palm Pad, to turn on or off the living room lamps connected to the home automation system. It is a temporary solution that replaces an old programmable plug'n power controller from Radio Shack (the ancestor of the MT14A by X10).

    The phone jacks purchased in April from a Great Canadian Dollar Store were inexpensive at $ 1.25 CDN. Lately I wanted to get more in two nearby stores belonging to this chain. Their inventory had been renewed, but there were no longer any surface mount telephone jacks. This is unfortunate, because I can not find anything similar on eBay or Aliexpress. Normally sized jacks are available but at around $ 4.00 US apiece.