mirror of https://github.com/arendst/Tasmota.git
TuyaMcu: Implement support for battery powered protocol
This commit is contained in:
parent
6529820e0a
commit
b2ea36a400
|
@ -37,8 +37,14 @@
|
|||
#define TUYA_CMD_STATE 0x07
|
||||
#define TUYA_CMD_QUERY_STATE 0x08
|
||||
|
||||
#define TUYA_LOW_POWER_CMD_WIFI_STATE 0x02
|
||||
#define TUYA_LOW_POWER_CMD_WIFI_RESET 0x03
|
||||
#define TUYA_LOW_POWER_CMD_WIFI_CONFIG 0x04
|
||||
#define TUYA_LOW_POWER_CMD_STATE 0x05
|
||||
|
||||
#define TUYA_TYPE_BOOL 0x01
|
||||
#define TUYA_TYPE_VALUE 0x02
|
||||
#define TUYA_TYPE_ENUM 0x04
|
||||
|
||||
#define TUYA_BUFFER_SIZE 256
|
||||
|
||||
|
@ -52,13 +58,15 @@ struct TUYA {
|
|||
uint8_t cmd_status = 0; // Current status of serial-read
|
||||
uint8_t cmd_checksum = 0; // Checksum of tuya command
|
||||
uint8_t data_len = 0; // Data lenght of command
|
||||
int8_t wifi_state = -2; // Keep MCU wifi-status in sync with WifiState()
|
||||
uint8_t wifi_state = -2; // Keep MCU wifi-status in sync with WifiState()
|
||||
uint8_t heartbeat_timer = 0; // 10 second heartbeat timer for tuya module
|
||||
#ifdef USE_ENERGY_SENSOR
|
||||
uint32_t lastPowerCheckTime = 0; // Time when last power was checked
|
||||
#endif // USE_ENERGY_SENSOR
|
||||
char *buffer = nullptr; // Serial receive buffer
|
||||
int byte_counter = 0; // Index in serial receive buffer
|
||||
bool low_power_mode = false; // Normal or Low power mode protocol
|
||||
bool send_success_next_second = false; // Second command success in low power mode
|
||||
} Tuya;
|
||||
|
||||
|
||||
|
@ -80,6 +88,8 @@ enum TuyaSupportedFunctions {
|
|||
TUYA_MCU_FUNC_POWER = 31,
|
||||
TUYA_MCU_FUNC_CURRENT,
|
||||
TUYA_MCU_FUNC_VOLTAGE,
|
||||
TUYA_MCU_FUNC_BATTERY_STATE,
|
||||
TUYA_MCU_FUNC_BATTERY_PERCENTAGE,
|
||||
TUYA_MCU_FUNC_REL1_INV = 41, // Inverted Relays
|
||||
TUYA_MCU_FUNC_REL2_INV,
|
||||
TUYA_MCU_FUNC_REL3_INV,
|
||||
|
@ -88,6 +98,7 @@ enum TuyaSupportedFunctions {
|
|||
TUYA_MCU_FUNC_REL6_INV,
|
||||
TUYA_MCU_FUNC_REL7_INV,
|
||||
TUYA_MCU_FUNC_REL8_INV,
|
||||
TUYA_MCU_FUNC_LOWPOWER_MODE = 51,
|
||||
TUYA_MCU_FUNC_LAST = 255
|
||||
};
|
||||
|
||||
|
@ -190,7 +201,8 @@ inline bool TuyaFuncIdValid(uint8_t fnId) {
|
|||
(fnId >= TUYA_MCU_FUNC_REL1 && fnId <= TUYA_MCU_FUNC_REL8) ||
|
||||
fnId == TUYA_MCU_FUNC_DIMMER ||
|
||||
(fnId >= TUYA_MCU_FUNC_POWER && fnId <= TUYA_MCU_FUNC_VOLTAGE) ||
|
||||
(fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV);
|
||||
(fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV) ||
|
||||
(fnId == TUYA_MCU_FUNC_LOWPOWER_MODE);
|
||||
}
|
||||
|
||||
uint8_t TuyaGetFuncId(uint8_t dpid) {
|
||||
|
@ -331,23 +343,10 @@ void TuyaResetWifi(void)
|
|||
}
|
||||
}
|
||||
|
||||
void TuyaPacketProcess(void)
|
||||
{
|
||||
void TuyaProcessStatePacket(void) {
|
||||
char scmnd[20];
|
||||
uint8_t fnId = TUYA_MCU_FUNC_NONE;
|
||||
uint8_t fnId = TuyaGetFuncId(Tuya.buffer[6]);
|
||||
|
||||
switch (Tuya.buffer[3]) {
|
||||
|
||||
case TUYA_CMD_HEARTBEAT:
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Heartbeat"));
|
||||
if (Tuya.buffer[6] == 0) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Detected MCU restart"));
|
||||
Tuya.wifi_state = -2;
|
||||
}
|
||||
break;
|
||||
|
||||
case TUYA_CMD_STATE:
|
||||
fnId = TuyaGetFuncId(Tuya.buffer[6]);
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: FnId=%d is set for dpId=%d"), fnId, Tuya.buffer[6]);
|
||||
// if (TuyaFuncIdValid(fnId)) {
|
||||
if (Tuya.buffer[5] == 5) { // on/off packet
|
||||
|
@ -386,7 +385,7 @@ void TuyaPacketProcess(void)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ENERGY_SENSOR
|
||||
#ifdef USE_ENERGY_SENSOR
|
||||
else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_VOLTAGE) {
|
||||
Energy.voltage[0] = (float)packetValue / 10;
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Voltage=%d"), Tuya.buffer[6], packetValue);
|
||||
|
@ -403,12 +402,62 @@ void TuyaPacketProcess(void)
|
|||
}
|
||||
Tuya.lastPowerCheckTime = Rtc.utc_time;
|
||||
}
|
||||
#endif // USE_ENERGY_SENSOR
|
||||
#endif // USE_ENERGY_SENSOR
|
||||
|
||||
}
|
||||
// } else {
|
||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Unknown FnId=%s for dpId=%s"), fnId, Tuya.buffer[6]);
|
||||
// }
|
||||
}
|
||||
|
||||
void TuyaLowPowerModePacketProcess(void) {
|
||||
switch (Tuya.buffer[3]) {
|
||||
case TUYA_CMD_QUERY_PRODUCT:
|
||||
TuyaHandleProductInfoPacket();
|
||||
TuyaSetWifiLed();
|
||||
break;
|
||||
|
||||
case TUYA_LOW_POWER_CMD_STATE:
|
||||
TuyaProcessStatePacket();
|
||||
Tuya.send_success_next_second = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TuyaHandleProductInfoPacket(void) {
|
||||
uint16_t dataLength = Tuya.buffer[4] << 8 | Tuya.buffer[5];
|
||||
char *data = &Tuya.buffer[6];
|
||||
AddLog_P2(LOG_LEVEL_INFO, PSTR("TYA: MCU Product ID: %.*s"), dataLength, data);
|
||||
}
|
||||
|
||||
void TuyaSendLowPowerSuccessIfNeeded(void) {
|
||||
uint8_t success = 1;
|
||||
|
||||
if (Tuya.send_success_next_second) {
|
||||
TuyaSendCmd(TUYA_LOW_POWER_CMD_STATE, &success, 1);
|
||||
Tuya.send_success_next_second = false;
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaNormalPowerModePacketProcess(void)
|
||||
{
|
||||
switch (Tuya.buffer[3]) {
|
||||
case TUYA_CMD_QUERY_PRODUCT:
|
||||
TuyaHandleProductInfoPacket();
|
||||
TuyaSendCmd(TUYA_CMD_MCU_CONF);
|
||||
break;
|
||||
|
||||
case TUYA_CMD_HEARTBEAT:
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Heartbeat"));
|
||||
if (Tuya.buffer[6] == 0) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Detected MCU restart"));
|
||||
Tuya.wifi_state = -2;
|
||||
}
|
||||
break;
|
||||
|
||||
case TUYA_CMD_STATE:
|
||||
TuyaProcessStatePacket();
|
||||
break;
|
||||
|
||||
case TUYA_CMD_WIFI_RESET:
|
||||
|
@ -419,7 +468,7 @@ void TuyaPacketProcess(void)
|
|||
|
||||
case TUYA_CMD_WIFI_STATE:
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: RX WiFi LED set ACK"));
|
||||
Tuya.wifi_state = WifiState();
|
||||
Tuya.wifi_state = TuyaGetTuyaWifiState();
|
||||
break;
|
||||
|
||||
case TUYA_CMD_MCU_CONF:
|
||||
|
@ -491,6 +540,10 @@ bool TuyaModuleSelected(void)
|
|||
light_type = LT_BASIC;
|
||||
}
|
||||
|
||||
if (TuyaGetDpId(TUYA_MCU_FUNC_LOWPOWER_MODE) != 0) {
|
||||
Tuya.low_power_mode = true;
|
||||
}
|
||||
|
||||
UpdateDevices();
|
||||
return true;
|
||||
}
|
||||
|
@ -505,7 +558,7 @@ void TuyaInit(void)
|
|||
// Get MCU Configuration
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Request MCU configuration"));
|
||||
|
||||
TuyaSendCmd(TUYA_CMD_MCU_CONF);
|
||||
TuyaSendCmd(TUYA_CMD_QUERY_PRODUCT);
|
||||
}
|
||||
}
|
||||
Tuya.heartbeat_timer = 0; // init heartbeat timer when dimmer init is done
|
||||
|
@ -546,7 +599,12 @@ void TuyaSerialInput(void)
|
|||
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_TUYA_MCU_RECEIVED));
|
||||
XdrvRulesProcess();
|
||||
|
||||
TuyaPacketProcess();
|
||||
if (!Tuya.low_power_mode) {
|
||||
TuyaNormalPowerModePacketProcess();
|
||||
} else {
|
||||
TuyaLowPowerModePacketProcess();
|
||||
}
|
||||
|
||||
Tuya.byte_counter = 0;
|
||||
Tuya.cmd_status = 0;
|
||||
Tuya.cmd_checksum = 0;
|
||||
|
@ -574,8 +632,8 @@ bool TuyaButtonPressed(void)
|
|||
return false; // Don't serve other buttons
|
||||
}
|
||||
|
||||
void TuyaSetWifiLed(void)
|
||||
{
|
||||
uint8_t TuyaGetTuyaWifiState(void) {
|
||||
|
||||
uint8_t wifi_state = 0x02;
|
||||
switch(WifiState()){
|
||||
case WIFI_MANAGER:
|
||||
|
@ -586,9 +644,23 @@ void TuyaSetWifiLed(void)
|
|||
break;
|
||||
}
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Set WiFi LED %d (%d)"), wifi_state, WifiState());
|
||||
if (MqttIsConnected()) {
|
||||
wifi_state = 0x04;
|
||||
}
|
||||
|
||||
TuyaSendCmd(TUYA_CMD_WIFI_STATE, &wifi_state, 1);
|
||||
return wifi_state;
|
||||
}
|
||||
|
||||
void TuyaSetWifiLed(void)
|
||||
{
|
||||
Tuya.wifi_state = TuyaGetTuyaWifiState();
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Set WiFi LED %d (%d)"), Tuya.wifi_state, WifiState());
|
||||
|
||||
if (Tuya.low_power_mode) {
|
||||
TuyaSendCmd(TUYA_LOW_POWER_CMD_WIFI_STATE, &Tuya.wifi_state, 1);
|
||||
} else {
|
||||
TuyaSendCmd(TUYA_CMD_WIFI_STATE, &Tuya.wifi_state, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ENERGY_SENSOR
|
||||
|
@ -643,12 +715,16 @@ bool Xdrv16(uint8_t function)
|
|||
result = TuyaButtonPressed();
|
||||
break;
|
||||
case FUNC_EVERY_SECOND:
|
||||
if (TuyaSerial && Tuya.wifi_state != WifiState()) { TuyaSetWifiLed(); }
|
||||
if (TuyaSerial && Tuya.wifi_state != TuyaGetTuyaWifiState()) { TuyaSetWifiLed(); }
|
||||
if (!Tuya.low_power_mode) {
|
||||
Tuya.heartbeat_timer++;
|
||||
if (Tuya.heartbeat_timer > 10) {
|
||||
Tuya.heartbeat_timer = 0;
|
||||
TuyaSendCmd(TUYA_CMD_HEARTBEAT);
|
||||
}
|
||||
} else {
|
||||
TuyaSendLowPowerSuccessIfNeeded();
|
||||
}
|
||||
break;
|
||||
case FUNC_SET_CHANNELS:
|
||||
result = TuyaSetChannels();
|
||||
|
|
Loading…
Reference in New Issue