From 8345491a658ba1485fc570262dbbfd8326716c91 Mon Sep 17 00:00:00 2001 From: Tilman Date: Tue, 14 Sep 2021 23:37:43 +0200 Subject: [PATCH] PUT volume for Doorbell-Speaker --- doorbell/Doorbell-Speaker.ino | 66 ++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/doorbell/Doorbell-Speaker.ino b/doorbell/Doorbell-Speaker.ino index cd7d17d..7f45ec0 100644 --- a/doorbell/Doorbell-Speaker.ino +++ b/doorbell/Doorbell-Speaker.ino @@ -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");