From 22c20ab5feb5f3211c0182775cb29d331cdf17da Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Sat, 27 Oct 2018 03:01:09 +0200 Subject: [PATCH] tuya: software-serial --- sonoff/language/bg-BG.h | 2 + sonoff/language/cs-CZ.h | 2 + sonoff/language/de-DE.h | 2 + sonoff/language/el-GR.h | 2 + sonoff/language/en-GB.h | 2 + sonoff/language/es-AR.h | 2 + sonoff/language/fr-FR.h | 2 + sonoff/language/he-HE.h | 2 + sonoff/language/hu-HU.h | 2 + sonoff/language/it-IT.h | 2 + sonoff/language/nl-NL.h | 2 + sonoff/language/pl-PL.h | 2 + sonoff/language/pt-BR.h | 2 + sonoff/language/pt-PT.h | 2 + sonoff/language/ru-RU.h | 2 + sonoff/language/tr-TR.h | 2 + sonoff/language/uk-UK.h | 2 + sonoff/language/zh-CN.h | 2 + sonoff/language/zh-TW.h | 2 + sonoff/sonoff_template.h | 19 ++++--- sonoff/xdrv_16_tuyadimmer.ino | 96 ++++++++++++++++++++--------------- 21 files changed, 106 insertions(+), 47 deletions(-) diff --git a/sonoff/language/bg-BG.h b/sonoff/language/bg-BG.h index 7f56e33d3..7e096f673 100644 --- a/sonoff/language/bg-BG.h +++ b/sonoff/language/bg-BG.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/cs-CZ.h b/sonoff/language/cs-CZ.h index a29f5741a..0cf34043b 100644 --- a/sonoff/language/cs-CZ.h +++ b/sonoff/language/cs-CZ.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/de-DE.h b/sonoff/language/de-DE.h index 9f11e57ca..6a783a45b 100644 --- a/sonoff/language/de-DE.h +++ b/sonoff/language/de-DE.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/el-GR.h b/sonoff/language/el-GR.h index beaaef57d..937464699 100644 --- a/sonoff/language/el-GR.h +++ b/sonoff/language/el-GR.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/en-GB.h b/sonoff/language/en-GB.h index 4e3aa0080..925080d3d 100644 --- a/sonoff/language/en-GB.h +++ b/sonoff/language/en-GB.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/es-AR.h b/sonoff/language/es-AR.h index 39f198681..d78c807da 100644 --- a/sonoff/language/es-AR.h +++ b/sonoff/language/es-AR.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/fr-FR.h b/sonoff/language/fr-FR.h index 44ecea2b1..bb32c2d2e 100644 --- a/sonoff/language/fr-FR.h +++ b/sonoff/language/fr-FR.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/he-HE.h b/sonoff/language/he-HE.h index 73b44c67b..d1aee3423 100644 --- a/sonoff/language/he-HE.h +++ b/sonoff/language/he-HE.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/hu-HU.h b/sonoff/language/hu-HU.h index 532d45643..91b005e39 100644 --- a/sonoff/language/hu-HU.h +++ b/sonoff/language/hu-HU.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/it-IT.h b/sonoff/language/it-IT.h index 61f361b89..2ed03ca35 100644 --- a/sonoff/language/it-IT.h +++ b/sonoff/language/it-IT.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/nl-NL.h b/sonoff/language/nl-NL.h index d2e913357..ca7be61af 100644 --- a/sonoff/language/nl-NL.h +++ b/sonoff/language/nl-NL.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pl-PL.h b/sonoff/language/pl-PL.h index 0f1303b30..3fd26f29b 100644 --- a/sonoff/language/pl-PL.h +++ b/sonoff/language/pl-PL.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-BR.h b/sonoff/language/pt-BR.h index 54d8448d7..dceadf085 100644 --- a/sonoff/language/pt-BR.h +++ b/sonoff/language/pt-BR.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-PT.h b/sonoff/language/pt-PT.h index 0c8e03d62..a7b9236ae 100644 --- a/sonoff/language/pt-PT.h +++ b/sonoff/language/pt-PT.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/ru-RU.h b/sonoff/language/ru-RU.h index 11fdf90b0..fc2dc2892 100644 --- a/sonoff/language/ru-RU.h +++ b/sonoff/language/ru-RU.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/tr-TR.h b/sonoff/language/tr-TR.h index 86838d0df..ea3f10ce6 100755 --- a/sonoff/language/tr-TR.h +++ b/sonoff/language/tr-TR.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/uk-UK.h b/sonoff/language/uk-UK.h index efc125351..cf9d8bcf3 100644 --- a/sonoff/language/uk-UK.h +++ b/sonoff/language/uk-UK.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/zh-CN.h b/sonoff/language/zh-CN.h index 3414bc8d1..15708dded 100644 --- a/sonoff/language/zh-CN.h +++ b/sonoff/language/zh-CN.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/language/zh-TW.h b/sonoff/language/zh-TW.h index b5f14dace..75c682396 100644 --- a/sonoff/language/zh-TW.h +++ b/sonoff/language/zh-TW.h @@ -527,6 +527,8 @@ #define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFRECV "RFrecv" +#define D_SENSOR_TUYA_TX "Tuya Tx" +#define D_SENSOR_TUYA_RX "Tuya Rx" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 13a6ffd0c..21227d379 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -128,9 +128,11 @@ enum UserSelectablePins { GPIO_SDS0X1_TX, // Nova Fitness SDS011 Serial interface GPIO_HX711_SCK, // HX711 Load Cell clock GPIO_HX711_DAT, // HX711 Load Cell data - GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin + GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin GPIO_RFSEND, // RF transmitter GPIO_RFRECV, // RF receiver + GPIO_TUYA_TX, // Tuya Serial interface + GPIO_TUYA_RX, // Tuya Serial interface GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -187,7 +189,8 @@ const char kSensorNames[] PROGMEM = D_SENSOR_DFR562 "|" D_SENSOR_SDS0X1_TX "|" D_SENSOR_HX711_SCK "|" D_SENSOR_HX711_DAT "|" D_SENSOR_TX20_TX "|" - D_SENSOR_RFSEND "|" D_SENSOR_RFRECV; + D_SENSOR_RFSEND "|" D_SENSOR_RFRECV "|" + D_SENSOR_TUYA_TX "|" D_SENSOR_TUYA_RX; /********************************************************************************************/ @@ -371,7 +374,9 @@ const uint8_t kGpioNiceList[GPIO_SENSOR_END] PROGMEM = { GPIO_SDM630_RX, // SDM630 Serial interface GPIO_PMS5003, // Plantower PMS5003 Serial interface GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin - GPIO_MP3_DFR562 // RB-DFR-562, DFPlayer Mini MP3 Player Serial interface + GPIO_MP3_DFR562, // RB-DFR-562, DFPlayer Mini MP3 Player Serial interface + GPIO_TUYA_TX, // Tuya Serial interface + GPIO_TUYA_RX // Tuya Serial interface }; const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { @@ -1150,12 +1155,14 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { { "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 GPIO_KEY1, // Virtual Button (controlled by MCU) - GPIO_TXD, // GPIO01 MCU serial control + GPIO_TXD, // GPIO01 MCU serial control GPIO_USER, - GPIO_RXD, // GPIO03 MCU serial control + GPIO_RXD, // GPIO03 MCU serial control GPIO_USER, GPIO_USER, - 0, 0, 0, 0, 0, 0, // Flash connection + 0, 0, + 0, + 0, 0, 0, // Flash connection GPIO_USER, GPIO_USER, GPIO_LED1, // GPIO14 Green Led diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 1a5776d4a..6ffa789b2 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -23,6 +23,10 @@ #define TUYA_DIMMER_ID 3 #endif +#include + +TasmotaSerial *TuyaSerial; + uint8_t tuya_new_dim = 0; // Tuya dimmer value temp boolean tuya_ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction uint8_t tuya_cmd_status = 0; // Current status of serial-read @@ -41,19 +45,19 @@ boolean TuyaSetPower() snprintf_P(log_data, sizeof(log_data), PSTR("TYA: SetDevicePower.rpower=%d"), rpower); AddLog(LOG_LEVEL_DEBUG); - Serial.write(0x55); // Tuya header 55AA - Serial.write(0xAA); - Serial.write(0x00); // version 00 - Serial.write(0x06); // Tuya command 06 - Serial.write(0x00); - Serial.write(0x05); // following data length 0x05 - Serial.write(0x01); // relay number 1,2,3 - Serial.write(0x01); - Serial.write(0x00); - Serial.write(0x01); - Serial.write(rpower); // status - Serial.write(0x0D + rpower); // checksum sum of all bytes in packet mod 256 - Serial.flush(); + TuyaSerial->write((uint8_t)0x55); // Tuya header 55AA + TuyaSerial->write((uint8_t)0xAA); + TuyaSerial->write((uint8_t)0x00); // version 00 + TuyaSerial->write((uint8_t)0x06); // Tuya command 06 + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x05); // following data length 0x05 + TuyaSerial->write((uint8_t)0x01); // relay number 1,2,3 + TuyaSerial->write((uint8_t)0x01); + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x01); + TuyaSerial->write((uint8_t)rpower); // status + TuyaSerial->write((uint8_t)0x0D + rpower); // checksum sum of all bytes in packet mod 256 + TuyaSerial->flush(); status = true; } @@ -66,22 +70,22 @@ void LightSerialDuty(uint8_t duty) if (duty < 25) { duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself } - Serial.write(0x55); // Tuya header 55AA - Serial.write(0xAA); - Serial.write(0x00); // version 00 - Serial.write(0x06); // Tuya command 06 - send order - Serial.write(0x00); - Serial.write(0x08); // following data length 0x08 - Serial.write(Settings.param[P_TUYA_DIMMER_ID]); // dimmer id - Serial.write(0x02); // type=value - Serial.write(0x00); // length hi - Serial.write(0x04); // length low - Serial.write(0x00); // - Serial.write(0x00); // - Serial.write(0x00); // - Serial.write( duty ); // dim value (0-255) - Serial.write( byte(Settings.param[P_TUYA_DIMMER_ID] + 19 + duty) ); // checksum:sum of all bytes in packet mod 256 - Serial.flush(); + TuyaSerial->write((uint8_t)0x55); // Tuya header 55AA + TuyaSerial->write((uint8_t)0xAA); + TuyaSerial->write((uint8_t)0x00); // version 00 + TuyaSerial->write((uint8_t)0x06); // Tuya command 06 - send order + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x08); // following data length 0x08 + TuyaSerial->write((uint8_t)Settings.param[P_TUYA_DIMMER_ID]); // dimmer id + TuyaSerial->write((uint8_t)0x02); // type=value + TuyaSerial->write((uint8_t)0x00); // length hi + TuyaSerial->write((uint8_t)0x04); // length low + TuyaSerial->write((uint8_t)0x00); // + TuyaSerial->write((uint8_t)0x00); // + TuyaSerial->write((uint8_t)0x00); // + TuyaSerial->write((uint8_t) duty ); // dim value (0-255) + TuyaSerial->write((uint8_t) byte(Settings.param[P_TUYA_DIMMER_ID] + 19 + duty) ); // checksum:sum of all bytes in packet mod 256 + TuyaSerial->flush(); snprintf_P(log_data, sizeof(log_data), PSTR( "TYA: Send Serial Packet Dim Value=%d (id=%d)"), duty, Settings.param[P_TUYA_DIMMER_ID]); AddLog(LOG_LEVEL_DEBUG); @@ -145,9 +149,9 @@ void TuyaPacketProcess() void TuyaSerialInput() { - while (Serial.available()) { + while (TuyaSerial->available()) { yield(); - serial_in_byte = Serial.read(); + serial_in_byte = TuyaSerial->read(); //snprintf_P(log_data, sizeof(log_data), PSTR("TYA: serial_in_byte %d, tuya_cmd_status %d, tuya_cmd_checksum %d, tuya_data_len %d, serial_in_byte_counter %d"), serial_in_byte, tuya_cmd_status, tuya_cmd_checksum, tuya_data_len, serial_in_byte_counter); //AddLog(LOG_LEVEL_DEBUG); @@ -215,20 +219,30 @@ void TuyaInit() if (!Settings.param[P_TUYA_DIMMER_ID]) { Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID; } - Serial.setDebugOutput(false); - ClaimSerial(); + if (!(pin[GPIO_TUYA_RX] < 99) || !(pin[GPIO_TUYA_TX] < 99)) { + pin[GPIO_TUYA_RX] = pin[GPIO_RXD]; + pin[GPIO_TUYA_TX] = pin[GPIO_TXD]; + } + TuyaSerial = new TasmotaSerial(pin[GPIO_TUYA_RX], pin[GPIO_TUYA_TX], 1); + if (TuyaSerial->begin(baudrate)) { + if (TuyaSerial->hardwareSerial()) { + ClaimSerial(); + Serial.setDebugOutput(false); + } + } // Get current status of MCU snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU state"); AddLog(LOG_LEVEL_DEBUG); - Serial.write(0x55); // header 55AA - Serial.write(0xAA); - Serial.write(0x00); // version 00 - Serial.write(0x08); // command 08 - get status - Serial.write(0x00); - Serial.write(0x00); // following data length 0x00 - Serial.write(0x07); // checksum:sum of all bytes in packet mod 256 - Serial.flush(); + + TuyaSerial->write((uint8_t)0x55); // header 55AA + TuyaSerial->write((uint8_t)0xAA); + TuyaSerial->write((uint8_t)0x00); // version 00 + TuyaSerial->write((uint8_t)0x08); // command 08 - get status + TuyaSerial->write((uint8_t)0x00); + TuyaSerial->write((uint8_t)0x00); // following data length 0x00 + TuyaSerial->write((uint8_t)0x07); // checksum:sum of all bytes in packet mod 256 + TuyaSerial->flush(); } boolean TuyaButtonPressed()