#include const int LOG_LEVEL = 1; // Just a general setting so we can control Serial output const int led = LED_BUILTIN; // On board LED, Pin 13 const int RECV_PIN = 7; // IR Receiver String input = ""; bool done = false; IRsend sendr; // Make Send Object, DANGER a fixed pin number is always used! (3) //IRrecv recvr(RECV_PIN); // Make Recever Object decode_results results; // Make Results Object to store received codes String result_0 = ""; String result_1 = ""; String result_2 = ""; void setup() { // Initalize on board LED pinMode(led, OUTPUT); digitalWrite(led, LOW); // Initalize input routine input.reserve(60); // Max input of chars, to merge into string done = false; // Initalize IR connections //recvr.enableIRIn(); //recvr.blink13(false); // Disabled Pin 13 blinking on IR signal received // Initalize Output routine Serial.begin(9600); // Serial COM's Port while(!Serial) { delay(10); } // Wait for conection Serial.println("Connected!"); digitalWrite(led, LOW); } void sendCode(long code, int bit_len, bool repeat) { // If repeat the code then repeat it 3 times if(repeat) { for(int x = 0; x <= 3; x++) { sendr.sendNEC(code, bit_len); delay(40); } } else { // Else send the code then repeat a blank 2 times sendr.sendNEC(code, bit_len); delay(40); for(int x = 0; x <= 2; x++) { sendr.sendNEC(0xFFFFFFFF, 32); delay(40); } } } void parse_by_comma(String data) { result_0 = ""; result_1 = ""; result_2 = ""; int at = 0; for(char c : data) { //Serial.println(c); if(at == 0) { if(c != ',') { result_0 += String(c); } else { at += 1; if (LOG_LEVEL >= 2) { Serial.print("> "); Serial.println(result_0); } } } else if(at == 1) { if(c != ',') { result_1 += String(c); } else { at += 1; if (LOG_LEVEL >= 2) { Serial.print("> "); Serial.println(result_1); } } } else if(at == 2) { result_2 += String(c); if (LOG_LEVEL >= 2) { Serial.print("> "); Serial.println(result_2); } break; } } } void parse_order() { long code; int bit_size; bool repeat; if(result_2.toInt() == 1) { repeat = true; } else { repeat = false; } // Converting a string into a long // https://stackoverflow.com/questions/29547115/how-to-convert-string-to-hex-value-in-c/29547549#29547549 code = strtol(result_0.c_str(), NULL, 16); bit_size = result_1.toInt(); if (result_0 != "" and result_1 != "" and result_2 != "") { sendCode(code, bit_size, repeat); if (LOG_LEVEL >= 1) { Serial.println(code); Serial.println(bit_size); Serial.println(repeat); } if (LOG_LEVEL >= 3) { Serial.print(result_0); Serial.print(","); Serial.print(result_1); Serial.print(","); Serial.println(result_2); } Serial.println("Sent!"); } else { Serial.println("Failed to parse give command."); } } void action(String act){ // Given String execute action if(act == "r") { // If it's r, let's repeat the last code we sent out parse_order(); } else { // If it's not any other command then let's assume it's a new code parse_by_comma(act); parse_order(); } /*if(act == "tv p"){ sendCode(0x57E3E817, 32, true); for(int x = 0; x <= 3; x++) { sendr.sendNEC(0x57E3E817, 32); delay(40); } Serial.println("tv power"); } else if(act == "m p"){ sendCode(0x807F02FD, 32, false); Serial.println("movie power"); }*/ } void loop() { // Are we sending/receiving data? if(Serial.available() > 0) { char in = (char)Serial.read(); if (LOG_LEVEL >= 3) { Serial.print(": "); Serial.println(in); } if (in == '\n') { done = true; } else { input += in; } } if(done){ action(input); // Execute command / handle command input = ""; done = false; } }