From b1f1c424e1118d1df9a09d5c9ec9e1d5db574409 Mon Sep 17 00:00:00 2001 From: eadmaster <925171+eadmaster@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:45:43 +0200 Subject: [PATCH] added rf serial command via rc-switch (#64) --- platformio.ini | 1 + src/serialcmds.cpp | 69 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index f2efeeb1..4a099f4b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,7 @@ lib_deps = NTPClient Timezone ESP32Time + rc-switch [env:m5stack-cplus2] diff --git a/src/serialcmds.cpp b/src/serialcmds.cpp index 988e32cb..483516e6 100644 --- a/src/serialcmds.cpp +++ b/src/serialcmds.cpp @@ -6,6 +6,8 @@ #include "TV-B-Gone.h" #include "cJSON.h" #include // for PRIu64 +#include + void SerialPrintHexString(uint64_t val) { @@ -49,7 +51,7 @@ void handleSerialCommands() { if(cmd_str.startsWith("ir") ) { - if(IrTx==0) IrTx = 44; // init issue? LED on CARDPUTER + if(IrTx==0) IrTx = LED; // quickfix init issue? CARDPUTER is 44 //IRsend irsend(IrTx); //inverted = false //Serial.println(IrTx); @@ -62,13 +64,6 @@ void handleSerialCommands() { //
and must be in hex format // e.g. ir tx NEC 04000000 08000000 - /* - const int ADD_LEN = 8; - const int CMD_LEN = 8; - void* sendIrCommandFuncPrt = NULL; - */ - - if(cmd_str.startsWith("ir tx nec ")){ String address = cmd_str.substring(10, 10+8); String command = cmd_str.substring(19, 19+8); @@ -138,6 +133,64 @@ void handleSerialCommands() { //backToMenu(); return; } // end of ir commands + + if(cmd_str.startsWith("rf") ) { + if(RfTx==0) RfTx=GROVE_SDA; // quick fix + pinMode(RfTx, OUTPUT); + //Serial.println(RfTx); + + RCSwitch mySwitch = RCSwitch(); + mySwitch.enableTransmit(RfTx); + + if(cmd_str.startsWith("rfsend")) { + // tasmota json command https://tasmota.github.io/docs/Tasmota-IR/#sending-ir-commands + // e.g. RfSend {"Data":"0x447503","Bits":24,"Protocol":1,"Pulse":174,"Repeat":10} // on + // e.g. RfSend {"Data":"0x44750C","Bits":24,"Protocol":1,"Pulse":174,"Repeat":10} // off + + cJSON *root = cJSON_Parse(cmd_str.c_str() + 6); + if (root == NULL) { + Serial.println("This is NOT json format"); + return; + } + unsigned int bits = 32; // defaults to 32 bits + const char *dataStr = ""; + int protocol = 1; // defaults to 1 + int pulse = 0; // 0 leave the library use the default value depending on protocol + int repeat = 10; + + cJSON * protocolItem = cJSON_GetObjectItem(root,"protocol"); + cJSON * dataItem = cJSON_GetObjectItem(root, "data"); + cJSON * bitsItem = cJSON_GetObjectItem(root,"bits"); + cJSON * pulseItem = cJSON_GetObjectItem(root,"pulse"); + cJSON * repeatItem = cJSON_GetObjectItem(root,"repeat"); + + if(protocolItem && cJSON_IsNumber(protocolItem)) protocol = protocolItem->valueint; + if(bitsItem && cJSON_IsNumber(bitsItem)) bits = bitsItem->valueint; + if(pulseItem && cJSON_IsNumber(pulseItem)) pulse = pulseItem->valueint; + if(repeatItem && cJSON_IsNumber(repeatItem)) repeat = repeatItem->valueint; + if(dataItem && cJSON_IsString(dataItem)) { + dataStr = dataItem->valuestring; + } else { + Serial.println("missing or invalid data to send"); + return; + } + //String dataStr = cmd_str.substring(36, 36+8); + uint64_t data = strtoul(dataStr, nullptr, 16); + //Serial.println(dataStr); + //SerialPrintHexString(data); + //Serial.println(bits); + + mySwitch.setProtocol(protocol); + if (pulse) { mySwitch.setPulseLength(pulse); } + mySwitch.setPulseLength(pulse); + mySwitch.setRepeatTransmit(repeat); + + mySwitch.send(data, bits); + + cJSON_Delete(root); + return; + } + } Serial.println("unsupported serial command" + cmd_str);