From e84f78b3954b19fd2b17c7bd6b0a4e5c5a38f337 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Sun, 28 Oct 2018 13:49:54 +0100 Subject: [PATCH 1/4] tuya: set wifi-state --- sonoff/xdrv_16_tuyadimmer.ino | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index eb700c322..b2b4da481 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -242,6 +242,16 @@ void TuyaInit() TuyaSerial->write((uint8_t)0x00); // following data length 0x00 TuyaSerial->write((uint8_t)0x07); // checksum:sum of all bytes in packet mod 256 TuyaSerial->flush(); + + TuyaSerial->write((uint8_t)0x55); // header 55AA + TuyaSerial->write((uint8_t)0xAA); + TuyaSerial->write((uint8_t)0x00); // version 00 + TuyaSerial->write((uint8_t)0x03); // command 03 - set wifi state + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x01); // following data length 0x01 + TuyaSerial->write((uint8_t)0x03); // wifi state 4 (configured and connected) + TuyaSerial->write((uint8_t)0x06); // checksum:sum of all bytes in packet mod 256 + TuyaSerial->flush(); } } From 09de17d385026d4869b0d4f11748e1b9f886fdf1 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Mon, 29 Oct 2018 01:10:57 +0100 Subject: [PATCH 2/4] tuya: reset wifi-led every second --- sonoff/xdrv_16_tuyadimmer.ino | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index b2b4da481..9877682dc 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -221,6 +221,21 @@ boolean TuyaModuleSelected() return true; } +void TuyaResetWifiLed(){ + snprintf_P(log_data, sizeof(log_data), "TYA: Reset WiFi LED"); + AddLog(LOG_LEVEL_DEBUG); + + TuyaSerial->write((uint8_t)0x55); // header 55AA + TuyaSerial->write((uint8_t)0xAA); + TuyaSerial->write((uint8_t)0x00); // version 00 + TuyaSerial->write((uint8_t)0x03); // command 03 - set wifi state + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x01); // following data length 0x01 + TuyaSerial->write((uint8_t)0x03); // wifi state 4 (configured and connected) + TuyaSerial->write((uint8_t)0x06); // checksum:sum of all bytes in packet mod 256 + TuyaSerial->flush(); +} + void TuyaInit() { if (!Settings.param[P_TUYA_DIMMER_ID]) { @@ -242,16 +257,6 @@ void TuyaInit() TuyaSerial->write((uint8_t)0x00); // following data length 0x00 TuyaSerial->write((uint8_t)0x07); // checksum:sum of all bytes in packet mod 256 TuyaSerial->flush(); - - TuyaSerial->write((uint8_t)0x55); // header 55AA - TuyaSerial->write((uint8_t)0xAA); - TuyaSerial->write((uint8_t)0x00); // version 00 - TuyaSerial->write((uint8_t)0x03); // command 03 - set wifi state - TuyaSerial->write((uint8_t)0x00); - TuyaSerial->write((uint8_t)0x01); // following data length 0x01 - TuyaSerial->write((uint8_t)0x03); // wifi state 4 (configured and connected) - TuyaSerial->write((uint8_t)0x06); // checksum:sum of all bytes in packet mod 256 - TuyaSerial->flush(); } } @@ -298,6 +303,8 @@ boolean Xdrv16(byte function) case FUNC_BUTTON_PRESSED: result = TuyaButtonPressed(); break; + case FUNC_EVERY_SECOND: + if(TuyaSerial) { TuyaResetWifiLed(); } } } return result; From b24449aa2b8f8dce1a0896952f34f25986664a3e Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Mon, 29 Oct 2018 01:27:45 +0100 Subject: [PATCH 3/4] tuya: add break --- sonoff/xdrv_16_tuyadimmer.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 9877682dc..afdcd3327 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -305,6 +305,7 @@ boolean Xdrv16(byte function) break; case FUNC_EVERY_SECOND: if(TuyaSerial) { TuyaResetWifiLed(); } + break; } } return result; From 80d5a23e399c3dcc29c7ac11c5a2af75048ecedb Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Mon, 29 Oct 2018 01:58:48 +0100 Subject: [PATCH 4/4] tuya: only send wifi-led-reset until ack is received --- sonoff/xdrv_16_tuyadimmer.ino | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index afdcd3327..bfc4fc903 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -33,6 +33,7 @@ boolean tuya_ignore_dim = false; // Flag to skip serial send to preve uint8_t tuya_cmd_status = 0; // Current status of serial-read uint8_t tuya_cmd_checksum = 0; // Checksum of tuya command uint8_t tuya_data_len = 0; // Data lenght of command +bool tuya_wifi_state = false; char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer int tuya_byte_counter = 0; // Index in serial receive buffer @@ -142,12 +143,15 @@ void TuyaPacketProcess() ExecuteCommand(scmnd, SRC_SWITCH); } } - else if (tuya_byte_counter == 8 && tuya_buffer[3] == 5 && tuya_buffer[5] == 1 && tuya_buffer[7] == 5 ) { // reset WiFi settings packet - to do: reset red MCU LED after WiFi is up + else if (tuya_byte_counter == 8 && tuya_buffer[3] == 5 && tuya_buffer[5] == 1 && tuya_buffer[7] == 5 ) { // reset WiFi settings packet AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi Reset Rcvd")); + TuyaResetWifi(); + } + else if (tuya_byte_counter == 7 && tuya_buffer[3] == 3 && tuya_buffer[6] == 2) { // WiFi LED has been sucessfully reset. - snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " 2"); - ExecuteCommand(scmnd, SRC_BUTTON); + AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED reset ACK")); + tuya_wifi_state = true; } } @@ -260,18 +264,24 @@ void TuyaInit() } } +void TuyaResetWifi() +{ + if (!Settings.flag.button_restrict) { + char scmnd[20]; + snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2); + ExecuteCommand(scmnd, SRC_BUTTON); + tuya_wifi_state = false; + } +} + boolean TuyaButtonPressed() { if ((PRESSED == XdrvMailbox.payload) && (NOT_PRESSED == lastbutton[XdrvMailbox.index])) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), XdrvMailbox.index +1); AddLog(LOG_LEVEL_DEBUG); + TuyaResetWifi(); - if (!Settings.flag.button_restrict) { - char scmnd[20]; - snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2); - ExecuteCommand(scmnd, SRC_BUTTON); - } } return true; // Serviced here } @@ -304,7 +314,7 @@ boolean Xdrv16(byte function) result = TuyaButtonPressed(); break; case FUNC_EVERY_SECOND: - if(TuyaSerial) { TuyaResetWifiLed(); } + if(TuyaSerial && !tuya_wifi_state) { TuyaResetWifiLed(); } break; } }