2023-02-12
md
Local Control of Two Sonoff Switches
<-Local Control of Sonoff Switches

One of my first home automation project was designed for the convenient use of two bedside lamps. The idea was that either occupant of the bed would be able to turn on or off both lamps without having to reach over someone who could be asleep or having to get out of bed and walk around it to turn off the lamp furthest away. This was by no means a new concept; I have seen this done in hotel rooms with four three-way switches. Because the bedside lamps were controlled by Sonoff Basic Wi-Fi switches, I implemented the idea in a different fashion. The following image shows how it was done back in 2017.

schematic and photo of remotes

The now deprecated Sonoff-MQTT-OTA-Arduino firmware made it possible to control the Sonoff relay with GPIO14 of the ESP8266 microcontroller. I just added two normally open push buttons in parallel to that pin. The same could be done with the replacement firmware Sonoff-Tasmota, now called Tasmota (see Updating Sonoff Switches with Local Controls from October 2017). Over the many months since that scheme was put into service, a couple of facts have emerged.

Futile attempts at solving the problem with the addition of R-C circuits and varying the button timing parameters were replaced with a new arrangement that has been running without a problem for a couple of months.

schematic and photo of new remotes

Without the long 2.3 metre Ethernet cable running across the bed between the two Sonoff switches, which was probably a sensitive antenna for all sorts of random electromagnetic emissions, there have been no false toggling of either bedside lamp.

remote button connections on the Sonoff board

The GPIO14 and ground connections are available on the programming header of the first revision of the Sonoff Basic. The connection to GPIO0 can be made by soldering a wire to one of the pins of the built-in tactile push button. I hot glued a female Dupont connector to the top of the board and drilled a slot in the upper half of the plastic case of the switch to allow a male Dupont connector through. Four-pin Dupont connectors were used to provide a key to ensure that the connectors were matched correctly. The N.C. receptacle is replaced with a filler on the female connector and there is no corresponding pin in the male connector. As far as I know, GPIO14 is not available in later revisions of the Sonoff Basic which is unfortunate.

The only interesting thing that remains to be discussed is the firmware running on the Sonoff. Version 9.1.0 of Theo Arend's excellent Tasmota is installed on the ESP8266. It is not the most recent version but it is what is on all the other similar devices in the house. The "clever" bit here is to modify the Sonoff Basic configuration, add a second button on GPIO14 and a corresponding non-existent relay on GPIO4. That second non-existent relay is needed to ensure that Tasmota does something with the button presses on GPIO14.

Tasmota configuration

Here is the backlog command I use to quickly configure the Sonoff in the Web console.

Backlog Devicename lampe-michel; Hostname lampe-michel; Topic lampe-michel; FriendlyName lampe-michel; mqtthost 192.168.1.22; loghost 192.168.1.22; syslog 1; template {"NAME":"Sonoff Mod","GPIO":[32,1,1,1,225,0,0,0,224,320,33,0,0,0],"FLAG":0,"BASE":1}; Module 0; DzIdx1 6; DzIdx2 5;

Use that command as a guide, it will be necessary to adjust the various device names, the IP address of the MQTT broker and syslog server (if one wants remote error logging) as well as the Domoticz device index numbers (DzIdx1 and DzIdx2).

When the extra button is pressed, Tasmota toggles the state of the fake relay and sends out an MQTT message that will be read by Domoticz. The latter will use that short message with the domoticz/in topic to update the state of its virtual second bedside lamp to On as shown below. Then Domoticz sends a longer message with the domoticz/out topic reporting on the state of the second bedside lamp. This message is interpreted by Tasmota running on the second lamp's Sonoff switch as an instruction to close its relay and thus turn the bedside lamp on.

[domoticz/in] - {"idx":5,"nvalue":1,"svalue":"","Battery":100,"RSSI":8} [domoticz/out] - { "Battery" : 100, "LastUpdate" : "2023-02-13 14:39:44", "RSSI" : 8, "description" : "", "dtype" : "Light/Switch", "hwid" : "2", "id" : "00014055", "idx" : 5, "name" : "Lampe Helene", "nvalue" : 1, "stype" : "Switch", "switchType" : "On/Off", "unit" : 1 }

If there is no MQTT broker on the local network, this could be accomplished with HTTP messages (see commands WebQuery and WebSend) using a rule (see Two-way light switches without MQTT).

<-Local Control of Sonoff Switches