
Here I will show how you can easily use a TuyaMCU device (dimmer) with my OpenBeken and connect it to Home Assistant . OpenBeken is an alternative, open source software for IoT devices, offering, among others independence from the manufacturer's cloud, compatibility with many ecosystems, configurability and support for various platforms, including BK7231T, BK7231N, XR809 and BL602.
For more information about OpenBK, please see: [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
For more information about BL602 support: Tasmota replacement for BL602, programming, pairing with Home Assistant.
What is TuyaMCU?
TuyaMCU is a communication protocol used between the microcontroller and the WiFi module present in Tuya products:


Purchase of a dimmer
I bought the product some time ago. It can be found by searching for MoesHouse DIY Smart WiFi Light LED Dimmer Switch Smart Life / Tuya APP Remote Control 1 Way Switch Works With Alexa Google Home - 1pcs . Now its price has probably increased a bit, but you can also find it cheaper on various websites:

The maximum power of lighting for this dimmer is 150W. The WiFi standard is 2.4GHz 802.11 b/g/n. It works with Tuya Smart / Smart Life applications. Its size is 51x46x22mm.

The set also includes instructions:






The interior of the dimmer
Now it's time to look inside.
It is really easy to remove the casing.
Here, the photo gallery will be a bit unusual, because I also have the same dimmer damaged, I got it from one of our users, so I will put both in the photos




Two dimmers next to each other, functional and damaged:


What does the dimmer have inside? First of all, the WB2S WiFi module + Holtek HT66F3195 microcontroller. They communicate via UART.

They are powered by BP2525 (transformerless power supply), and next to it we have a whole circuit associated with a dimmer (S3M rectifier diodes), an optocoupler to detect the sine cycle, an unmarked controller circuit and ... SIF12N65F. Originally I was expecting a triac here, but this is an N-channel MOSFET (Vds = 650V, RdsOn = 0.6Ohm, Id = 12A). This explains these four rectifier diodes. This dimmer apparently rectifies the mains voltage and then keyed it with a transistor.
WB2S programming
I did the flashing just like in the previous topics in the series, by bkWriter 1.60.
I desoldered the WB2S from the board using a flux and a soldre wick. It's the easiest way for me because it makes sure that nothing on the RX / TX line will interfere with the loading of the firmware.





After that, you can run the OBK according to these topics:
Qiachip Smart Switch - BK7231N / CB2S - interior, programming
Light switch from the USA - Gosund Smart Switch SW5-A-V2.1 - BK7231T
Garden Tuya CCWFIO232PK Double Relay - BK7231T - Programming
OpenBeken configuration
After the basic configuration that I discussed in the previous topics in the series, prepare a startup script (a file named autoexec.bat with the following content):
Code: text
And in the App panel, drag it to the OBK file system:

This script will execute after device boot. After dragging itself, it will not be executed, you have to restart device manually (e.g. with a button from the WWW panel).
After uploading:

Meaning of individual commands:
- startDriver TuyaMCU - runs the TuyaMCU driver
- setChannelType 1 toggle - set the type of channel number 1 to On / Off
- setChannelType 2 dimmer - sets the type of channel number 2 on the dimmer
- tuyaMcu_setDimmerRange 0 1000 - sets the value range of the TuyaMCU dimmer (for GUI it is from 0 to 100 always, to 0 to 1000 it is converted only for MCU)
- linkTuyaMCUOutputToChannel 1 bool 1 - connects TuyaMCU dpID number 1 with bool type channel number 1
- linkTuyaMCUOutputToChannel 2 val 2 - connects dpID number 2 of TuyaMCU with channel number 2 of value type
The dpID values can be different for different kinds of devices. If we want to add support for something new, we need to get to know it, e.g. by listening for packages. The dpID values can also be taken from the descriptions of devices intended for Tasmota, because Tuya often only changes the WiFi module and the rest remains the same.
After a restart in the OpenBK panel, we have a separate switch and dimmer:

This is how the different levels of brightness look like:




How does the button work?
At first glance, you might think that the button is not supported by OpenBeken. But nothing could be more wrong!
In this product, the button is connected to the TuyaMCU, not to the module with WiFi. This TuyaMCU supports the button. We have everything ready. We connect the button to the appropriate pin (S for switch):

Both bistable and monostable buttons can be used. The button works in such a way that after switching on the brightness gradually increases and we can choose what level we want by simply switching it again. If, on the other hand, the light is on, the quick switching on and off of the button will turn it off completely. All of this is done by HT66F3195.
Home Assistant support
Pairing with Home Assistant is very simple, all thanks to the automatic mapping of OpenBeken channels to TuyaMCU variables. The TuyaMCU dimmer is operated from the HA level in the same way as an ordinary PWM-based dimmer. Here is a sample Yaml code:
Code: yaml
We have two separate channels - one determines the state of the device (on / off), the other - the brightness level.
Final application test:
[movie: 9a87cdbc15] https://filmy.elektroda.pl/95_1654602681.mp4 [/ movie: 9a87cdbc15]
Final button test:
[movie: 9a87cdbc15] https://filmy.elektroda.pl/4_1654602765.mp4 [/ movie: 9a87cdbc15]
What is noticeable in the movies? First of all, a step-by-step animation of the brightness shift that comes from Holtek's layout and cannot be changed. This is why the Home Assistant slider "clicks".
Also, there is a slight delay in reporting the status, but that's normal.
Support for scripts and commands over HTTP
OpenBeken supports sending Tasmota-style commands via the so-called "Tasmota HTTP". You can even send them via an ordinary browser.
This command will turn the lamp on at full brightness:
http://192.168.0.115/cm?cmnd=backlog%20setChannel%201%201;%20setChannel%202%20100
The one at 50% brightness:
http://192.168.0.115/cm?cmnd=backlog%20setChannel%201%201;%20setChannel%202%2050
It will turn it off after 5 seconds:
http://192.168.0.115/cm?cmnd=addRepeatingEvent%205%201%20setChannel%201%200
(addRepeatingEvent syntax: repetition period, number of repetitions, command)
There are also commands that add a value to the current brightness value, for example, but more on that another time.
Related topic
TuyaMCU is used in many devices, it also can be found in WiFi / RF433MHz fan controller. I recommend reading the topic:
QIACHIP Universal WIFI Ceiling Fan Light Remote Control Kit - BK7231N - CB2S
Summary
It seems that TuyaMCU is already well supported. Everything works well with MQTT and Home Assistant (along with both-way communication and status updates), and even allows you to script simple automations within OpenBeken itself.
Soon I will test more devices from TuyaMCU and also add more possibilities for the command interpreter (maybe possibility to execute simple scripts).
PS: And if you are interested in a slightly different dimmer with a different UART protocol (no longer TuyaMCU, but a different text protocol), please read this topic .
Cool? Ranking DIY