libraries / rc-switch-2.52 / examples / Webserver / Webserver.pdeon commit Added link to project report (97a3ba0)
   1/*
   2  A simple RCSwitch/Ethernet/Webserver demo
   3  
   4  http://code.google.com/p/rc-switch/
   5*/
   6
   7#include <SPI.h>
   8#include <Ethernet.h>
   9#include <RCSwitch.h>
  10
  11// Ethernet configuration
  12byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
  13byte ip[] = { 192,168,0, 2 };                        // IP Address
  14EthernetServer server(80);                           // Server Port 80
  15
  16// RCSwitch configuration
  17RCSwitch mySwitch = RCSwitch();
  18int RCTransmissionPin = 7;
  19
  20// More to do...
  21// You should also modify the processCommand() and 
  22// httpResponseHome() functions to fit your needs.
  23
  24
  25
  26/**
  27 * Setup
  28 */
  29void setup() {
  30  Ethernet.begin(mac, ip);
  31  server.begin();
  32  mySwitch.enableTransmit( RCTransmissionPin );
  33}
  34
  35/**
  36 * Loop
  37 */
  38void loop() {
  39  char* command = httpServer();
  40}
  41
  42/**
  43 * Command dispatcher
  44 */
  45void 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);
  54  }
  55}
  56
  57/**
  58 * HTTP Response with homepage
  59 */
  60void httpResponseHome(EthernetClient c) {
  61  c.println("HTTP/1.1 200 OK");
  62  c.println("Content-Type: text/html");
  63  c.println();
  64  c.println("<html>");
  65  c.println("<head>");
  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>");
  70  c.println("</head>");
  71  c.println("<body>");
  72  c.println(    "<h1>RCSwitch Webserver Demo</h1>");
  73  c.println(    "<ul>");
  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>");
  76  c.println(    "</ul>");
  77  c.println(    "<ul>");
  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>");
  80  c.println(    "</ul>");
  81  c.println(    "<hr>");
  82  c.println(    "<a href=\"http://code.google.com/p/rc-switch/\">http://code.google.com/p/rc-switch/</a>");
  83  c.println("</body>");
  84  c.println("</html>");
  85}
  86
  87/**
  88 * HTTP Redirect to homepage
  89 */
  90void httpResponseRedirect(EthernetClient c) {
  91  c.println("HTTP/1.1 301 Found");
  92  c.println("Location: /");
  93  c.println();
  94}
  95
  96/**
  97 * HTTP Response 414 error
  98 * Command must not be longer than 30 characters
  99 **/
 100void httpResponse414(EthernetClient c) {
 101  c.println("HTTP/1.1 414 Request URI too long");
 102  c.println("Content-Type: text/plain");
 103  c.println();
 104  c.println("414 Request URI too long");
 105}
 106
 107/**
 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).
 111 */
 112char*  httpServer() {
 113  EthernetClient client = server.available();
 114  if (client) {
 115    char sReturnCommand[32];
 116    int nCommandPos=-1;
 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);
 125          } else {
 126            processCommand(sReturnCommand);
 127            httpResponseRedirect(client);
 128          }
 129          break;
 130        }
 131        if (nCommandPos>-1) {
 132          sReturnCommand[nCommandPos++] = c;
 133        }
 134        if (c == '?' && nCommandPos == -1) {
 135          nCommandPos = 0;
 136        }
 137      }
 138      if (nCommandPos > 30) {
 139        httpResponse414(client);
 140        sReturnCommand[0] = '\0';
 141        break;
 142      }
 143    }
 144    if (nCommandPos!=-1) {
 145      sReturnCommand[nCommandPos] = '\0';
 146    }
 147    // give the web browser time to receive the data
 148    delay(1);
 149    client.stop();
 150    
 151    return sReturnCommand;
 152  }
 153  return '\0';
 154}