Merge pull request #4178 from chaosmaster/tuya-sofware-serial

Tuya: Add support for software-serial
This commit is contained in:
Theo Arends 2018-10-27 08:33:22 +02:00 committed by GitHub
commit a17bfabf59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 107 additions and 48 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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 "А"

View File

@ -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"

View File

@ -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 "А"

View File

@ -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 "安"

View File

@ -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 "安"

View File

@ -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,9 +1155,9 @@ 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_USER, // GPIO01 MCU serial control
GPIO_USER,
GPIO_RXD, // GPIO03 MCU serial control
GPIO_USER, // GPIO03 MCU serial control
GPIO_USER,
GPIO_USER,
0, 0, 0, 0, 0, 0, // Flash connection

View File

@ -23,6 +23,10 @@
#define TUYA_DIMMER_ID 3
#endif
#include <TasmotaSerial.h>
TasmotaSerial *TuyaSerial = nullptr;
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
@ -36,24 +40,24 @@ boolean TuyaSetPower()
uint8_t rpower = XdrvMailbox.index;
int16_t source = XdrvMailbox.payload;
if (source != SRC_SWITCH ) { // ignore to prevent loop from pushing state from faceplate interaction
if (source != SRC_SWITCH && TuyaSerial) { // ignore to prevent loop from pushing state from faceplate interaction
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;
}
@ -62,26 +66,26 @@ boolean TuyaSetPower()
void LightSerialDuty(uint8_t duty)
{
if (duty > 0 && !tuya_ignore_dim ) {
if (duty > 0 && !tuya_ignore_dim && TuyaSerial) {
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 && 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,32 @@ 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)) { // fallback to hardware-serial if not explicitly selected
pin[GPIO_TUYA_RX] = 1;
pin[GPIO_TUYA_TX] = 3;
}
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();
if(TuyaSerial){
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()