arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
22.22k stars 4.81k forks source link

Tuya MCU V2 does not complete initialisation on WIFI-101 door/window contact switch. #17542

Closed hydra closed 1 year ago

hydra commented 1 year ago

PROBLEM DESCRIPTION

The TuyaMCU V2 initialisation sequence does not complete, it gets stuck at stage 8, the log reports:

00:00:03.711 TYA: Protocol timeout state 8 -> 0

REQUESTED INFORMATION

Make sure your have performed every step and checked the applicable boxes before submitting your issue. Thank you!

11:55:01.939 CMD: Grp 0, Cmd 'GPIO', Idx 1, Len 3, Pld 255, Data '255'
11:55:01.948 MQT: stat/switch1/RESULT = {"GPIO0":{"0":"None"},"GPIO1":{"2272":"Tuya Tx"},"GPIO2":{"0":"None"},"GPIO3":{"2304":"Tuya Rx"},"GPIO4":{"0":"None"},"GPIO5":{"0":"None"},"GPIO9":{"0":"None"},"GPIO10":{"0":"None"},"GPIO12":{"0":"None"},"GPIO13":{"0":"None"},"GPIO14":{"0":"None"},"GPIO15":{"0":"None"},"GPIO16":{"0":"None"},"GPIO17":{"0":"None"}}
11:55:02.440 MQT: tasmota/discovery/E0980610F709/config = {"ip":"192.168.18.175","dn":"Switch1","fn":["Switch 1",null,null,null,null,null,null,null],"hn":"switch1-5897","mac":"E0980610F709","md":"Tuya MCU","ty":1,"if":0,"ofln":"Offline","onln":"Online","state":["OFF","ON","TOGGLE","HOLD"],"sw":"12.3.1.2","t":"switch1","ft":"%prefix%/%topic%/","tp":["cmnd","stat","tele"],"rl":[1,0,0,0,0,0,0,0],"swc":[-1,-1,-1,-1,-1,-1,-1,-1],"swn":[null,null,null,null,null,null,null,null],"btn":[0,0,0,0,0,0,0,0],"so":{"4":0,"11":0,"13":0,"17":0,"20":0,"30":0,"68":0,"73":0,"82":0,"114":0,"117":0},"lk":0,"lt_st":0,"sho":[0,0,0,0],"sht":[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],"ver":1} (retained)
11:55:02.448 MQT: tasmota/discovery/E0980610F709/sensors = {"sn":{"Time":"2022-12-30T11:55:02"},"ver":1} (retained)
  Configuration output here:

N/A

N/A

- [ ] Provide the output of this command: `Status 0`:
```lua
  STATUS 0 output here:

12:05:46.741 CMD: Status 0
12:05:46.743 SRC: WebConsole from 192.168.18.49
12:05:46.746 CMD: Grp 0, Cmd 'STATUS', Idx 1, Len 1, Pld 0, Data '0'
12:05:46.752 MQT: stat/switch1/STATUS = {"Status":{"Module":54,"DeviceName":"Switch1","FriendlyName":["Switch 1"],"Topic":"switch1","ButtonTopic":"0","Power":0,"PowerOnState":3,"LedState":1,"LedMask":"FFFF","SaveData":1,"SaveState":1,"SwitchTopic":"0","SwitchMode":[0,0,0,0,0,0,0,0],"ButtonRetain":0,"SwitchRetain":0,"SensorRetain":0,"PowerRetain":0,"InfoRetain":0,"StateRetain":0,"StatusRetain":0}}
12:05:46.776 MQT: stat/switch1/STATUS1 = {"StatusPRM":{"Baudrate":9600,"SerialConfig":"8N1","GroupTopic":"tasmotas","OtaUrl":"OTA update can brick your device","RestartReason":"External System","Uptime":"0T00:11:00","StartupUTC":"2022-12-30T10:54:46","Sleep":50,"CfgHolder":4617,"BootCount":6,"BCResetTime":"2022-12-29T20:30:47","SaveCount":15,"SaveAddress":"F8000"}}
12:05:46.796 MQT: stat/switch1/STATUS2 = {"StatusFWR":{"Version":"12.3.1.2(battery)","BuildDateTime":"2022-12-30T11:27:05","Boot":7,"Core":"2_7_4_9","SDK":"2.2.2-dev(38a443e)","CpuFrequency":80,"Hardware":"ESP8266EX","CR":"350/699"}}
12:05:46.815 MQT: stat/switch1/STATUS3 = {"StatusLOG":{"SerialLog":0,"WebLog":4,"MqttLog":0,"SysLog":0,"LogHost":"","LogPort":514,"SSId":["SeriouslyPro!","Avatel_91"],"TelePeriod":300,"Resolution":"558180C0","SetOption":["0000800B","2805C80001000600003C5A00192800000000","00018000","00006000","04004000","00000000"]}}
12:05:46.839 MQT: stat/switch1/STATUS4 = {"StatusMEM":{"ProgramSize":489,"Free":512,"Heap":29,"ProgramFlashSize":1024,"FlashSize":2048,"FlashChipId":"1540C8","FlashFrequency":40,"FlashMode":"DOUT","Features":["00000809","08100582","04008001","00000003","00000000","00000800","00000000","00000000","54000000","00000080"],"Drivers":"1,2,4,7,10,12,16,29,62","Sensors":"1,2"}}
12:05:46.857 MQT: stat/switch1/STATUS5 = {"StatusNET":{"Hostname":"switch1-5897","IPAddress":"192.168.18.175","Gateway":"192.168.18.1","Subnetmask":"255.255.255.0","DNSServer1":"192.168.18.1","DNSServer2":"0.0.0.0","Mac":"E0:98:06:10:F7:09","Webserver":2,"HTTP_API":1,"WifiConfig":4,"WifiPower":17.0}}
12:05:46.876 MQT: stat/switch1/STATUS6 = {"StatusMQT":{"MqttHost":"192.168.18.5","MqttPort":1883,"MqttClientMask":"DVES_%06X","MqttClient":"DVES_10F709","MqttUser":"DVES_USER","MqttCount":1,"MAX_PACKET_SIZE":1200,"KEEPALIVE":30,"SOCKET_TIMEOUT":4}}
12:05:46.893 MQT: stat/switch1/STATUS7 = {"StatusTIM":{"UTC":"2022-12-30T11:05:46","Local":"2022-12-30T12:05:46","StartDST":"2022-03-27T02:00:00","EndDST":"2022-10-30T03:00:00","Timezone":"+01:00"}}
12:05:46.912 MQT: stat/switch1/STATUS10 = {"StatusSNS":{"Time":"2022-12-30T12:05:46"}}
12:05:46.929 MQT: stat/switch1/STATUS11 = {"StatusSTS":{"Time":"2022-12-30T12:05:46","Uptime":"0T00:11:00","UptimeSec":660,"Heap":29,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"OFF","Wifi":{"AP":1,"SSId":"SeriouslyPro!","BSSId":"D8:3B:BF:FB:56:0D","Channel":5,"Mode":"11n","RSSI":82,"Signal":-59,"LinkCount":1,"Downtime":"0T00:00:05"}}}

TO REPRODUCE

Flash 'tasmota-battery' that contains the new TuyaMCU V2 code to a Wifi-101 device. Connect it to Wifi as usual using the web-ui.

Module 54
Backlog SetOption1 1; SetOption65 1; SetOption66 1
TuyaMCU 51,51

EXPECTED BEHAVIOUR

Tuya Initialisation completes.

SCREENSHOTS

image

ADDITIONAL CONTEXT

I reverse engineered the schematic, PDF version attached:

schematic.pdf

schematic

I dumped the original firmware, attached:

20221228-2007-contact_sensor_1_1M.zip

I took trace logs of 3v3, Reed Switch, Wifi RX/TX from the original firmware using Logic, I exported the data as CSV and also created Excel comparisons and analyzed the data a little to find the details of which command the original firmware is using the get the status of the reed switch, logs attached:

20221230-1201-original-firmware-logic-trace-logs-and-csv-exports-and-comparisons.zip

I also compiled the same firmware but re-enabled the V1 code, logs attached:

log - v1 - reed closed.txt log - v1 - reed open.txt log - v2 - reed closed.txt log - v2 - reed open.txt

(Please, remember to close the issue when the problem has been addressed)

hydra commented 1 year ago

Datasheets for the key components on the WIFI-101 PCB:

Charge Controller: 405442xf.pdf

MCU: HT66F0175_185v161.pdf

Transistors/FETs: transistor_sot23_S8050_J3Y.pdf transistor_sot23_AO3401_A19T.pdf

Wifi Module: Users-Manual-3525098.pdf

Images of the PCB with WiFi module removed to enable reverse engineering of the schematic:

IMG_20221222_000438 IMG_20221222_000430

Jason2866 commented 1 year ago

Probably related to #17190

hydra commented 1 year ago

I'm fairly adept at coding, if there's anything specific in the codebase, or even just hints, let me know and I'll take a look.

arendst commented 1 year ago

Due to lack of support Tuya V2 in development has been reverted to V1. Sorry.

hydra commented 1 year ago

@arendst What do you mean by 'lack of support'? What's the issue?

I also tried this particular device with V1 code by using the build_special branch and revering the #ifdef as per master branch but the device doesn't ever show the door contact switch status. See the logs attached.

The 'https://github.com/sillyfrog/Tasmota-Tuya-Helper' bookmarklet tool doesn't work with either V1 or V2 code and always shows a backlog command.

Any ideas on how to get either the device working with either V1 or the V2 code?

arendst commented 1 year ago

See #17190 and other tuya related issues not being addressed by either original maintainers (as they moved on) or users willing and able to dive into the issues.

The V2 version seems to be more error prone than the V1 version so that's why V2 never made it into production and was replaced by V1 in development recently.

I don't own any tuya related hardware so cannot help here.

hydra commented 1 year ago

@arendst ok, understood, thanks for the clarification.

Today I did a new build on the latest build_special branch which now has the V1 code as the default and used SerialSend to get the status of the contact switch.

For anyone else following along, here's the full set commands to get the Wifi-101 contact sensor working, once you've flashed tasmota-battery (2023/01/02):

Module 54

Backlog SetOption1 1; SetOption65 1; SetOption66 1

TuyaMCU 51,51

Rule1 ON TuyaReceived#Data=55AA00050005030400010213 DO publish2 stat/%topic%/BATTERY high ENDON ON TuyaReceived#Data=55AA00050005030400010112 DO publish2 stat/%topic%/BATTERY medium ENDON ON TuyaReceived#Data=55AA00050005030400010011 DO publish2 stat/%topic%/BATTERY low ENDON

Rule1 1

Rule2 ON TuyaReceived#Data=55AA0002000001 DO SerialSend5 55aa000500010005 ENDON

Rule2 1

Here's two logs showing the output:

Closed-To-Open:

00:00:00.001 HDW: ESP8266EX
00:00:00.050 CFG: Loaded from flash at F8, Count 59
00:00:00.054 SER: Set to 8N1 9600 bit/s
00:00:00.056 CFG: CR 350/699, Busy 0
00:00:00.061 SNS: Hardware Serial
00:00:00.062 TYA: Request MCU configuration at 9600 bps
00:00:00.062 SRC: Restart
00:00:00.075 TYA: Send "55aa0006000501010001000d"
00:00:00.108 Project tasmota - Switch1 Version 12.3.1.3(battery)-2_7_4_9(2023-01-02T12:30:25)
00:00:00.125 TYA: Set WiFi LED 2 (-1)
00:00:00.134 TYA: Send "55aa000200010204"
00:00:00.152 RSL: RESULT = {"TuyaReceived":{"Data":"55AA0002000001","Cmnd":2}}
00:00:00.173 RUL: TUYARECEIVED#DATA=55AA0002000001 performs "SerialSend5 55aa000500010005"
00:00:00.173 SRC: Rule
00:00:00.175 CMD: Grp 0, Cmd 'SERIALSEND', Idx 5, Len 16, Pld 55, Data '55aa000500010005'
00:00:00.177 RSL: RESULT = {"SerialSend":"Done"}
00:00:00.189 WIF: Checking connection...
00:00:00.190 WIF: Attempting connection...
00:00:00.528 WIF: Connecting to AP1 SeriouslyPro! Channel 5 BSSId D8:3B:BF:FB:56:0D in mode 11n as switch1-5897...
00:00:01.752 WIF: Checking connection...
00:00:01.753 WIF: Attempting connection...
00:00:02.752 WIF: Checking connection...
00:00:02.753 WIF: Attempting connection...
00:00:03.753 WIF: Checking connection...
00:00:03.754 WIF: Connected
00:00:04.007 HTP: Web server active on switch1-5897 with IP address 192.168.18.175
00:00:04.412 HTP: Console
00:00:04.516 WIF: Sending Gratuitous ARP
00:00:04.517 NTP: Sync time...
00:00:04.530 WIF: DNS resolved '2.pool.ntp.org' (178.215.228.24) in 13 ms
00:00:04.805 RTC: UTC 2023-01-02T12:18:48, DST 2023-03-26T02:00:00, STD 2023-10-29T03:00:00
13:18:48.000 RTC: Synced by NTP
13:18:48.001 TYA: Set WiFi LED 3 (0)
13:18:48.010 TYA: Send "55aa000200010305"
13:18:48.046 RSL: RESULT = {"TuyaReceived":{"Data":"55AA0002000001","Cmnd":2}}
13:18:48.064 RUL: TUYARECEIVED#DATA=55AA0002000001 performs "SerialSend5 55aa000500010005"
13:18:48.065 SRC: Rule
13:18:48.067 CMD: Grp 0, Cmd 'SERIALSEND', Idx 5, Len 16, Pld 55, Data '55aa000500010005'
13:18:48.070 RSL: RESULT = {"SerialSend":"Done"}
13:18:48.039 MQT: Attempting connection...
13:18:48.041 WIF: DNS resolved '192.168.18.5' (192.168.18.5) in 1 ms
13:18:48.052 MQT: Connected
13:18:48.055 MQT: tele/switch1/LWT = Online (retained)
13:18:48.059 MQT: cmnd/switch1/POWER = 
13:18:48.061 MQT: Subscribe to cmnd/switch1/#
13:18:48.064 MQT: Subscribe to cmnd/tasmotas/#
13:18:48.066 SRC: MQTT
13:18:48.068 CMD: Grp 0, Cmd 'POWER', Idx 1, Len 0, Pld -99, Data ''
13:18:48.072 MQT: stat/switch1/RESULT = {"POWER":"OFF"}
13:18:48.076 MQT: stat/switch1/POWER = OFF
13:18:48.078 MQT: Subscribe to cmnd/DVES_10F709_fb/#
13:18:48.084 MQT: tele/switch1/INFO1 = {"Info1":{"Module":"Tuya MCU","Version":"12.3.1.3(battery)","FallbackTopic":"cmnd/DVES_10F709_fb/","GroupTopic":"cmnd/tasmotas/"}}
13:18:48.100 MQT: tele/switch1/INFO2 = {"Info2":{"WebServerMode":"Admin","Hostname":"switch1-5897","IPAddress":"192.168.18.175"}}
13:18:48.114 MQT: tele/switch1/INFO3 = {"Info3":{"RestartReason":"Power On","BootCount":30}}
13:18:48.128 MQT: stat/switch1/RESULT = {"POWER":"OFF"}
13:18:48.130 MQT: stat/switch1/POWER = OFF
13:18:48.139 TYA: Send "55aa0001000000"
13:18:48.192 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA000100247B2270223A2277316E7A786D657831616E716D666D78222C2276223A22312E302E30227D1A","Cmnd":1,"CmndData":"7B2270223A2277316E7A786D657831616E716D666D78222C2276223A22312E302E30227D"}}
13:18:48.209 TYA: MCU Product ID: {"p":"w1nzxmex1anqmfmx","v":"1.0.0"}
13:18:48.211 TYA: Set WiFi LED 4 (0)
13:18:48.220 TYA: Send "55aa000200010406"
13:18:48.240 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA0002000001","Cmnd":2}}
13:18:48.262 RUL: TUYARECEIVED#DATA=55AA0002000001 performs "SerialSend5 55aa000500010005"
13:18:48.264 SRC: Rule
13:18:48.266 CMD: Grp 0, Cmd 'SERIALSEND', Idx 5, Len 16, Pld 55, Data '55aa000500010005'
13:18:48.270 MQT: stat/switch1/RESULT = {"SerialSend":"Done"}
13:18:48.277 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA00050005030400010112","Cmnd":5,"CmndData":"0304000101"}}
13:18:48.293 RUL: TUYARECEIVED#DATA=55AA00050005030400010112 performs "publish2 stat/switch1/BATTERY medium"
13:18:48.295 SRC: Rule
13:18:48.298 CMD: Grp 0, Cmd 'PUBLISH', Idx 2, Len 27, Pld -99, Data 'stat/switch1/BATTERY medium'
13:18:48.303 MQT: stat/switch1/BATTERY = medium (retained)
13:18:48.314 TYA: fnId=0 is set for dpId=3
13:18:48.318 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA0005000501010001010D","Cmnd":5,"CmndData":"0101000101"}}
13:18:48.337 TYA: fnId=11 is set for dpId=1
13:18:48.338 TYA: RX Relay-1 --> MCU State: On Current State:Off
13:18:48.340 SRC: Switch
13:18:48.345 MQT: stat/switch1/RESULT = {"POWER":"ON"}
13:18:48.349 MQT: stat/switch1/POWER = ON
13:18:48.423 TYA: Send "55aa000500010106"
13:18:48.573 CFG: Saved to flash at F7, Count 60, Bytes 4096

Open-to-Closed:

00:00:00.188 WIF: Checking connection...
00:00:00.189 WIF: Attempting connection...
00:00:00.527 WIF: Connecting to AP1 SeriouslyPro! Channel 5 BSSId D8:3B:BF:FB:56:0D in mode 11n as switch1-5897...
00:00:01.751 WIF: Checking connection...
00:00:01.752 WIF: Attempting connection...
00:00:02.751 WIF: Checking connection...
00:00:02.752 WIF: Attempting connection...
00:00:03.751 WIF: Checking connection...
00:00:03.752 WIF: Connected
00:00:04.005 HTP: Web server active on switch1-5897 with IP address 192.168.18.175
00:00:04.404 WIF: Sending Gratuitous ARP
00:00:04.406 NTP: Sync time...
00:00:04.450 WIF: DNS resolved '2.pool.ntp.org' (51.195.26.117) in 44 ms
00:00:04.504 RTC: UTC 2023-01-02T12:16:50, DST 2023-03-26T02:00:00, STD 2023-10-29T03:00:00
13:16:50.001 RTC: Synced by NTP
13:16:50.002 TYA: Set WiFi LED 3 (0)
13:16:50.011 TYA: Send "55aa000200010305"
13:16:50.032 RSL: RESULT = {"TuyaReceived":{"Data":"55AA0002000001","Cmnd":2}}
13:16:50.051 RUL: TUYARECEIVED#DATA=55AA0002000001 performs "SerialSend5 55aa000500010005"
13:16:50.052 SRC: Rule
13:16:50.054 CMD: Grp 0, Cmd 'SERIALSEND', Idx 5, Len 16, Pld 55, Data '55aa000500010005'
13:16:50.056 RSL: RESULT = {"SerialSend":"Done"}
13:16:51.036 MQT: Attempting connection...
13:16:51.038 WIF: DNS resolved '192.168.18.5' (192.168.18.5) in 0 ms
13:16:51.048 MQT: Connected
13:16:51.052 MQT: tele/switch1/LWT = Online (retained)
13:16:51.055 MQT: cmnd/switch1/POWER = 
13:16:51.057 MQT: Subscribe to cmnd/switch1/#
13:16:51.060 MQT: Subscribe to cmnd/tasmotas/#
13:16:51.062 SRC: MQTT
13:16:51.064 CMD: Grp 0, Cmd 'POWER', Idx 1, Len 0, Pld -99, Data ''
13:16:51.068 MQT: stat/switch1/RESULT = {"POWER":"ON"}
13:16:51.072 MQT: stat/switch1/POWER = ON
13:16:51.074 MQT: Subscribe to cmnd/DVES_10F709_fb/#
13:16:51.079 MQT: tele/switch1/INFO1 = {"Info1":{"Module":"Tuya MCU","Version":"12.3.1.3(battery)","FallbackTopic":"cmnd/DVES_10F709_fb/","GroupTopic":"cmnd/tasmotas/"}}
13:16:51.096 MQT: tele/switch1/INFO2 = {"Info2":{"WebServerMode":"Admin","Hostname":"switch1-5897","IPAddress":"192.168.18.175"}}
13:16:51.111 MQT: tele/switch1/INFO3 = {"Info3":{"RestartReason":"Power On","BootCount":28}}
13:16:51.125 MQT: stat/switch1/RESULT = {"POWER":"ON"}
13:16:51.127 MQT: stat/switch1/POWER = ON
13:16:51.135 TYA: Send "55aa0001000000"
13:16:51.193 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA000100247B2270223A2277316E7A786D657831616E716D666D78222C2276223A22312E302E30227D1A","Cmnd":1,"CmndData":"7B2270223A2277316E7A786D657831616E716D666D78222C2276223A22312E302E30227D"}}
13:16:51.210 TYA: MCU Product ID: {"p":"w1nzxmex1anqmfmx","v":"1.0.0"}
13:16:51.212 TYA: Set WiFi LED 4 (0)
13:16:51.221 TYA: Send "55aa000200010406"
13:16:51.242 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA0002000001","Cmnd":2}}
13:16:51.263 RUL: TUYARECEIVED#DATA=55AA0002000001 performs "SerialSend5 55aa000500010005"
13:16:51.265 SRC: Rule
13:16:51.267 CMD: Grp 0, Cmd 'SERIALSEND', Idx 5, Len 16, Pld 55, Data '55aa000500010005'
13:16:51.271 MQT: stat/switch1/RESULT = {"SerialSend":"Done"}
13:16:51.278 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA00050005030400010112","Cmnd":5,"CmndData":"0304000101"}}
13:16:51.294 RUL: TUYARECEIVED#DATA=55AA00050005030400010112 performs "publish2 stat/switch1/BATTERY medium"
13:16:51.296 SRC: Rule
13:16:51.299 CMD: Grp 0, Cmd 'PUBLISH', Idx 2, Len 27, Pld -99, Data 'stat/switch1/BATTERY medium'
13:16:51.303 MQT: stat/switch1/BATTERY = medium (retained)
13:16:51.315 TYA: fnId=0 is set for dpId=3
13:16:51.319 MQT: tele/switch1/RESULT = {"TuyaReceived":{"Data":"55AA0005000501010001000C","Cmnd":5,"CmndData":"0101000100"}}
13:16:51.341 TYA: fnId=11 is set for dpId=1
13:16:51.342 TYA: RX Relay-1 --> MCU State: Off Current State:On
13:16:51.344 SRC: Switch
13:16:51.349 MQT: stat/switch1/RESULT = {"POWER":"OFF"}
13:16:51.353 MQT: stat/switch1/POWER = OFF
13:16:51.374 HTP: Console
13:16:51.478 TYA: Send "55aa000500010106"
13:16:51.680 CFG: Saved to flash at F4, Count 55, Bytes 4096

This is the firmware I used: tasmota-battery.zip

Source: https://github.com/Jason2866/Tasmota-build/tree/42327e20498839574ecc080bd7708952ed6bd7d1

Commit: 42327e20498839574ecc080bd7708952ed6bd7d1

Note that the MCU seems to become unresponsive after a few seconds after power-up even with the bench-psu connected to the battery terminals and the Wifi module powered on by connected the WiFi enable MOSFET Gate connected to GND. So you have to power-cycle the whole thing, then issue serialsend and/or other MCU commands in the web-ui console as soon as the device boots.

hydra commented 1 year ago

Instructions for flashing:

1) Carefully desolder battery wires. 2) Connect USB UART: Wifi Module TX -> RX Wifi Module RX -> TX GND -> GND. 3) Connect wire between Q1 Gate (next to resistor with 104 marking) to GND. 4) Connect Wifi Module GPIO0 to GND via a momentary or toggle switch. 5) Connect bench PSU to battery pads, 4.2V current limit 500mA. 6) Close GPIO0/BOOT toggle switch. 7) Power on bench PSU, release toggle switch. (~30mA draw) 8) Backup firmware:

esptool -b 115200 --port COM7 read_flash 0x000000 0x100000 contact_sensor_1M.bin

Log:

esptool.py v4.4
Serial port COM7
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting...
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 24:62:ab:1f:04:e4
Uploading stub...
Running stub...
Stub running...
1048576 (100 %)
1048576 (100 %)
Read 1048576 bytes at 0x00000000 in 94.3 seconds (88.9 kbit/s)...
Hard resetting via RTS pin...

9) Install tasmota, either using PIO or pre-built. a)Build and flash tasmota using PlatformIO:

pio run -e tasmota-battery --target upload --upload-port COM7

Log:

Processing tasmota-battery (platform: https://github.com/tasmota/platform-espressif8266/releases/download/2022.12.0/platform-espressif8266.zip; framework: arduino; board: esp8266_1M)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif8266/esp8266_1M.html
PLATFORM: Espressif 8266 (2022.12.0) > Espressif Generic ESP8266 Tasmota 1M sketch NO FS
HARDWARE: ESP8266 80MHz, 80KB RAM, 972.00KB Flash
PACKAGES:
 - framework-arduinoespressif8266 @ 2.7.4+9
 - tool-esptoolpy @ 1.40401.0 (4.4.1)
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 1.200.0 (2.0)
 - toolchain-xtensa @ 2.40802.200502 (4.8.2)
Converting tasmota.ino
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ strict
Found 26 compatible libraries
Scanning dependencies...
Dependency Graph
|-- headers
|   |-- EEPROM @ 1.0
|   |-- ESP8266WebServer @ 1.0
|   |   |-- ESP8266WiFi @ 1.0
|   |-- ESP8266WiFi @ 1.0
|-- EEPROM 24C128_256_521
|   |-- Wire @ 1.0
|-- UdpListener @ 1.0
|-- Unishox Compressor Decompressor highly customized and optimized for ESP8266 and Tasmota @ 1.0
|-- base64 @ 1.1.1
|-- DNSServer @ 1.1.1
|   |-- ESP8266WiFi @ 1.0
|-- ESP8266HTTPClient @ 1.2
|   |-- ESP8266WiFi @ 1.0
|-- ESP8266httpUpdate @ 1.3
|   |-- ESP8266HTTPClient @ 1.2
|   |   |-- ESP8266WiFi @ 1.0
|   |-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|   |-- ESP8266WiFi @ 1.0
|-- ESP8266WebServer @ 1.0
|   |-- ESP8266WiFi @ 1.0
|-- ESP8266WiFi @ 1.0
|-- Ext-printf @ 1.0
|-- JSMN JSON parser customized and optimized for ESP8266 and Tasmota @ 1.0
|-- LinkedList
|-- LittleFS(esp8266) @ 0.1.0
|-- TasmotaLList @ 1.0
|-- PubSubClient @ 2.8
|-- SD(esp8266) @ 2.0.0
|   |-- SDFS @ 0.1.0
|   |   |-- ESP8266SdFat @ 1.1.0
|   |   |   |-- SPI @ 1.0
|   |   |-- SPI @ 1.0
|-- ESP8266SdFat @ 1.1.0
|   |-- SPI @ 1.0
|-- SPI @ 1.0
|-- TasmotaSerial @ 3.6.0
|-- Ticker @ 1.0
|-- Wire @ 1.0
Building in release mode
*** use provided user_config_override.h as planned ***
*** use provided platformio_override.ini as planned ***
*** use provided platformio_tasmota_cenv.ini as planned ***
Compiling .pio\build\tasmota-battery\src\tasmota.ino.cpp.o
D:/Users/Hydra/Documents/dev/legacy/embedded/Sonoff-Tasmota/tasmota/tasmota_support/settings.ino: In function 'void SettingsDefaultSet2()':
D:/Users/Hydra/Documents/dev/legacy/embedded/Sonoff-Tasmota/tasmota/tasmota_support/settings.ino:998:25: warning: large integer implicitly truncated to unsigned type [-Woverflow]
   flag5.mqtt_persistent |= ~MQTT_CLEAN_SESSION;
                         ^
Linking .pio\build\tasmota-battery\firmware.elf
Retrieving maximum program size .pio\build\tasmota-battery\firmware.elf
Checking size .pio\build\tasmota-battery\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=====     ]  46.2% (used 37876 bytes from 81920 bytes)
Flash: [=====     ]  48.2% (used 493176 bytes from 1023984 bytes)
Building .pio\build\tasmota-battery\firmware.bin
Creating BIN file ".pio\build\tasmota-battery\firmware.bin" using "D:\Users\Hydra\.platformio\packages\framework-arduinoespressif8266\bootloaders\eboot\eboot.elf" and ".pio\build\tasmota-battery\firmware.elf"
bin_map_copy([".pio\build\tasmota-battery\firmware.bin"], [".pio\build\tasmota-battery\firmware.elf"])
map_gzip([".pio\build\tasmota-battery\firmware.bin"], [".pio\build\tasmota-battery\firmware.elf"])
bin_gzip([".pio\build\tasmota-battery\firmware.bin"], [".pio\build\tasmota-battery\firmware.elf"])
Compression reduced firmware size by 70% (was 497328 bytes, now 347781 bytes)
Configuring upload protocol...
AVAILABLE: espota, esptool
CURRENT: upload_protocol = esptool
Looking for upload port...
Using manually specified: COM7
Uploading .pio\build\tasmota-battery\firmware.bin
esptool.py v4.5-dev
Serial port COM7
Connecting...
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 24:62:ab:1f:04:e4
Stub is already running. No upload is necessary.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00079fff...
Compressed 497328 bytes to 354543...
Writing at 0x00000000... (4 %)
Writing at 0x00005c8e... (9 %)
Writing at 0x0000b444... (13 %)
Writing at 0x0001099f... (18 %)
Writing at 0x0001607e... (22 %)
Writing at 0x0001b6f1... (27 %)
Writing at 0x00020a2f... (31 %)
Writing at 0x00025c3e... (36 %)
Writing at 0x0002b650... (40 %)
Writing at 0x000313d8... (45 %)
Writing at 0x00037261... (50 %)
Writing at 0x0003cc4d... (54 %)
Writing at 0x000422f2... (59 %)
Writing at 0x00047181... (63 %)
Writing at 0x0004c2a3... (68 %)
Writing at 0x0005173f... (72 %)
Writing at 0x00056b28... (77 %)
Writing at 0x0005c23b... (81 %)
Writing at 0x00061970... (86 %)
Writing at 0x00067f3c... (90 %)
Writing at 0x00070069... (95 %)
Writing at 0x00075bc1... (100 %)
Wrote 497328 bytes (354543 compressed) at 0x00000000 in 31.7 seconds (effective 125.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
========================================================================== [SUCCESS] Took 46.28 seconds ==========================================================================

Environment      Status    Duration
---------------  --------  ------------
tasmota-battery  SUCCESS   00:00:46.283
=========================================================================== 1 succeeded in 00:00:46.283 ===========================================================================
***********************************************************************************************************************************************************************************

b) Flash pre-built tasmota-battery.bin.

esptool -b 115200 --port COM7 write_flash --erase-all 0x000000 tasmota-battery.bin

10) Power off bench PSU. 11) Connect the Q1 GATE to GND. 12) Power on the bench PSU. (~65mA draw) 13) After a few moments a tasmota wifi AP should appear. 14) Connect to AP and go to http://192.168.4.1/ 15) Connect it your Wifi using the WebUI. 16) After it connects to the wifi you'll see the full Tasmota UI. 17) Configure your MQTT settings. 18) Configure secondary Wifi as required. 19) In the console, issue these commands, one at a time.

Module 54

^ this will cause a reboot.

Backlog SetOption1 1; SetOption65 1; SetOption66 1
TuyaMCU 51,51
Rule1 ON TuyaReceived#Data=55AA00050005030400010213 DO publish2 stat/%topic%/BATTERY high ENDON ON TuyaReceived#Data=55AA00050005030400010112 DO publish2 stat/%topic%/BATTERY medium ENDON ON TuyaReceived#Data=55AA00050005030400010011 DO publish2 stat/%topic%/BATTERY low ENDON
Rule1 1
Rule2 ON TuyaReceived#Data=55AA0002000001 DO SerialSend5 55aa000500010005 ENDON
Rule2 1

20) Configure other settings as required. 21) Backup config to a file using the WebUI. 22) Power off Bench PSU. 23) Disconnect Q1 GATE from GND. 24) Power on Bench PSU. Note that the device will power on, transmit it's status via MQTT and then power off, this only takes a few seconds and you can observe the current draw on the bench PSU to know when the Wifi module is powered off by the MCU.

you should see an MQTT message stat/<topic>/POWER = OFF sent to your MQTT broker.

25) hold the magnet next to the reed switch.

you should see an MQTT message stat/<topic>/POWER = ON sent to your MQTT broker.

26) Power off bench PSU and reverse steps 5-1 above, then re-assemble the case.

To restore firmware:

esptool -b 115200 --port COM7 write_flash --erase-all 0x000000 contact_sensor_1M.bin

Reconfiguring on battery:

If you press-and-hold the 'Reset' switch for 5 seconds and release, the device will flash the red LED and stay powered on for about 2 minutes during which time the Wifi module is powered on and you can connect to it over the network.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't any activity in last few weeks. It will be closed if no further activity occurs. Thank you for your contributions.

hydra commented 1 year ago

@arendst Is it worth noting somewhere on the site that a maintainer for the Tuya related code is needed? Is there a list of current maintainers for certain portions of the codebase?

Jason2866 commented 1 year ago

Listed here https://github.com/arendst/Tasmota/blob/development/CODE_OWNERS.md but this does not implicate that the owner still does maintain. For Tuya code there is currently no maintainer

barbudor commented 1 year ago

Myself and a few others have recently made some maintenance on Tuya V1

Regarding Tuya V2 it was a hit and run and it is now disabled by default. I don't know what justified this work (Tuya V2) and why the contributor walked away but that cannot work V2 is now dead code

If you have some comments on V1, let us know and I may be in a position to help

hydra commented 1 year ago

Hi @barbudor.

Firstly, thanks for doing some maintenance!

My observations with the setup above is that time-from contact opening/closing, to messages being received by my MQTT broker, is quite long, and sometimes they do not appear at all. I can see the contact switch light coming on, so I know it's powering on ok. For example, if I open or close a door sometimes I do not get any MQTT messages. If I then wait 10 seconds, and then close or open the same door the message may come though.

Can the the process be sped up somehow? The original Tuya firmware on the wifi module was faster for sure, as my logic analyzer traces show.

barbudor commented 1 year ago

Have you retested since we disabled V2 and reverted to V1 ? I won't invest a minute into V2

hydra commented 1 year ago

Have you retested since we disabled V2 and reverted to V1 ? I won't invest a minute into V2

yes, the my previous comment was relating to the V1 code, as detailed here: https://github.com/arendst/Tasmota/issues/17542#issuecomment-1368906746 (above)

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't any activity in last few weeks. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 1 year ago

This issue was automatically closed because of being stale. Feel free to open a new one if you still experience this problem.

btsimonh commented 1 year ago

@barbudor - you only had to ask? (about V2). see https://github.com/arendst/Tasmota/issues/17190

benjaminnestler commented 9 months ago

Could it be the case that the original issue of @hydra is that his device seems to be using the Tuya low power wifi protocol.

Take a look at the responses messages of the Tuya mcu. The header including the version byte 3 is not equal to the Tuya standard wifi protocol. Take a look at #20608.

The issue with either tuya v1 or tuya v2 seems to be the misinterpretation of the protocol commands of tuya mcu due to communicate with the wrong tuya protocol.