xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
3k stars 638 forks source link

Firmware for Tuya dimmer #1729

Closed davebuk closed 4 years ago

davebuk commented 5 years ago

Hi all. How can I look to build a firmware for the following device: https://www.amazon.co.uk/OurLeeme-Dimmer-Control-Function-Compatible/dp/B07RHCFKQ2

I have got one coming this week so I won't know what chip setup is inside but will post when I do. I wonder if the Sonoff wall dimmer will work as base code?

davebuk commented 5 years ago

@mcspr @xoseperez Hi all. The device has arrived. Is it as simple as using a standard dimmer environment and changing the GPIO config, or does a separate environment need to be created?

IMG_20190529_111602 IMG_20190529_105029 IMG_20190529_104843

UPDATE** Tuya Pins

Pin locations if others need them. To flash, the fourth pin 'Flash' needs to be held at GND before power applied and kept at GND during flashing. I think it disables Tx/Rx communication to the MCU. You then need to boot with the GPIO 0 pin held to GND, then released a few seconds after boot. The Tx/Rx wires need to be connected directly to the TYWE3S pins.

mcspr commented 5 years ago

Yes. Since you are just testing, anything with LIGHT dimmer would do. Just make sure to use 1MB environment

davebuk commented 5 years ago

Great, I'll have a go at the weekend. Is there a way to find out the GPIO assignments? Looking at the soldered board, GPIO 0, 14 and 15 are connected. I guess the on board button connects to 'RST' and maybe 0 for BOOT, so is the 'BUTTON' GPIO (not sure if this needs to be defined for the dimmer hardware).

Other than that, I also guess 14 / 15 control the two colour LED and the opposite one controls the dimmer output? One will be the 'LED' GPIO and the other will be the 'LIGHT' GPIO.

mcspr commented 5 years ago

Use multimeter? Button thing is interesting, because it kind-of does different things - GPIO0 for flash / some SW button control, but RST will reset the chip itself. No software interaction there. TX RX are going to the secondary MCU then? (or I am missing some turn)

davebuk commented 5 years ago

The only measurement I got was 9K ohms from one of the ESP pins to one of the LED legs. Other resistances were around 2M ohm.

I need to work out which of the two header pins are TX and RX respectively, but 3v3 and ground are obvious. I guess the through hole header pin pads would be the ones to use for flashing, or should I take wires directly from the ESP chip Tx/ Rx connections?

Thanks.

mcspr commented 5 years ago

Either way. I sure does look like tx rx are right there.

About dimming, it looks like manufacturer used Tuya SDK to set up the secondary controller. Tasmota repo already has an example of what it sends / receives: https://docs.tuya.com/en/mcu/mcu-protocol.html https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/xdrv_16_tuyadimmer.ino

davebuk commented 5 years ago

How do the dimming signals work for an espurna LED dimmer. Do they send signals using the Tx/Rx lines to a MCU which then controls the output drivers?

mcspr commented 5 years ago

What I mean is we don't have LIGHT_PROVIDER for Tuya SDK dimmer variant (yet), if this thing uses serial communication with secondary MCU. Most common LIGHT_PROVIDER_DIMMER generates PWM signal on gpio pin, which translates into brightness.

davebuk commented 5 years ago

The two header solder pads that probably are TX/ Rx are not directly connected to the TX/ Rx of the ESP, they are connected to the STM8S003F3 chip below. I was unsure how the dimming was controlled using a single GPIO.

How long would it take to bring serial dimming into espurna? I may go and have a little play with tasmota for this device.

mcspr commented 5 years ago

It would be useful to know if it is really using that serial protocol. Something like this is already done for rfbridge, so at least writing Serial logic would not be a big problem. Basic idea that I got from the Tuya docs is that ESP should send out heartbeat messages when it boots (every 3s, flagged 0x00) and during normal operation (every 10s, flagged 0x01), then the second controller will accept commands / send out current state.

Another thing to do while it is wired is to download the original firmware .bin via esptool.py (0x100000 as size). edit: baud setting may vary, maybe use 115200 to be safe

davebuk commented 5 years ago

Trying to upload tasmota using pins on the board or pins wired directly to the TYWE3S fails. I don't get communication. Tried with button held at boot, button still held after boot, GPIO 0 to GND at boot and held to GND after boot with no success. The device is not in normal operation so it's doing something different at boot, plus the status LED flashes green after a few seconds then stays very dimly lit green.

I've not used esptool other than the espeasy GUI version to flash. I'll have to look at this over the next few days and see if I can first establish communication then get the current firmware extracted.

mcspr commented 5 years ago

Is it the same with GPIO0? Not aware of GPIO2 operation, this wiki states that it needs to be high: https://github.com/esp8266/esp8266-wiki/wiki/Boot-Process#esp-boot-modes

One thing ESP should always do when resetting is output lines like these on GPIO1 (TX), at 74880 speed:

pressing RST button on d1 mini when GPIO0 is pulled LOW

 ets Jan  8 2013,rst cause:2, boot mode:(1,7)

pressing RST without anything connected

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12 < this is eboot from Arduino, after this line things are sw-specific 
~ld
davebuk commented 5 years ago

Sorry, meant GPIO 0 not 2, post above edited. Apologies!

davebuk commented 5 years ago

I'm am having issues trying to confirm I have connection, but connected to ESP chip Tx/Rx and carrying out the above using putty I get:

ets Jan  8 2013,rst cause:2, boot mode:(1,2)
ets Jan  8 2013,rst cause:2, boot mode:(3,2)

load 0x40100000, len 2592, room 16
tail 0
chksum 0xf3
load 0x3ffe8000, len 764, room 8
tail 4
chksum 0x92
load 0x3ffe82fc, len 676, room 4
tail 0
chksum 0x22
csum 0x22

2nd boot version : 1.7(5d6f877)
SPI Speed : 40MHz
SPI Mode : QIO
SPI Flash Size & Map: 8Mbit(512KB+512KB)
jump to run user1 @ 1000

OS SDK ver: 2.0.0(e8c5810) compiled @ Jan 25 2019 14:26:04
phy ver: 1055_9, pp ver: 10.7

rf cal sector: 121
idle_task_hdl : 40107a90,prio:0, stack:384
tim_task_hdl : 40107bd8, prio:2,stack:512
mcspr commented 5 years ago

But it works. Second log is non-Arduino bootloader, logging that it loaded the manufacturer's firmware. Which now can be safely backed up by esptoolpy and something else uploaded instead

davebuk commented 5 years ago

I have tried

esptool.py -p COM3 -b 1155200 chip_id and esptool.py -p COM3 -b 115200 read_flash 0 0x100000 flash_contents.bin after setting GPIO 0 to GND during boot and neither work. Constant time out errors. One time I had, invalid head of packet 0xff but can't get esptool.py chip_id to output anything.

mcspr commented 5 years ago

Does it work without the -b ... param?

davebuk commented 5 years ago

Nope. :-)

c:\Python27\Scripts>esptool.py -p COM3 read_flash 0 0x100000 flash_contents.bin
esptool.py v2.6
Serial port COM3
Connecting........_____....._____....._____....._____....._____....._____....._____

A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header

c:\Python27\Scripts>esptool.py -p COM3 chip_id
esptool.py v2.6
Serial port COM3
Connecting........_____....._____....._____....._____....._____....._____....._____

A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header

c:\Python27\Scripts>
davebuk commented 5 years ago

Trying same thing on a Sonoff Basic V2 with espurna on gives:

c:\Python27\Scripts>esptool.py chip_id
esptool.py v2.6
Found 1 serial ports
Serial port COM3
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: 84:0d:8e:57:a1:15
Uploading stub...
Running stub...
Stub running...
Chip ID: 0x0057a115
Hard resetting via RTS pin...
mcspr commented 5 years ago

Maybe slower? -b 9600 If it fails seemingly at random, maybe wiring is causing problems. Putty can show boot mode, to make sure uploader mode is on. We want a single line with (1,...). Just make sure to disconnect putty before running esptool itself

davebuk commented 5 years ago

No different at 9600. I'll do some more testing over the next few days and see what I can up with.

davebuk commented 5 years ago

Got it. Using info from [(https://github.com/arendst/Sonoff-Tasmota/wiki/Tuya-Dimmer)], I connected GND to the forth header pin while booting and holding GPIO 0 to ground. I now have:

c:\Python27\Scripts>esptool.py chip_id
esptool.py v2.6
Found 1 serial ports
Serial port COM3
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: bc:dd:c2:a6:cc:bd
Uploading stub...
Running stub...
Stub running...
Chip ID: 0x00a6ccbd
Hard resetting via RTS pin...
davebuk commented 5 years ago

Downloaded file is here: flash_contents.zip

Thanks for your help. Am I ok to try Tasmota on this device now?

mcspr commented 5 years ago

Yes. Thank you for the file! I just tried it with 1M dev board and see the heartbeat on hw serial, as expected.

If you don't mind going slightly offtopic, can you try uploading some basic (literally sonoff-basic should be ok for both espurna and tasmota) espurna image first? I was not able to reproduce the issue from https://github.com/xoseperez/espurna/issues/1663 and I wonder if recent dev builds solved it or I am missing some other bug because of espota.py usage. I tried upgrading via Web too, no problems :/

davebuk commented 5 years ago

No problem. Just to be sure, you would like espurna dev uploaded via esptool then once initialised, try tasmota using espurna web OTA?

mcspr commented 5 years ago

Correct. The test is if Tasmota sonoff.bin or sonoff-basic.bin work after updating from ESPurna via Web OTA

davebuk commented 5 years ago

Working correctly for me. Uploaded a self built version of Sonoff Basic 1.13.6dev of espurna using esptool.py. Espurna boots correctly and I connected to my WiFi. Re-booted then used espurna OTA to flash Tasmota 'sonoff.bin'. Rebooted and device is now showing as sonoff device in wireless networks. Connected and setup Tasmota WiFi credentials and device is now connected to my WiFi and accessible using Tasmota web GUI.

Hope that is of some help?

davebuk commented 5 years ago

The dimmer works with Tasmota using device configuration 'Tuya Dimmer 54'. ON/OFF toggle and dimming works using web GUI.

mcspr commented 5 years ago

Great. Thanks for checking the OTA process!

Now the only thing left is to implement tuya dimmer module.

davebuk commented 5 years ago

Thanks for your help. I've learnt alot over the past few days! I'm sure it'll only take you a few days to implement the dimmer code for espurna! Let me know if I can help with any testing.

davebuk commented 5 years ago

In addition to dimming, would the ability to dim using external push button or switch be viable. Ref #1331 ?

mcspr commented 5 years ago

Button dimming is interesting in this context. idk why that pr never got split up. It got an important addition too - different scheduling method for transitions to make them more consistent. Although, based on the documentation (and tuyadimmer tasmota code, again), that button is intended to reset wifi or all settings

Some things I've noticed after drafting a module for this at https://github.com/xoseperez/espurna/compare/dev...mcspr:experimental/tuya (WARNING! will crash right now, guaranteed :)

davebuk commented 5 years ago

The 'button' option was to use spare GPIOs on the ESP and espurna software to allow extra external buttons for dimming control. I'm in the UK and here we don't normally have a 240V neutral at the light switches. I'd plan to fit one of these devices in the ceiling and have dimmer control using the two wires that come from the wall switch. They'd have to perhaps be configured so double press to brighten up and long press to dim down, with single press to toggle on/off.

From what I understand of the tuya docs and your knowledge, the ESP is only there to provide the interface to the outside world. The MCU does all the control via serial from the ESP, not GPIO pins. Like you said, even the physical button on the device must connect to the MCU rather than the ESP because holding it down at boot wouldn't let me flash it. I had to connect the ESP GPIO 0 to GND to get it to work (while the third header pin was also at GND throughout the flash).

davebuk commented 5 years ago

There is some info here: https://www.letscontrolit.com/forum/viewtopic.php?t=5431

Not sure if it's of any use?

mcspr commented 5 years ago

Oh. I was referencing what I read at https://docs.tuya.com/en/mcu/mcu-protocol.html#3query-the-working-mode-of-the-set-wi-fi-module, secondary chip can sometimes send real pin numbers. Just for compatibility sake, but we are not dealing with one of those here. Long-press from the PR is a bit weird though. I think that was meant to go to the library code instead, probably need to add that there first... Double,triple,long click methods should work right now.

2nd link is interesting. Per the same doc latest section, functional protocol that sort-of matches length 8 and 4 byte value is "temperature", but the forum post got dpID 03 instead of 02. https://www.letscontrolit.com/forum/viewtopic.php?t=5431#p31388 (which is later used as 2 for some reason, and back again)

55AA 00 06 0008 -> 03 <- 02 0004 00000038 4E

this hints as to why tasmota code remembers byte in that position from the data mcu sends out initially (as it looks like, documentation protocols are just examples)

Other documentation on the tuya website is in chinese and "mcu simulator" app I got excited about does need some kind of protocol description .json edit: Example colorlights project does mention "brightness" value setter with the same dpID, so maybe that's where the manufacturer got that id

mcspr commented 5 years ago

I have updated https://github.com/mcspr/espurna/tree/experimental/tuya with the tuya-generic-dimmer platformio env to build the firmware .bin I think the communication is ok now, debugging rx/tx is on by default (tuyaDebug setting, 0/1). Having re-read the documentation, I think 55aa0304000006 should be the packet for the button (aka wifi reset)? No handler for that yet, only DP config and switch / brightness commands from the ESP to the MCU.

davebuk commented 5 years ago

Great work. I'll try and find some time later today to build and test.

davebuk commented 5 years ago

I have built and uploaded using Tasmota minimal .bin then espurna firmare.bin via ota. The device first showed as an espurna-(chip ID) but with no security for the WiFi AP. After a power cycle, I was able to connect and login to the web GUI. Not sure if some Tasmota config was still partially loaded as my WiFi SSID was already there, just no password.

I can access the device via my WiFi but do not have any control of the output. Using 'info' in the debug section I get:

[571338] [WEBSOCKET] #5 connected, ip: 192.168.1.121, url: /ws
[574884] [TYUA] OUT: 55aa00000000ff
[574908] [TYUA] IN: 55aa000000010101
[574911] [TYUA] OUT: 55aa000300010407
[574935] [TYUA] IN: 55aa0003000002
[580406] [WEBSOCKET] Requested action: dbgcmd
[580415] 

---8<-------

[580417] [MAIN] ESPURNA 1.13.6-dev
[580418] [MAIN] xose.perez@gmail.com
[580419] [MAIN] http://tinkerman.cat

[580422] [MAIN] CPU chip ID: 0xA6CCBD
[580424] [MAIN] CPU frequency: 80 MHz
[580424] [MAIN] SDK version: 1.5.3(aec24ac9)
[580425] [MAIN] Core version: 2.3.0
[580426] [MAIN] Core revision: 159542381
[580429] [MAIN] Build time: 1560706427
[580431] 
[580432] [MAIN] Flash chip ID: 0x1440C8
[580437] [MAIN] Flash speed: 40000000 Hz
[580439] [MAIN] Flash mode: DOUT
[580441] 
[580443] [MAIN] Flash size (CHIP)   :  1048576 bytes /  256 sectors (   0 to  255)
[580445] [MAIN] Flash size (SDK)    :  1048576 bytes /  256 sectors (   0 to  255)
[580447] [MAIN] Reserved            :     4096 bytes /    1 sectors (   0 to    0)
[580449] [MAIN] Firmware size       :   504320 bytes /  124 sectors (   1 to  124)
[580454] [MAIN] Max OTA size        :   253952 bytes /   62 sectors ( 125 to  186)
[580456] [MAIN] SPIFFS size         :   262144 bytes /   64 sectors ( 187 to  250)
[580458] [MAIN] EEPROM size         :     4096 bytes /    1 sectors ( 251 to  251)
[580460] [MAIN] Reserved            :    16384 bytes /    4 sectors ( 252 to  255)
[580463] 
[580465] [MAIN] EEPROM sectors: 251, 250
[580466] [MAIN] EEPROM current: 250
[580467] 
[580468] [MAIN] EEPROM:  4096 bytes initially |   996 bytes used (24%) |  3100 bytes free (75%)
[580469] [MAIN] Heap  : 33056 bytes initially | 18952 bytes used (57%) | 14104 bytes free (42%)
[580473] [MAIN] Stack :  4096 bytes initially |  1936 bytes used (47%) |  2160 bytes free (52%)
[580474] 
[580477] [MAIN] Boot version: 31
[580479] [MAIN] Boot mode: 1
[580480] [MAIN] Last reset reason: Power on
[580482] [MAIN] Last reset info: flag: 0
[580484] 
[580485] [MAIN] Board: TUYA_GENERIC_DIMMER
[580487] [MAIN] Support: ALEXA API BROKER BUTTON DEBUG_TELNET DEBUG_WEB DOMOTICZ HOMEASSISTANT LED MDNS_SERVER MQTT NTP SCHEDULER TELNET TERMINAL THINGSPEAK WEB 
[580490] [MAIN] WebUI image: LIGHT
[580491] 
[580492] [MAIN] Firmware MD5: e9c4025aa1b736841ffbffe0e1bbfc07
[580496] [MAIN] Power: 3190 mV
[580498] [MAIN] Power saving delay value: 10 ms
[580499] 

---8<-------

[580500] +OK
[583887] [TYUA] OUT: 55aa00000000ff
[583912] [TYUA] IN: 55aa000000010101
[583914] [TYUA] OUT: 55aa000300010407
[583937] [TYUA] IN: 55aa0003000002
[592897] [TYUA] OUT: 55aa00000000ff
[592921] [TYUA] IN: 55aa000000010101
[592924] [TYUA] OUT: 55aa000300010407
[592947] [TYUA] IN: 55aa0003000002
[601899] [TYUA] OUT: 55aa00000000ff
[601923] [TYUA] IN: 55aa000000010101
[601926] [TYUA] OUT: 55aa000300010407
[601948] [TYUA] IN: 55aa0003000002
[610905] [TYUA] OUT: 55aa00000000ff
[610930] [TYUA] IN: 55aa000000010101
[610932] [TYUA] OUT: 55aa000300010407
[610955] [TYUA] IN: 55aa0003000002
[619908] [TYUA] OUT: 55aa00000000ff
[619932] [TYUA] IN: 55aa000000010101
[619935] [TYUA] OUT: 55aa000300010407
[619958] [TYUA] IN: 55aa0003000002
[628910] [TYUA] OUT: 55aa00000000ff
[628935] [TYUA] IN: 55aa000000010101
[628937] [TYUA] OUT: 55aa000300010407
[628960] [TYUA] IN: 55aa0003000002 

The status page looks like:

tuya001

After changing brightness on the status page I get:

[321009] [WEBSOCKET] Requested action: brightness
[321027] [TYUA] OUT: 55aa0006000803020004000000ff15
[322369] [TYUA] OUT: 55aa00000000ff
[322393] [TYUA] IN: 55aa000000010101
[322396] [TYUA] OUT: 55aa000300010407
[322418] [TYUA] IN: 55aa0003000002
[331377] [TYUA] OUT: 55aa00000000ff
[331401] [TYUA] IN: 55aa000000010101
[331404] [TYUA] OUT: 55aa000300010407
[331426] [TYUA] IN: 55aa0003000002

but the light is not controllable. It will toggle on and off by pressing the button on the device giving:

[442577] [TYUA] IN: 55aa00070008020200040000001026
[442579] [TUYA] Unknown DP id=2 type=2
[442591] [TYUA] IN: 55aa0007000501010001010f
[442614] [TYUA] IN: 55aa0007000802020004000000ff15
[442615] [TUYA] Unknown DP id=2 type=2
[445528] [TYUA] IN: 55aa0007000501010001000e
[445542] [TYUA] IN: 55aa0007000802020004000000ff15
[445545] [TUYA] Unknown DP id=2 type=2
[445557] [TYUA] IN: 55aa0007000501010001000e
[445569] [TYUA] IN: 55aa00070008020200040000001026
[445570] [TUYA] Unknown DP id=2 type=2
[448472] [TYUA] OUT: 55aa00000000ff

but again, the light cannot a adjusted in brightness from the web GUI.

Hope that is of some help?

mcspr commented 5 years ago

It is a strange AP mode issue. I did notice some code in tasmota modifying SDK wifi settings storage on wifi disconnect, maybe that what causes it. But as you have noted, it goes away after reset because we modify it again.

Thank you for logs! Brightness control must work after doing set tuyaDimmerDP 2 and rebooting, I have missed setSetting call somewhere in the queries but I got some new idea for that anyways. And one virtual relay, I'll update the hardware config with 1 dummy relay and RELAY_PROVIDER_LIGHT It is strange, however, that it sends 16 (55aa0007000802020004000000 --> 10 <-- 26) as OFF brightness value. But it would not matter, as relay would turn the light off first.

And I think I'll add tuya device description + discovered protocols to the info banners, so its not only displayed on boot.

Button does send anything different on double, triple or long presses?

davebuk commented 5 years ago

I hadn't realised you had added that code. The following is single on/off, double, triple, off, 5 seconds held on, off, 10 seconds held on, off, 30 seconds held on, off.

For reference, the built in LED is off when off and RED when the light is on.

[616544] [WEBSOCKET] #8 connected, ip: 192.168.1.121, url: /ws
[623703] [TYUA] OUT: 55aa00000000ff
[623727] [TYUA] IN: 55aa000000010101
[623730] [TYUA] OUT: 55aa000300010407
[623752] [TYUA] IN: 55aa0003000002
[632704] [TYUA] OUT: 55aa00000000ff
[632728] [TYUA] IN: 55aa000000010101
[632731] [TYUA] OUT: 55aa000300010407
[632754] [TYUA] IN: 55aa0003000002
[641712] [TYUA] OUT: 55aa00000000ff
[641736] [TYUA] IN: 55aa000000010101
[641739] [TYUA] OUT: 55aa000300010407
[641762] [TYUA] IN: 55aa0003000002
[650722] [TYUA] OUT: 55aa00000000ff
[650746] [TYUA] IN: 55aa000000010101
[650749] [TYUA] OUT: 55aa000300010407
[650771] [TYUA] IN: 55aa0003000002
[659727] [TYUA] OUT: 55aa00000000ff
[659752] [TYUA] IN: 55aa000000010101
[659755] [TYUA] OUT: 55aa000300010407
[659778] [TYUA] IN: 55aa0003000002
[668022] [TYUA] IN: 55aa0007000501010001010f
[668046] [TYUA] IN: 55aa00070008020200040000001026
[668049] [TUYA] Unknown DP id=2 type=2
[668050] [TYUA] IN: 55aa0007000501010001010f
[668074] [TYUA] IN: 55aa0007000802020004000000ff15
[668076] [TUYA] Unknown DP id=2 type=2
[668731] [TYUA] OUT: 55aa00000000ff
[668755] [TYUA] IN: 55aa000000010101
[668758] [TYUA] OUT: 55aa000300010407
[668781] [TYUA] IN: 55aa0003000002
[669466] [TYUA] IN: 55aa0007000501010001000e
[669480] [TYUA] IN: 55aa0007000802020004000000ff15
[669482] [TUYA] Unknown DP id=2 type=2
[669495] [TYUA] IN: 55aa0007000501010001000e
[669509] [TYUA] IN: 55aa00070008020200040000001026
[669511] [TUYA] Unknown DP id=2 type=2
[670643] [TYUA] IN: 55aa0007000501010001010f
[670667] [TYUA] IN: 55aa00070008020200040000001026
[670670] [TUYA] Unknown DP id=2 type=2
[670672] [TYUA] IN: 55aa0007000501010001010f
[670695] [TYUA] IN: 55aa0007000802020004000000ff15
[670697] [TUYA] Unknown DP id=2 type=2
[670954] [TYUA] IN: 55aa0007000501010001000e
[670979] [TYUA] IN: 55aa0007000802020004000000ff15
[670981] [TUYA] Unknown DP id=2 type=2
[670983] [TYUA] IN: 55aa0007000501010001000e
[671006] [TYUA] IN: 55aa00070008020200040000001026
[671008] [TUYA] Unknown DP id=2 type=2
[672027] [TYUA] IN: 55aa0007000501010001010f
[672041] [TYUA] IN: 55aa00070008020200040000001026
[672044] [TUYA] Unknown DP id=2 type=2
[672056] [TYUA] IN: 55aa0007000501010001010f
[672070] [TYUA] IN: 55aa0007000802020004000000ff15
[672072] [TUYA] Unknown DP id=2 type=2
[672482] [TYUA] IN: 55aa0007000501010001000e
[672496] [TYUA] IN: 55aa0007000802020004000000ff15
[672499] [TUYA] Unknown DP id=2 type=2
[672511] [TYUA] IN: 55aa0007000501010001000e
[672524] [TYUA] IN: 55aa00070008020200040000001026
[672526] [TUYA] Unknown DP id=2 type=2
[672895] [TYUA] IN: 55aa0007000501010001010f
[672910] [TYUA] IN: 55aa00070008020200040000001026
[672913] [TUYA] Unknown DP id=2 type=2
[672925] [TYUA] IN: 55aa0007000501010001010f
[672939] [TYUA] IN: 55aa0007000802020004000000ff15
[672941] [TUYA] Unknown DP id=2 type=2
[675774] [TYUA] IN: 55aa0007000501010001000e
[675788] [TYUA] IN: 55aa0007000802020004000000ff15
[675791] [TUYA] Unknown DP id=2 type=2
[675803] [TYUA] IN: 55aa0007000501010001000e
[675816] [TYUA] IN: 55aa00070008020200040000001026
[675819] [TUYA] Unknown DP id=2 type=2
[677617] [TYUA] IN: 55aa0007000501010001010f
[677631] [TYUA] IN: 55aa00070008020200040000001026
[677634] [TUYA] Unknown DP id=2 type=2
[677646] [TYUA] IN: 55aa0007000501010001010f
[677661] [TYUA] IN: 55aa0007000802020004000000ff15
[677664] [TUYA] Unknown DP id=2 type=2
[677738] [TYUA] OUT: 55aa00000000ff
[677761] [TYUA] IN: 55aa000000010101
[677764] [TYUA] OUT: 55aa000300010407
[677787] [TYUA] IN: 55aa0003000002
[684085] [TYUA] IN: 55aa0007000501010001000e
[684099] [TYUA] IN: 55aa0007000802020004000000ff15
[684101] [TUYA] Unknown DP id=2 type=2
[684114] [TYUA] IN: 55aa0007000501010001000e
[684129] [TYUA] IN: 55aa00070008020200040000001026
[684132] [TUYA] Unknown DP id=2 type=2
[685892] [TYUA] IN: 55aa0007000501010001010f
[685916] [TYUA] IN: 55aa00070008020200040000001026
[685919] [TUYA] Unknown DP id=2 type=2
[685921] [TYUA] IN: 55aa0007000501010001010f
[685944] [TYUA] IN: 55aa0007000802020004000000ff15
[685945] [TUYA] Unknown DP id=2 type=2
[686743] [TYUA] OUT: 55aa00000000ff
[686767] [TYUA] IN: 55aa000000010101
[686770] [TYUA] OUT: 55aa000300010407
[686793] [TYUA] IN: 55aa0003000002
[695752] [TYUA] OUT: 55aa00000000ff
[695776] [TYUA] IN: 55aa000000010101
[695779] [TYUA] OUT: 55aa000300010407
[695802] [TYUA] IN: 55aa0003000002
[698344] [TYUA] IN: 55aa0007000501010001000e
[698358] [TYUA] IN: 55aa0007000802020004000000ff15
[698361] [TUYA] Unknown DP id=2 type=2
[698373] [TYUA] IN: 55aa0007000501010001000e
[698386] [TYUA] IN: 55aa00070008020200040000001026
[698388] [TUYA] Unknown DP id=2 type=2
[699895] [TYUA] IN: 55aa0007000501010001010f
[699909] [TYUA] IN: 55aa00070008020200040000001026
[699911] [TUYA] Unknown DP id=2 type=2
[699924] [TYUA] IN: 55aa0007000501010001010f
[699938] [TYUA] IN: 55aa0007000802020004000000ff15
[699941] [TUYA] Unknown DP id=2 type=2
[704754] [TYUA] OUT: 55aa00000000ff
[704778] [TYUA] IN: 55aa000000010101
[704781] [TYUA] OUT: 55aa000300010407
[704804] [TYUA] IN: 55aa0003000002
[713756] [TYUA] OUT: 55aa00000000ff
[713780] [TYUA] IN: 55aa000000010101
[713783] [TYUA] OUT: 55aa000300010407
[713806] [TYUA] IN: 55aa0003000002
[720252] [TYUA] IN: 55aa0007000501010001000e
[720266] [TYUA] IN: 55aa0007000802020004000000ff15
[720268] [TUYA] Unknown DP id=2 type=2
[720282] [TYUA] IN: 55aa0007000501010001000e
[720294] [TYUA] IN: 55aa00070008020200040000001026
[720295] [TUYA] Unknown DP id=2 type=2
[722766] [TYUA] OUT: 55aa00000000ff
[722790] [TYUA] IN: 55aa000000010101
[722793] [TYUA] OUT: 55aa000300010407
[722816] [TYUA] IN: 55aa0003000002
[731772] [TYUA] OUT: 55aa00000000ff
[731796] [TYUA] IN: 55aa000000010101
[731799] [TYUA] OUT: 55aa000300010407
[731823] [TYUA] IN: 55aa0003000002
[740780] [TYUA] OUT: 55aa00000000ff
[740804] [TYUA] IN: 55aa000000010101
[740807] [TYUA] OUT: 55aa000300010407
[740830] [TYUA] IN: 55aa0003000002
[749784] [TYUA] OUT: 55aa00000000ff
[749808] [TYUA] IN: 55aa000000010101
[749811] [TYUA] OUT: 55aa000300010407
[749834] [TYUA] IN: 55aa0003000002
[758789] [TYUA] OUT: 55aa00000000ff
[758814] [TYUA] IN: 55aa000000010101
[758817] [TYUA] OUT: 55aa000300010407
[758840] [TYUA] IN: 55aa0003000002
davebuk commented 5 years ago

On a side note, after flashing using the Tasmota minimal, then espurna 493KB .bin file, I only have 258048 bytes free for an OTA update. I guess using the two step method with Tasmota has re-sized the flash memory? Can I get back to two 512KB halves using OTA uploads or will I just need to erase/upload espurna using the serial ports?

mcspr commented 5 years ago

Button does not seem to do anything other than sending brightness 0 to 255 and back, switch ON OFF. What does the paper manual say about wifi modes? I did find one example: https://fccid.io/ZZH-WF500D/User-Manual/Users-Manual-3846958 Pressing 5 times in a row and holding 6th should trigger WiFi config? Maybe that will work?

That flash size issue is my bad, I copied the build flags string from the other wemos board which also had it wrong. It should be ${common.build_flags_1m0m} instead of ${common.build_flags}. With platformio defaults we reserve 256Kb for SPIFFS and can't use that space for firmware. No way to OTA upgrade I'm afraid, It will reject firmware based on size first, not looking at the new flash layout.

davebuk commented 5 years ago

The manuals says that if the devices doesn't boot with a rapidly flashing LED, hold the button down for 15 seconds and confirm the LED is now flashing rapidly.

Pressing 5 times and holding on the sixth gives:

[421641] [TYUA] OUT: 55aa00000000ff
[421664] [TYUA] IN: 55aa000000010101
[421666] [TYUA] OUT: 55aa000300010407
[421689] [TYUA] IN: 55aa0003000002
[430649] [TYUA] OUT: 55aa00000000ff
[430674] [TYUA] IN: 55aa000000010101
[430678] [TYUA] OUT: 55aa000300010407
[430701] [TYUA] IN: 55aa0003000002
[437921] [TYUA] IN: 55aa0007000501010001010f
[437934] [TYUA] IN: 55aa00070008020200040000001026
[437936] [TUYA] Unknown DP id=2 type=2
[437947] [TYUA] IN: 55aa0007000501010001010f
[437970] [TYUA] IN: 55aa0007000802020004000000ff15
[437971] [TUYA] Unknown DP id=2 type=2
[438453] [TYUA] IN: 55aa0007000501010001000e
[438468] [TYUA] IN: 55aa0007000802020004000000ff15
[438470] [TUYA] Unknown DP id=2 type=2
[438483] [TYUA] IN: 55aa0007000501010001000e
[438495] [TYUA] IN: 55aa00070008020200040000001026
[438496] [TUYA] Unknown DP id=2 type=2
[438999] [TYUA] IN: 55aa0007000501010001010f
[439013] [TYUA] IN: 55aa00070008020200040000001026
[439014] [TUYA] Unknown DP id=2 type=2
[439026] [TYUA] IN: 55aa0007000501010001010f
[439041] [TYUA] IN: 55aa0007000802020004000000ff15
[439042] [TUYA] Unknown DP id=2 type=2
[439535] [TYUA] IN: 55aa0007000501010001000e
[439559] [TYUA] IN: 55aa0007000802020004000000ff15
[439562] [TUYA] Unknown DP id=2 type=2
[439563] [TYUA] IN: 55aa0007000501010001000e
[439588] [TYUA] IN: 55aa00070008020200040000001026
[439591] [TUYA] Unknown DP id=2 type=2
[439654] [TYUA] OUT: 55aa00000000ff
[439678] [TYUA] IN: 55aa000000010101
[439681] [TYUA] OUT: 55aa000300010407
[439704] [TYUA] IN: 55aa0003000002
[440105] [TYUA] IN: 55aa0007000501010001010f
[440118] [TYUA] IN: 55aa00070008020200040000001026
[440120] [TUYA] Unknown DP id=2 type=2
[440131] [TYUA] IN: 55aa0007000501010001010f
[440153] [TYUA] IN: 55aa0007000802020004000000ff15
[440157] [TUYA] Unknown DP id=2 type=2
[440640] [TYUA] IN: 55aa0007000501010001000e
[440654] [TYUA] IN: 55aa0007000802020004000000ff15
[440655] [TUYA] Unknown DP id=2 type=2
[440667] [TYUA] IN: 55aa0007000501010001000e
[440678] [TYUA] IN: 55aa00070008020200040000001026
[440679] [TUYA] Unknown DP id=2 type=2
[448658] [TYUA] OUT: 55aa00000000ff
[448682] [TYUA] IN: 55aa000000010101
[448683] [TYUA] OUT: 55aa000300010407
[448705] [TYUA] IN: 55aa0003000002

Does it matter in the output code it says [TYUA] rather than [TUYA]? When you are ready, let me know when to try building the next test version.

mcspr commented 5 years ago

I was expecting to see either 55aa0004... or 55aa0005... somewhere. Huh. Maybe the 15 second hold would send it. And TYUA thing is just a misspelling in the logging method 🤷‍♂

Will do. I want to try to replace static switch / light config with some dynamic detection.

ot. researching some other tuya modules, they can be pretty expressive: https://blakadder.github.io/templates/oil_diffuser_550ml.html

davebuk commented 5 years ago

@mcspr Have you found any time to look any more into the code? The external button press for dimming is not something I need for my current application, it was just a nice to have. Do you have any working code that will enable power toggle using the built in switch and on/off and dimming control using MQTT?

mcspr commented 5 years ago

I'll try to come up with something mergeable this week.

Current tree is not working, but I did include the relay definition and flash layout fix 2 weeks ago: https://github.com/mcspr/espurna/commit/bca19f4eab5c3702404b6a9b878911e4711b60dc Have you tried setting additional params in the terminal for the previous running build (bc last message shows Unknown DP id=2 type=2, including reboot to apply)?

set tuyaDimmerDP 2
set useTransitions 0

First one will apply the correct message DP, so both In and Out messages are parsed by both sides (e.g. 55aa00070008020200040000001026). Second one I think is optional, it disables transitions if they look strange.

davebuk commented 5 years ago

Failed to build, /code/espurna/tuya.ino:23:27: fatal error: tuya_protocol.h: No such file or directory

Unless I'm looking in the wrong place, I can't find tuya_protocol.h

mcspr commented 5 years ago

It's not commited yet, note "not working" part. :) I have not tested that thing yet, but uploaded part of it as a backup.

If you want just the fix mentioned above, git checkout <commit hash> (add -b ... to checkout into a separate branch)

davebuk commented 5 years ago

Sorry, my misunderstanding ;-) I'll wait until you have the code ready to merge. I am currently running the tasmota firmware so no rush.

Thanks again.

mcspr commented 5 years ago

I have updated the experimental branch with protocol discovery and better (hopefully!) relay / channel interactions. Based on the logs above, we are using 1 light channel and 1 switch that controls it.

Discovery should work out configuration for both. If not:

set tuyaSwitch0 1
set tuyaChannel0 2
reset

I tried with what I think discovery looks like and the config above.

edit: Additional terminal commands tuya.show displays current configuration tuya.save in case of discovery, this will save current configuration to flash

Sidenote: while there is no Serial logging here, it can be remedied by using Serial1 (GPIO2 aka TX1, no RX there):

-DDEBUG_PORT=Serial1 -DDEBUG_SERIAL_SUPPORT=1

It would be interesting to see early boot logs.