I had a lovely person help me with optimizing my code however, I am having one small problem. I am outputting my digital pin's state to the webserver but when they are LOW they disappear from the webserver for some reason rather than unchecking the checkbox I created
void processRequest(EthernetClient& client, String requestStr) {
Serial.println(requestStr);
// Send back different response based on request string
if (requestStr.startsWith("GET /status")) {
Serial.println(F("polled for status!"));
client.println(F("<p><strong>Bar Taps</strong></p>"));
client.println(F("<hr />"));
ProcessCheckbox(client);
writeClientResponse(client, "STATUS OF TAPS");
} else if (requestStr.startsWith("GET /reset")) {
Serial.println(F("Room reset"));
resetGame();
writeClientResponse(client, "ok");
} else if (requestStr.startsWith("GET /trigger")) {
Serial.println(F("Network prop trigger"));
onSolve();
writeClientResponse(client, "ok");
} else {
writeClientResponseNotFound(client);
}
}
/*
HTTP helper functions
*/
void listenForEthernetClients() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(F("Got a client"));
// Grab the first HTTP header (GET /status HTTP/1.1)
String requestStr;
boolean firstLine = true;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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) {
processRequest(client, requestStr);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
firstLine = false;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
if (firstLine) {
requestStr.concat(c);
}
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
void writeClientResponse(EthernetClient& client, String bodyStr) {
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/plain"));
client.println(F("Access-Control-Allow-Origin: *")); // ERM will not be able to connect without this header!
client.println();
client.print(bodyStr);
}
void writeClientResponseNotFound(EthernetClient& client) {
// send a standard http response header
client.println(F("HTTP/1.1 404 Not Found"));
client.println(F("Access-Control-Allow-Origin: *")); // ERM will not be able to connect without this header!
client.println();
}
void outputCheckbox(EthernetClient& cl, bool checked, char* type) {
cl.println(F("<input type=\"checkbox\" name=\"Tap2\" value=\"2\" onclick=\"submit();"));
if ( checked) {
cl.println(F("\"checked"));
}
cl.print(F(">"));
cl.print(type);
cl.println(F("</p>"));
}
void ProcessCheckbox(EthernetClient& cl)
{
outputCheckbox( cl, digitalRead(inputPins[0]), "MGD 1" );
outputCheckbox( cl, digitalRead(inputPins[1]), "MGD 2" );
outputCheckbox( cl, digitalRead(inputPins[2]), "Coors 1" );
outputCheckbox( cl, digitalRead(inputPins[3]), "Coors 2" );
outputCheckbox( cl, digitalRead(inputPins[4]), "Coors 3" );
outputCheckbox( cl, digitalRead(inputPins[5]), "Natural Light" );
outputCheckbox( cl, digitalRead(inputPins[6]), "Miller Highlife" );
outputCheckbox( cl, digitalRead(inputPins[7]), "Pabst" );
outputCheckbox( cl, digitalRead(inputPins[8]), "Michelob" );
}
FULL CODE: Arduino Cloud