-
GPIO wired
12/08/2020 at 21:06 • 0 commentsWired GPIO3 (Rx) through a 1 kOhm resistor to the new screw terminal and pulled up with a 10 kOhm one, no capacitor or protection diodes so far but seems working fine, the device booting up in any switch position and all position combinations properly recognized.
Before shrink wraps (too thick ones I admit) were shrank:
The "S_OUT" terminal is tied to ground, will eventually add a bypass capacitor (like 0.1 - 1 uF) between that and the new terminal.
In Tasmota template configuration I defined GPIO3 to be Switch 2 and to see the state of the other switch, defined the unused GPIO14 to be an imaginary "Relay 3". Tasmota doesn't directly show switch states (yet) but does show states of relays, real or not, and relayX by default follows switchX, essentially showing my switch state.
Note that I have set switches to "Switch_n", which just means the internal pull-up resistors aren't used, as I have the external resistors. Without the "_n" you could do without the 10 kOhm pull-up resistor, but whenever not too inconvenient, I like to set my pull-ups explicitly in hardware.
In the main page I can see the switch states, unless I have changed them by MQTT or these buttons. The first ON & Toggle combo are linked to the real on-board relay, the other group just to one unconnected GPIO, but that's fine as I just want to get the switch state to Node-Red, and just in case be able to control and see it here.Using the serial Read pin as input doesn't seem to affect normal usage of the device, but could as well disable serial logging (dunno if it affects Rx really but might disable hardware serial). The command is "SerialLog 0":
Tasmota documents using buttons and switches here. The interesting options are SwitchTopic and SwitchMode.
After reading the documentation I wasn't completely sure (an English phrase meaning "no slightest clue") how they actually worked, so here's some simple testing:
SwitchTopic2 0
22:12:42 MQT: stat/power/mini1/RESULT = {"POWER2":"OFF"}
22:12:42 MQT: stat/power/mini1/POWER2 = OFFSwitchTopic2 1
22:09:51 MQT: cmnd/power/mini1/POWER2 = OFF <<---The MQTT command summoned by the "SwitchTopic 1" spell
22:09:51 MQT: stat/power/mini1/RESULT = {"POWER2":"OFF"}
22:09:51 MQT: stat/power/mini1/POWER2 = OFFSo, doesn't really matter, I get some MQTT anyway. Without the "virtual relay" might matter.
"SwitchMode 0" and "SwitchMode 1" (or 2 for inverted) work exactly the same when just the physical switched used, but with "0" it gets out of sync if MQTT or the buttons are used to switch the state. With "0" any change of the switch changes the state, even if it already was in the logical target state, with "1" the mechanical switch stays in sync with the button. "2" would be an "inverted 1", if the logical on/off states are opposite to what Tasmota shows. "2" is the one that I'm using as then a closed switch would be shown as "On" in Tasmota.
Which SwitchMode is for you?
- If you could draw "On" to one side of the switch and "Off" to the other (like conventional room light switch), use "1"
- A variation of the previous line, if Tasmota shows the switch state opposite to what you'd expect, use "2"
- If the physical position of the switch(es) doesn't relate to the expected state (like a corridor/stairway pair), use "0"
To check the actual state of the switches, you can use the "status 10" command:
-
Figuring out the best way to do it
12/06/2020 at 22:03 • 0 commentsSo, there is one input and I need two - easy task as there are only four GPIOs used (button, relay+led, another LED, S2) and all the open firmwares support using any GPIO for anything. The stock Sonoff firmware I didn't look at, I prefer to control my devices myself instead of some Chinese cloud service.
I see a ESP8285 on the PCB so I already know it can be done. First drilled one more hole for replacing the stock gray (black in V1) two wire screw terminal by a three wire one, using a piece of proto board as a template .
Then need to figure out the most convenient GPIO to connect the new input to, GPIO0 and 2 would have nice pads, but the device won't boot, if any of those in a wrong state thus need to use something else.
Didn't find a convenient pin layout diagram for the 8285 so added GPIOs to the datasheet's diagram:
Stock GPIOs:
- 0: Input, button (doubling as flash mode switch), wired to a pad
- 1: Wired to the Tx pad
- 2: Available, wired to a pad, but having boot restrictions
- 3: Wired to the Rx pad, might be viable
- 4: Input, S2 switch screw terminal
- 5: Available, no pad
- 6, 7, 8, 9, 10: Used by internal flash (long story short)
- 12: Output, relay and red LED
- 13: Output, blue LED
- 14: Available, no pad
- 15: Boot restrictions
- 16: OTA jumper in V1, apparently not used in V2. Cannot be tied to ground, doesn't support interrupts etc.
GPIOs 5 and 14 are available, fully free to use and both are the last pin of their row so relatively accessible, but need soldering to the tiny 0.5 mm pads, which might need some practice and microscope (both of which I do have but prefer to productise stuff to be applicable to broader public).
GPIO 3 = Rx has a pad and doesn't look to be used for anything (except flashing of course), might be a more convenient choice.
In the older V1 version (with external antenna included, V2 has a tiny internal antenna and pads for an external one) GPIO 16 is routed to a 2 pin header, thus might be viable in V1 (just use a resistor between the pin and ground) but not in V2.
At least in the V2, the S2 input is connected to the pin (GPIO 4) via a 1kOhm resistor, and the pin is pulled up by a 10 kOhm resistor:
In addition to the resistors there is a ceramic capacitor C23 (measured 32 uF in my devices) and a transient-voltage-suppression diode array D4 protecting against possible high voltage pulses induced to the switch line.
I'd suggest adding at least similar-ish resistors and some capacitor (value is not critical, something like 1 uF should do fine) to the new line. The protection diode array would be good to have but if you're not going to accidentally connect mains voltage to the switch line and your location doesn't have much lightning spikes, it'll probably be fine without (your consideration of course, I'm not recommending this).