diff --git a/doorbell/klingel_v3_wemos/klingel_v3_wemos.ino b/doorbell/klingel_v3_wemos/klingel_v3_wemos.ino index 16d0fd1..ae7b1a3 100644 --- a/doorbell/klingel_v3_wemos/klingel_v3_wemos.ino +++ b/doorbell/klingel_v3_wemos/klingel_v3_wemos.ino @@ -1,5 +1,8 @@ /* * dfPlayer auf Wemos D1: https://forum.arduino.cc/t/dfplayer-mini-mit-wemos-d1-uno/677980/3 + * Details dfPlayer: https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299#target_3 + * https://wolles-elektronikkiste.de/dfplayer-mini-ansteuerung-mit-dem-arduino + * W5500 Ethernet Shield: https://www.arduino.cc/en/Tutorial/LibraryExamples/WebServer */ #include "DFRobotDFPlayerMini.h" @@ -10,12 +13,15 @@ DFRobotDFPlayerMini dfPlayer; -int volume = 5; +int volume = 22; byte mac[] = { 0x02, 0x60, 0x0D, 0xF0, 0x0D, 0x04 }; IPAddress ip(192, 168, 178, 60); EthernetServer server(80); +String header; +String body; +unsigned long lastring = 0; void setup() { WiFi.mode(WIFI_OFF); @@ -23,20 +29,21 @@ void setup() { Serial.begin(115200); Serial.println(); Serial.println("Starting server"); - + Ethernet.init(5); // W5500 CS PIN auf D1 Ethernet.begin(mac, ip); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield not found"); while (true) { - delay(1); + delay(5000); } } - if (Ethernet.linkStatus() == LinkOFF) { - Serial.println("Ethernet cable is not connected."); + + while (Ethernet.linkStatus() == LinkOFF) { + Serial.println("Ethernet cable is not connected"); + delay(5000); } - Serial1.begin(9600);// TX liegt auf Pin D4 RX geht nicht !! @@ -56,7 +63,6 @@ void setup() { void loop() { EthernetClient client = server.available(); if (client) { - Serial.println("new client"); // an http request ends with a blank line bool currentLineIsBlank = true; @@ -86,32 +92,136 @@ void loop() { } Serial.write(c); + header += c; + // 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 (header.indexOf("GET /status") >= 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.println(" \"online\": true,"); + client.print(" \"volume\": "); + client.print(volume); + client.println(","); + client.print(" \"lastring\": "); + if (lastring <= 0) { + client.println(-1); + } + else { + client.println(millis() - lastring); + } + 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; + } + // send a standard http response header client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/plain"); - client.println("Connection: close"); // the connection will be closed after completion of the response + client.println("Content-type: text/html; charset=utf-8"); + client.println("Connection: close"); client.println(); - client.println("bonjour"); - dfPlayer.play(1); + + // turns the GPIOs on and off + if (header.indexOf("GET /bell/on") >= 0) { + dfPlayer.play(1); // play the first mp3 + lastring = millis(); + } + else if (header.indexOf("GET /setvolume/") >= 0) { // yes, I know this is not RESTful + String str1 = header; + str1 = str1.substring(header.indexOf("GET /setvolume/") + 15); + volume = str1.substring(0, str1.indexOf(" ")).toInt(); + + if (volume < 0) { + volume = 0; + } + else if (volume > 30) { + volume = 30; + } + + dfPlayer.volume(volume); + Serial.print("Volume set to "); + Serial.println(volume); + } + + // Display the HTML web page + client.println(""); + client.println("
"); + client.println(""); + client.println(""); + + client.println("