Page 2 of 3
Re: IR Sender/Receiver built, now what?
Posted: Tuesday 25 August 2015 17:58
by Justintime
flwvanes wrote:Hi all,
I have a MySensors IR receiver in combination with domoticz running for some months now and is working perfect for my needs.
I am using the X10 IR codes (because I found a library on the internet to decode the X10 IR codes with an arduino) which I can send via my Harmony remote control.
In domoticz I have defined 16 switches which are controlled by the MySensors sketch.....quite easy but it does the trick for me
I have included the sketch, the link to the library I could not find anymore but if you are interested in the X10 library please send me a PM.
Greetings Frank
Code: Select all
// 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
#include <MySensor.h>
#include <SPI.h>
#include <X10ex.h>
#include <X10ir.h>
// X10 Infrared Receiver Library
X10ir x10ir = X10ir(
1, // Receive Interrupt Number (1 = Standard Arduino External Interrupt)
3, // Receive Interrupt Pin (Pin 3 must be used with interrupt 1)
'A', // Default House Code
infraredEvent // Event triggered when IR message is received
);
#define LED_PIN 4
MySensor gw;
MyMessage msg(0,V_LIGHT);
#define MAX_BTN 16
boolean last_state[MAX_BTN];
int send_btn;
void setup()
{
x10ir.begin();
gw.begin(incomingMessage);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("IR Sensor", "1.1");
// Register a sensors to gw. Use binary light for test purposes.
for (int i=0; i<MAX_BTN; i++)
{
gw.present(i, S_LIGHT);
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 0);
}
void loop()
{
gw.process();
//If IR code received
if(send_btn > 0)
{
digitalWrite(LED_PIN, 1);
if(last_state[send_btn-1])
{
Serial.print("IR ");
Serial.print(send_btn-1);
Serial.println(" --> Off");
gw.send(msg.setSensor(send_btn-1).set(0));
last_state[send_btn-1] = 0;
}
else
{
Serial.print("IR ");
Serial.print(send_btn-1);
Serial.println(" --> On");
gw.send(msg.setSensor(send_btn-1).set(1));
last_state[send_btn-1] = 1;
}
send_btn = 0;
delay(300);
digitalWrite(LED_PIN, 0);
}
}
void incomingMessage(const MyMessage &message)
{
Serial.println("Received from Domoticz");
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT)
{
digitalWrite(LED_PIN, 1);
if(message.sensor<16)
{
last_state[message.sensor] = message.getInt();
}
delay(100);
digitalWrite(LED_PIN, 0);
}
}
// Process commands received from X10 compatible IR remote
void infraredEvent(char house, byte unit, byte command, bool isRepeat)
{
if(!isRepeat)
{
Serial.println(command);
// Handle Address Command (House + Unit)
if(command == CMD_ADDRESS)
{
Serial.println("cmd == CMD_ADDRESS");
Serial.println(house);
Serial.println(unit);
Serial.println(command);
if(unit <= 16)
{
Serial.println("Switch...");
send_btn = unit;
}
}
}
}
What device did you use on the Harmony?
Re: IR Sender/Receiver built, now what?
Posted: Tuesday 25 August 2015 19:48
by flwvanes
Justintime wrote:flwvanes wrote:Hi all,
I have a MySensors IR receiver in combination with domoticz running for some months now and is working perfect for my needs.
I am using the X10 IR codes (because I found a library on the internet to decode the X10 IR codes with an arduino) which I can send via my Harmony remote control.
In domoticz I have defined 16 switches which are controlled by the MySensors sketch.....quite easy but it does the trick for me
I have included the sketch, the link to the library I could not find anymore but if you are interested in the X10 library please send me a PM.
Greetings Frank
Code: Select all
// 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
#include <MySensor.h>
#include <SPI.h>
#include <X10ex.h>
#include <X10ir.h>
// X10 Infrared Receiver Library
X10ir x10ir = X10ir(
1, // Receive Interrupt Number (1 = Standard Arduino External Interrupt)
3, // Receive Interrupt Pin (Pin 3 must be used with interrupt 1)
'A', // Default House Code
infraredEvent // Event triggered when IR message is received
);
#define LED_PIN 4
MySensor gw;
MyMessage msg(0,V_LIGHT);
#define MAX_BTN 16
boolean last_state[MAX_BTN];
int send_btn;
void setup()
{
x10ir.begin();
gw.begin(incomingMessage);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("IR Sensor", "1.1");
// Register a sensors to gw. Use binary light for test purposes.
for (int i=0; i<MAX_BTN; i++)
{
gw.present(i, S_LIGHT);
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 0);
}
void loop()
{
gw.process();
//If IR code received
if(send_btn > 0)
{
digitalWrite(LED_PIN, 1);
if(last_state[send_btn-1])
{
Serial.print("IR ");
Serial.print(send_btn-1);
Serial.println(" --> Off");
gw.send(msg.setSensor(send_btn-1).set(0));
last_state[send_btn-1] = 0;
}
else
{
Serial.print("IR ");
Serial.print(send_btn-1);
Serial.println(" --> On");
gw.send(msg.setSensor(send_btn-1).set(1));
last_state[send_btn-1] = 1;
}
send_btn = 0;
delay(300);
digitalWrite(LED_PIN, 0);
}
}
void incomingMessage(const MyMessage &message)
{
Serial.println("Received from Domoticz");
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT)
{
digitalWrite(LED_PIN, 1);
if(message.sensor<16)
{
last_state[message.sensor] = message.getInt();
}
delay(100);
digitalWrite(LED_PIN, 0);
}
}
// Process commands received from X10 compatible IR remote
void infraredEvent(char house, byte unit, byte command, bool isRepeat)
{
if(!isRepeat)
{
Serial.println(command);
// Handle Address Command (House + Unit)
if(command == CMD_ADDRESS)
{
Serial.println("cmd == CMD_ADDRESS");
Serial.println(house);
Serial.println(unit);
Serial.println(command);
if(unit <= 16)
{
Serial.println("Switch...");
send_btn = unit;
}
}
}
}
What device did you use on the Harmony?
I used the "Marmitek IR-7242" device.
Re: IR Sender/Receiver built, now what?
Posted: Tuesday 25 August 2015 20:04
by flwvanes
Justintime wrote:Thanks... I will try this out. Does every command make a new virtual device/switch in Domoticz?
No, this sketch is only working for 16 units, but you can expand it with more units by using the house code aswell (each house has 16 units).
(The X10 code is build of a "House", "Unit" and "Command" code if I remember well

)
Within Domoticz I use the switches within a lua script to control some lights in the living room.
Code: Select all
commandArray = {}
if (devicechanged['IR 00'] == 'On') then
if (otherdevices['Staande lamp'] == 'Off') then
commandArray['Staande lamp']='On'
print('IR Control - Staande lamp = ON')
else
commandArray['Staande lamp']='Off'
print('IR Control - Staande lamp = OFF')
end
commandArray['IR 00']='Off'
end
if (devicechanged['IR 01'] == 'On') then
if (otherdevices['Haard verlichting'] == 'Off') then
commandArray['Haard verlichting']='On'
print('IR Control - Haard verlichting = ON')
else
commandArray['Haard verlichting']='Off'
print('IR Control - Haard verlichting = OFF')
end
commandArray['IR 01']='Off'
end
if (devicechanged['IR 02'] == 'On') then
if (otherdevices['Tuin verlichting'] == 'Off') then
commandArray['Tuin verlichting']='On'
print('IR Control - Tuin verlichting = ON')
else
commandArray['Tuin verlichting']='Off'
print('IR Control - Tuin verlichting = OFF')
end
commandArray['IR 02']='Off'
end
if (devicechanged['IR 03'] == 'On') then
if (otherdevices['Rooster'] == 'Closed') then
commandArray['Rooster']='On'
print('IR Control - Rooster = OPEN')
else
commandArray['Rooster']='Off'
print('IR Control - Rooster = OFF')
end
commandArray['IR 03']='Off'
end
if (devicechanged['IR 04'] == 'On') then
if (otherdevices['Hang lamp'] == 'Off') then
commandArray['Hang lamp']='On'
print('IR Control - Hang lamp = ON')
else
commandArray['Hang lamp']='Off'
print('IR Control - Hang lamp = OFF')
end
commandArray['IR 04']='Off'
end
if (devicechanged['IR 15'] == 'On') then
commandArray['Staande lamp']='Off'
commandArray['Hang lamp']='Off'
commandArray['Tuin verlichting']='Off'
commandArray['Haard verlichting']='Off'
print('IR Control - Staande lamp, Haard verlichting en Tuin verlichting = OFF')
commandArray['IR 15']='Off'
end
return commandArray
Re: IR Sender/Receiver built, now what?
Posted: Tuesday 25 August 2015 20:11
by Justintime
I have flashed the Arduino. And the marmitek device added in the Harmony. But there are no codes rolling by in Arduino Serial.
When i compile the arduino sketch there was a library missing the X10ex.h.
It was not in the link you have posted, so i grabbed it from somewhere....
Maybe thats why?? The Node is regonized in the serial and send is "ok"
Re: IR Sender/Receiver built, now what?
Posted: Tuesday 25 August 2015 20:50
by flwvanes
Justintime wrote:I have flashed the Arduino. And the marmitek device added in the Harmony. But there are no codes rolling by in Arduino Serial.
When i compile the arduino sketch there was a library missing the X10ex.h.
It was not in the link you have posted, so i grabbed it from somewhere....
Maybe thats why?? The Node is regonized in the serial and send is "ok"
You are correct.....I found the wrong library
I will send you a PM with the correct one.
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 15:40
by gizmocuz
some heads up:
in the latest beta i implemented the mysensors S_IR_RECEIVE and S_IR_SEND
going to make two posts
Post 1:
When playing with the IR sample, i noticed that 99% of my remote buttons where not recognized, i only received pulsed, instead of a code
So.... i look at the IR library, and it has a nice example to make a 32bit hash of the received IR pulses.
This proves to be working very good !
I made a new sketch, that is using this hash code, and send it to domoticz (S_IR_RECEIVE)
A new switch (type mdremote) is created, and you can use this for example as the activation code of a scene
Next, when setting the type of the switch to 'push on' (as it is only received) you can also push the button in domoticz, and it will send the code to the sketch (S_IR_SEND)
you can do what you want with it
The sketch is:
Code: Select all
/**
* 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 <[email protected]>
* 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;
IRdecodeHash Hash_Decoder;
//As some remotes could have different hashes for the same button (when they are not good received or other reasons)
//we only let the following hashes go through
#define MYCODES_LENGTH 2
const unsigned long myHashes[MYCODES_LENGTH]={
0x0BE3BB37, //volume up
0xD4D9F2A7, //volume down
};
//decode_results results;
MySensor gw;
MyMessage msgir(CHILD_1, V_IR_RECEIVE);
void setup()
{
irrecv.enableIRIn(); // Start the ir receiver
gw.begin(incomingMessage,254);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("IR Sensor", "1.1");
// Register a sensors to gw. Use binary light for test purposes.
gw.present(CHILD_1, S_IR);
Serial.println("Ready to receive...");
}
void loop()
{
gw.process();
if (irrecv.GetResults(&decoder)) {
Hash_Decoder.copyBuf(&decoder);//copy the results to the hash decoder
//decoder.decode();
//decoder.DumpResults();
Hash_Decoder.decode();
if (!FindHashInOurTable(Hash_Decoder.hash)) {
Serial.print("Unknown IR hash received: ");
Serial.println(Hash_Decoder.hash, HEX);
}
else
{
Serial.print("Known IR hash received: ");
Serial.println(Hash_Decoder.hash, HEX);
gw.send(msgir.set(Hash_Decoder.hash));
}
delay(1000);
irrecv.resume();
/*
char buffer[10];
sprintf(buffer, "%08lx", decoder.value);
// Send ir result to gw
Serial.println(buffer);
//gw.send(msg.set(buffer));
*/
}
}
bool FindHashInOurTable(const unsigned long hash)
{
for (int ii=0; ii<MYCODES_LENGTH; ii++)
{
if (myHashes[ii]==hash)
return true;
}
return false;
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_IR_SEND) {
unsigned long ir_code=(unsigned long)message.getLong();
Serial.print("Gateway received code: ");
Serial.println(ir_code, HEX);
//irsend.send(NEC, ir_code, 32); // Vol up yamaha ysp-900
//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("");
}
*/
But of course when your remote is recognized, you can make your own sketch, and use the S_IR_RECEIVED / S_IR_SEND types
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 15:44
by gizmocuz
Post 2:
What i am trying to do, is create a sketch to receive a raw IR code (normal remote, HAVC, ....) store it to disk,
and be able to load from disk, and send the raw pulses.
For me, i am trying to control an airco, this can only be done with capturing the 'raw' pulses, and send these 'raw' pulses.
So i am at the moment playing with an arduino uno with a ethernet sketch, and an sd card.
Via ethernet you can send simple commands like:
Learn <id> (for example Learn 1)
(This will learn a new raw pulse, and stores it on the sd card as file '1')
Send <id> (for example Send 1)
(This will load the sd card file '1', and send it out)
So this is for receiving/sending IR, not receiving IR and send it to domoticz
Because, every time the pulses are slightly different.
My current problems:
- making a sketch with ethernet/sd/ir with the default libraries results in a to large sketch to upload.
- at the moment i am using a custom ethernet library, this reduced a lot !, still to enough
- i replaced all strings in my sketch by encapsulating them in F("aaa"), this also reduced the sketch (user variables) a lot!, still not enough
- my test sketch now has 150 user variable space left, this produces a warning and can be dangerous
Of course a arduino mega would not have this problem, but... like to keep it small.
(for sure, a mysensor node would not work on the uno, thats why i am looking about a ethernet interface)
What i try to make, is good for universal IR transmit (capturing raw, sending out), it is not IR receive -> domoticz
Once i have it working, i will post all files needed to build my sketch.
We could use it with mysensors on a arduino mega, but then we need an addional V_IR_LEARN
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 18:30
by Justintime
gizmocuz wrote:some heads up:
in the latest beta i implemented the mysensors S_IR_RECEIVE and S_IR_SEND
going to make two posts
Post 1:
...
Playing around with the Node. I see the ir signals gives a nice code.
In the Log i see the node being regonized. And as i understand for every command it will make a new switch/device? But i dont see new devices/switches.
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 18:31
by gizmocuz
You have to edit the sketch and add your hashes
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 18:32
by gizmocuz
Best to debug with the arduino ide serial monitor
Re: IR Sender/Receiver built, now what?
Posted: Saturday 05 September 2015 18:51
by Justintime
gizmocuz wrote:Best to debug with the arduino ide serial monitor
AHA got it.... very nice... Now i can have endless buttons. Those 16 were to short for me. But was also a nice working sketch.
Great work..thanks.
Re: IR Sender/Receiver built, now what?
Posted: Sunday 06 September 2015 15:22
by gizmocuz
nice tutorial here... maybe should be in another section
http://www.makeuseof.com/tag/how-to-con ... ny-remote/
Re: IR Sender/Receiver built, now what?
Posted: Sunday 06 September 2015 17:08
by Justintime
I control my rgb led cove by Domoticz and Milight. Made several scenes with different colors. Linked the scene to a Mysensors command. And done. Works great.
Re: IR Sender/Receiver built, now what?
Posted: Sunday 06 September 2015 17:49
by gizmocuz

it was a nice example how to use custom ir codes with the harmony
in reality you would just use the harmony actions as the scene code, and fire you lighting settings

Re: IR Sender/Receiver built, now what?
Posted: Friday 19 February 2016 19:36
by blitzkneisser
Hi!
I have built a mysensors-IR node to control my ceiling fan. So far everything worked fine... I modified your sketch by adding my hashes, they were recognized and added in Domoticz as switches. Sending an IR command is also recognized by domoticz in the protocol.
Now I'd like to reverse that process and send IR commands via domoticz. I also converted the switches to "switch on" buttons. 2 Problems here, I hope someone can help me...
First thing: Hardware. Receiving works fine from the Button on the remote to domoticz. But when connecting the transmitter to Pin 3 like mentioned in the code (and disconnecting the receiver) I can't send anything as according to the serial monitor the node is constantly "receiving" some unknown IR hash, so sending is blocked. So... is it necessary to have the receiver
and the transmitter connected?
When connecting transmitter and receiver, there is no wrong hash received, but sending still does not seem to work. Could be a problem with the software, which leads me to the...
Second thing: Software. In the hash, the transmit IR part is reduced to sending a message in the serial monitor. That works fine, when I press the button in domoticz, the assigned hash is sent to the node. But how do I have to configure the rest of the send function? I checked the IR header file and tried
Code: Select all
irsend.send(HASH_CODE, ir_code, 32);
but that does not seem to work, the IR transmitter remains silent. So... what am I doing wrong?
And, as the receiver pin is defined in the code, do I also have to define the transmitter pin?
Thanks for reading until here, I'd welcome any help I get!
Re: IR Sender/Receiver built, now what?
Posted: Saturday 20 February 2016 8:12
by gizmocuz
A Hash is just a Hash and not the actual code.
So lets say you can make a capture of the real IR code, save this to disk, and also include it in your sketch
for this IR code, a hash is received, lets say the hash is 123456
this hash is used as ID in domoticz, and when domoticz sends a switch command to the node, it sends 123456
your node has to look if it is 123456, and then send the real IR code from above
Re: IR Sender/Receiver built, now what?
Posted: Saturday 20 February 2016 20:43
by blitzkneisser
Of course, silly me, that's not what a hash is all about. I guess I just hoped for an easy solution without much thinking.
I'll try to embedd the codes in my sketch, it's not that much of them...
Thanks
Re: IR Sender/Receiver built, now what?
Posted: Wednesday 16 March 2016 5:25
by wxop
gizmocuz wrote:
My current problems:
- making a sketch with ethernet/sd/ir with the default libraries results in a to large sketch to upload.
- at the moment i am using a custom ethernet library, this reduced a lot !, still to enough
- i replaced all strings in my sketch by encapsulating them in F("aaa"), this also reduced the sketch (user variables) a lot!, still not enough
- my test sketch now has 150 user variable space left, this produces a warning and can be dangerous
Of course a arduino mega would not have this problem, but... like to keep it small.
Why not take a cheap (3euros) NodeMcu (ESP8266 ).
http://www.aliexpress.com/item/NodeMcu- ... 29505.html
- it is small as a nano, (including wifi). and can even be smaller (other ESP8266 versions)
- flash it with Arduino firmware
- connect a ethernet shied , or maybe
just use the onboard wifi module 
- optionally add a mysensor radio module
-
ESP8266 has 4M of flash, YES 4 Mbytes !!!!!!
.. a "little" bit more than the usual 32k
some helpfull building info at mysensors :
http://www.mysensors.org/build/esp8266_gateway
HTH
Re: IR Sender/Receiver built, now what?
Posted: Wednesday 16 March 2016 8:36
by blitzkneisser
There's also the possibility to store user variables in program memory. I too had the user variable space warning - and the sketch did not work, but I had some program memory available. So I just stored the variables there, can be done with progmem.
I stored all my IR code sequences in program memory, see this thread for further details:
http://www.domoticz.com/forum/viewtopic ... 42&t=10678
Re: IR Sender/Receiver built, now what?
Posted: Wednesday 16 March 2016 8:38
by gizmocuz
True, but with an ethernet sketch, sdcard sketch and ir sketch and some code it was to much
even used alternative ethernet/sdcard libraries that where smaller