2 A simple RCSwitch/Ethernet/Webserver demo
4 https://github.com/sui77/rc-switch/
11 // Ethernet configuration
12 uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
13 uint8_t ip[] = { 192,168,0, 2 }; // IP Address
14 EthernetServer server(80); // Server Port 80
16 // RCSwitch configuration
17 RCSwitch mySwitch = RCSwitch();
18 int RCTransmissionPin = 7;
21 // You should also modify the processCommand() and
22 // httpResponseHome() functions to fit your needs.
30 Ethernet.begin(mac, ip);
32 mySwitch.enableTransmit( RCTransmissionPin );
39 char* command = httpServer();
45 void processCommand(char* command) {
46 if (strcmp(command, "1-on") == 0) {
47 mySwitch.switchOn(1,1);
48 } else if (strcmp(command, "1-off") == 0) {
49 mySwitch.switchOff(1,1);
50 } else if (strcmp(command, "2-on") == 0) {
51 mySwitch.switchOn(1,2);
52 } else if (strcmp(command, "2-off") == 0) {
53 mySwitch.switchOff(1,2);
58 * HTTP Response with homepage
60 void httpResponseHome(EthernetClient c) {
61 c.println("HTTP/1.1 200 OK");
62 c.println("Content-Type: text/html");
66 c.println( "<title>RCSwitch Webserver Demo</title>");
67 c.println( "<style>");
68 c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
69 c.println( "</style>");
72 c.println( "<h1>RCSwitch Webserver Demo</h1>");
74 c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
75 c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
78 c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
79 c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
82 c.println( "<a href=\"https://github.com/sui77/rc-switch/\">https://github.com/sui77/rc-switch/</a>");
88 * HTTP Redirect to homepage
90 void httpResponseRedirect(EthernetClient c) {
91 c.println("HTTP/1.1 301 Found");
92 c.println("Location: /");
97 * HTTP Response 414 error
98 * Command must not be longer than 30 characters
100 void httpResponse414(EthernetClient c) {
101 c.println("HTTP/1.1 414 Request URI too long");
102 c.println("Content-Type: text/plain");
104 c.println("414 Request URI too long");
108 * Process HTTP requests, parse first request header line and
109 * call processCommand with GET query string (everything after
110 * the ? question mark in the URL).
113 EthernetClient client = server.available();
115 char sReturnCommand[32];
117 sReturnCommand[0] = '\0';
118 while (client.connected()) {
119 if (client.available()) {
120 char c = client.read();
121 if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
122 sReturnCommand[nCommandPos] = '\0';
123 if (strcmp(sReturnCommand, "\0") == 0) {
124 httpResponseHome(client);
126 processCommand(sReturnCommand);
127 httpResponseRedirect(client);
131 if (nCommandPos>-1) {
132 sReturnCommand[nCommandPos++] = c;
134 if (c == '?' && nCommandPos == -1) {
138 if (nCommandPos > 30) {
139 httpResponse414(client);
140 sReturnCommand[0] = '\0';
144 if (nCommandPos!=-1) {
145 sReturnCommand[nCommandPos] = '\0';
147 // give the web browser time to receive the data
151 return sReturnCommand;