// Enable debug prints
//#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <IRremote.h>
unsigned int base[] = {4500, 4450, 600, 550, 550, 550, 550, 550, 550, 1650, 550, 550, 550, 550, 550, 1650, 550, 550, 4500, 4450, 600, 550, 600, 550, 600, 550, 600, 1600, 600, 550, 600, 550, 600, 1600, 600, 500, 4550, 4400, 650, 500, 600, 550, 600, 550, 600, 1600, 600, 550, 600, 550, 600, 1600, 600, 500, 4500, 4450, 600, 550, 600, 550, 600, 550, 600, 1600, 550, 550, 550, 550, 550, 1650, 550, 550, 4500, 4450, 600, 550, 550, 550, 550, 550, 550, 1650, 550, 550, 550, 550, 550, 1650, 550};
unsigned int clean[] = {4550, 4400, 600, 550, 600, 1600, 600, 550, 600, 1600, 600, 550, 600, 1600, 600, 550, 600, 550, 4450, 4450, 600, 550, 550, 1650, 600, 550, 600, 1650, 600, 550, 550, 1650, 600, 550, 600, 550, 4450, 4450, 600, 550, 600, 1650, 600, 550, 550, 1650, 600, 550, 550, 1650, 600, 550, 550, 550, 4450, 4450, 600, 550, 600, 1650, 600, 550, 550, 1700, 550, 600, 550, 1700, 600, 550, 600, 550, 4450, 4450, 600, 550, 600, 1650, 550, 600, 550, 1700, 550, 600, 500, 1700, 550, 600, 550};
#define CHILD_1 3 // childId
IRsend irsend;
bool initialValueSent = false;
MyMessage msg(CHILD_1, V_STATUS);
void setup()
{
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("IR Sensor", "1.0");
// Register a sensors to Use binary light for test purposes.
present(CHILD_1, S_BINARY);
}
void loop()
{
if (!initialValueSent) {
Serial.println("Sending initial value");
send(msg.set(0));
Serial.println("Requesting initial value from controller");
request(CHILD_1, V_STATUS);
wait(2000, C_SET, V_STATUS);
}
}
void receive(const MyMessage &message) {
int khz = 38; // 38kHz carrier frequency for the NEC protocol
if (message.type == V_STATUS) {
if (!initialValueSent) {
Serial.println("Receiving initial value from controller");
initialValueSent = true;
}
int incomingRelayStatus = message.getInt();
if (incomingRelayStatus) {
irsend.sendRaw(base, sizeof(base) / sizeof(base[0]), khz);
} else {
irsend.sendRaw(clean, sizeof(clean) / sizeof(clean[0]), khz);
}
// Send state as feedback to controller.
send(msg.set(incomingRelayStatus?1:0));
}
}