tuya: software-serial

This commit is contained in:
Joel Stein 2018-10-27 03:01:09 +02:00
parent 77d54be267
commit 22c20ab5fe
21 changed files with 106 additions and 47 deletions

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "А" #define D_UNIT_AMPERE "А"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "A" #define D_UNIT_AMPERE "A"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "А" #define D_UNIT_AMPERE "А"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "安" #define D_UNIT_AMPERE "安"

View File

@ -527,6 +527,8 @@
#define D_SENSOR_TX20_TX "TX20" #define D_SENSOR_TX20_TX "TX20"
#define D_SENSOR_RFSEND "RFSend" #define D_SENSOR_RFSEND "RFSend"
#define D_SENSOR_RFRECV "RFrecv" #define D_SENSOR_RFRECV "RFrecv"
#define D_SENSOR_TUYA_TX "Tuya Tx"
#define D_SENSOR_TUYA_RX "Tuya Rx"
// Units // Units
#define D_UNIT_AMPERE "安" #define D_UNIT_AMPERE "安"

View File

@ -131,6 +131,8 @@ enum UserSelectablePins {
GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin
GPIO_RFSEND, // RF transmitter GPIO_RFSEND, // RF transmitter
GPIO_RFRECV, // RF receiver GPIO_RFRECV, // RF receiver
GPIO_TUYA_TX, // Tuya Serial interface
GPIO_TUYA_RX, // Tuya Serial interface
GPIO_SENSOR_END }; GPIO_SENSOR_END };
// Programmer selectable GPIO functionality offset by user selectable GPIOs // 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_DFR562 "|" D_SENSOR_SDS0X1_TX "|"
D_SENSOR_HX711_SCK "|" D_SENSOR_HX711_DAT "|" D_SENSOR_HX711_SCK "|" D_SENSOR_HX711_DAT "|"
D_SENSOR_TX20_TX "|" 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_SDM630_RX, // SDM630 Serial interface
GPIO_PMS5003, // Plantower PMS5003 Serial interface GPIO_PMS5003, // Plantower PMS5003 Serial interface
GPIO_TX20_TXD_BLACK, // TX20 Transmission Pin 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 = { const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = {
@ -1155,7 +1160,9 @@ const mytmplt kModules[MAXMODULE] PROGMEM = {
GPIO_RXD, // GPIO03 MCU serial control GPIO_RXD, // GPIO03 MCU serial control
GPIO_USER, GPIO_USER,
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_USER, GPIO_USER,
GPIO_LED1, // GPIO14 Green Led GPIO_LED1, // GPIO14 Green Led

View File

@ -23,6 +23,10 @@
#define TUYA_DIMMER_ID 3 #define TUYA_DIMMER_ID 3
#endif #endif
#include <TasmotaSerial.h>
TasmotaSerial *TuyaSerial;
uint8_t tuya_new_dim = 0; // Tuya dimmer value temp 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 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 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); snprintf_P(log_data, sizeof(log_data), PSTR("TYA: SetDevicePower.rpower=%d"), rpower);
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
Serial.write(0x55); // Tuya header 55AA TuyaSerial->write((uint8_t)0x55); // Tuya header 55AA
Serial.write(0xAA); TuyaSerial->write((uint8_t)0xAA);
Serial.write(0x00); // version 00 TuyaSerial->write((uint8_t)0x00); // version 00
Serial.write(0x06); // Tuya command 06 TuyaSerial->write((uint8_t)0x06); // Tuya command 06
Serial.write(0x00); TuyaSerial->write((uint8_t)0x00);
Serial.write(0x05); // following data length 0x05 TuyaSerial->write((uint8_t)0x05); // following data length 0x05
Serial.write(0x01); // relay number 1,2,3 TuyaSerial->write((uint8_t)0x01); // relay number 1,2,3
Serial.write(0x01); TuyaSerial->write((uint8_t)0x01);
Serial.write(0x00); TuyaSerial->write((uint8_t)0x00);
Serial.write(0x01); TuyaSerial->write((uint8_t)0x01);
Serial.write(rpower); // status TuyaSerial->write((uint8_t)rpower); // status
Serial.write(0x0D + rpower); // checksum sum of all bytes in packet mod 256 TuyaSerial->write((uint8_t)0x0D + rpower); // checksum sum of all bytes in packet mod 256
Serial.flush(); TuyaSerial->flush();
status = true; status = true;
} }
@ -66,22 +70,22 @@ void LightSerialDuty(uint8_t duty)
if (duty < 25) { if (duty < 25) {
duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
} }
Serial.write(0x55); // Tuya header 55AA TuyaSerial->write((uint8_t)0x55); // Tuya header 55AA
Serial.write(0xAA); TuyaSerial->write((uint8_t)0xAA);
Serial.write(0x00); // version 00 TuyaSerial->write((uint8_t)0x00); // version 00
Serial.write(0x06); // Tuya command 06 - send order TuyaSerial->write((uint8_t)0x06); // Tuya command 06 - send order
Serial.write(0x00); TuyaSerial->write((uint8_t)0x00);
Serial.write(0x08); // following data length 0x08 TuyaSerial->write((uint8_t)0x08); // following data length 0x08
Serial.write(Settings.param[P_TUYA_DIMMER_ID]); // dimmer id TuyaSerial->write((uint8_t)Settings.param[P_TUYA_DIMMER_ID]); // dimmer id
Serial.write(0x02); // type=value TuyaSerial->write((uint8_t)0x02); // type=value
Serial.write(0x00); // length hi TuyaSerial->write((uint8_t)0x00); // length hi
Serial.write(0x04); // length low TuyaSerial->write((uint8_t)0x04); // length low
Serial.write(0x00); // TuyaSerial->write((uint8_t)0x00); //
Serial.write(0x00); // TuyaSerial->write((uint8_t)0x00); //
Serial.write(0x00); // TuyaSerial->write((uint8_t)0x00); //
Serial.write( duty ); // dim value (0-255) TuyaSerial->write((uint8_t) 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 TuyaSerial->write((uint8_t) byte(Settings.param[P_TUYA_DIMMER_ID] + 19 + duty) ); // checksum:sum of all bytes in packet mod 256
Serial.flush(); 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]); 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); AddLog(LOG_LEVEL_DEBUG);
@ -145,9 +149,9 @@ void TuyaPacketProcess()
void TuyaSerialInput() void TuyaSerialInput()
{ {
while (Serial.available()) { while (TuyaSerial->available()) {
yield(); 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); //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); //AddLog(LOG_LEVEL_DEBUG);
@ -215,20 +219,30 @@ void TuyaInit()
if (!Settings.param[P_TUYA_DIMMER_ID]) { if (!Settings.param[P_TUYA_DIMMER_ID]) {
Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID; Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID;
} }
Serial.setDebugOutput(false); 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(); ClaimSerial();
Serial.setDebugOutput(false);
}
}
// 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);
Serial.write(0x55); // header 55AA
Serial.write(0xAA); TuyaSerial->write((uint8_t)0x55); // header 55AA
Serial.write(0x00); // version 00 TuyaSerial->write((uint8_t)0xAA);
Serial.write(0x08); // command 08 - get status TuyaSerial->write((uint8_t)0x00); // version 00
Serial.write(0x00); TuyaSerial->write((uint8_t)0x08); // command 08 - get status
Serial.write(0x00); // following data length 0x00 TuyaSerial->write((uint8_t)0x00);
Serial.write(0x07); // checksum:sum of all bytes in packet mod 256 TuyaSerial->write((uint8_t)0x00); // following data length 0x00
Serial.flush(); TuyaSerial->write((uint8_t)0x07); // checksum:sum of all bytes in packet mod 256
TuyaSerial->flush();
} }
boolean TuyaButtonPressed() boolean TuyaButtonPressed()