Created
April 21, 2015 15:42
-
-
Save rogiervandenberg/1d9c138bbc44524c004c to your computer and use it in GitHub Desktop.
Control lights with an Arduino remotely with 433Mhz, Source code as used in the video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <RemoteSwitch.h> | |
#include <RemoteReceiver.h> | |
#include <Ethernet.h> | |
#include <SPI.h> | |
#include <WebServer.h> | |
#include <EEPROM.h> | |
#include <EEPROMAnything.h> | |
//Connections to the Arduino board | |
const int ledPin = 9; | |
const int transmitterPin = 8; | |
//Duration of an RF-command | |
const unsigned int period = 326; | |
//RF codes for switching the lights | |
unsigned long aOn; | |
unsigned long aOff; | |
unsigned long bOn; | |
unsigned long bOff; | |
unsigned long cOn; | |
unsigned long cOff; | |
unsigned long dOn; | |
unsigned long dOff; | |
int currentLearning = 0; | |
//Hier wordt nog niets mee gedaan | |
boolean aIsAan = false; | |
boolean bIsAan = false; | |
boolean cIsAan = false; | |
/* Settings voor de CHIP en het display | |
The 74HC595 using a protocol called SPI (for more details http://www.arduino.cc/en/Tutorial/ShiftOut) | |
Which has three pins | |
*/ | |
int data = 5; | |
int clock = 3; | |
int latch = 4; | |
//Used for single LED manipulation | |
int ledState = 0; | |
const int ON = HIGH; | |
const int OFF = LOW; | |
//Webserver settings | |
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
static uint8_t ip[] = { 192, 168, 0, 201 }; | |
#define PREFIX "/" | |
WebServer webserver(PREFIX, 80); | |
/* This command is set as the default command for the server. It | |
* handles both GET and POST requests. For a GET, it returns a simple | |
* page with some buttons. For a POST, it saves the value posted to | |
* the buzzDelay variable, affecting the output of the speaker */ | |
void toggleLightCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
if (type == WebServer::POST) | |
{ | |
bool repeat; | |
char name[16], value[16]; | |
do | |
{ | |
repeat = server.readPOSTparam(name, 16, value, 16); | |
if (strcmp(name, "command") == 0) | |
{ | |
switchLights(value); | |
} | |
else if (strcmp(name, "setup") == 0) | |
{ | |
enableReceiver(); | |
} | |
} while (repeat); | |
server.httpSeeOther(PREFIX); | |
return; | |
} | |
/* for a GET or HEAD, send the standard "it's all OK headers" */ | |
server.httpSuccess(); | |
/* we don't output the body for a HEAD request */ | |
if (type == WebServer::GET) | |
{ | |
/* store the HTML in program memory using the P macro */ | |
P(message) = | |
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">" | |
"<html><head>" | |
"<title>Lampen Woonkamer</title>" | |
"<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"http://rogier.nu/l/library/download/554?format=save_to_disk&ext=.css\" />" | |
"<meta name=\"viewport\" content=\"width=320\">" | |
"</head>" | |
"<body><h1>Schakel de verlichting</h1><form action='/' method='POST'>" | |
"<p>Midden-lamp<br /><button class='positive' name='command' value='aOn'>Aan</button> <button class='negative' name='command' value='aOff'>Uit</button></p>" | |
"<p>Hoek-lamp<br /><button class='positive' name='command' value='bOn'>Aan</button> <button class='negative' name='command' value='bOff'>Uit</button></p>" | |
"<p>Rode lamp<br /><button class='positive' name='command' value='cOn'>Aan</button> <button class='negative' name='command' value='cOff'>Uit</button></p>" | |
"<p><button class='neutral' name='setup' value='setup'>Setup</button> </p></form>" | |
"</body></html>"; | |
server.printP(message); | |
} | |
} | |
void switchLights(String value) { | |
if (value == "aOn") { sendSwitchSignal(aOn); } | |
if (value == "aOff") { sendSwitchSignal(aOff); } | |
if (value == "bOn") { sendSwitchSignal(bOn); } | |
if (value == "bOff") { sendSwitchSignal(bOff); } | |
if (value == "cOn") { sendSwitchSignal(cOn); } | |
if (value == "cOff") { sendSwitchSignal(cOff); } | |
if (value == "dOn") { sendSwitchSignal(dOn); } | |
if (value == "dOff") { sendSwitchSignal(dOff); } | |
} | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
initializeCodes(); | |
RemoteReceiver::init(0, 3, receiveCode); | |
//Disable the receiver; otherwise it might pick up the retransmit as well. | |
RemoteReceiver::disable(); | |
pinMode(data, OUTPUT); | |
pinMode(clock, OUTPUT); | |
pinMode(latch, OUTPUT); | |
Ethernet.begin(mac, ip); | |
webserver.setDefaultCommand(&toggleLightCmd); | |
webserver.begin(); | |
} | |
void loop() { | |
//process incoming connections one at a time forever | |
webserver.processConnection(); | |
/* Enable this to test the display | |
int delayTime = 1000; //the number of milliseconds to delay between LED updates | |
for(int i = 0; i < 10; i++){ | |
setNumber(i); | |
delay(delayTime); | |
} | |
*/ | |
} | |
void initializeCodes() { | |
unsigned long lo = 0xFFFFFFFF; | |
int n = 0; | |
//RF codes for switching the lights | |
n = EEPROM_readAnything(20, lo); | |
aOn = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(25, lo); | |
aOff = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(30, lo); | |
bOn = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(35, lo); | |
bOff = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(40, lo); | |
cOn = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(45, lo); | |
cOff = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(50, lo); | |
dOn = lo; | |
lo = 0xFFFFFFFF; | |
n = EEPROM_readAnything(55, lo); | |
dOff = lo = 0xFFFFFFFF; | |
Serial.print("Lamp 1 aan = "); | |
Serial.println(aOn); | |
} | |
void storeCodes(int codeNumber, unsigned long code) { | |
Serial.print("Storing code "); | |
Serial.print(code); | |
Serial.print(" , in number "); | |
Serial.println(codeNumber); | |
if (codeNumber == 1) { EEPROM_writeAnything(20, code); } | |
if (codeNumber == 2) { EEPROM_writeAnything(25, code); } | |
if (codeNumber == 3) { EEPROM_writeAnything(30, code); } | |
if (codeNumber == 4) { EEPROM_writeAnything(35, code); } | |
if (codeNumber == 5) { EEPROM_writeAnything(40, code); } | |
if (codeNumber == 6) { EEPROM_writeAnything(45, code); } | |
if (codeNumber == 7) { EEPROM_writeAnything(50, code); } | |
if (codeNumber == 8) { EEPROM_writeAnything(55, code); } | |
} | |
void sendSwitchSignal(unsigned long actionCode) { | |
digitalWrite(ledPin, HIGH); | |
//Disable the receiver; otherwise it might pick up the retransmit as well. | |
// RemoteReceiver::disable(); | |
//Need interrupts for delay | |
interrupts(); | |
unsigned long code; | |
//Copy the received code. | |
code = actionCode & 0xFFFFF; //truncate to 20 bits for show; receivedCode is never more than 20 bits.. | |
//Add the period duration to the code. Range: [0..511] (9 bit) | |
code |= (unsigned long)period << 23; | |
//Add the number of repeats to the code. Range: [0..7] (3 bit). The actual number of repeats will be 2^(repeats), | |
//in this case 8 | |
code |= 3L << 20; | |
RemoteSwitch::sendTelegram(code,transmitterPin); | |
digitalWrite(ledPin, LOW); | |
} | |
void enableReceiver() | |
{ | |
RemoteReceiver::enable(); | |
digitalWrite(ledPin, HIGH); | |
setNumber(++currentLearning); | |
} | |
void receiveCode(unsigned long receivedCode, unsigned int period) { | |
//Need interrupts for delay | |
interrupts(); | |
unsigned long code; | |
//Copy the received code. | |
code = receivedCode & 0xFFFFF; //truncate to 20 bits for show; receivedCode is never more than 20 bits.. | |
//Add the period duration to the code. Range: [0..511] (9 bit) | |
code |= (unsigned long)period << 23; | |
//Add the number of repeats to the code. Range: [0..7] (3 bit). The actual number of repeats will be 2^(repeats), | |
//in this case 8 | |
code |= 3L << 20; | |
storeCodes(currentLearning, code); | |
digitalWrite(ledPin, LOW); | |
delay(1000); | |
digitalWrite(ledPin, HIGH); | |
setNumber(++currentLearning); | |
if(currentLearning == 8) { | |
for(int i = 0; i < 10; i++){ | |
digitalWrite(ledPin, LOW); | |
delay(100); | |
digitalWrite(ledPin, HIGH); | |
delay(100); | |
} | |
currentLearning = 0; | |
setNumber(-1); | |
RemoteReceiver::disable(); | |
//Set the new codes according to memory | |
initializeCodes(); | |
} | |
} | |
/* LED DISPLAY CONTROL *******************************************************************************/ | |
/* | |
* updateLEDs() - sends the LED states set in ledStates to the 74HC595 | |
* sequence | |
*/ | |
void updateLEDs(int value){ | |
digitalWrite(latch, LOW); //Pulls the chips latch low | |
shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register | |
digitalWrite(latch, HIGH); //Pulls the latch high displaying the data | |
} | |
//These are used in the bitwise math that we use to change individual LEDs | |
//For more details http://en.wikipedia.org/wiki/Bitwise_operation | |
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; | |
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111}; | |
/* | |
* changeLED(int led, int state) - changes an individual LED | |
* LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON | |
*/ | |
void changeLED(int led, int state){ | |
ledState = ledState & masks[led]; //clears ledState of the bit we are addressing | |
if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to ledState | |
updateLEDs(ledState); //send the new LED state to the shift register | |
} | |
//Changing the 7-segment display | |
void setNumber(int number) | |
{ | |
changeLED(0, LOW); | |
changeLED(1, LOW); | |
changeLED(2, LOW); | |
changeLED(3, LOW); | |
changeLED(4, LOW); | |
changeLED(5, LOW); | |
changeLED(6, LOW); | |
if(number == 0) { | |
changeLED(0, HIGH); | |
changeLED(1, HIGH); | |
changeLED(2, HIGH); | |
changeLED(4, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 1) { | |
changeLED(2, HIGH); | |
changeLED(5, HIGH); | |
} | |
else if (number == 2) { | |
changeLED(0, HIGH); | |
changeLED(2, HIGH); | |
changeLED(3, HIGH); | |
changeLED(4, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 3) { | |
changeLED(0, HIGH); | |
changeLED(2, HIGH); | |
changeLED(3, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 4) { | |
changeLED(1, HIGH); | |
changeLED(2, HIGH); | |
changeLED(3, HIGH); | |
changeLED(5, HIGH); | |
} | |
else if (number == 5) { | |
changeLED(0, HIGH); | |
changeLED(1, HIGH); | |
changeLED(3, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 6) { | |
changeLED(0, HIGH); | |
changeLED(1, HIGH); | |
changeLED(3, HIGH); | |
changeLED(4, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 7) { | |
changeLED(0, HIGH); | |
changeLED(2, HIGH); | |
changeLED(5, HIGH); | |
} | |
else if (number == 8) { | |
changeLED(0, HIGH); | |
changeLED(1, HIGH); | |
changeLED(2, HIGH); | |
changeLED(3, HIGH); | |
changeLED(4, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == 9) { | |
changeLED(0, HIGH); | |
changeLED(1, HIGH); | |
changeLED(2, HIGH); | |
changeLED(3, HIGH); | |
changeLED(5, HIGH); | |
changeLED(6, HIGH); | |
} | |
else if (number == -1) { | |
changeLED(0, LOW); | |
changeLED(1, LOW); | |
changeLED(2, LOW); | |
changeLED(3, LOW); | |
changeLED(4, LOW); | |
changeLED(5, LOW); | |
changeLED(6, LOW); | |
} | |
else if (number == -2) { | |
changeLED(0, LOW); | |
changeLED(3, LOW); | |
changeLED(6, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment