Created
March 22, 2017 05:29
-
-
Save theepicsnail/c3c45df83187556ff6cf402fc707aba1 to your computer and use it in GitHub Desktop.
Send RF signals via web server
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 <SPI.h> | |
#include <Ethernet.h> | |
#include <RCSwitch.h> | |
// Switch settings | |
typedef struct Config{ | |
String name; | |
long code[2]; | |
}; | |
Config outlets [5] = { | |
// Name Off On | |
{"Switch 1", {1070396, 1070387}}, | |
{"Switch 2", {1070540, 1070531}}, | |
{"Switch 3", {1070860, 1070851}}, | |
{"Switch 4", {1072396, 1072387}}, | |
{"Switch 5", {1078540, 1078531}}}; | |
// Ethernet settings | |
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x02, 0xD9}; | |
RCSwitch mySwitch(); | |
EthernetServer server(80); | |
EthernetClient client; | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
// Setup the transmitter | |
mySwitch.enableTransmit(8); | |
mySwitch.setPulseLength(185); | |
// Setup the network card (and http server) | |
Ethernet.begin(mac); | |
server.begin(); | |
Serial.print("server is at "); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() { | |
client = server.available(); | |
if (!client) return; | |
handleRequest(); | |
client.stop(); | |
} | |
byte request[9]; // "GET /?X=Y" | |
void handleRequest(){ | |
if (client.readBytes(request, 9) != 9)return; | |
if (request[7] == '=') | |
setLight(request[6]-'0', request[8]-'0'); | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println("Connection: close"); // the connection will be closed after completion of the response | |
client.println(); | |
client.print("<!DOCTYPE HTML><html><head><title>Switchomatic</title><link rel=\"icon\" href=\"data:;base64,iVBORw0KGgo=\"></head><form><table><tbody>"); | |
for(int i = 0; i < 5 ; i++) { | |
client.print("<tr><td>");client.print(outlets[i].name); | |
client.print("</td><td><button name='");client.print(i); | |
client.print("' value='1'>ON</button></td><td><button name='");client.print(i); | |
client.print("' value='0'>OFF</button></tr>"); | |
} | |
client.print("</tbody></table></form></html>"); | |
} | |
void setLight(int light, bool set) { | |
if(light < 0 || light >= 5) return; | |
Serial.print(light); | |
Serial.print(" -> "); | |
Serial.println(set); | |
mySwitch.send(outlets[light].code[set], 24); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment