From 166f6ce3a6428ca4f61a85f58701207d2123d27a Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Mon, 29 Oct 2018 23:35:00 +0100 Subject: [PATCH 1/6] tuya: proper indication of wifi-state --- sonoff/xdrv_16_tuyadimmer.ino | 37 +++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index bfc4fc903..65d316ca8 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -33,7 +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; +uint8_t tuya_wifi_state = 0xFF; char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer int tuya_byte_counter = 0; // Index in serial receive buffer @@ -148,10 +148,10 @@ void TuyaPacketProcess() 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. + else if (tuya_byte_counter == 7 && tuya_buffer[3] == 3 && tuya_buffer[6] == 2) { // WiFi LED has been sucessfully set. - AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED reset ACK")); - tuya_wifi_state = true; + AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED set ACK")); + tuya_wifi_state = WifiState(); } } @@ -225,8 +225,25 @@ boolean TuyaModuleSelected() return true; } -void TuyaResetWifiLed(){ - snprintf_P(log_data, sizeof(log_data), "TYA: Reset WiFi LED"); +void TuyaSetWifiLed(){ + uint8_t wifi_state = 0x02; + switch(WifiState()){ + case WIFI_SMARTCONFIG: + wifi_state = 0x00; + break; + case WIFI_MANAGER: + case WIFI_WPSCONFIG: + wifi_state = 0x01; + break; + case WIFI_WAIT: + case WIFI_RETRY: + wifi_state = 0x02; + case WIFI_RESTART: + wifi_state = 0x03; + break; + } + + snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", WifiState(), wifi_state); AddLog(LOG_LEVEL_DEBUG); TuyaSerial->write((uint8_t)0x55); // header 55AA @@ -235,8 +252,8 @@ void TuyaResetWifiLed(){ 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->write((uint8_t)wifi_state); // wifi state (0x00 == smartconfig, 0x01 == AP, 0x02 == not connected, 0x03 == connected) + TuyaSerial->write((uint8_t)(0x03 + wifi_state)); // checksum:sum of all bytes in packet mod 256 TuyaSerial->flush(); } @@ -268,9 +285,9 @@ void TuyaResetWifi() { if (!Settings.flag.button_restrict) { char scmnd[20]; + tuya_wifi_state = 0xFF; snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2); ExecuteCommand(scmnd, SRC_BUTTON); - tuya_wifi_state = false; } } @@ -314,7 +331,7 @@ boolean Xdrv16(byte function) result = TuyaButtonPressed(); break; case FUNC_EVERY_SECOND: - if(TuyaSerial && !tuya_wifi_state) { TuyaResetWifiLed(); } + if(TuyaSerial && tuya_wifi_state!=WifiState()) { TuyaSetWifiLed(); } break; } } From ccd10e151fcc70f05480746eb611868c9259f7c1 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Mon, 29 Oct 2018 23:45:18 +0100 Subject: [PATCH 2/6] tuya: wifi-state: add default state --- sonoff/xdrv_16_tuyadimmer.ino | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 65d316ca8..218ef98fd 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -235,12 +235,14 @@ void TuyaSetWifiLed(){ case WIFI_WPSCONFIG: wifi_state = 0x01; break; - case WIFI_WAIT: - case WIFI_RETRY: - wifi_state = 0x02; case WIFI_RESTART: wifi_state = 0x03; break; + case WIFI_WAIT: + case WIFI_RETRY: + default: + wifi_state = 0x02; + break; } snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", WifiState(), wifi_state); From 4a03b1c2cdfd7f31991a462c21697a9c0a3817fd Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 30 Oct 2018 01:11:08 +0100 Subject: [PATCH 3/6] tuya: wifi-state switch debug-arguments --- sonoff/xdrv_16_tuyadimmer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 218ef98fd..271cbb476 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -245,7 +245,7 @@ void TuyaSetWifiLed(){ break; } - snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", WifiState(), wifi_state); + snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, WifiState()); AddLog(LOG_LEVEL_DEBUG); TuyaSerial->write((uint8_t)0x55); // header 55AA From 8f3f1f395824a398e0219fafb001c3ff794871f0 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 30 Oct 2018 05:33:36 +0100 Subject: [PATCH 4/6] tuya: implement seperate WifiState() so we actually know, when we're not connected --- sonoff/xdrv_16_tuyadimmer.ino | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 271cbb476..a24043289 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -33,7 +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 -uint8_t tuya_wifi_state = 0xFF; +int8_t tuya_wifi_state = -2; char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer int tuya_byte_counter = 0; // Index in serial receive buffer @@ -151,7 +151,7 @@ void TuyaPacketProcess() else if (tuya_byte_counter == 7 && tuya_buffer[3] == 3 && tuya_buffer[6] == 2) { // WiFi LED has been sucessfully set. AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED set ACK")); - tuya_wifi_state = WifiState(); + tuya_wifi_state = TuyaWifiState(); } } @@ -225,9 +225,20 @@ boolean TuyaModuleSelected() return true; } +int TuyaWifiState() +{ + int state = -1; + + if ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0)) { + state = WIFI_RESTART; + } + if (wifi_config_type) { state = wifi_config_type; } + return state; +} + void TuyaSetWifiLed(){ uint8_t wifi_state = 0x02; - switch(WifiState()){ + switch(TuyaWifiState()){ case WIFI_SMARTCONFIG: wifi_state = 0x00; break; @@ -238,14 +249,12 @@ void TuyaSetWifiLed(){ case WIFI_RESTART: wifi_state = 0x03; break; - case WIFI_WAIT: - case WIFI_RETRY: default: wifi_state = 0x02; break; } - snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, WifiState()); + snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, TuyaWifiState()); AddLog(LOG_LEVEL_DEBUG); TuyaSerial->write((uint8_t)0x55); // header 55AA @@ -287,7 +296,7 @@ void TuyaResetWifi() { if (!Settings.flag.button_restrict) { char scmnd[20]; - tuya_wifi_state = 0xFF; + tuya_wifi_state = -1; snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2); ExecuteCommand(scmnd, SRC_BUTTON); } @@ -333,7 +342,7 @@ boolean Xdrv16(byte function) result = TuyaButtonPressed(); break; case FUNC_EVERY_SECOND: - if(TuyaSerial && tuya_wifi_state!=WifiState()) { TuyaSetWifiLed(); } + if(TuyaSerial && tuya_wifi_state!=TuyaWifiState()) { TuyaSetWifiLed(); } break; } } From faab6f122144bf14e527b6ae08d00a56614ffacf Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 30 Oct 2018 15:34:31 +0100 Subject: [PATCH 5/6] tuya: modify WifiState() in support.ino to reflect not-connected-state instead of reimplementing it --- sonoff/sonoff.ino | 4 ++-- sonoff/support.ino | 2 +- sonoff/xdrv_16_tuyadimmer.ino | 19 ++++--------------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 2258ab240..98af3ef9f 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1110,7 +1110,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) wifi_state_flag = Settings.sta_config; snprintf_P(stemp1, sizeof(stemp1), kWifiConfig[Settings.sta_config]); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIFICONFIG "\":\"%s " D_JSON_SELECTED "\"}"), stemp1); - if (WifiState() != WIFI_RESTART) { + if (WifiState() > WIFI_RESTART) { // snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s after restart"), mqtt_data); restart_flag = 2; } @@ -1795,7 +1795,7 @@ void ButtonHandler() // Success } else { if (multipress[button_index] < 3) { // Single or Double press - if (WifiState()) { // WPSconfig, Smartconfig or Wifimanager active + if (WifiState() > WIFI_RESTART) { // WPSconfig, Smartconfig or Wifimanager active restart_flag = 1; } else { ExecuteCommandPower(button_index + multipress[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally diff --git a/sonoff/support.ino b/sonoff/support.ino index 67d55a8ab..aab21da37 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1613,7 +1613,7 @@ void WifiCheck(uint8_t param) int WifiState() { - int state; + int state = -1; if ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0)) { state = WIFI_RESTART; diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index a24043289..4cdc7bef4 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -151,7 +151,7 @@ void TuyaPacketProcess() else if (tuya_byte_counter == 7 && tuya_buffer[3] == 3 && tuya_buffer[6] == 2) { // WiFi LED has been sucessfully set. AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED set ACK")); - tuya_wifi_state = TuyaWifiState(); + tuya_wifi_state = WifiState(); } } @@ -225,20 +225,9 @@ boolean TuyaModuleSelected() return true; } -int TuyaWifiState() -{ - int state = -1; - - if ((WL_CONNECTED == WiFi.status()) && (static_cast(WiFi.localIP()) != 0)) { - state = WIFI_RESTART; - } - if (wifi_config_type) { state = wifi_config_type; } - return state; -} - void TuyaSetWifiLed(){ uint8_t wifi_state = 0x02; - switch(TuyaWifiState()){ + switch(WifiState()){ case WIFI_SMARTCONFIG: wifi_state = 0x00; break; @@ -254,7 +243,7 @@ void TuyaSetWifiLed(){ break; } - snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, TuyaWifiState()); + snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, WifiState()); AddLog(LOG_LEVEL_DEBUG); TuyaSerial->write((uint8_t)0x55); // header 55AA @@ -342,7 +331,7 @@ boolean Xdrv16(byte function) result = TuyaButtonPressed(); break; case FUNC_EVERY_SECOND: - if(TuyaSerial && tuya_wifi_state!=TuyaWifiState()) { TuyaSetWifiLed(); } + if(TuyaSerial && tuya_wifi_state!=WifiState()) { TuyaSetWifiLed(); } break; } } From 9125823da3fd0c267a1f85dea77192acb8ea5a4e Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 30 Oct 2018 16:56:57 +0100 Subject: [PATCH 6/6] tuya: remove default from switch, since wifi_state is already initialized to 0x02 --- sonoff/xdrv_16_tuyadimmer.ino | 3 --- 1 file changed, 3 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 4cdc7bef4..91fe17581 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -238,9 +238,6 @@ void TuyaSetWifiLed(){ case WIFI_RESTART: wifi_state = 0x03; break; - default: - wifi_state = 0x02; - break; } snprintf_P(log_data, sizeof(log_data), "TYA: Set WiFi LED to state %d (%d)", wifi_state, WifiState());