Skip to content

Commit

Permalink
added rf serial command via rc-switch (pr3y#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Jul 16, 2024
1 parent a0136d7 commit b1f1c42
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ lib_deps =
NTPClient
Timezone
ESP32Time
rc-switch


[env:m5stack-cplus2]
Expand Down
69 changes: 61 additions & 8 deletions src/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "TV-B-Gone.h"
#include "cJSON.h"
#include <inttypes.h> // for PRIu64
#include <RCSwitch.h>



void SerialPrintHexString(uint64_t val) {
Expand Down Expand Up @@ -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);
Expand All @@ -62,13 +64,6 @@ void handleSerialCommands() {
// <address> and <command> 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);
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b1f1c42

Please sign in to comment.