Posted By

icotoi on 12/02/11


Tagged

webserver arduino


Versions (?)

WebServer


 / Published in: C++
 

Arduino webserver

  1. void doWebServerLoop() {
  2. // listen for incoming clients
  3. Client client = server.available();
  4. unsigned char have_http_get = 0;
  5. unsigned char i= 0;
  6. if (client) {
  7. // an http request ends with a blank line
  8. boolean currentLineIsBlank = true;
  9. while (client.connected()) {
  10. if (client.available()) {
  11. char c = client.read();
  12. if (c == '?') {
  13. have_http_get = 1;
  14. char buf[10];
  15. memset(buf, 0, sizeof(buf));
  16. char cursor = 0;
  17. // We have a GET request
  18.  
  19. // Read R0 value (Relay0)
  20. memset(buf, 0, sizeof(buf));
  21. cursor = 0;
  22. c = client.read();
  23. while(c!='&') {
  24. buf[cursor] = c;
  25. cursor++;
  26. if(c == 'R')
  27. cursor=0;
  28. if(c == '0')
  29. cursor=0;
  30. if(c == '=')
  31. cursor=0;
  32. c = client.read();
  33. }
  34. int bounce_rel = atoi(buf);
  35. if(bounce_rel == 1)
  36. bounceRelayState(0);
  37. // Read R1 value (Relay1)
  38. memset(buf, 0, sizeof(buf));
  39. cursor = 0;
  40. c = client.read();
  41. while(c!='&') {
  42. buf[cursor] = c;
  43. cursor++;
  44. if(c == 'R')
  45. cursor=0;
  46. if(c == '1')
  47. cursor=0;
  48. if(c == '=')
  49. cursor=0;
  50. c = client.read();
  51. }
  52. bounce_rel = atoi(buf);
  53. if(bounce_rel == 1)
  54. bounceRelayState(1);
  55. // Read R2 value (Relay2)
  56. memset(buf, 0, sizeof(buf));
  57. cursor = 0;
  58. c = client.read();
  59. while(c!='&') {
  60. buf[cursor] = c;
  61. cursor++;
  62. if(c == 'R')
  63. cursor=0;
  64. if(c == '2')
  65. cursor=0;
  66. if(c == '=')
  67. cursor=0;
  68. c = client.read();
  69. }
  70. bounce_rel = atoi(buf);
  71. if(bounce_rel == 1)
  72. bounceRelayState(2);
  73. // Read R3 value (Relay3)
  74. memset(buf, 0, sizeof(buf));
  75. cursor = 0;
  76. c = client.read();
  77. while(c!='&') {
  78. buf[cursor] = c;
  79. cursor++;
  80. if(c == 'R')
  81. cursor=0;
  82. if(c == '3')
  83. cursor=0;
  84. if(c == '=')
  85. cursor=0;
  86. c = client.read();
  87. }
  88. bounce_rel = atoi(buf);
  89. if(bounce_rel == 1)
  90. bounceRelayState(3);
  91. // Save data to EEPROM
  92. saveDataToEEPROM();
  93.  
  94. }
  95.  
  96. // if you've gotten to the end of the line (received a newline
  97. // character) and the line is blank, the http request has ended,
  98. // so you can send a reply
  99. if (c == '\n' && currentLineIsBlank) {
  100. if(have_http_get == 1) {
  101. // send refresh to shorten URL
  102. client.println("HTTP/1.1 200 OK");
  103. client.println("Refresh: 0; url=/");
  104. break;
  105. } else {
  106. // send a standard http response header
  107. client.println("HTTP/1.1 200 OK");
  108. client.println("Content-type: text/html
  109. ");
  110. client.println("<HTML><HEAD>");
  111. client.println("<TITLE>Relays2Web</TITLE>");
  112. client.println("</HEAD><BODY>");
  113. client.println("<div id='wrapper'>");
  114. client.println("<FORM METHOD='GET' ACTION='index.ard'>");
  115. // output the value of each analog input pin
  116. client.println("<table cellspacing='0' cellpadding='0'>");
  117. client.println(" <tr><th colspan='2'>Values</th></tr>");
  118. for(i = 0; i<6; i++) {
  119. client.print(" <tr><td>Analog input ");
  120. client.print(i, DEC);
  121. client.print(": </td><td class='td_center'>");
  122. client.print(AnalogValues[i],DEC);
  123. client.print(" Volts </td>");
  124. client.print("</tr>");
  125. }
  126. client.println("</table>");
  127. for (i=0; i<4; i++) {
  128. client.print("<INPUT TYPE='hidden' NAME='R");
  129. client.print(i, DEC);
  130. client.print("' VALUE='");
  131. client.print(0, DEC);
  132. client.print("' ID='Rel");
  133. client.print(i, DEC);
  134. client.println("'>");
  135. }
  136. for (int i=0; i<4; i++) {
  137. client.println("<script type = 'text/javascript'>");
  138. client.print(" function switchRel");
  139. client.print(i, DEC);
  140. client.println(" () {");
  141. client.print(" document.getElementById('Rel");
  142. client.print(i, DEC);
  143. client.println("').value= '1' ;");
  144. client.println("document.forms[0].submit();");
  145. client.println("}");
  146. client.println("</script>");
  147. }
  148. client.println("<INPUT TYPE='hidden' NAME='Z' VALUE='0'>");
  149. client.println("</INPUT>");
  150. client.println("</FORM>");
  151.  
  152. client.println("<table cellspacing='0' cellpadding='0'>"
  153. "<tr>"
  154. "<th colspan='3'>"
  155. "Relay status"
  156. "</th>"
  157. "</tr>");
  158. for(i=0; i<4; i++) {
  159. client.print("<tr><td>Relay ");
  160. client.print(i, DEC);
  161. client.print("</td><td> &nbsp;State:");
  162. client.println(RelayVals[i], DEC);
  163. client.println("</td><td width='100px' class='td_center'>");
  164. client.print("<INPUT TYPE='BUTTON' VALUE='Change this' onClick='switchRel");
  165. client.print(i, DEC);
  166. client.print("()'></td></tr>");
  167. }
  168. client.print("</table>");
  169. client.print("<div id='footer'>System uptime:");
  170. unsigned long upTime = millis()/1000;
  171. unsigned int upDays = upTime/86400;
  172. unsigned int upHours = (upTime/3600) - (upDays * 24);
  173. unsigned int upMinutes = (upTime/60) - (upHours * 60) - (upDays * 1440);
  174. upTime = upTime - (upMinutes *60);
  175. upTime = upTime - (upHours * 3600);
  176. upTime = upTime - (upDays * 86400);
  177.  
  178. if(upDays > 0) {
  179. client.print(upDays, DEC);
  180. client.print(" days & ");
  181. }
  182. if(upHours > 0) {
  183. client.print(upHours, DEC);
  184. client.print(" hours & ");
  185. }
  186. if(upMinutes > 0) {
  187. client.print(upMinutes, DEC);
  188. client.print(" minutes & ");
  189. }
  190. client.print(upTime, DEC);
  191. client.print(" seconds");
  192. client.println("</div>"
  193. "</div>"
  194. "</body></html>");
  195. break;
  196. }
  197. }
  198. if (c == '\n') {
  199. // you're starting a new line
  200. currentLineIsBlank = true;
  201. }
  202. else if (c != '\r') {
  203. // you've gotten a character on the current line
  204. currentLineIsBlank = false;
  205. }
  206. }
  207. }
  208. // give the web browser time to receive the data
  209. delay(1);
  210. // close the connection:
  211. client.stop();
  212. }
  213. }

Report this snippet  

You need to login to post a comment.