TuyaMcu: Implement support for battery powered protocol

This commit is contained in:
Shantur Rathore 2019-10-14 14:20:47 +01:00 committed by Shantur Rathore
parent 6529820e0a
commit b2ea36a400
1 changed files with 155 additions and 79 deletions

View File

@ -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,12 +343,110 @@ 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]);
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
if (fnId >= TUYA_MCU_FUNC_REL1 && fnId <= TUYA_MCU_FUNC_REL8) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Relay-%d --> MCU State: %s Current State:%s"), fnId - TUYA_MCU_FUNC_REL1 + 1, Tuya.buffer[10]?"On":"Off",bitRead(power, fnId - TUYA_MCU_FUNC_REL1)?"On":"Off");
if ((power || Settings.light_dimmer > 0) && (Tuya.buffer[10] != bitRead(power, fnId - TUYA_MCU_FUNC_REL1))) {
ExecuteCommandPower(fnId - TUYA_MCU_FUNC_REL1 + 1, Tuya.buffer[10], SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
}
} else if (fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Relay-%d-Inverted --> MCU State: %s Current State:%s"), fnId - TUYA_MCU_FUNC_REL1_INV + 1, Tuya.buffer[10]?"Off":"On",bitRead(power, fnId - TUYA_MCU_FUNC_REL1_INV) ^ 1?"Off":"On");
if (Tuya.buffer[10] != bitRead(power, fnId - TUYA_MCU_FUNC_REL1_INV) ^ 1) {
ExecuteCommandPower(fnId - TUYA_MCU_FUNC_REL1_INV + 1, Tuya.buffer[10] ^ 1, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
}
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"),fnId - TUYA_MCU_FUNC_SWT1 + 1,Tuya.buffer[10], SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1));
if (SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1) != Tuya.buffer[10]) {
SwitchSetVirtual(fnId - TUYA_MCU_FUNC_SWT1, Tuya.buffer[10]);
SwitchHandler(1);
}
}
}
else if (Tuya.buffer[5] == 8) { // Long value packet
bool tuya_energy_enabled = (XNRG_16 == energy_flg);
uint16_t packetValue = Tuya.buffer[12] << 8 | Tuya.buffer[13];
if (fnId == TUYA_MCU_FUNC_DIMMER) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), packetValue);
Tuya.new_dim = changeUIntScale(packetValue, 0, Settings.dimmer_hw_max, 0, 100);
if ((power || Settings.flag3.tuya_apply_o20) && (Tuya.new_dim > 0) && (abs(Tuya.new_dim - Settings.light_dimmer) > 1)) {
Tuya.ignore_dim = true;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), Tuya.new_dim );
ExecuteCommand(scmnd, SRC_SWITCH);
}
}
#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);
} else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_CURRENT) {
Energy.current[0] = (float)packetValue / 1000;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[6], packetValue);
} else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_POWER) {
Energy.active_power[0] = (float)packetValue / 10;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[6], packetValue);
if (Tuya.lastPowerCheckTime != 0 && Energy.active_power[0] > 0) {
Energy.kWhtoday += (float)Energy.active_power[0] * (Rtc.utc_time - Tuya.lastPowerCheckTime) / 36;
EnergyUpdateToday();
}
Tuya.lastPowerCheckTime = Rtc.utc_time;
}
#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"));
@ -347,68 +457,7 @@ void TuyaPacketProcess(void)
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
if (fnId >= TUYA_MCU_FUNC_REL1 && fnId <= TUYA_MCU_FUNC_REL8) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Relay-%d --> MCU State: %s Current State:%s"), fnId - TUYA_MCU_FUNC_REL1 + 1, Tuya.buffer[10]?"On":"Off",bitRead(power, fnId - TUYA_MCU_FUNC_REL1)?"On":"Off");
if ((power || Settings.light_dimmer > 0) && (Tuya.buffer[10] != bitRead(power, fnId - TUYA_MCU_FUNC_REL1))) {
ExecuteCommandPower(fnId - TUYA_MCU_FUNC_REL1 + 1, Tuya.buffer[10], SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
}
} else if (fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Relay-%d-Inverted --> MCU State: %s Current State:%s"), fnId - TUYA_MCU_FUNC_REL1_INV + 1, Tuya.buffer[10]?"Off":"On",bitRead(power, fnId - TUYA_MCU_FUNC_REL1_INV) ^ 1?"Off":"On");
if (Tuya.buffer[10] != bitRead(power, fnId - TUYA_MCU_FUNC_REL1_INV) ^ 1) {
ExecuteCommandPower(fnId - TUYA_MCU_FUNC_REL1_INV + 1, Tuya.buffer[10] ^ 1, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
}
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"),fnId - TUYA_MCU_FUNC_SWT1 + 1,Tuya.buffer[10], SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1));
if (SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1) != Tuya.buffer[10]) {
SwitchSetVirtual(fnId - TUYA_MCU_FUNC_SWT1, Tuya.buffer[10]);
SwitchHandler(1);
}
}
}
else if (Tuya.buffer[5] == 8) { // Long value packet
bool tuya_energy_enabled = (XNRG_16 == energy_flg);
uint16_t packetValue = Tuya.buffer[12] << 8 | Tuya.buffer[13];
if (fnId == TUYA_MCU_FUNC_DIMMER) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), packetValue);
Tuya.new_dim = changeUIntScale(packetValue, 0, Settings.dimmer_hw_max, 0, 100);
if ((power || Settings.flag3.tuya_apply_o20) && (Tuya.new_dim > 0) && (abs(Tuya.new_dim - Settings.light_dimmer) > 1)) {
Tuya.ignore_dim = true;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), Tuya.new_dim );
ExecuteCommand(scmnd, SRC_SWITCH);
}
}
#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);
} else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_CURRENT) {
Energy.current[0] = (float)packetValue / 1000;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[6], packetValue);
} else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_POWER) {
Energy.active_power[0] = (float)packetValue / 10;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[6], packetValue);
if (Tuya.lastPowerCheckTime != 0 && Energy.active_power[0] > 0) {
Energy.kWhtoday += (float)Energy.active_power[0] * (Rtc.utc_time - Tuya.lastPowerCheckTime) / 36;
EnergyUpdateToday();
}
Tuya.lastPowerCheckTime = Rtc.utc_time;
}
#endif // USE_ENERGY_SENSOR
}
// } else {
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Unknown FnId=%s for dpId=%s"), fnId, Tuya.buffer[6]);
// }
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,11 +715,15 @@ bool Xdrv16(uint8_t function)
result = TuyaButtonPressed();
break;
case FUNC_EVERY_SECOND:
if (TuyaSerial && Tuya.wifi_state != WifiState()) { TuyaSetWifiLed(); }
Tuya.heartbeat_timer++;
if (Tuya.heartbeat_timer > 10) {
Tuya.heartbeat_timer = 0;
TuyaSendCmd(TUYA_CMD_HEARTBEAT);
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: