PUT volume for Doorbell-Speaker

This commit is contained in:
2021-09-14 23:37:43 +02:00
parent e8e39de55f
commit 8345491a65
+61 -5
View File
@@ -27,6 +27,7 @@ const char* password = "5275274365464843";
WiFiServer server(80); // Set web server port number to 80
String header;
String body;
const int ledPin = 26;
int charCount = 0;
@@ -88,7 +89,6 @@ void setupServer() {
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
@@ -96,22 +96,77 @@ void loop() {
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
//Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
if (header.indexOf("GET /volume") >= 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type: application/json; charset=utf-8");
client.println("Connection: close");
client.println();
client.println("{");
client.print(" \"value\": ");
client.println(volume);
client.println("}");
break;
}
else if (header.indexOf("PUT /volume") >= 0) {
while (client.connected()) {
char c = client.read();
body += c;
if (c == '}') {
break;
}
}
String str1 = body;
str1 = str1.substring(str1.indexOf("value"));
str1 = str1.substring(str1.indexOf(":") + 1);
while (str1.length() > 0 && str1.startsWith(" ")) {
str1 = str1.substring(1);
}
int ix = 0;
while (isDigit(str1.charAt(ix))) {
ix++;
}
if (ix > 0) {
volume = str1.substring(0, ix).toInt();
if (volume < 0) {
volume = 0;
}
else if (volume > 30) {
volume = 30;
}
dfPlayer.volume(volume);
Serial.print("Volume set to ");
Serial.println(volume);
client.println("HTTP/1.1 200 OK");
}
else {
client.println("HTTP/1.1 400 Bad Request");
}
client.println("Connection: close");
break;
}
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Content-type: text/html; charset=utf-8");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /bell/on") >= 0) {
dfPlayer.play(1); //Play the first mp3
dfPlayer.play(1); // play the first mp3
}
else if (header.indexOf("GET /volume/") >= 0) { // yes, I know this is not RESTful
String str1 = header;
@@ -126,7 +181,7 @@ void loop() {
}
dfPlayer.volume(volume);
Serial.print("volume set to ");
Serial.print("Volume set to ");
Serial.println(volume);
}
@@ -159,6 +214,7 @@ void loop() {
}
// Clear the header variable
header = "";
body = "";
// Close the connection
client.stop();
Serial.println("Client disconnected");