1 | #include <RCSwitch.h>
|
2 |
|
3 | #include <SPI.h>
|
4 | #include <Ethernet.h>
|
5 |
|
6 | // MAC address from Ethernet shield sticker under board
|
7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
8 | IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
|
9 | EthernetServer server(80); // create a server at port 80
|
10 | RCSwitch mySwitch = RCSwitch();
|
11 | String HTTP_req; // stores the HTTP request
|
12 | void setup()
|
13 | {
|
14 | Ethernet.begin(mac, ip); // initialize Ethernet device
|
15 | server.begin(); // start to listen for clients
|
16 | Serial.begin(9600); // for diagnostics
|
17 | Serial.print("server is at ");
|
18 | Serial.println(Ethernet.localIP());
|
19 | mySwitch.enableTransmit(5);
|
20 | }
|
21 |
|
22 | void loop()
|
23 | {
|
24 | EthernetClient client = server.available(); // try to get client
|
25 |
|
26 | if (client) { // got client?
|
27 | boolean currentLineIsBlank = true;
|
28 | while (client.connected()) {
|
29 | if (client.available()) { // client data available to read
|
30 | char c = client.read(); // read 1 byte (character) from client
|
31 | HTTP_req += c; // save the HTTP request 1 char at a time
|
32 | // last line of client request is blank and ends with \n
|
33 | // respond to client only after last line received
|
34 | if (c == '\n' && currentLineIsBlank) {
|
35 | // send a standard http response header
|
36 | client.println("HTTP/1.1 200 OK");
|
37 | client.println("Content-Type: text/html");
|
38 | client.println("Connection: close");
|
39 | client.println();
|
40 | // send web page
|
41 | client.println("<!DOCTYPE html>");
|
42 | client.println("<html>");
|
43 | client.println("<head>");
|
44 | client.println("<title>Arduino Homecontrol</title>");
|
45 | client.println("</head>");
|
46 | client.println("<body>");
|
47 | client.println("<h1>SWITCH</h1>");
|
48 | client.println("<p>Click to switch on and off.</p>");
|
49 | client.println("<form method=\"get\">");
|
50 | ProcessCheckbox(client,0);
|
51 | client.println("</form>");
|
52 | client.println("<form method=\"get\">");
|
53 | ProcessCheckbox(client,1);
|
54 | client.println("</form>");
|
55 | client.println("</body>");
|
56 | client.println("</html>");
|
57 | //Serial.print(HTTP_req);
|
58 | HTTP_req = ""; // finished with request, empty string
|
59 | break;
|
60 | }
|
61 | // every line of text received from the client ends with \r\n
|
62 | if (c == '\n') {
|
63 | // last character on line of received text
|
64 | // starting new line with next character read
|
65 | currentLineIsBlank = true;
|
66 | }
|
67 | else if (c != '\r') {
|
68 | // a text character was received from client
|
69 | currentLineIsBlank = false;
|
70 | }
|
71 | } // end if (client.available())
|
72 | } // end while (client.connected())
|
73 | delay(1); // give the web browser time to receive the data
|
74 | client.stop(); // close the connection
|
75 | } // end if (client)
|
76 |
|
77 | }
|
78 |
|
79 | int stat[]={0,0};
|
80 | // switch LED and send back HTML for LED checkbox
|
81 | void ProcessCheckbox(EthernetClient cl,int num){
|
82 |
|
83 | int sender=0;
|
84 | String box="s";
|
85 | box+=num;
|
86 | box+="=111";
|
87 | if (HTTP_req.indexOf(box) > -1&&HTTP_req.indexOf(box) <7) {
|
88 | if(stat[num]){
|
89 | stat[num]=0;
|
90 | }else{
|
91 | stat[num]=1;
|
92 | }
|
93 |
|
94 | }
|
95 | if (stat[num]) { // switch LED on
|
96 |
|
97 | if(num==0){
|
98 | mySwitch.send(21,24);
|
99 | }else{
|
100 | mySwitch.send(16405,24);
|
101 | }
|
102 |
|
103 | // checkbox is checked
|
104 | cl.print("<input type=\"checkbox\" name=\"s");
|
105 | cl.print(num);
|
106 | cl.print("\" value=\"111\" \onclick=\"submit();\" checked>Switch");
|
107 | cl.println(num+1);
|
108 | }
|
109 | else { // switch LED off
|
110 |
|
111 | if(num==0){
|
112 | mySwitch.send(20,24);
|
113 | }else{
|
114 | mySwitch.send(16404,24);
|
115 | }
|
116 |
|
117 | // checkbox is unchecked
|
118 | cl.print("<input type=\"checkbox\" name=\"s");
|
119 | cl.print(num);
|
120 | cl.print("\" value=\"111\" \onclick=\"submit();\">Switch");
|
121 | cl.println(num+1);
|
122 | }
|
123 | }
|