Page 1 sur 1

[RESOLU] IR Sensor ??

Publié : 04 oct. 2016, 17:38
par gawel3164
Salut,
Les vacances sont finies....on déterre le fer à souder! :lol: La domotique me poussant à la fainéantise, j'ai besoin d'un nouveau sensor. Quand je veux regarder la télé, il me faut la télécommande de la télé, celle du home cinéma et celle du décodeur TNT... Il y en a au moins 2 de trop!! Le but serait d'allumer les 3 appareils en même temps (ça je devrais y arriver en envoyant les 3 codes en même temps). J'ai fait un sensor IR pour tester, et j'arrive à allumer et éteindre mon home cinéma. Par contre, je n'ai pas réussi a comprendre comment modifier le sketch pour piloter plusieurs appareils, ni même si c'était possible... Peut-on avec un seul sensor, avoir plusieurs switches virtuels pour piloter différents fonctions (allumer, monter le volume, changer de chaine)??? Quelqu'un pourrait m'éclairer svp? Merci
Voici le sketch utilisé que j'ai modifié pour l'adapter à mon home cinéma:

Code : Tout sélectionner

/**
 * 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 - Henrik EKblad
 * 
 * DESCRIPTION
 * Example sketch showing how to control ir devices
 * An IR LED must be connected to Arduino PWM pin 3.
 * An optional ir receiver can be connected to PWM pin 8. 
 * All receied ir signals will be sent to gateway device stored in VAR_1.
 * When binary light on is clicked - sketch will send volume up ir command
 * When binary light off is clicked - sketch will send volume down ir command
 * http://www.mysensors.org/build/ir
 */

#include <MySensor.h>
#include <SPI.h>
#include <IRLib.h>

int RECV_PIN = 8;

#define CHILD_1  3  // childId

IRsend irsend;
IRrecv irrecv(RECV_PIN);
IRdecode decoder;
//decode_results results;
unsigned int Buffer[RAWBUF];
MySensor gw;
MyMessage msg(CHILD_1, V_VAR1);

void setup()  
{  
  irrecv.enableIRIn(); // Start the ir receiver
  decoder.UseExtnBuf(Buffer);
  gw.begin(incomingMessage);

  // Send the sketch version information to the gateway and Controller
  gw.sendSketchInfo("IR Sensor", "1.0");

  // Register a sensors to gw. Use binary light for test purposes.
  gw.present(CHILD_1, S_LIGHT);
}


void loop() 
{
  gw.process();
  if (irrecv.GetResults(&decoder)) {
    irrecv.resume(); 
    decoder.decode();
    decoder.DumpResults();
        
    char buffer[10];
    sprintf(buffer, "%08lx", decoder.value);
    // Send ir result to gw
    gw.send(msg.set(buffer));
  }
}



void incomingMessage(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LIGHT) {
     int incomingRelayStatus = message.getInt();
     if (incomingRelayStatus == 1) {
      irsend.send(NEC, 0x4BB620DF, 32); // ONKYO  ON
     } else {
      irsend.send(NEC, 0x4B36E21D, 32); // ONKYO  OFF
     }
     // Start receiving ir again...
    irrecv.enableIRIn(); 
  }
}
    
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
/*void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.print("Unknown encoding: ");
  } 
  else if (results->decode_type == NEC) {
    Serial.print("Decoded NEC: ");
  } 
  else if (results->decode_type == SONY) {
    Serial.print("Decoded SONY: ");
  } 
  else if (results->decode_type == RC5) {
    Serial.print("Decoded RC5: ");
  } 
  else if (results->decode_type == RC6) {
    Serial.print("Decoded RC6: ");
  }
  else if (results->decode_type == PANASONIC) {	
    Serial.print("Decoded PANASONIC - Address: ");
    Serial.print(results->panasonicAddress,HEX);
    Serial.print(" Value: ");
  }
  else if (results->decode_type == JVC) {
     Serial.print("Decoded JVC: ");
  }
  Serial.print(results->value, HEX);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  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("");
}
*/

Re: IR Sensor ??

Publié : 04 oct. 2016, 18:40
par vil1driver
Salut,

Bien sûr tu peux créer plusieurs enfants et détecter dans le message reçu (en plus du statut) quel enfant en est la source et lancer le code ir correspondant.

Par exemple

Code : Tout sélectionner

if (message.sensor == CHILD_1) {
if (incomingRelayStatus == 1) {
Blabla... 
} 
} 
if (message.sensor == CHILD_2) {
if (incomingRelayStatus == 1) {
Blabla... 
} 
} 
Ou simplement sur le même enfant envoyer plusieurs codes ir

Code : Tout sélectionner

if (incomingRelayStatus == 1) {
      irsend.send(NEC, 0x4BB620DF, 32); // ONKYO  ON
      Peut-être une légère pause ici
      irsend.send(NEC, 0x4B473892F, 32); // TV  ON

Re: IR Sensor ??

Publié : 04 oct. 2016, 18:49
par gawel3164
Super!! Merci beaucoup!! ;) Y a plus qu'a!

Re: IR Sensor ??

Publié : 05 oct. 2016, 17:44
par gawel3164
Grace à l'aide précieuse de Vil1driver (merci! :) ) et après un peu de lecture, voici le sketch qui fonctionne. Je partage pour ceux que ça peut intéresser (a adapter a votre matériel en fonction des commandes recherchées et des codes télécommande).

Code : Tout sélectionner

/**
 * 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 - Henrik EKblad
 * 
 * DESCRIPTION
 * Example sketch showing how to control ir devices
 * An IR LED must be connected to Arduino PWM pin 3.
 * An optional ir receiver can be connected to PWM pin 8. 
 * All receied ir signals will be sent to gateway device stored in VAR_1.
 * When binary light on is clicked - sketch will send volume up ir command
 * When binary light off is clicked - sketch will send volume down ir command
 * http://www.mysensors.org/build/ir
 */

#include <MySensor.h>
#include <SPI.h>
#include <IRLib.h>

int RECV_PIN = 8;

#define CHILD_1  3  // childId
#define CHILD_2  4  // childId
#define CHILD_3  5  // childId
#define CHILD_4  6  // childId
#define CHILD_5  7  // childId

IRsend irsend;
IRrecv irrecv(RECV_PIN);
IRdecode decoder;
//decode_results results;
unsigned int Buffer[RAWBUF];
MySensor gw;
MyMessage msg(CHILD_1, V_VAR1);

void setup()  
{  
  irrecv.enableIRIn(); // Start the ir receiver
  decoder.UseExtnBuf(Buffer);
  gw.begin(incomingMessage);

  // Send the sketch version information to the gateway and Controller
  gw.sendSketchInfo("IR Sensor", "1.0");

  // Register a sensors to gw. Use binary light for test purposes.
  gw.present(CHILD_1, S_LIGHT);
  gw.present(CHILD_2, S_LIGHT);
  gw.present(CHILD_3, S_LIGHT);
  gw.present(CHILD_4, S_LIGHT);
  gw.present(CHILD_5, S_LIGHT);

  
}


void loop() 
{
  gw.process();
  if (irrecv.GetResults(&decoder)) {
    irrecv.resume(); 
    decoder.decode();
    decoder.DumpResults();
        
    char buffer[10];
    sprintf(buffer, "%08lx", decoder.value);
    // Send ir result to gw
    gw.send(msg.set(buffer));
  }
}



void incomingMessage(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LIGHT) {
     int incomingRelayStatus = message.getInt();

     //Allumage TV+Home cinema+adaptateur TNT
     if (message.sensor == CHILD_1) {
     if (incomingRelayStatus == 1) {
      irsend.send(NEC, 0x4BB620DF, 32); // ONKYO ON
      irsend.send(NEC, 0x205D38C7, 32); // TNT ON
      irsend.send(RC6, 0x1000C, 20); // TV ON      
     } else {
      irsend.send(NEC, 0x4B36E21D, 32); // ONKYO OFF
      irsend.send(NEC, 0x205D38C7, 32); // TNT OFF
      irsend.send(RC6, 0x1000C, 20); // TV OFF
           }
           }
     //Volume+ Home Cinema      
     if (message.sensor == CHILD_2) {
     if (incomingRelayStatus == 1) {
      irsend.send(NEC, 0x4BB640BF, 32); // VOL+ ONKYO
      } 
      }

     //Volume- Home Cinema      
     if (message.sensor == CHILD_3) {
     if (incomingRelayStatus == 1) {
      irsend.send(NEC, 0x4BB6C03F, 32); //  VOL- ONKYO
      } 
      }

     //Chanel+ adaptateur TNT      
     if (message.sensor == CHILD_4) {
     if (incomingRelayStatus == 1) {
     irsend.send(NEC, 0x205D609F, 32); // CH+ ADAPTATEUR TNT
      } 
      }
      
      
      //chanel- adaptateurTNT     
     if (message.sensor == CHILD_5) {
     if (incomingRelayStatus == 1) {
     irsend.send(NEC, 0x205D40BF, 32); // CH- ADAPTATEUR TNT
     } 
     }     

           
     // Start receiving ir again...
    irrecv.enableIRIn(); 
  }
}
    
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
/*void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.print("Unknown encoding: ");
  } 
  else if (results->decode_type == NEC) {
    Serial.print("Decoded NEC: ");
  } 
  else if (results->decode_type == SONY) {
    Serial.print("Decoded SONY: ");
  } 
  else if (results->decode_type == RC5) {
    Serial.print("Decoded RC5: ");
  } 
  else if (results->decode_type == RC6) {
    Serial.print("Decoded RC6: ");
  }
  else if (results->decode_type == PANASONIC) {	
    Serial.print("Decoded PANASONIC - Address: ");
    Serial.print(results->panasonicAddress,HEX);
    Serial.print(" Value: ");
  }
  else if (results->decode_type == JVC) {
     Serial.print("Decoded JVC: ");
  }
  Serial.print(results->value, HEX);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  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("");
}
*/