ESP.RGBWiFi: Added timeout
This commit is contained in:
parent
c033b60c1b
commit
045f7bcad2
1 changed files with 91 additions and 84 deletions
|
@ -115,93 +115,100 @@ void loop() {
|
|||
WiFiClient client = server.available();
|
||||
if (client) {
|
||||
// Wait until the client sends some data
|
||||
Serial.println("Client connected");
|
||||
while(!client.available()){
|
||||
Serial.print("Client connected");
|
||||
unsigned char timeouts = 10;
|
||||
while(!client.available() && timeouts > 0){
|
||||
delay(10);
|
||||
timeouts -= 1;
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
if (timeouts > 0) {
|
||||
// Read the first line of the request
|
||||
String req = client.readStringUntil('\r');
|
||||
Serial.println(req);
|
||||
client.flush();
|
||||
|
||||
// Check if new value should be set or values should be read
|
||||
if (req.indexOf("/set/") != -1) {
|
||||
// Request of the form "/set/mode/red/green/blue/"
|
||||
// Delimiters for values
|
||||
int indexMode, indexRed, indexGreen, indexBlue, endIndexBlue;
|
||||
indexMode = req.indexOf("/set/") + 5;
|
||||
indexRed = req.indexOf("/",indexMode + 1);
|
||||
indexGreen = req.indexOf("/",indexRed + 1);
|
||||
indexBlue = req.indexOf("/",indexGreen + 1);
|
||||
endIndexBlue = req.indexOf("/",indexBlue + 1);
|
||||
// Check if all values were filled in the request
|
||||
if (indexMode == -1 || indexRed == -1 || indexGreen == -1
|
||||
|| indexBlue == -1 || endIndexBlue == -1) {
|
||||
// Invalid request, abort
|
||||
Serial.println("Invalid request");
|
||||
client.stop();
|
||||
} else {
|
||||
// Parse values in request
|
||||
int mode = req.substring(indexMode, indexRed).toInt();
|
||||
int red = req.substring(indexRed + 1, indexGreen).toInt();
|
||||
int green = req.substring(indexGreen + 1, indexBlue).toInt();
|
||||
int blue = req.substring(indexBlue + 1, endIndexBlue).toInt();
|
||||
|
||||
// Sanitize values
|
||||
if (mode < 0) { mode = 0; }
|
||||
if (red < 0) { red = 0; }
|
||||
if (green < 0) { green = 0; }
|
||||
if (blue < 0) { blue = 0; }
|
||||
if (mode > 3) { mode = 3; }
|
||||
if (red > 255) { red = 255; }
|
||||
if (green > 255) { green = 255; }
|
||||
if (blue > 255) { blue = 255; }
|
||||
|
||||
// Update values
|
||||
modeValue = mode;
|
||||
|
||||
|
||||
breathScale = 0.999;
|
||||
breathAdjustment = -0.001;
|
||||
decColor = 0;
|
||||
|
||||
if (modeValue % 2 == 0) {
|
||||
redValue = red;
|
||||
greenValue = green;
|
||||
blueValue = blue;
|
||||
}
|
||||
if (modeValue % 2 != 0) {
|
||||
redValue = 255;
|
||||
greenValue = 0;
|
||||
blueValue = 0;
|
||||
}
|
||||
|
||||
persistToEEPROM();
|
||||
}
|
||||
} else if (req.indexOf("/get/") != -1) {
|
||||
|
||||
} else {
|
||||
Serial.println("invalid request");
|
||||
client.stop();
|
||||
}
|
||||
|
||||
client.flush();
|
||||
|
||||
// Prepare the response
|
||||
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
|
||||
response += modeValue;
|
||||
response += "\n";
|
||||
response += redValue;
|
||||
response += "\n";
|
||||
response += greenValue;
|
||||
response += "\n";
|
||||
response += blueValue;
|
||||
response += "\n";
|
||||
|
||||
// Send the response to the client
|
||||
client.print(response);
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Read the first line of the request
|
||||
String req = client.readStringUntil('\r');
|
||||
Serial.println(req);
|
||||
client.flush();
|
||||
|
||||
// Check if new value should be set or values should be read
|
||||
if (req.indexOf("/set/") != -1) {
|
||||
// Request of the form "/set/mode/red/green/blue/"
|
||||
// Delimiters for values
|
||||
int indexMode, indexRed, indexGreen, indexBlue, endIndexBlue;
|
||||
indexMode = req.indexOf("/set/") + 5;
|
||||
indexRed = req.indexOf("/",indexMode + 1);
|
||||
indexGreen = req.indexOf("/",indexRed + 1);
|
||||
indexBlue = req.indexOf("/",indexGreen + 1);
|
||||
endIndexBlue = req.indexOf("/",indexBlue + 1);
|
||||
// Check if all values were filled in the request
|
||||
if (indexMode == -1 || indexRed == -1 || indexGreen == -1
|
||||
|| indexBlue == -1 || endIndexBlue == -1) {
|
||||
// Invalid request, abort
|
||||
Serial.println("Invalid request");
|
||||
client.stop();
|
||||
} else {
|
||||
// Parse values in request
|
||||
int mode = req.substring(indexMode, indexRed).toInt();
|
||||
int red = req.substring(indexRed + 1, indexGreen).toInt();
|
||||
int green = req.substring(indexGreen + 1, indexBlue).toInt();
|
||||
int blue = req.substring(indexBlue + 1, endIndexBlue).toInt();
|
||||
|
||||
// Sanitize values
|
||||
if (mode < 0) { mode = 0; }
|
||||
if (red < 0) { red = 0; }
|
||||
if (green < 0) { green = 0; }
|
||||
if (blue < 0) { blue = 0; }
|
||||
if (mode > 3) { mode = 3; }
|
||||
if (red > 255) { red = 255; }
|
||||
if (green > 255) { green = 255; }
|
||||
if (blue > 255) { blue = 255; }
|
||||
|
||||
// Update values
|
||||
modeValue = mode;
|
||||
|
||||
|
||||
breathScale = 0.999;
|
||||
breathAdjustment = -0.001;
|
||||
decColor = 0;
|
||||
|
||||
if (modeValue % 2 == 0) {
|
||||
redValue = red;
|
||||
greenValue = green;
|
||||
blueValue = blue;
|
||||
}
|
||||
if (modeValue % 2 != 0) {
|
||||
redValue = 255;
|
||||
greenValue = 0;
|
||||
blueValue = 0;
|
||||
}
|
||||
|
||||
persistToEEPROM();
|
||||
}
|
||||
} else if (req.indexOf("/get/") != -1) {
|
||||
|
||||
} else {
|
||||
Serial.println("invalid request");
|
||||
client.stop();
|
||||
}
|
||||
|
||||
client.flush();
|
||||
|
||||
// Prepare the response
|
||||
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
|
||||
response += modeValue;
|
||||
response += "\n";
|
||||
response += redValue;
|
||||
response += "\n";
|
||||
response += greenValue;
|
||||
response += "\n";
|
||||
response += blueValue;
|
||||
response += "\n";
|
||||
|
||||
// Send the response to the client
|
||||
client.print(response);
|
||||
delay(1);
|
||||
client.flush();
|
||||
Serial.println("Disconnecting client");
|
||||
// The client will actually be disconnected
|
||||
|
|
Loading…
Reference in a new issue