tuya send 6 command to send raw data to the tuya mcu

This commit is contained in:
Benny Nestler 2022-03-30 00:08:27 +02:00
parent 685642eff4
commit d41a143446
1 changed files with 50 additions and 8 deletions

View File

@ -43,6 +43,7 @@
#define TUYA_LOW_POWER_CMD_WIFI_CONFIG 0x04
#define TUYA_LOW_POWER_CMD_STATE 0x05
#define TUYA_TYPE_RAW 0x00
#define TUYA_TYPE_BOOL 0x01
#define TUYA_TYPE_VALUE 0x02
#define TUYA_TYPE_STRING 0x03
@ -147,6 +148,9 @@ TuyaSend2 11,100 -> Sends integer (Type 2) data 100 to dpId 11 (Max data length
TuyaSend2 11,0xAABBCCDD -> Sends 4 bytes (Type 2) data to dpId 11 (Max data length 4 bytes)
TuyaSend3 11,ThisIsTheData -> Sends the supplied string (Type 3) to dpId 11 ( Max data length not-known)
TuyaSend4 11,1 -> Sends enum (Type 4) data 1 to dpId 11 (Max data length 1 bytes)
TuyaSend5 11,ABCD -> Sends an HEX string (Type 3) data to dpId
TuyaSend6 11,ABCD -> Sends raw (Type 0) data to dpId
*/
void CmndTuyaSend(void) {
@ -186,6 +190,8 @@ void CmndTuyaSend(void) {
TuyaSendHexString(dpId, data);
} else if (4 == XdrvMailbox.index) {
TuyaSendEnum(dpId, strtoul(data, nullptr, 0));
} else if (6 == XdrvMailbox.index) {
TuyaSendRaw(dpId, data);
}
}
}
@ -501,6 +507,24 @@ void TuyaSendEnum(uint8_t id, uint32_t value)
TuyaSendState(id, TUYA_TYPE_ENUM, (uint8_t*)(&value));
}
static uint16_t convertHexStringtoBytes (uint8_t * dest, char src[], uint16_t src_len){
if (NULL == dest || NULL == src || 0 == src_len){
return 0;
}
char hexbyte[3];
hexbyte[2] = 0;
uint16_t i;
for (i = 0; i < src_len; i++) {
hexbyte[0] = src[2*i];
hexbyte[1] = src[2*i+1];
dest[i] = strtol(hexbyte, NULL, 16);
}
return i;
}
void TuyaSendHexString(uint8_t id, char data[]) {
uint16_t len = strlen(data)/2;
@ -511,14 +535,7 @@ void TuyaSendHexString(uint8_t id, char data[]) {
payload_buffer[2] = len >> 8;
payload_buffer[3] = len & 0xFF;
char hexbyte[3];
hexbyte[2] = 0;
for (uint16_t i = 0; i < len; i++) {
hexbyte[0] = data[2*i];
hexbyte[1] = data[2*i+1];
payload_buffer[4+i] = strtol(hexbyte,NULL,16);
}
(void) convertHexStringtoBytes(&payload_buffer[4], data, len);
TuyaSendCmd(TUYA_CMD_SET_DP, payload_buffer, payload_len);
}
@ -540,6 +557,31 @@ void TuyaSendString(uint8_t id, char data[]) {
TuyaSendCmd(TUYA_CMD_SET_DP, payload_buffer, payload_len);
}
void TuyaSendRaw(uint8_t id, char data[]) {
AddLog(LOG_LEVEL_ERROR, PSTR("TYA: Send Raw-Data from string: %s"), data);
char* beginPos = strchr(data, 'x');
if(!beginPos) {
beginPos = strchr(data, 'X');
}
if(!beginPos) {
beginPos = data;
} else {
beginPos += 1;
}
uint16_t strSize = strlen(beginPos);
uint16_t len = strSize/2;
uint16_t payload_len = 4 + len;
uint8_t payload_buffer[payload_len];
payload_buffer[0] = id;
payload_buffer[1] = TUYA_TYPE_RAW;
payload_buffer[2] = len >> 8;
payload_buffer[3] = len & 0xFF;
(void) convertHexStringtoBytes(&payload_buffer[4], beginPos, len);
TuyaSendCmd(TUYA_CMD_SET_DP, payload_buffer, payload_len);
}
bool TuyaSetPower(void)
{
bool status = false;