-
1Flash the Sonoff Mini with Tasmota
The Sonoff mini has a "DIY mode", which is for you, if you prefer learning a new rather complex proprietary process instead of wasting two seconds to pop open the case and another 45 to align a prommer to the flashing pads and flashing the normal way. I was too lazy (as engineers are - if we weren't lazy, we would still spend our days running after the next species to finish) and just popped out the case to find the row of flashing pins, wired my pogo comb properly to the UART (see the picture) and flashed the latest Tasmota with Tasmotizer or Tasmota-Pyflasher.
To get to the flashing mode, keep the button on the other side pressed while powering/resetting the device. Or adjust one pogo pin at the "KEY_IN" pad and use a flashing adapter's flashing control button.
Of course you can as well just solder the 4-5 wires (use thin coated wire like wire wrap wire) to the pads, not much slower if you got only one device to flash.
After flashing, power the device either by any 3V3 or 5V pads inside, or by assembling the device and connecting AC wires to the N & L In terminals. When powered, follow Tasmota instructions; first connect phone or something to the device's AP, write down the MAC address (I always scribe that on the device case with a permanent felt pen), open 192.168.4.1 with a browser, configure WiFi, reboot, find device with Fing/router DHCP list/whatever (now having the MAC address helps), point your browser to that address, configure the rest.
Now we can use the vanilla Sonoff Mini template: {"NAME":"Sonoff Mini","GPIO":[17,0,0,0,9,0,0,0,21,56,0,0,255],"FLAG":0,"BASE":1}
The device's Tasmota main page should now show one "ON" or "OFF" and "Toggle", check that the Toggle button changes the relay state, you should hear a relay click.
-
2Add the new terminal
Detach AC wires, pry open the device with a screwdriver/club card/<insert your favourite tool>, find the screw terminal blocks. In V2 the switch terminals are gray, in V1 black, find the ones marked S_IN and S_OUT = the ones with no thick solder snakes coming from them.
Those screw terminal blocks as 2-way and 3-way are very common; you probably have a practically vacant 3-way one in some Chinese device which you never figured out how to use as your Mandarin sucks. Or already have bagful of those waiting for this exact moment (now you know), like I did. With a hot soldering iron solder and pry off the S_IN / S_OUT block (minor violence required, the block has a small tab inside its neighbour). Use a piece of proto board as a template and drill a 1.5 mm (take the inch conversion as homework, if needed) hole two proto board holes away from S_IN. Place a 3-way screw terminal in the holes, solder.
Now that we have the terminal, need to wire a GPIO to that. GPIO3 = RxD has a nice pad and we won't probably need it for serial communication anymore so lets use that. The stock switch circuit has a 1 kOhm resistor between the terminal and GPIO, lets do the same. To get a good pull-up for the pin when the switch is open, wire a 10 kOhm (copied from the stock S_IN circuit) between the terminal and the 3V pad.
Should look somewhat like this before moving the shrink tube pieces around the exposed resistor ends to be shrunk:
The stock circuit also has a noise filter capacitor (I measured 32 uF but can be much smaller, down to maybe 0.1 uF, bigger makes it slower which may help in (electrically) noisy conditions) between the terminal and ground. Could now, or when having strange spontaneous state changes, add a 0.1uF or bigger capacitor between the terminal and S_OUT (=ground). The original circuit also has a transient-voltage-suppression diode array D4 for protection against lightnings etc spikes, that wouldn't be a bad idea.
After wired and careful visual checking (you want to get the loose solder blobs away before connecting to AC), assemble the device again, attach AC, check if the IP address still responds.
-
3Configuring Tasmota
Now that we have the dirty stuff done, lets make Tasmota know about it. Open browser at the device's IP address, you should see the Tasmota main page. First click "Console" and there type "SerialLog 0"<enter>, that will free the serial lines to us:
Navigate to Main Menu->Configuration->Template, add a switch and a relay (more about that later):
Blue ones are the stock relay and S1 / S2 switch, red ones are the new ones.
I have defined the switches as Switch_n as that disables ESP's built-in pull-ups, which are not needed as we have external resistors. Just a tiny amount of electricity consumption = heat removed from the chip this way, no functional difference. Professionally I have learned to be careful with these tiny currents that tend otherwise cumulate to be huge.
Defining GPIO3 as "Switch" and leaving out the 10 kOhm pull-up resistor would probably work fine as well; the internal pull-ups just are rather weak and I prefer to pick values myself.
After clicking Save the device will reboot and you'll see something new on the main page:
The shiny new rightmost set of ON / Toggle are the "relay" at GPIO1 we just defined - there is no real relay but Tasmota doesn't know that and shows us nice state display and control for that. The stock switch input is now Switch1 on the left and the new one is Switch2 on the right, and these switches are automatically linked to the relays "1" = the real one and "2" = a virtual one that is just a GPIO.
Now that we are done with the UI, lets go to Console. Some commands we could use:
- SwitchMode<switch number> <mode> - Configure the switch type; mode = 0 for corridor/stairway switch pairs where switch orientation doesn't relate to the state, mode = 2 for conventional switches with "On" and "Off" positions. Mode = 1 would be this inverted, if need to swap "On" and "Off". For example: SwitchMode2 2 - set the new switch to normal on/off mode.
- PowerOnState<relay number> <mode> - Configure the initial mode of "relays" after startup, 0=off, 1=on, 3=keep last. Unfortunately so far no mode to use whatever the switches are set to.
- SerialLog 0 / 1 - This disables or enables (when no external wires connected) serial logging so that we can use the pins for our purposes, or get logging if needed
That's pretty much it, you should now have a Sonoff Mini connectable to a dual switch where one switch affects the relay (and sends MQTT messages) and the other just sends MQTT to control a light somewhere or do anything you like. Just as these things should.
-
4Adding an external relay for the second circuit (optional)
If you happen to need another relay (or an electrical output for something else) for the new switch instead of just sending state changes over MQTT, you sure can, it's your device. To make it more convenient and reliable, we chose the GPIO1 with its nice "Tx" pad as the virtual relay pin, instead of for example GPIO14, that would have been totally free - but without a pad.
Relays are too power hungry to be directly driven by a GPIO, thus get a 3V3 TTL input relay module with a FET driver, like these. Solid state relays may be more convenient packages but beware they are not real switches and might have problems with non-resistive loads like switching power supplies, LED lamps etc.
Whatever you are connecting to, better consider any wires and devices connected to this kind of AC devices "hot", or having high voltage, as circuits not supposed to be exposed aren't guaranteed to be safe to touch. Install everything in a proper junction box, use optocouplers if really need to get wired signals out / in.
BTW, instead of adding another relay into a Sonoff Mini, there are dual relay devices like this Blitzwolf BW-SS5 2-gang, just slightly bigger (but you miss the fun):
Unlike the Sonoff Mini, this BW-SS5 practically requires extension blocks for both phase and neutral as you'll need the loads AND the device having wires to neutral, plus the switches (usually combined to one) AND the device connected to phase. To the mini on the other hand you can connect everything (except the missing second relay of course) directly thus it can fit significantly smaller spaces. If you need the second relay, the Blitzwolf is likely more compact, more convenient at least. A feature I don't like is that the switch is AC based, requiring mains grade cabling and switch, and a bit tweaking as from AC the MCU is receiving pulses instead of digital high/low. I had to run on console "SwitchDebounce 209" to make the outputs stable and not affecting each other. This practically means integrating the input, which causes a noticeable, but not very distracting, delay between flicking a switch and lights turning on/off.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.