I want to use Sonoff-basic with the following switch, because it is equipped with an LED that I want to use as a remote indicator of the relay status.
This is the internal circuit diagram of the switch (3 pins):
The switch must operate in 'OR' with respect to the control MQTT commands.
- If the switch is OFF, the relay is controlled with the MQTT commands or with the button included in Sonoff, and the LED indicates the status of the relay.
- If the switch is ON, the relay is closed and the LED is on
- When the ON switch goes OFF, the relay status depends on the received MQTT commands: the relay can be closed (and LED on) or open (and LED off).
You have to imagine a timed heating, with the possibility to switch it ON manually at any moment. Once the manual intervention has been completed, the timing must continue undisturbed.
By connecting the switch as shown in the figure (the added pulldown resistor is 4.7 KΩ):
- if GPIO14 is programmed as input, the switch position can be read,
- if GPIO14 is programmed as output, and has a high value, the LED will light even if the switch is open (required current: 1.7 mA).
The software must therefore operate in polling (it is not possible to generate an interrupt when the LED is ON and the switch goes from OFF to ON): the SW must read the switch status every 100 ms, and after must turn ON the LED with the switch open when necessary.
You could create an ad hoc SW with Arduino IDE, but it is easier to use esp_mqtt which offers, among other things, a MQTT broker + client with a script language that is ideal for these tasks.
In the firmware dir are the compiled files, so you can simply update the Sonoff FW using esptoot with the following command (in WIN 10, COM6 can change):
esptool.py --port COM6 write_flash -fs 1MB -fm dout 0x00000 0x00000.bin 0x10000 0x10000.bin
For more information on how to program a Sonoff, see http://randomnerdtutorials.com/how-to-flash-a-custom-firmware-to-sonoff/
You must then load on Sonoff the script code here (sonoffscript.txt), e.g. using a local web server:
script http://192.168.???.???/www/sonoff/sonoffscript.txt
Last you must wire the switch to 3.3V, GIPIO14, GND: all are on the 5 pin adapter used to program Sonoff.
Now you can control the Sonoff relay old-way, using the switch, or via any MQTT client (also on smartphone), and you don't neeed any external MQTT-broker.
Comments