Last active
August 29, 2015 14:19
-
-
Save rogiervandenberg/1f923f0760225054946d to your computer and use it in GitHub Desktop.
Control lights with an Arduino remotely with 433Mhz, Simplified source using GET requests and without 7-segment LED display
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 <RemoteTransmitter.h> | |
#include <Ethernet.h> | |
#include <SPI.h> | |
#include <WebServer.h> | |
//Connections to the Arduino board | |
const int ledPin = 9; | |
const int transmitterPin = 8; | |
//Duration of a RF command | |
const unsigned int period = 326; | |
//RF codes for switching the lights | |
const unsigned long aOn = 175682; //Middenlamp | |
const unsigned long aOff = 175686; | |
const unsigned long bOn = 176654; //Hoeklamp | |
const unsigned long bOff = 176658; | |
const unsigned long cOn = 176978; //Rode lamp | |
const unsigned long cOff = 176982; | |
const unsigned long dOn = 177086; //Boekenkast | |
const unsigned long dOff = 177090; | |
const unsigned long eOn = 57584; //Voordeur | |
const unsigned long eOff = 57588; | |
//Bijhouden van de status van de lampen | |
boolean aIsAan = false; | |
boolean bIsAan = false; | |
boolean cIsAan = false; | |
boolean dIsAan = false; | |
boolean eIsAan = false; | |
//Stukjes output | |
P(message1) = | |
"<!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>" | |
"<p><br /><a class='positive' href='/allesaan'>Aan</a> " | |
"<a class='negative' href='/allesuit'>Uit</a> Schakel alle lampen" | |
"<p><br /><br /><a class='"; | |
P(message2) = "' href='/aOn'>Aan</a> <a class='"; | |
P(message3) = | |
"' href='/aOff'>Uit</a>Kerstboom</p>" | |
"<p><br /><br /><a class='"; | |
P(message4) = "' href='/bOn'>Aan</a> <a class='"; | |
P(message5) = | |
"' href='/bOff'>Uit</a>Hoek-lamp</p>" | |
"<p><br /><br /><a class='"; | |
P(message6) = "' href='/dOn'>Aan</a> <a class='"; | |
P(message7) = | |
"' href='/dOff'>Uit</a>Boekenkast</p>" | |
"<p><br /><br /><a class='"; | |
P(message8) = "' href='/eOn'>Aan</a> <a class='"; | |
P(message9) = | |
"' href='/eOff'>Uit</a>Voordeur</p>" | |
"<p><br /><br /><a class='"; | |
P(message10) = "' href='/cOn'>Aan</a> <a class='"; | |
P(message11) = | |
"' href='/cOff'>Uit</a>Rode lamp</p>"; | |
P(messageEnd) = | |
"</p></body></html>"; | |
P(positive) = "positive"; | |
P(positiveOn) = "positive-on"; | |
P(negative) = "negative"; | |
P(negativeOn) = "negative-on"; | |
//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); | |
long previousResetCheckMillis = 0; | |
long intervalResetCheck = 1800000; //half uur | |
//-------------------------------------------------------------------------------------------------------------- | |
/* 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. */ | |
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
if (type == WebServer::POST) | |
{ | |
Serial.println("ToggleLightCMD"); | |
bool repeat; | |
char name[16], value[16]; | |
do | |
{ | |
repeat = server.readPOSTparam(name, 16, value, 16); | |
Serial.print("Name: "); | |
Serial.println(name); | |
Serial.print("Value: "); | |
Serial.println(value); | |
if (strcmp(name, "command") == 0) | |
{ | |
switchLights(value); | |
} | |
} while (repeat); | |
server.httpSeeOther("/"); | |
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) | |
{ | |
server.printP(message1); | |
server.printP(aIsAan ? positiveOn : positive); | |
server.printP(message2); | |
server.printP(aIsAan ? negative : negativeOn); | |
server.printP(message3); | |
server.printP(bIsAan ? positiveOn : positive); | |
server.printP(message4); | |
server.printP(bIsAan ? negative : negativeOn); | |
server.printP(message5); | |
server.printP(dIsAan ? positiveOn : positive); | |
server.printP(message6); | |
server.printP(dIsAan ? negative : negativeOn); | |
server.printP(message7); | |
server.printP(eIsAan ? positiveOn : positive); | |
server.printP(message8); | |
server.printP(eIsAan ? negative : negativeOn); | |
server.printP(message9); | |
server.printP(cIsAan ? positiveOn : positive); | |
server.printP(message10); | |
server.printP(cIsAan ? negative : negativeOn); | |
server.printP(message11); | |
server.printP(messageEnd); | |
} | |
} | |
void allOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("aOn"); | |
switchLights("bOn"); | |
switchLights("cOn"); | |
switchLights("dOn"); | |
switchLights("eOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void allOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("aOff"); | |
switchLights("bOff"); | |
switchLights("cOff"); | |
switchLights("dOff"); | |
switchLights("eOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void aOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("aOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void aOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("aOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void bOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("bOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void bOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("bOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void cOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("cOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void cOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("cOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void dOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("dOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void dOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("dOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void eOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("eOn"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void eOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) | |
{ | |
switchLights("eOff"); | |
server.httpSeeOther("/"); | |
return; | |
} | |
void switchLights(String value) { | |
if (value == "aOn") { sendSwitchSignal(aOn); aIsAan = true; } | |
if (value == "aOff") { sendSwitchSignal(aOff); aIsAan = false; } | |
if (value == "bOn") { sendSwitchSignal(bOn); bIsAan = true; } | |
if (value == "bOff") { sendSwitchSignal(bOff); bIsAan = false; } | |
if (value == "cOn") { sendSwitchSignal(cOn); cIsAan = true; } | |
if (value == "cOff") { sendSwitchSignal(cOff); cIsAan = false; } | |
if (value == "dOn") { sendSwitchSignal(dOn); dIsAan = true; } | |
if (value == "dOff") { sendSwitchSignal(dOff); dIsAan = false; } | |
if (value == "eOn") { sendSwitchSignal(eOn); eIsAan = true; } | |
if (value == "eOff") { sendSwitchSignal(eOff); eIsAan = false; } | |
} | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
Ethernet.begin(mac, ip); | |
webserver.setDefaultCommand(&defaultCmd); | |
webserver.addCommand("allesaan", &allOnCmd); | |
webserver.addCommand("allesuit", &allOffCmd); | |
webserver.addCommand("aOn", &aOnCmd); | |
webserver.addCommand("aOff", &aOffCmd); | |
webserver.addCommand("bOn", &bOnCmd); | |
webserver.addCommand("bOff", &bOffCmd); | |
webserver.addCommand("cOn", &cOnCmd); | |
webserver.addCommand("cOff", &cOffCmd); | |
webserver.addCommand("dOn", &dOnCmd); | |
webserver.addCommand("dOff", &dOffCmd); | |
webserver.addCommand("eOn", &eOnCmd); | |
webserver.addCommand("eOff", &eOffCmd); | |
webserver.begin(); | |
} | |
void loop() { | |
//process incoming connections one at a time forever | |
webserver.processConnection(); | |
unsigned long currentMillis = millis(); | |
if(currentMillis - previousResetCheckMillis > intervalResetCheck) { | |
previousResetCheckMillis = currentMillis; | |
resetTheLights(); | |
} | |
} | |
void resetTheLights() | |
{ | |
if(aIsAan == false) { switchLights("aOff"); } | |
if(bIsAan == false) { switchLights("bOff"); } | |
if(cIsAan == false) { switchLights("cOff"); } | |
if(dIsAan == false) { switchLights("dOff"); } | |
if(eIsAan == false) { switchLights("eOff"); } | |
} | |
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); | |
RemoteTransmitter::sendCode(transmitterPin, actionCode, period, 3); | |
// RemoteReceiver::enable(); | |
digitalWrite(ledPin, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment