Merge pull request #4244 from chaosmaster/tuya-autoconfig

tuya: autoconfig and other updates
This commit is contained in:
Theo Arends 2018-11-02 08:04:49 +01:00 committed by GitHub
commit 4e956cb582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 19 deletions

View File

@ -354,7 +354,7 @@
//#define USE_MP3_PLAYER // Use of the DFPlayer Mini MP3 Player RB-DFR-562 commands: play, volume and stop //#define USE_MP3_PLAYER // Use of the DFPlayer Mini MP3 Player RB-DFR-562 commands: play, volume and stop
#define MP3_VOLUME 10 // Set the startup volume on init, the range can be 0..30(max) #define MP3_VOLUME 10 // Set the startup volume on init, the range can be 0..30(max)
#define USE_TUYA_DIMMER // Add support for Tuya Serial Dimmer #define USE_TUYA_DIMMER // Add support for Tuya Serial Dimmer
#define TUYA_DIMMER_ID 3 // Default dimmer Id #define TUYA_DIMMER_ID 0 // Default dimmer Id
// Power monitoring sensors ----------------------- // Power monitoring sensors -----------------------
#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code) #define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code)

2
sonoff/sonoff_post.h Executable file → Normal file
View File

@ -104,7 +104,7 @@ void KNX_CB_Action(message_t const &msg, void *arg);
#define USE_MP3_PLAYER // Use of the DFPlayer Mini MP3 Player RB-DFR-562 commands: play, volume and stop #define USE_MP3_PLAYER // Use of the DFPlayer Mini MP3 Player RB-DFR-562 commands: play, volume and stop
#define MP3_VOLUME 10 // Set the startup volume on init, the range can be 0..30(max) #define MP3_VOLUME 10 // Set the startup volume on init, the range can be 0..30(max)
#define USE_TUYA_DIMMER // Add support for Tuya Serial Dimmer #define USE_TUYA_DIMMER // Add support for Tuya Serial Dimmer
#define TUYA_DIMMER_ID 3 // Default dimmer Id #define TUYA_DIMMER_ID 0 // Default dimmer Id
#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code) #define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code)
#define USE_PZEM_AC // Add support for PZEM014,016 Energy monitor (+1k1 code) #define USE_PZEM_AC // Add support for PZEM014,016 Energy monitor (+1k1 code)
#define USE_PZEM_DC // Add support for PZEM003,017 Energy monitor (+1k1 code) #define USE_PZEM_DC // Add support for PZEM003,017 Energy monitor (+1k1 code)

View File

@ -1154,7 +1154,7 @@ const mytmplt kModules[MAXMODULE] PROGMEM = {
}, },
{ "Tuya Dimmer", // Tuya Dimmer (ESP8266 w/ separate MCU dimmer) { "Tuya Dimmer", // Tuya Dimmer (ESP8266 w/ separate MCU dimmer)
// https://www.amazon.com/gp/product/B07CTNSZZ8/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 // https://www.amazon.com/gp/product/B07CTNSZZ8/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1
GPIO_KEY1, // Virtual Button (controlled by MCU) GPIO_USER, // Virtual Button (controlled by MCU)
GPIO_USER, // GPIO01 MCU serial control GPIO_USER, // GPIO01 MCU serial control
GPIO_USER, GPIO_USER,
GPIO_USER, // GPIO03 MCU serial control GPIO_USER, // GPIO03 MCU serial control
@ -1163,7 +1163,7 @@ const mytmplt kModules[MAXMODULE] PROGMEM = {
0, 0, 0, 0, 0, 0, // Flash connection 0, 0, 0, 0, 0, 0, // Flash connection
GPIO_USER, GPIO_USER,
GPIO_USER, GPIO_USER,
GPIO_LED1, // GPIO14 Green Led GPIO_USER, // GPIO14 Green Led
GPIO_USER, GPIO_USER,
GPIO_USER, GPIO_USER,
0 0

View File

@ -20,7 +20,7 @@
#ifdef USE_TUYA_DIMMER #ifdef USE_TUYA_DIMMER
#ifndef TUYA_DIMMER_ID #ifndef TUYA_DIMMER_ID
#define TUYA_DIMMER_ID 3 #define TUYA_DIMMER_ID 0
#endif #endif
#define TUYA_BUFFER_SIZE 256 #define TUYA_BUFFER_SIZE 256
@ -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_status = 0; // Current status of serial-read
uint8_t tuya_cmd_checksum = 0; // Checksum of tuya command uint8_t tuya_cmd_checksum = 0; // Checksum of tuya command
uint8_t tuya_data_len = 0; // Data lenght of command uint8_t tuya_data_len = 0; // Data lenght of command
int8_t tuya_wifi_state = -2; int8_t tuya_wifi_state = -2; // Keep MCU wifi-status in sync with WifiState()
char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer
int tuya_byte_counter = 0; // Index in serial receive buffer int tuya_byte_counter = 0; // Index in serial receive buffer
@ -113,6 +113,10 @@ void TuyaPacketProcess()
if (tuya_byte_counter == 7 && tuya_buffer[3] == 14 ) { // heartbeat packet if (tuya_byte_counter == 7 && tuya_buffer[3] == 14 ) { // heartbeat packet
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: 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;
}
} }
else if (tuya_byte_counter == 12 && tuya_buffer[3] == 7 && tuya_buffer[5] == 5) { // on/off packet else if (tuya_byte_counter == 12 && tuya_buffer[3] == 7 && tuya_buffer[5] == 5) { // on/off packet
@ -128,6 +132,12 @@ void TuyaPacketProcess()
snprintf_P(log_data, sizeof(log_data), PSTR("TYA: Rcvd Dim State=%d"), tuya_buffer[13]); snprintf_P(log_data, sizeof(log_data), PSTR("TYA: Rcvd Dim State=%d"), tuya_buffer[13]);
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
if (!Settings.param[P_TUYA_DIMMER_ID]) {
snprintf_P(log_data, sizeof(log_data), PSTR("TYA: Autoconfiguring Dimmer ID %d"), tuya_buffer[6]);
AddLog(LOG_LEVEL_DEBUG);
Settings.param[P_TUYA_DIMMER_ID] = tuya_buffer[6];
}
tuya_new_dim = round(tuya_buffer[13] * (100. / 255.)); tuya_new_dim = round(tuya_buffer[13] * (100. / 255.));
if((power) && (tuya_new_dim > 0) && (abs(tuya_new_dim - Settings.light_dimmer) > 2)) { if((power) && (tuya_new_dim > 0) && (abs(tuya_new_dim - Settings.light_dimmer) > 2)) {
@ -143,16 +153,40 @@ void TuyaPacketProcess()
ExecuteCommand(scmnd, SRC_SWITCH); 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 else if (tuya_byte_counter == 8 && tuya_buffer[3] == 5 && tuya_buffer[5] == 1) { // reset WiFi settings packet
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi Reset Rcvd")); AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi Reset Rcvd"));
TuyaResetWifi(); TuyaResetWifi();
} }
else if (tuya_byte_counter == 7 && tuya_buffer[3] == 3 && tuya_buffer[6] == 2) { // WiFi LED has been sucessfully set. 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")); AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: WiFi LED set ACK Rcvd"));
tuya_wifi_state = WifiState(); tuya_wifi_state = WifiState();
} }
else if (tuya_buffer[3] == 2) { // MCU configuration packet
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: MCU configuration Rcvd"));
if (tuya_buffer[5] == 2) {
uint8_t led1_gpio = tuya_buffer[6];
uint8_t key1_gpio = tuya_buffer[7];
boolean key1_set = false;
boolean led1_set = false;
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
if (Settings.my_gp.io[i] == GPIO_LED1) led1_set = true;
else if (Settings.my_gp.io[i] == GPIO_KEY1) key1_set = true;
}
if(!Settings.my_gp.io[led1_gpio] && !led1_set){
Settings.my_gp.io[led1_gpio] = GPIO_LED1;
restart_flag = 2;
}
if(!Settings.my_gp.io[key1_gpio] && !key1_set){
Settings.my_gp.io[key1_gpio] = GPIO_KEY1;
restart_flag = 2;
}
}
TuyaRequestState();
}
} }
void TuyaSerialInput() void TuyaSerialInput()
@ -218,8 +252,11 @@ void TuyaSerialInput()
boolean TuyaModuleSelected() boolean TuyaModuleSelected()
{ {
if (!(pin[GPIO_TUYA_RX] < 99) || !(pin[GPIO_TUYA_TX] < 99)) { // fallback to hardware-serial if not explicitly selected if (!(pin[GPIO_TUYA_RX] < 99) || !(pin[GPIO_TUYA_TX] < 99)) { // fallback to hardware-serial if not explicitly selected
pin[GPIO_TUYA_RX] = 1; pin[GPIO_TUYA_TX] = 1;
pin[GPIO_TUYA_TX] = 3; pin[GPIO_TUYA_RX] = 3;
Settings.my_gp.io[1] = GPIO_TUYA_TX;
Settings.my_gp.io[3] = GPIO_TUYA_RX;
restart_flag = 2;
} }
light_type = LT_SERIAL; light_type = LT_SERIAL;
return true; return true;
@ -254,15 +291,8 @@ void TuyaSetWifiLed(){
TuyaSerial->flush(); TuyaSerial->flush();
} }
void TuyaInit() void TuyaRequestState(){
{ if(TuyaSerial) {
if (!Settings.param[P_TUYA_DIMMER_ID]) {
Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID;
}
TuyaSerial = new TasmotaSerial(pin[GPIO_TUYA_RX], pin[GPIO_TUYA_TX], 1);
if (TuyaSerial->begin(9600)) {
if (TuyaSerial->hardwareSerial()) { ClaimSerial(); }
// Get current status of MCU // Get current status of MCU
snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU state"); snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU state");
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
@ -278,6 +308,29 @@ void TuyaInit()
} }
} }
void TuyaInit()
{
if (!Settings.param[P_TUYA_DIMMER_ID]) {
Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID;
}
TuyaSerial = new TasmotaSerial(pin[GPIO_TUYA_RX], pin[GPIO_TUYA_TX], 1);
if (TuyaSerial->begin(9600)) {
if (TuyaSerial->hardwareSerial()) { ClaimSerial(); }
// Get MCU Configuration
snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU configuration");
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)0x02); // command 02 - get configuration
TuyaSerial->write((uint8_t)0x00);
TuyaSerial->write((uint8_t)0x00); // following data length 0x00
TuyaSerial->write((uint8_t)0x01); // checksum:sum of all bytes in packet mod 256
TuyaSerial->flush();
}
}
void TuyaResetWifi() void TuyaResetWifi()
{ {
if (!Settings.flag.button_restrict) { if (!Settings.flag.button_restrict) {