This is the sketch i'm using for controlling a number of sockets around the house.
/* RF433Mhz Transmitter node:
This sketch allows On-Off-Keying (OOK) control of four 433Mhz relay outlets
which utilize the common PT2262 encoding chip for signal transmission. The sketch could
be easily expanded to include additional outlets. The sensor node consists of a nano
connected to a NRF24L01 and a 433Mhz transmitter connected to pin 3, 5V and Gnd. The
transmitter can run off of 3.3V as well.
The sketch is based on the MySensors project (http://www.mysensors.org).
Submitted by Dwalt.
*/
// Include related libraries
#include <MySensor.h>
#include <SPI.h>
#include <RF24.h>
#include <RCSwitch.h>
// Define Constants
#define RF433_CHILD_ID 0
#define NUMBER_OF_OUTLETS 4 // Each outlet will have 2 OOK codes
//#define SEND_DATA 3
#define CODE_1On 6287582
#define CODE_1Off 6287574
#define CODE_2On 6287580
#define CODE_2Off 6287572
#define CODE_3On 6287578
#define CODE_3Off 6287570
#define CODE_4On 6287577
#define CODE_4Off 6287569
MySensor gw;
RCSwitch mySwitch = RCSwitch();
void setup()
{
mySwitch.enableTransmit(3);
Serial.begin(115200); // Not sure why this was included
// The node is mains powered, so why not make it a repeater.
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to gateway
gw.sendSketchInfo("RF433", "1.1");
// Register outlets to gw (they will be created as child devices)
for(int i=0; i<NUMBER_OF_OUTLETS;i++) {
gw.present(i+1, S_LIGHT);
}
}
void loop() {
gw.process();
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_LIGHT) {
int incomingLightState = message.getBool();
int incomingOutlet = message.sensor;
Serial.print("Outlet #: ");
Serial.println(message.sensor);
Serial.print("Command: ");
Serial.println(message.getBool());
if (incomingOutlet==1) {
if (incomingLightState==1) {
// Turn on socket 1
Serial.println("Turn on Socket 1");
mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
delay(50);
}
if (incomingLightState==0) {
// Turn off socket 1
Serial.println("Turn off Socket 1");
mySwitch.send(CODE_1Off, 24);
delay(50);
}
}
if (incomingOutlet==2) {
if (incomingLightState==1) {
// Turn on socket 2
Serial.println("Turn on Socket 2");
mySwitch.send(CODE_2On, 24);
delay(50);
}
if (incomingLightState==0) {
// Turn off socket 2
Serial.println("Turn off Socket 2");
mySwitch.send(CODE_2Off, 24);
delay(50);
}
}
if (incomingOutlet==3) {
if (incomingLightState==1) {
// Turn on socket 3
Serial.println("Turn on Socket 3");
mySwitch.send(CODE_3On, 24);
delay(50);
}
if (incomingLightState==0) {
// Turn off socket 3
Serial.println("Turn off Socket 3");
mySwitch.send(CODE_3Off, 24);
delay(50);
}
}
if (incomingOutlet==4) {
if (incomingLightState==1) {
// Turn on socket 4
Serial.println("Turn on Socket 4");
mySwitch.send(CODE_4On, 24);
delay(50);
}
if (incomingLightState==0) {
// Turn off socket 4
Serial.println("Turn off Socket 4");
mySwitch.send(CODE_4Off, 24);
delay(50);
}
}
}
delay(50);
}