IR Record and Playback module


  • Contest Winner

    On special request i've created a IR (InfraRed remote) record and playback sensor.
    This is based on the example IR sensor but now one can record a IR - signal from an remote and let your controller send the IR code on request. http://www.mysensors.org/build/ir

    Up to 10 codes can be recorded and playback

    Please note this plugin relies on a message type which is not yet available in the MySensors core (a pull request has been made)
    This Arduino library https://github.com/z3t0/Arduino-IRremote/releases is used for decoding and encoding:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Changed for MySensors usage by Bart Eversdijk
     * 
     * DESCRIPTION
     *
     * IRrecord: record and play back IR signals as a minimal 
     * An IR detector/demodulator must be connected to the input RECV_PIN.
     * An IR LED must be connected to the output PWM pin 3.
     * A button must be connected to the input BUTTON_PIN; this is the
     * send button.
     * A visible LED can be connected to STATUS_PIN to provide status.
     *
     * The logic is:
     * If the button is pressed, send the IR code.
     * If an IR code is received, record it.
     *
     * Version 0.11 September, 2009
     * Copyright 2009 Ken Shirriff
     * http://arcfn.com
     */
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <IRremote.h>  // https://github.com/z3t0/Arduino-IRremote/releases
    
    int RECV_PIN     = 8;
    #define NODEID   5
    #define CHILD_1  2  // childId
    
    #define MY_RAWBUF  50
    const char * TYPE2STRING[] = {
    		"UNKONWN",
    		"RC5",
    		"RC6",
    		"NEC",
    		"Sony",
    		"Panasonic",
    		"JVC",
    		"SAMSUNG",
    		"Whynter",
    		"AIWA RC T501",
    		"LG",
    		"Sanyo",
    		"Mitsubishi",
    		"Dish",
    		"Sharp",
    		"Denon"
    };
    #define Type2String(x)   TYPE2STRING[x < 0 ? 0 : x]
    #define AddrTxt          F(" addres: 0x")
    #define ValueTxt         F(" value: 0x")
    #define NATxt            F(" - not implemented/found")
    
    // Raw or unknown codes requires an Arduino with a larger memory like a MEGA and some changes to store in EEPROM (now max 255 bytes)
    // #define IR_SUPPORT_UNKNOWN_CODES
    typedef union
    {
      struct
      {
        decode_type_t type;            // The type of code
        unsigned long value;           // The data bits if type is not raw
        int           len;             // The length of the code in bits
        unsigned int  address;         // Used by Panasonic & Sharp [16-bits]
      } code;
    #ifdef IR_SUPPORT_UNKNOWN_CODES      
      struct
      {
        decode_type_t type;             // The type of code
        unsigned int  codes[MY_RAWBUF];
        byte          count;           // The number of interval samples
      } raw;
    #endif
    } IRCode;
    
    #define           MAX_IR_CODES     10
    IRCode            StoredIRCodes[MAX_IR_CODES];
    
    IRrecv            irrecv(RECV_PIN);
    IRsend            irsend;
    decode_results    ircode;
    
    
    #define           NO_PROG_MODE 0xFF
    byte              progModeId       = NO_PROG_MODE;
    
    MySensor gw;
    MyMessage msgIrReceive(CHILD_1, V_IR_RECEIVE);
    MyMessage msgIrRecord(CHILD_1, V_IR_RECORD); 
    
    void setup()  
    {  
      gw.begin(incomingMessage, NODEID);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("IR Rec/Playback", "1.0");
    
      // Register a sensors to gw. Use binary light for test purposes.
      gw.present(CHILD_1, S_IR);
    
      // Tell MYS Controller that we're NOT recording
      gw.send(msgIrRecord.set(0));
      
      Serial.println(F("Recall EEPROM settings"));
      recallEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes);
    
      // Start the ir receiver
      irrecv.enableIRIn(); 
      
      Serial.println(F("Init done..."));
    }
    
    void loop() 
    {
      gw.process();
      
      if (irrecv.decode(&ircode)) {
          dump(&ircode);
          if (progModeId != NO_PROG_MODE) {
             // If we are in PROG mode (Recording) store the new IR code and end PROG mode
             if (storeRCCode(progModeId)) {
                Serial.print(F("Stored "));
              
                // If sucessfull RC decode and storage --> also update the EEPROM
                storeEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes);
                progModeId = NO_PROG_MODE;
               
                // Tell MYS Controller that we're done recording
                gw.send(msgIrRecord.set(0));
             }
          } else {
             // If we are in Playback mode just tell the MYS Controll we did receive an IR code
             if (ircode.decode_type != UNKNOWN) {
                 if (ircode.value != REPEAT) {
                   // Look if we found a stored preset 0 => not found
                   byte num = lookUpPresetCode(&ircode);
                   if (num) {
                       // Send IR decode result to the MYS Controller
                       Serial.print(F("Found code for preset #"));
                       Serial.println(num);
                       gw.send(msgIrReceive.set(num));
                   }
                 }
             }
        }
        // Wait a while before receive next IR-code
        delay(500);
        
        // Start receiving again
        irrecv.resume();
      }
    }
    
    void incomingMessage(const MyMessage &message) {
        //Serial.print(F("New message: "));
        //Serial.println(message.type);
       
       if (message.type == V_IR_RECORD) { // IR_RECORD V_VAR1
          // Get IR record requets for index : paramvalue
          progModeId = message.getByte() % MAX_IR_CODES;
          
          // Tell MYS Controller that we're now in recording mode
          gw.send(msgIrRecord.set(1));
          
          Serial.print(F("Record new IR for: "));
          Serial.println(progModeId);
       }
      
       if (message.type == V_IR_SEND) {
          // Send an IR code from offset: paramvalue - no check for legal value
          Serial.print(F("Send IR preset: "));
          Serial.print(message.getByte());
          sendRCCode(message.getByte() % MAX_IR_CODES);
       }
    
       // Start receiving ir again...
       irrecv.enableIRIn(); 
    }
    
    
    byte lookUpPresetCode (decode_results *ircode)
    {
        for (byte index = 0; index < MAX_IR_CODES; index++)
        {
          if ( StoredIRCodes[index].code.type  == ircode->decode_type &&
               StoredIRCodes[index].code.value == ircode->value       &&
               StoredIRCodes[index].code.len   == ircode->bits)      {
              // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index
              return (index == 0) ? MAX_IR_CODES : index;
          }  
        }
        // not found so return 0
        return 0;
    }
        
    // Stores the code for later playback
    bool storeRCCode(byte index) {
    
      if (ircode.decode_type == UNKNOWN) {
    #ifdef IR_SUPPORT_UNKNOWN_CODES  
          Serial.println(F("Received unknown code, saving as raw"));
          // To store raw codes:
          // Drop first value (gap)
          // As of v1.3 of IRLib global values are already in microseconds rather than ticks
          // They have also been adjusted for overreporting/underreporting of marks and spaces
          byte rawCount = min(ircode.rawlen - 1, MY_RAWBUF);
          for (int i = 1; i <= rawCount; i++) {
            StoredIRCodes[index].raw.codes[i - 1] = ircode.rawbuf[i]; // Drop the first value
          };
          return true;
    #else 
          return false;
        }
    #endif
    
      if (ircode.value == REPEAT) {
          // Don't record a NEC repeat value as that's useless.
          Serial.println(F("repeat; ignoring."));
          return false;
       }
       StoredIRCodes[index].code.type      = ircode.decode_type;
       StoredIRCodes[index].code.value     = ircode.value;
       StoredIRCodes[index].code.address   = ircode.address;      // Used by Panasonic & Sharp [16-bits]
       StoredIRCodes[index].code.len       = ircode.bits;
       Serial.print(F(" value: 0x"));
       Serial.println(ircode.value, HEX);
       return true;
    }
    
    void sendRCCode(byte index) {
    
    #ifdef IR_SUPPORT_UNKNOWN_CODES  
       if(StoredIRCodes[index].code.type == UNKNOWN) {
          // Assume 38 KHz
          irsend.sendRaw(StoredIRCodes[index].raw.codes, StoredIRCodes[index].raw.count, 38);
          Serial.println(F("Sent raw"));
          return;
       }
    #endif
    
       Serial.print(F(" - sent "));
       Serial.print(Type2String(StoredIRCodes[index].code.type));
       if (StoredIRCodes[index].code.type == RC5) {
           // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit
           StoredIRCodes[index].code.value ^= 0x0800;
           irsend.sendRC5(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        } 
        else if (StoredIRCodes[index].code.type == RC6) {
           // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit
           StoredIRCodes[index].code.value ^= 0x10000;
           irsend.sendRC6(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
       }
       else if (StoredIRCodes[index].code.type == NEC) {
           irsend.sendNEC(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        } 
        else if (StoredIRCodes[index].code.type == SONY) {
           irsend.sendSony(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        } 
        else if (StoredIRCodes[index].code.type == PANASONIC) {
           irsend.sendPanasonic(StoredIRCodes[index].code.address, StoredIRCodes[index].code.value);
           Serial.print(AddrTxt);
           Serial.println(StoredIRCodes[index].code.address, HEX);
        }
        else if (StoredIRCodes[index].code.type == JVC) {
           irsend.sendJVC(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len, false);
        }
        else if (StoredIRCodes[index].code.type == SAMSUNG) {
           irsend.sendSAMSUNG(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        }
        else if (StoredIRCodes[index].code.type == WHYNTER) {
           irsend.sendWhynter(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        }
        else if (StoredIRCodes[index].code.type == AIWA_RC_T501) {
           irsend.sendAiwaRCT501(StoredIRCodes[index].code.value);
        }
        else if (StoredIRCodes[index].code.type == LG || StoredIRCodes[index].code.type == SANYO || StoredIRCodes[index].code.type == MITSUBISHI) {
           Serial.println(NATxt);
           return;
        }
        else if (StoredIRCodes[index].code.type == DISH) {
           irsend.sendDISH(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        }
        else if (StoredIRCodes[index].code.type == SHARP) {
           irsend.sendSharp(StoredIRCodes[index].code.address, StoredIRCodes[index].code.value);
           Serial.print(AddrTxt);
           Serial.println(StoredIRCodes[index].code.address, HEX);
        }
        else if (StoredIRCodes[index].code.type == DENON) {
           irsend.sendDenon(StoredIRCodes[index].code.value, StoredIRCodes[index].code.len);
        }
        else {
          // No valid IR type, found it does not make sense to broadcast
          Serial.println(NATxt);
          return; 
        }
        Serial.print(" ");
        Serial.println(StoredIRCodes[index].code.value, HEX);
    }    
    
    // Dumps out the decode_results structure.
    void dump(decode_results *results) {
        int count = results->rawlen;
        
        Serial.print(F("Received : "));
        Serial.print(results->decode_type, DEC);
        Serial.print(F(" "));
        Serial.print(Type2String(results->decode_type));
      
        if (results->decode_type == PANASONIC) {	
          Serial.print(AddrTxt);
          Serial.print(results->address,HEX);
          Serial.print(ValueTxt);
        }
        Serial.print(F(" "));
        Serial.print(results->value, HEX);
        Serial.print(F(" ("));
        Serial.print(results->bits, DEC);
        Serial.println(F(" bits)"));
      
        if (results->decode_type == UNKNOWN) {
          Serial.print(F("Raw ("));
          Serial.print(count, DEC);
          Serial.print(F("): "));
      
          for (int i = 0; i < count; i++) {
            if ((i % 2) == 1) {
              Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
            } 
            else {
              Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
            }
            Serial.print(" ");
          }
          Serial.println("");
        }
    }
    
    // Store IR record struct in EEPROM   
    void storeEeprom(byte len, byte *buf)
    {
        gw.saveState(0, len);
        for (byte i = 1; i < min(len, 100); i++, buf++)
        {
           gw.saveState(i, *buf);
        }
    }
    
    void recallEeprom(byte len, byte *buf)
    {
        if (gw.loadState(0) != len)
        {
           Serial.print(F("Corrupt EEPROM preset values and Clear EEPROM"));
           for (byte i = 1; i < min(len, 100); i++, buf++)
           {
               *buf = 0;
               storeEeprom(len, buf);
           }
           return;
        }
        for (byte i = 1; i < min(len, 100); i++, buf++)
        {
           *buf = gw.loadState(i);
        }
    }   
        
    

  • Admin

    @BartE

    You are the king!

    It's a bit late here now so I'll merge this things tomorrow.


  • Admin

    @BartE, awesome! Your sketch should work perfectly with the IR Blaster. Perhaps you would be a good beta tester - where are you located?


  • Contest Winner

    @blacey this i've though about that too. (the IR-led i've tested with has a poor broadcast range)
    I'm living in the Netherlands


  • Contest Winner

    Adding some Vera screenshots
    upload-89cc6ee3-ce20-44d0-a53c-79305c023d6c
    upload-1d15bc07-5e6d-46ce-87df-cae8adbb5159



  • @BartE As I'm already using a sensor that I just control with light switch buttons in vera, I'd like to not have to add a IR receiver to the current board. Does this have the ability to let me just add the hex codes I already have?

    Either way this is awesome.


  • Contest Winner

    @pete1450 i've added your feature request in version 1.1 of my plug in sketch You can add up to 240 RC HEX commands, from line 111, if you use an Arduino with enough memory. 40 will do with an Arduino UNO

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Changed for MySensors useage by Bart Eversdijk
     * Version 1.1 - Added option to record manual presets up to 240
     * 
     * DESCRIPTION
     *
     * IRrecord: record and play back IR signals as a minimal 
     * An IR detector/demodulator must be connected to the input RECV_PIN.
     * An IR LED must be connected to the output PWM pin 3.
     * A button must be connected to the input BUTTON_PIN; this is the
     * send button.
     * A visible LED can be connected to STATUS_PIN to provide status.
     *
     * The logic is:
     * If the button is pressed, send the IR code.
     * If an IR code is received, record it.
     *
     * Version 0.11 September, 2009
     * Copyright 2009 Ken Shirriff
     * http://arcfn.com
     */
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <IRremote.h>  // https://github.com/z3t0/Arduino-IRremote/releases
    
    int RECV_PIN     = 8;
    #define NODEID   5
    //#define NODEID   NULL
    #define CHILD_1  2  // childId
    
    #define MY_RAWBUF  50
    const char * TYPE2STRING[] = {
    		"UNKONWN",
    		"RC5",
    		"RC6",
    		"NEC",
    		"Sony",
    		"Panasonic",
    		"JVC",
    		"SAMSUNG",
    		"Whynter",
    		"AIWA RC T501",
    		"LG",
    		"Sanyo",
    		"Mitsubishi",
    		"Dish",
    		"Sharp",
    		"Denon"
    };
    #define Type2String(x)   TYPE2STRING[x < 0 ? 0 : x]
    #define AddrTxt          F(" addres: 0x")
    #define ValueTxt         F(" value: 0x")
    #define NATxt            F(" - not implemented/found")
    
    // Raw or unknown codes requires an Arduino with a larger memory like a MEGA and some changes to store in EEPROM (now max 255 bytes)
    // #define IR_SUPPORT_UNKNOWN_CODES
    typedef union
    {
      struct
      {
        decode_type_t type;            // The type of code
        unsigned long value;           // The data bits if type is not raw
        int           len;             // The length of the code in bits
        unsigned int  address;         // Used by Panasonic & Sharp [16-bits]
      } code;
    #ifdef IR_SUPPORT_UNKNOWN_CODES      
      struct
      {
        decode_type_t type;             // The type of code
        unsigned int  codes[MY_RAWBUF];
        byte          count;           // The number of interval samples
      } raw;
    #endif
    } IRCode;
    
    #define           MAX_STORED_IR_CODES     10
    IRCode            StoredIRCodes[MAX_STORED_IR_CODES];
    
    IRrecv            irrecv(RECV_PIN);
    IRsend            irsend;
    decode_results    ircode;
    
    #define           NO_PROG_MODE 0xFF
    byte              progModeId       = NO_PROG_MODE;
    
    // Manual Preset IR values
    // VERA call: luup.call_action("urn:schemas-arduino-cc:serviceId:ArduinoIr1", "SendIrCode", {Index=15}, <device number>)
    // One can add up to 240 preset codes (if your memory lasts) to see to correct data connect the Arduino with this plug in and
    // look at the serial monitor while pressing the desired RC button
    IRCode PresetIRCodes[] = {
        { { RC5, 0x01,       12, 0 } },  // 11 - RC5 key "1" 
        { { RC5, 0x02,       12, 0 } },  // 12 - RC5 key "2"
        { { RC5, 0x03,       12, 0 } },  // 13 - RC5 key "3"
        { { NEC, 0xFF30CF,   32, 0 } },  // 14 - NEC key "1"
        { { NEC, 0xFF18E7,   32, 0 } },  // 15 - NEC key "2"
        { { NEC, 0xFF7A85,   32, 0 } },  // 16 - NEC key "3"
        { { NEC, 0xFF10EF,   32, 0 } },  // 17 - NEC key "4"
        { { NEC, 0xFF38C7,   32, 0 } },  // 18 - NEC key "5"
        { { RC6, 0x800F2401, 36, 0 } },  // 19 - RC6 key "1" MicroSoft Mulitmedia RC
        { { RC6, 0x800F2402, 36, 0 } }   // 20 - RC6 key "2" MicroSoft Mulitmedia RC
    };
    #define MAX_PRESET_IR_CODES  (sizeof(PresetIRCodes)/sizeof(IRCode))
    #define MAX_IR_CODES (MAX_STORED_IR_CODES + MAX_PRESET_IR_CODES)
    
    MySensor gw;
    MyMessage msgIrReceive(CHILD_1, V_IR_RECEIVE);
    MyMessage msgIrRecord(CHILD_1, V_IR_RECORD); 
    
    void setup()  
    {  
      gw.begin(incomingMessage, NODEID);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("IR Rec/Playback", "1.1");
    
      // Register a sensors to gw. Use binary light for test purposes.
      gw.present(CHILD_1, S_IR);
    
      // Tell MYS Controller that we're NOT recording
      gw.send(msgIrRecord.set(0));
      
      Serial.println(F("Recall EEPROM settings"));
      recallEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes);
    
      // Start the ir receiver
      irrecv.enableIRIn(); 
      
      Serial.println(F("Init done..."));
    }
    
    void loop() 
    {
      gw.process();
      
      if (irrecv.decode(&ircode)) {
          dump(&ircode);
          if (progModeId != NO_PROG_MODE) {
             // If we are in PROG mode (Recording) store the new IR code and end PROG mode
             if (storeRCCode(progModeId)) {
                Serial.print(F("Stored "));
              
                // If sucessfull RC decode and storage --> also update the EEPROM
                storeEeprom(sizeof(StoredIRCodes), (byte *)&StoredIRCodes);
                progModeId = NO_PROG_MODE;
               
                // Tell MYS Controller that we're done recording
                gw.send(msgIrRecord.set(0));
             }
          } else {
             // If we are in Playback mode just tell the MYS Controll we did receive an IR code
             if (ircode.decode_type != UNKNOWN) {
                 if (ircode.value != REPEAT) {
                   // Look if we found a stored preset 0 => not found
                   byte num = lookUpPresetCode(&ircode);
                   if (num) {
                       // Send IR decode result to the MYS Controller
                       Serial.print(F("Found code for preset #"));
                       Serial.println(num);
                       gw.send(msgIrReceive.set(num));
                   }
                 }
             }
        }
        // Wait a while before receive next IR-code
        delay(500);
        
        // Start receiving again
        irrecv.resume();
      }
    }
    
    void incomingMessage(const MyMessage &message) {
        //Serial.print(F("New message: "));
        //Serial.println(message.type);
       
       if (message.type == V_IR_RECORD) { // IR_RECORD V_VAR1
          // Get IR record requets for index : paramvalue
          progModeId = message.getByte() % MAX_STORED_IR_CODES;
          
          // Tell MYS Controller that we're now in recording mode
          gw.send(msgIrRecord.set(1));
          
          Serial.print(F("Record new IR for: "));
          Serial.println(progModeId);
       }
      
       if (message.type == V_IR_SEND) {
          // Send an IR code from offset: paramvalue - no check for legal value
          Serial.print(F("Send IR preset: "));
          byte code = message.getByte() % MAX_IR_CODES;
          if (code == 0) {
            code = MAX_IR_CODES;
          }
          Serial.print(code);
          sendRCCode(code);
       }
    
       // Start receiving ir again...
       irrecv.enableIRIn(); 
    }
    
    
    byte lookUpPresetCode (decode_results *ircode)
    {
        // Get rit of the RC5/6 toggle bit when looking up
        if (ircode->decode_type == RC5)  {
            ircode->value = ircode->value & 0x7FF;
        }
        if (ircode->decode_type == RC6)  {
            ircode->value = ircode->value & 0xFFFF7FFF;
        }
        for (byte index = 0; index < MAX_STORED_IR_CODES; index++) {
          if ( StoredIRCodes[index].code.type  == ircode->decode_type &&
               StoredIRCodes[index].code.value == ircode->value       &&
               StoredIRCodes[index].code.len   == ircode->bits)      {
              // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index
              return (index == 0) ? MAX_STORED_IR_CODES : index;
          }  
        }
        
        for (byte index = 0; index < MAX_PRESET_IR_CODES; index++) {
          if ( PresetIRCodes[index].code.type  == ircode->decode_type &&
               PresetIRCodes[index].code.value == ircode->value       &&
               PresetIRCodes[index].code.len   == ircode->bits)      {
              // The preset number starts with 1 so the last is stored as 0 -> fix this when looking up the correct index
              return ((index == 0) ? MAX_PRESET_IR_CODES : index) + MAX_STORED_IR_CODES;
          }  
        }
        // not found so return 0
        return 0;
    }
        
    // Stores the code for later playback
    bool storeRCCode(byte index) {
    
      if (ircode.decode_type == UNKNOWN) {
    #ifdef IR_SUPPORT_UNKNOWN_CODES  
          Serial.println(F("Received unknown code, saving as raw"));
          // To store raw codes:
          // Drop first value (gap)
          // As of v1.3 of IRLib global values are already in microseconds rather than ticks
          // They have also been adjusted for overreporting/underreporting of marks and spaces
          byte rawCount = min(ircode.rawlen - 1, MY_RAWBUF);
          for (int i = 1; i <= rawCount; i++) {
            StoredIRCodes[index].raw.codes[i - 1] = ircode.rawbuf[i]; // Drop the first value
          };
          return true;
    #else 
          return false;
        }
    #endif
    
       if (ircode.value == REPEAT) {
           // Don't record a NEC repeat value as that's useless.
           Serial.println(F("repeat; ignoring."));
           return false;
       }
       // Get rit of the toggle bit when storing RC5/6 
       if (ircode.decode_type == RC5)  {
            ircode.value = ircode.value & 0x07FF;
       }
       if (ircode.decode_type == RC6)  {
            ircode.value = ircode.value & 0xFFFF7FFF;
       }
    
       StoredIRCodes[index].code.type      = ircode.decode_type;
       StoredIRCodes[index].code.value     = ircode.value;
       StoredIRCodes[index].code.address   = ircode.address;      // Used by Panasonic & Sharp [16-bits]
       StoredIRCodes[index].code.len       = ircode.bits;
       Serial.print(F(" value: 0x"));
       Serial.println(ircode.value, HEX);
       return true;
    }
    
    void sendRCCode(byte index) {
       IRCode *pIr = ((index <= MAX_STORED_IR_CODES) ? &StoredIRCodes[index % MAX_STORED_IR_CODES] : &PresetIRCodes[index - MAX_STORED_IR_CODES - 1]);
       
    #ifdef IR_SUPPORT_UNKNOWN_CODES  
       if(pIr->code.type == UNKNOWN) {
          // Assume 38 KHz
          irsend.sendRaw(pIr->raw.codes, pIr->raw.count, 38);
          Serial.println(F("Sent raw"));
          return;
       }
    #endif
    
       Serial.print(F(" - sent "));
       Serial.print(Type2String(pIr->code.type));
       if (pIr->code.type == RC5) {
           // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause
           pIr->code.value ^= 0x0800;
           for (byte i=0; i < 3; i++) {
             if (i > 0) { delay(100); } 
             irsend.sendRC5(pIr->code.value, pIr->code.len);
           }
        } 
        else if (pIr->code.type == RC6) {
           // For RC5 and RC6 there is a toggle bit for each succesor IR code sent alway toggle this bit, needs to repeat the command 3 times with 100 mS pause
           if (pIr->code.len == 20) {
                  pIr->code.value ^= 0x10000;
           }
           for (byte i=0; i < 3; i++) {
             if (i > 0) { delay(100); } 
             irsend.sendRC6(pIr->code.value, pIr->code.len);
           }
       }
       else if (pIr->code.type == NEC) {
           irsend.sendNEC(pIr->code.value, pIr->code.len);
        } 
        else if (pIr->code.type == SONY) {
           irsend.sendSony(pIr->code.value, pIr->code.len);
        } 
        else if (pIr->code.type == PANASONIC) {
           irsend.sendPanasonic(pIr->code.address, pIr->code.value);
           Serial.print(AddrTxt);
           Serial.println(pIr->code.address, HEX);
        }
        else if (pIr->code.type == JVC) {
           irsend.sendJVC(pIr->code.value, pIr->code.len, false);
        }
        else if (pIr->code.type == SAMSUNG) {
           irsend.sendSAMSUNG(pIr->code.value, pIr->code.len);
        }
        else if (pIr->code.type == WHYNTER) {
           irsend.sendWhynter(pIr->code.value, pIr->code.len);
        }
        else if (pIr->code.type == AIWA_RC_T501) {
           irsend.sendAiwaRCT501(pIr->code.value);
        }
        else if (pIr->code.type == LG || pIr->code.type == SANYO || pIr->code.type == MITSUBISHI) {
           Serial.println(NATxt);
           return;
        }
        else if (pIr->code.type == DISH) {
          // need to repeat the command 4 times with 100 mS pause
          for (byte i=0; i < 4; i++) {
             if (i > 0) { delay(100); } 
               irsend.sendDISH(pIr->code.value, pIr->code.len);
          }
        }
        else if (pIr->code.type == SHARP) {
           irsend.sendSharp(pIr->code.address, pIr->code.value);
           Serial.print(AddrTxt);
           Serial.println(pIr->code.address, HEX);
        }
        else if (pIr->code.type == DENON) {
           irsend.sendDenon(pIr->code.value, pIr->code.len);
        }
        else {
          // No valid IR type, found it does not make sense to broadcast
          Serial.println(NATxt);
          return; 
        }
        Serial.print(" ");
        Serial.println(pIr->code.value, HEX);
    }    
    
    // Dumps out the decode_results structure.
    void dump(decode_results *results) {
        int count = results->rawlen;
        
        Serial.print(F("Received : "));
        Serial.print(results->decode_type, DEC);
        Serial.print(F(" "));
        Serial.print(Type2String(results->decode_type));
      
        if (results->decode_type == PANASONIC) {	
          Serial.print(AddrTxt);
          Serial.print(results->address,HEX);
          Serial.print(ValueTxt);
        }
        Serial.print(F(" "));
        Serial.print(results->value, HEX);
        Serial.print(F(" ("));
        Serial.print(results->bits, DEC);
        Serial.println(F(" bits)"));
      
        if (results->decode_type == UNKNOWN) {
          Serial.print(F("Raw ("));
          Serial.print(count, DEC);
          Serial.print(F("): "));
      
          for (int i = 0; i < count; i++) {
            if ((i % 2) == 1) {
              Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
            } else {
              Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
            }
            Serial.print(" ");
          }
          Serial.println("");
        }
    }
    
    // Store IR record struct in EEPROM   
    void storeEeprom(byte len, byte *buf)
    {
        gw.saveState(0, len);
        for (byte i = 1; i < min(len, 100); i++, buf++) {
           gw.saveState(i, *buf);
        }
    }
    
    void recallEeprom(byte len, byte *buf)
    {
        if (gw.loadState(0) != len) {
           Serial.print(F("Corrupt EEPROM preset values and Clear EEPROM"));
           for (byte i = 1; i < min(len, 100); i++, buf++) {
               *buf = 0;
               storeEeprom(len, buf);
           }
           return;
        }
        for (byte i = 1; i < min(len, 100); i++, buf++) {
           *buf = gw.loadState(i);
        }
    }
    

    I will update the Vera code as well in git
    With Vera this luup code controlls the extra presets:
    luup.call_action("urn:schemas-arduino-cc:serviceId:ArduinoIr1", "SendIrCode", {Index=15}, <device number>)

    Vera UI5 Screen shot:
    upload-0bb76cf0-b984-4b30-b9b3-cfb03ffe9648


  • Admin

    240 RC-commands ought to be enough for anybody...

    quote-Bill-Gates-640k-ought-to-be-enough-for-anybody-89027.png



  • 😹 👍



  • @BartE Wow that's great. Thanks!



  • I get this error in UI7

    Arduino:1.6.7 (Windows 7), Placa:"Arduino Nano, ATmega328"

    WARNING: Category '' in library UIPEthernet is not valid. Setting to 'Uncategorized'
    D:\Users\Colosus\Desktop\ir_prueba\ir_prueba.ino: In function 'void setup()':

    ir_prueba:141: error: 'msgIrRecord' was not declared in this scope

    gw.send(msgIrRecord.set(0));

           ^
    

    D:\Users\Colosus\Desktop\ir_prueba\ir_prueba.ino: In function 'void loop()':

    ir_prueba:168: error: 'msgIrRecord' was not declared in this scope

             gw.send(msgIrRecord.set(0));
    
                     ^
    

    D:\Users\Colosus\Desktop\ir_prueba\ir_prueba.ino: In function 'void incomingMessage(const MyMessage&)':

    ir_prueba:197: error: 'V_IR_RECORD' was not declared in this scope

    if (message.type == V_IR_RECORD) { // IR_RECORD V_VAR1
    
                        ^
    

    'msgIrRecord' was not declared in this scope


  • Contest Winner

    This sketch has been updated to MySensors 2.0 and added example library https://www.mysensors.org/build/ir


Log in to reply
 

Suggested Topics

  • 8
  • 1
  • 3
  • 1
  • 5
  • 90

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts