
I will show here a slightly less typical type of WiFi transmitter, which allows you to use the bistable button you had previously to control the device. I will also show how you can convert any other kind of smart switch to work with external bistable button by using OpenBeken.
Classic sonoff-type IoT relays have only a small momentary button (monostable) on the housing, so you can't reuse existing bistable button for a lamp. Of course, leaving its switch in front of the intelligent relay will not do you any good, because if it was turned off by its button, the relay would not work anyway.
To be clear - the bistable button has two states in which it stays in when you switch it (it stays on "On" or "Off"), and the monostable one has one constant state, when you press it, it always returns to the first state when released.
There is, however, a good solution to this problem - just connect the aforementioned bistable button to our smart relay and handle it in the program code in such a way that the change of its state resulted in a change of the relay state . It is convenient because then you can also change the status of the relay via WiFi. The only thing that will change then is that the "On" and "Off" positions on the bistable button will lose their importance then, because its actual position will not matter for the controller, only the act of switching it.
Of course, there are ready-made solutions for this that work with the Tuya application, but here I will present the use of such a relay together with my open firwmare for Beken family deals . This will ensure independence from the manufacturer's servers, security and anonymity (the cloud will not start collecting data when you use the devices). In the topic I will also attach some details about the implementation of such a switch in C code.
In addition, I will discuss flashing OpenBeken to WB2S module here again, because in this particular case of the relay there is a problem with it - you will have to cut one trace.
CAUTION
The relays described here operate on a deadly mains voltage potential. In addition, most relays of this type only cut off one wire (there is a single relay), and it depends on the user which one, so it is possible that unfortunately you will cut the neutral and the live will be permanently connected to the controlled device ... I assume that the reader will tell you about this knows everyone and is aware of how to connect everything electrically according to the art. Here I will focus on programming smart switches.
In addition, it is also worth remembering that often the power supplies from such gadgets are also not separated (and e.g.. there may be a phase potential on the GND connected to the WB2S).
Purchase and interior of ZN268131
I bought ZN268131 from one of the websites selling online. It is quite a popular product. It can be found even after 20-30 PLN with free shipping. It is not always marked with the symbol I am showing here.
ZN268131 is distinguished by the presence of a screw terminal for the switch - they are labelled S1 and S2.
It comes in a box like this:




To upload the firmware, remove the plastic cover:


From the bottom you see the 5V transformerless power supply (AP8506), behind it there is a 3.3V LDO regulator providing a stable 3.3V for the WiFi module, then the (KEY1) button, connection of the board with the WiFi module (only 5 signals go there, actually 3.3V, GND, output to the relay (through Q1 transistor) and two inputs - one for the large bistable button, the other for the small one on the housing.
After removing, you can see the WiFI module (WB2S or BK7231T, I don't know why without a metal case this time), there are also some protections (fuse resistor F2, blue varistor) and a filter on the input (two electrolytic capacitors, EC1 and EC2, and a choke between them) ):





Programming ZN268131
Never attempt to power the relay from mains and flash it at the same time! Power it separately, via 5V connected before AMS1117:

In addition, only the RX and TX need to be connected to the WB2S pins:

Initially, I tried to do this:

Unfortunately it did not work. It quickly turned out that the RX pin is also used here as a button and the pull up resistor on this signal interferes with the programming.
The solution is to temporarily cut the track so that it can be easily repaired and clip on the WB2S side:

These 4 cables are enough.
The rest of the programmer is a USB to UART converter in 3.3V mode and a separate power input (I can not take 5V from USB, because to reboot the device I have to connect and disconnect the power, and when I use USB for this, the relay will take too much current and USB from the USB will reset and the USB to UART converter will be "lost" and the programming will fail):

Programming procedure:
1. in bkWriter 1.60 select the batch, start programming
2. while waiting, disconnect and connect the 5V power supply to reboot
3. the module should program itself without errors
This is for the BK7231T - for the BK7231N use a Python programmer.
After programming, fix the cut path:

OpenBeken configuration for ZN268131
I have already described the entire configuration procedure for OpenBK7231T several times. Here I will only show you how to configure the "magic" pin from the bistable button:

That's all - just set the ToggleChannelOnToggle role. The role of "Button" is for momentary buttons.
For the curious - in the code of the ToggleChannelOnToggle program it is organized very simply, it uses the same timer as regular buttons, and also eliminates the problem of debouncing (contact vibration):
Code: c
Calling the CHANNEL_Toggle function switches the channel value (here: relay). In OpenBK it is possible to map multiple outputs to one channel.
Test circuit
From now on, you can control the relay with any classic toggle switch, for example one from the box:



Everything works, you just need to get used to the fact that from now on the state of this switch ("pressed down / up") does not determine the status of the relay, because you can always switch externally from the Home Assistant panel.
A practical example of use
This time without much comment. For a trial, I connected the module for a rather old and cracked lamp, which I saved from electro-waste:





And can you convert an ordinary Smart Switch to one supporting bistable button?
With original Tuya firmware it would be not possible, as we don't have access to source code and there is no pins configurator, but with OpenBeken, i t's very simple.
Just take any Sonoff clone with my OpenBK, for example:

This one has WB2S inside:

Quote:
Pin No. Symbol I/O type Function 1 VBAT P Power supply pin (3.3 V), which is connected to the VBAT pin on the internal IC 2 PWM2 I/O Common GPIO, which is connected to the P8 pin on the internal IC 3 GND P Power supply reference ground pin 4 PWM1 I/O Common GPIO, which is connected to the P7 pin on the internal IC 5 1RX I/O UART1_RXD, which is used as a user-side serial interface pin and is connected to the P10 pin on the internal IC 6 PWM0 I/O Common GPIO, which is connected to the P6 pin on the internal IC 7 1TX I/O UART1_TXD, which is used as a user-side serial interface pin and is connected to the P11 pin on the internal IC 8 AD AI ADC pin, which is connected to the P23 pin on the internal IC 9 PWM4 I/O Common GPIO, which is connected to the P24 pin on the internal IC 10 CEN I Low-level reset, high-level active (internally pulled high) Docking IC-CEN 11 PWM5 I / O Common GPIO, which is connected to the P26 pin on the internal IC
It is easy to find an unused GPIO. You can see that there are several of them:


Bring the wire to it, the other wire to ground. You don't need a pull-up resistor as the BK7231N and T have built-in, configurable pull-ups.

Lead the wires out of the housing (there will only be 3.3V on them, they do not have to be thick):

And it's almost ready - you still need to configure the roles of the chosen pin on the OpenBK web panel and connect the bistable button.
NOTE - depending on the used relay model and connection, there may be a live potential from the mains on the used wires !!
For example, you can convert an old lamp (yes, also saved from e-waste):

Use the original switch:



Connections, additionally I made a joint on a wire so that pulling it by the cord does not tear it out:



Ready:

From now on, the lamp still supports its old button, but it's also smart. For example, you can turn off all the lights (including the lamp) from the Home Assistant when you leave the house.
What else can the additional button be used for?
The possibilities are very large. For example, you can remake a simple LED strip controller, such as described here:
WiFi SmartLife single-color LED strip dimmer - test, interior, diagram
so that it has two buttons (one for increasing the brightness, the other for dimming) and you can also add an LED to it informing about the WiFi status.
The buttons increasing / decreasing the brightness of the PWM channel in OpenBeken already require simple scripting, which my firmware also supports:
AddEventHandler OnClick 0 addChannel 1 -10 0 100
AddEventHandler OnClick 1 addChannel 1 10 0 100
AddEventHandler OnDblClick 0 setChannel 1 0
AddEventHandler OnDblClick 1 setChannel 1 100
The command syntax is simple. After "AddEventHandler" you have the name of the event (OnClick, OnDblClick, OnHold or other), then the pin index (where the button is) and then the command to be executed.
"addChannel" adds a given value to the channel with a given index (it can be negative), the last two arguments are the range to which the channel value can change (PWM is from 0 to 100).
"setChannel" just sets the channel.
Really simple, you only need to solder two buttons (even without pull-up resistors because they are automatically configurable in OpenBeken) and a LED with a resistor, give them the Button / WiFi_LED role and add the above script to autorun.bat in the LittleFS file system in OpenBeken.
For convenience, I used the housing from the telephone filter:





Such a modification significantly improves the use of the LED strip that I have in the workshop. The original Tuya firmware only supported turning it on and off with a single button on the housing, and after my modification I have full control over the brightness.
Summary
ZN268131 seems to be a very ingenious and convenient gadget, but use of open source software (OpenBeken, unless someone knows an alternative to the BK systems used now? ESP8266 is more and more difficult to find in IoT devices) Makes it possible to take any "regular" WiFi controlled relay and make it work like ZN268131.
ZN268131 can also be used with original Tuya software, but this has been discussed more than once.
Of course, when you order Sonoff instead of ZN268131, you can upload Tasmota - but can you, really? Sonoff S40 has already made a "surprise" for users and instead of ESP8266 it has BL602 inside - which by the way I also support in my firmware ...
PS: And when it comes to my saved from electro-waste and converted lamps, both of them already have their places in the workshop and are used only for long-term tests of my OpenBK batch (I verify the stability of the software, the functionality of reconnecting to WiFi in the event of signal loss, long-term operation of the module without its rebooting, etc.).
Cool? Ranking DIY