/ Published in: C++
Arduino webserver
Expand |
Embed | Plain Text
void doWebServerLoop() { // listen for incoming clients Client client = server.available(); unsigned char have_http_get = 0; unsigned char i= 0; if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '?') { have_http_get = 1; char buf[10]; memset(buf, 0, sizeof(buf)); char cursor = 0; // We have a GET request // Read R0 value (Relay0) memset(buf, 0, sizeof(buf)); cursor = 0; c = client.read(); while(c!='&') { buf[cursor] = c; cursor++; if(c == 'R') cursor=0; if(c == '0') cursor=0; if(c == '=') cursor=0; c = client.read(); } int bounce_rel = atoi(buf); if(bounce_rel == 1) bounceRelayState(0); // Read R1 value (Relay1) memset(buf, 0, sizeof(buf)); cursor = 0; c = client.read(); while(c!='&') { buf[cursor] = c; cursor++; if(c == 'R') cursor=0; if(c == '1') cursor=0; if(c == '=') cursor=0; c = client.read(); } bounce_rel = atoi(buf); if(bounce_rel == 1) bounceRelayState(1); // Read R2 value (Relay2) memset(buf, 0, sizeof(buf)); cursor = 0; c = client.read(); while(c!='&') { buf[cursor] = c; cursor++; if(c == 'R') cursor=0; if(c == '2') cursor=0; if(c == '=') cursor=0; c = client.read(); } bounce_rel = atoi(buf); if(bounce_rel == 1) bounceRelayState(2); // Read R3 value (Relay3) memset(buf, 0, sizeof(buf)); cursor = 0; c = client.read(); while(c!='&') { buf[cursor] = c; cursor++; if(c == 'R') cursor=0; if(c == '3') cursor=0; if(c == '=') cursor=0; c = client.read(); } bounce_rel = atoi(buf); if(bounce_rel == 1) bounceRelayState(3); // Save data to EEPROM saveDataToEEPROM(); } // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { if(have_http_get == 1) { // send refresh to shorten URL client.println("HTTP/1.1 200 OK"); client.println("Refresh: 0; url=/"); break; } else { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-type: text/html "); client.println("<HTML><HEAD>"); client.println("<TITLE>Relays2Web</TITLE>"); client.println("</HEAD><BODY>"); client.println("<div id='wrapper'>"); client.println("<FORM METHOD='GET' ACTION='index.ard'>"); // output the value of each analog input pin client.println("<table cellspacing='0' cellpadding='0'>"); client.println(" <tr><th colspan='2'>Values</th></tr>"); for(i = 0; i<6; i++) { client.print(" <tr><td>Analog input "); client.print(i, DEC); client.print(": </td><td class='td_center'>"); client.print(AnalogValues[i],DEC); client.print(" Volts </td>"); client.print("</tr>"); } client.println("</table>"); for (i=0; i<4; i++) { client.print("<INPUT TYPE='hidden' NAME='R"); client.print(i, DEC); client.print("' VALUE='"); client.print(0, DEC); client.print("' ID='Rel"); client.print(i, DEC); client.println("'>"); } for (int i=0; i<4; i++) { client.println("<script type = 'text/javascript'>"); client.print(" function switchRel"); client.print(i, DEC); client.println(" () {"); client.print(" document.getElementById('Rel"); client.print(i, DEC); client.println("').value= '1' ;"); client.println("document.forms[0].submit();"); client.println("}"); client.println("</script>"); } client.println("<INPUT TYPE='hidden' NAME='Z' VALUE='0'>"); client.println("</INPUT>"); client.println("</FORM>"); client.println("<table cellspacing='0' cellpadding='0'>" "<tr>" "<th colspan='3'>" "Relay status" "</th>" "</tr>"); for(i=0; i<4; i++) { client.print("<tr><td>Relay "); client.print(i, DEC); client.print("</td><td> State:"); client.println(RelayVals[i], DEC); client.println("</td><td width='100px' class='td_center'>"); client.print("<INPUT TYPE='BUTTON' VALUE='Change this' onClick='switchRel"); client.print(i, DEC); client.print("()'></td></tr>"); } client.print("</table>"); client.print("<div id='footer'>System uptime:"); unsigned long upTime = millis()/1000; unsigned int upDays = upTime/86400; unsigned int upHours = (upTime/3600) - (upDays * 24); unsigned int upMinutes = (upTime/60) - (upHours * 60) - (upDays * 1440); upTime = upTime - (upMinutes *60); upTime = upTime - (upHours * 3600); upTime = upTime - (upDays * 86400); if(upDays > 0) { client.print(upDays, DEC); client.print(" days & "); } if(upHours > 0) { client.print(upHours, DEC); client.print(" hours & "); } if(upMinutes > 0) { client.print(upMinutes, DEC); client.print(" minutes & "); } client.print(upTime, DEC); client.print(" seconds"); client.println("</div>" "</div>" "</body></html>"); break; } } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } }
You need to login to post a comment.
