From 2bc301ef18552523abaef6549eca26253e0b3308 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Thu, 5 Sep 2019 11:02:01 +0100 Subject: [PATCH] Created TuyaMCU (markdown) --- TuyaMCU.md | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 TuyaMCU.md diff --git a/TuyaMCU.md b/TuyaMCU.md new file mode 100644 index 00000000..81b0aa6d --- /dev/null +++ b/TuyaMCU.md @@ -0,0 +1,238 @@ +Many newer Tuya devices are designed to have a secondary MCU along with a ESP8266 based Tuya Wifi module (TYWE3S, TYWE2S, etc). + +In these devices Tuya Wifi module takes care of Wifi and software features while the MCU controls the hardware based on commands from Wifi module and reports status back to Wifi module. All this communication happens over UART (Serial) with ESP8266. + + +## TuyaMCU Module (former Tuya Serial Dimmer) + +TuyaMCU module does the job of mapping devices behind MCU to Tasmota devices. TuyaMCU module introduces a new command `TuyaMCU` which maps the functionIds to DpIds. Module supports maximum of 16 configured FunctionIds. + +### DpId + +All the devices (relays, dimmers, switches) controlled by MCU are identified by a DpId. Whenever a command is sent to MCU, this DpId dictates which relay or dimmer needs to be changed and same is used when the status is received from MCU. There is no way the Wifi module firmware can know the function of each DpId, so just like GPIO pin configuration for TuyaMCU devices we need to map DpId with its functions. + + +### Function Ids + +FunctionID defines what type of Device is located at DpId. These functions are similar to default Tamsota GPIOs (Relays, LEDs, Dimmer like PWM, etc). Currently TuyaMCU module supports the following FunctionIds + +-|- +Function | FunctionId +Switch1 to Switch4 | 1 to 4 +Relay1 to Relay8 | 11 to 18 +Dimmer | 21 +Power (in deci Watt) | 31 +Current (in milli Amps) | 32 +Voltage (in deci Volt) | 33 +Relay Inverted1 to Relay Inverted8 | 41 to 48 + +## TuyaMCU command + +TuyaMCU command takes 1 param with functionId and DpId comma separated ie + +`TuyaMCU ,` +where `fnId` is functionId and `dpId` is dpId to map to. + +If any existing entry with same `fnId` or `dpId` is already present, it will be updated to the new values. + +If any of `fnId` or `dpId` is `0` the entry is removed + +If no param is provided then `TuyaMCU` command just prints the current mapping values. + +example: + +`TuyaMCU 11,1` +This command maps Relay1 (FunctionID 11) to DpId 1. + +Output of TuyaMCU is the current mapping of fnIds and DpId. + +## Identification of FunctionIds and DpIds + +As mentioned earlier, the hardware is controlled by MCU and Wifi module sends commands to MCU over serial to control the hardware. Each hardware has a DP ID which can be different for different models even for same manufacturer. We need to identify these DpIdse. + +To do that, + +1. You need to go to `Configure` -> `Console` option in Tasmota web interface. +2. Type command `weblog 4` to enable verbose logging in web interface. +3. Observe that after every 9-10 seconds you will see things like + +``` +16:44:47 TYA: Heartbeat +16:44:49 TYA: RX Packet: "55aa0107000501010001000f" +16:44:49 TYA: FnId=0 is set for dpId=1 +16:44:49 TYA: RX Packet: "55aa01070005020100010010" +16:44:49 TYA: FnId=0 is set for dpId=2 +16:44:49 TYA: RX Packet: "55aa01070005030100010011" +16:44:49 TYA: FnId=0 is set for dpId=3 +16:44:49 TYA: RX Packet: "55aa01070005040100010012" +16:44:49 TYA: FnId=0 is set for dpId=4 +16:44:49 TYA: RX Packet: "55aa0107000807020004000000001c" +16:44:49 TYA: FnId=0 is set for dpId=7 +16:44:49 TYA: RX Packet: "55aa0107000808020004000000001d" +16:44:49 TYA: FnId=0 is set for dpId=8 +16:44:49 TYA: RX Packet: "55aa0107000809020004000000001e" +16:44:49 TYA: FnId=0 is set for dpId=9 +16:44:49 TYA: RX Packet: "55aa010700080a020004000000001f" +16:44:49 TYA: FnId=0 is set for dpId=0 +16:44:49 TYA: RX Packet: "55aa0107000865020004000000007a" +16:44:49 TYA: FnId=0 is set for dpId=101 +16:44:49 TYA: RX Packet: "55aa0107000866020004000000007b" +16:44:49 TYA: FnId=0 is set for dpId=102 +16:44:49 TYA: RX Packet: "55aa0107000867020004000000007c" +16:44:49 TYA: FnId=0 is set for dpId=103 +16:44:49 TYA: RX Packet: "55aa0107000868020004000009870d" +16:44:49 TYA: FnId=0 is set for dpId=104 +``` + +4. The function ids are printed as `TYA: FnId=0 is set for dpId=XXX` We will use these XXX ids in guide ahead + + +### Setting up Dimmer + +We need to configure 3 functions of dimmer + +1. Dimming DpId +2. Dimming Range +3. Power metering if supported. +4. Dimming less than 10% + +#### Dimming DP ID + +Dimmer FunctionId is `21`. DpId generally is `2` or `3`. The easiest is to try both + +1. Goto console and type `TuyaMCU 21,2` and wait for it to reboot. +2. enter command `dimmer 10` and `dimmer 100` + +If your bulb responds to `dimmer` commands you have successfully configured dimmer function id. Note it for future. +If not try id `3` and if even `3` doesn't work keep trying ids from all unknown ids from log till one works. + + +#### Dimming Range + +Once you have figured out the dimming function id, we need to find the maximum dimming range. Once dimming id is set, the logs will keep showing like + +``` +16:50:55 TYA: Heartbeat +16:50:55 TYA: RX Packet: "55aa03070005010100010011" +16:50:55 TYA: RX Relay-1 --> MCU State: Off Current State:Off +16:50:55 TYA: RX Packet: "55aa03070008020200040000000720" +16:50:55 TYA: FnId=21 is set for dpId=2 +16:50:55 TYA: RX Dim State=7 +``` + +Now using the hardware buttons increase the dimmer to its maximum and observe the log. The `Dim State=XXX` shows the current dimmer level reported by MCU. Once the dimmer is at max, use this number. Now we need to tell Tasmota to use this as maximum. This controlled by [SetOption43](https://github.com/arendst/Sonoff-Tasmota/wiki/Commands#Setoption43) +We can set it using `SetOption43 XXX` where XXX is the maximum dimmer state reported in logs. + +Once set try `dimmer 100` in the logs and check if the brightness of bulb is same is maximum set using hardware buttons. + + +#### Power metering + +Some Tuya MCU devices support Power measurement support over serial. For this its better to use a bulb with known rating. + +Observe in console, the logs would show like + +```16:51:13 TYA: RX Packet: "55aa03070005010100010011" +16:51:13 TYA: FnId=11 is set for dpId=1 +16:51:13 TYA: RX Device-1 --> MCU State: Off Current State:Off +16:51:13 TYA: RX Packet: "55aa03070008020200040000000720" +16:51:13 TYA: FnId=21 is set for dpId=2 +16:51:13 TYA: RX Dim State=7 +16:51:13 TYA: RX Packet: "55aa0307000803020004000000001a" +16:51:13 TYA: FnId=0 is set for dpId=3 +16:51:13 TYA: RX Packet: "55aa0307000804020004000000251b" +16:51:13 TYA: FnId=0 is set for dpId=4 +16:51:13 TYA: RX Packet: "55aa0307000805020004000000591c" +16:51:13 TYA: FnId=0 is set for dpId=5 +16:51:13 TYA: RX Packet: "55aa03070008060200040000098fb5" +16:51:13 TYA: FnId=0 is set for dpId=6 +``` + +In here we are interested in the 3 digits before last 2 digits for example +`98f` in `"55aa03070008060200040000098fb5"` + +1. Make sure the bulb if off at the moment. +2. Find out the voltage standard of your country (generally 220, 240, 110v) from [table here](https://www.worldstandards.eu/electricity/plug-voltage-by-country/) For example we have 240 +3. Multiply that number by 10 (2400) and Convert that number (2400) to Hex using any [hex convertor](https://www.binaryhexconverter.com/decimal-to-hex-converter) which gives you 0x960. +4. Now see the number nearest to `960` in the logs. In our case it is `98f`. So we expect thats the voltage which is `ID=6` in our example. +5. Set voltage function Id 33 by entering command `TuyaMCU 33,6` + +6. Now set dimmer to 100% by command `dimmer 100` and observe logs. +7. Now we need the power rating of your bulb example `9W`. Multiply by 10 (90) and convert to hex which gives us `5A`. Check which unknown ID is close to `5A` in our case is `59` for `ID=5`. This is the id of `Active Power` function. +8. Set the active power function id 31 using command `TuyaMCU 31,5` + +9. Once Power and Voltage are set the logs will start showing + +``` +16:51:13 TYA: RX Packet: "55aa03070005010100010011" +16:51:13 TYA: FnId=11 is set for dpId=1 +16:51:13 TYA: RX Device-1 --> MCU State: Off Current State:Off +16:51:13 TYA: RX Packet: "55aa03070008020200040000000720" +16:51:13 TYA: FnId=21 is set for dpId=2 +16:51:13 TYA: RX Dim State=7 +16:51:13 TYA: RX Packet: "55aa0307000803020004000000001a" +16:51:13 TYA: FnId=0 is set for dpId=3 +16:51:13 TYA: RX Packet: "55aa0307000804020004000000251b" +16:51:13 TYA: FnId=0 is set for dpId=4 +16:51:13 TYA: RX Packet: "55aa0307000805020004000000591c" +16:51:13 TYA: FnId=31 is set for dpId=5 +16:51:13 TYA: Rx ID=5 Active_Power=9 +16:51:13 TYA: RX Packet: "55aa03070008060200040000098fb5" +16:51:13 TYA: FnId=33 is set for dpId=6 +16:51:13 TYA: Rx ID=6 Voltage=244.7 +``` + +10. To get current Id, Calculate `Current = Power / Voltage ( 9 / 244.7 ) = 0.0367`. `Multiply this by 1000 = 36.77.` +Now convert `36` to hex which is `0x24`. This is closest to `25` which is our current id 4. + +11. Set the current Function Id 32 using command `TuyaMCU 32,4`. + +12. Observe the logs should start showing + +``` +16:51:13 TYA: RX Packet: "55aa03070005010100010011" +16:51:13 TYA: FnId=11 is set for dpId=1 +16:51:13 TYA: RX Device-1 --> MCU State: Off Current State:Off +16:51:13 TYA: RX Packet: "55aa03070008020200040000000720" +16:51:13 TYA: FnId=21 is set for dpId=2 +16:51:13 TYA: RX Dim State=7 +16:51:13 TYA: RX Packet: "55aa0307000803020004000000001a" +16:51:13 TYA: FnId=0 is set for dpId=3 +16:51:13 TYA: RX Packet: "55aa0307000804020004000000251b" +16:51:13 TYA: FnId=32 is set for dpId=4 +16:51:13 TYA: Rx ID=4 Current=312 +16:51:13 TYA: RX Packet: "55aa0307000805020004000000591c" +16:51:13 TYA: FnId=31 is set for dpId=5 +16:51:13 TYA: Rx ID=5 Active_Power=9 +16:51:13 TYA: RX Packet: "55aa03070008060200040000098fb5" +16:51:13 TYA: FnId=33 is set for dpId=6 +16:51:13 TYA: Rx ID=6 Voltage=2447 +``` + +13. Power and current should change on dimming high / low. Tasmota web gui should show power values now. + +#### Dimming less than 10% + +By default tasmota blocks dimming less than 10% for dimmers. Some dimmers can handle less than 10% very well. + +1. Turn the dimmer to lowest using hardware button. +2. Check the `Dim state=` in logs to see whats minimum. +3. If `Dim state` is less than 10% of maximum range that was set you need to unset [SetOption69](https://github.com/arendst/Sonoff-Tasmota/wiki/Commands#Setoption69) by `SetOption69 0` command. + +### Setting up Switches + +For switches we need to + +1. Configure the number of relays +2. Configure Power metering. + +#### Configure number of relays + +By default TuyaMCU module expects 1 gang switch. There is no way currently to detect the number of relays present in MCU based switch, we need to tell it to Tasmota using FunctionIds 12 to 18 for Relay2 to Relay4 +So for 4 gang switch we need to set `TuyaMCU 12,2`, `TuyaMCU 13,3` and `TuyaMCU 14,4` if the DpIds for Relays 2-4 are `2`,`3`,`4`. + +TIP: You can configure all in one go using `backlog TuyaMCU 12,2; TuyaMCU 13,3; TuyaMCU 14,4` + +#### Configure power metering + +Power metering configuration is same as for dimmers. Check the power metering section for dimmers. \ No newline at end of file