Skip to content

Commit

Permalink
Tuya sequence fix (#18085)
Browse files Browse the repository at this point in the history
* no additional command while waiting for response

* send wifi state 4 when mqtt is disabled
  • Loading branch information
barbudor authored Mar 3, 2023
1 parent eb62555 commit edb2fad
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ struct TUYA {
uint32_t ignore_dimmer_cmd_timeout = 0; // Time until which received dimmer commands should be ignored
bool ignore_tuyareceived = false; // When a modeset changes ignore stat
bool active;
uint32_t time_last_cmd; // to compute timeout on response and not sending another message
} Tuya;

#define TUYA_CMD_TIMEOUT 200

#define D_JSON_TUYA_MCU_RECEIVED "TuyaReceived"

#define D_PRFX_TUYA "Tuya"
Expand Down Expand Up @@ -477,6 +480,7 @@ void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len
}
TuyaSerial->write(checksum);
TuyaSerial->flush();
Tuya.time_last_cmd = millis() | 1; // cheap trick to avoid 0
snprintf_P(log_data, sizeof(log_data), PSTR("%s%02x\""), log_data, checksum);
AddLogData(LOG_LEVEL_DEBUG, log_data);
}
Expand Down Expand Up @@ -1287,6 +1291,7 @@ void TuyaSerialInput(void)
uint8_t dpDataType = 0;
char DataStr[15];
bool isCmdToSuppress = false;
Tuya.time_last_cmd = 0;

if (len > 0) {
ResponseAppend_P(PSTR(",\"CmndData\":\"%s\""), ToHex_P((unsigned char*)&Tuya.buffer[6], len, hex_char, sizeof(hex_char)));
Expand Down Expand Up @@ -1416,7 +1421,9 @@ uint8_t TuyaGetTuyaWifiState(void) {
break;
}

if (MqttIsConnected()) {
// When Wifi is connected, Say "connected to cloud" if mqtt is disabled or mqtt is connected
// avoid MCU to resets ESP to desperately get state 4 while MQTT is not enabled
if ((3 == wifi_state) && (!Settings->flag.mqtt_enabled || MqttIsConnected())) {
wifi_state = 0x04;
}

Expand Down Expand Up @@ -1567,7 +1574,7 @@ void TuyaSensorsShow(bool json)
case 82:
case 83:
case 84:
WSContentSend_PD(PSTR("{s}Timer%d{m}%d{e}"), (sensor-80), Tuya.Sensors[sensor-71]); // No UoM for timers since they can be sec or min
WSContentSend_PD(PSTR("{s}Timer%d{m}%u{e}"), (sensor-80), Tuya.Sensors[sensor-71]); // No UoM for timers since they can be sec or min
break;
}
}
Expand Down Expand Up @@ -1650,6 +1657,12 @@ bool Xdrv16(uint32_t function) {
result = TuyaButtonPressed();
break;
case FUNC_EVERY_SECOND:
if (Tuya.time_last_cmd) {
if ((millis()-Tuya.time_last_cmd) < TUYA_CMD_TIMEOUT)
break; // don't send anything if we are already waiting for an answer
else
Tuya.time_last_cmd = 0;
}
if (TuyaSerial && Tuya.wifi_state != TuyaGetTuyaWifiState()) { TuyaSetWifiLed(); }
if (!Tuya.low_power_mode) {
Tuya.heartbeat_timer++;
Expand Down

0 comments on commit edb2fad

Please sign in to comment.