Webserver.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. A simple RCSwitch/Ethernet/Webserver demo
  3. https://github.com/sui77/rc-switch/
  4. */
  5. #include <SPI.h>
  6. #include <Ethernet.h>
  7. #include <RCSwitch.h>
  8. // Ethernet configuration
  9. uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
  10. uint8_t ip[] = { 192,168,0, 2 }; // IP Address
  11. EthernetServer server(80); // Server Port 80
  12. // RCSwitch configuration
  13. RCSwitch mySwitch = RCSwitch();
  14. int RCTransmissionPin = 7;
  15. // More to do...
  16. // You should also modify the processCommand() and
  17. // httpResponseHome() functions to fit your needs.
  18. /**
  19. * Setup
  20. */
  21. void setup() {
  22. Ethernet.begin(mac, ip);
  23. server.begin();
  24. mySwitch.enableTransmit( RCTransmissionPin );
  25. }
  26. /**
  27. * Loop
  28. */
  29. void loop() {
  30. char* command = httpServer();
  31. }
  32. /**
  33. * Command dispatcher
  34. */
  35. void processCommand(char* command) {
  36. if (strcmp(command, "1-on") == 0) {
  37. mySwitch.switchOn(1,1);
  38. } else if (strcmp(command, "1-off") == 0) {
  39. mySwitch.switchOff(1,1);
  40. } else if (strcmp(command, "2-on") == 0) {
  41. mySwitch.switchOn(1,2);
  42. } else if (strcmp(command, "2-off") == 0) {
  43. mySwitch.switchOff(1,2);
  44. }
  45. }
  46. /**
  47. * HTTP Response with homepage
  48. */
  49. void httpResponseHome(EthernetClient c) {
  50. c.println("HTTP/1.1 200 OK");
  51. c.println("Content-Type: text/html");
  52. c.println();
  53. c.println("<html>");
  54. c.println("<head>");
  55. c.println( "<title>RCSwitch Webserver Demo</title>");
  56. c.println( "<style>");
  57. c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
  58. c.println( "</style>");
  59. c.println("</head>");
  60. c.println("<body>");
  61. c.println( "<h1>RCSwitch Webserver Demo</h1>");
  62. c.println( "<ul>");
  63. c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
  64. c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
  65. c.println( "</ul>");
  66. c.println( "<ul>");
  67. c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
  68. c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
  69. c.println( "</ul>");
  70. c.println( "<hr>");
  71. c.println( "<a href=\"https://github.com/sui77/rc-switch/\">https://github.com/sui77/rc-switch/</a>");
  72. c.println("</body>");
  73. c.println("</html>");
  74. }
  75. /**
  76. * HTTP Redirect to homepage
  77. */
  78. void httpResponseRedirect(EthernetClient c) {
  79. c.println("HTTP/1.1 301 Found");
  80. c.println("Location: /");
  81. c.println();
  82. }
  83. /**
  84. * HTTP Response 414 error
  85. * Command must not be longer than 30 characters
  86. **/
  87. void httpResponse414(EthernetClient c) {
  88. c.println("HTTP/1.1 414 Request URI too long");
  89. c.println("Content-Type: text/plain");
  90. c.println();
  91. c.println("414 Request URI too long");
  92. }
  93. /**
  94. * Process HTTP requests, parse first request header line and
  95. * call processCommand with GET query string (everything after
  96. * the ? question mark in the URL).
  97. */
  98. char* httpServer() {
  99. EthernetClient client = server.available();
  100. if (client) {
  101. char sReturnCommand[32];
  102. int nCommandPos=-1;
  103. sReturnCommand[0] = '\0';
  104. while (client.connected()) {
  105. if (client.available()) {
  106. char c = client.read();
  107. if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
  108. sReturnCommand[nCommandPos] = '\0';
  109. if (strcmp(sReturnCommand, "\0") == 0) {
  110. httpResponseHome(client);
  111. } else {
  112. processCommand(sReturnCommand);
  113. httpResponseRedirect(client);
  114. }
  115. break;
  116. }
  117. if (nCommandPos>-1) {
  118. sReturnCommand[nCommandPos++] = c;
  119. }
  120. if (c == '?' && nCommandPos == -1) {
  121. nCommandPos = 0;
  122. }
  123. }
  124. if (nCommandPos > 30) {
  125. httpResponse414(client);
  126. sReturnCommand[0] = '\0';
  127. break;
  128. }
  129. }
  130. if (nCommandPos!=-1) {
  131. sReturnCommand[nCommandPos] = '\0';
  132. }
  133. // give the web browser time to receive the data
  134. delay(1);
  135. client.stop();
  136. return sReturnCommand;
  137. }
  138. return '\0';
  139. }