From b7828706e28adc577edd1362398698de17554cb7 Mon Sep 17 00:00:00 2001 From: Tilman Date: Thu, 23 Jan 2020 14:46:23 +0000 Subject: [PATCH] Doorbell-Speaker now automatically reconnects to wifi --- Doorbell-Speaker.ino | 181 ++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 88 deletions(-) diff --git a/Doorbell-Speaker.ino b/Doorbell-Speaker.ino index c8cdf77..5c21211 100644 --- a/Doorbell-Speaker.ino +++ b/Doorbell-Speaker.ino @@ -7,23 +7,26 @@ https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP3 https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ */ +/* Anmerkungen: +- Übertragung aus Arduino IDE mit Board "ESP32 Dev Module", siehe https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md +- Zum Übertragen an das ESP-32-WROOM muss (manchmal?) der BOOT-Button gedrückt werden +*/ + #include #include "DFRobotDFPlayerMini.h" #include HardwareSerial hwSerial(1); DFRobotDFPlayerMini dfPlayer; -int volume = 5; +int volume = 22; const char* ssid = "LeWe"; const char* password = "5275274365464843"; WiFiServer server(80); // Set web server port number to 80 String header; -String ledState = ""; const int ledPin = 26; -unsigned long timestamp = 0; void setup() @@ -32,10 +35,22 @@ void setup() hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX Serial.begin(115200); -// WiFi & LED ================================================================== pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); + Serial.println("Connecting to dfPlayer"); + dfPlayer.begin(hwSerial); //Use softwareSerial to communicate with mp3 + dfPlayer.setTimeOut(500); //Set serial communication time out 500ms + dfPlayer.volume(volume); //Set volume value (0~30). + dfPlayer.EQ(DFPLAYER_EQ_NORMAL); + dfPlayer.outputDevice(DFPLAYER_DEVICE_SD); + + setupServer(); +} + +void setupServer() { + digitalWrite(ledPin, LOW); + // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); @@ -51,106 +66,96 @@ void setup() Serial.println(WiFi.localIP()); server.begin(); delay(500); - - dfPlayer.begin(hwSerial); //Use softwareSerial to communicate with mp3 - dfPlayer.setTimeOut(500); //Set serial communication time out 500ms - dfPlayer.volume(volume); //Set volume value (0~30). - dfPlayer.EQ(DFPLAYER_EQ_NORMAL); - dfPlayer.outputDevice(DFPLAYER_DEVICE_SD); - ledState = "on"; + dfPlayer.play(1); //Play the first mp3 + digitalWrite(ledPin, HIGH); - timestamp = millis(); - dfPlayer.play(1); //Play the first mp3 - + Serial.println("Setup completed"); } void loop() { - WiFiClient client = server.available(); // Listen for incoming clients + if ((WiFi.status() == WL_CONNECTED)) { - if (ledState == "on" && (millis() - timestamp) > 2000) { - ledState = "off"; - digitalWrite(ledPin, LOW); - } - - if (client) { - String currentLine = ""; // make a String to hold incoming data from the client - 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 - 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) { - // 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("Connection: close"); - client.println(); - - // turns the GPIOs on and off - if (header.indexOf("GET /bell/on") >= 0) { - ledState = "on"; - digitalWrite(ledPin, HIGH); - timestamp = millis(); - dfPlayer.play(1); //Play the first mp3 - } - else if (header.indexOf("GET /volume/") >= 0) { // yes, I know this is not RESTful - String str1 = header; - str1 = str1.substring(header.indexOf("GET /volume/") + 12); - volume = str1.substring(0, str1.indexOf(" ")).toInt(); + WiFiClient client = server.available(); // Listen for incoming clients + + if (client) { + String currentLine = ""; // make a String to hold incoming data from the client + 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 + 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) { + // 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("Connection: close"); + client.println(); - if (volume < 0) { - volume = 0; + // turns the GPIOs on and off + if (header.indexOf("GET /bell/on") >= 0) { + dfPlayer.play(1); //Play the first mp3 } - else if (volume > 30) { - volume = 30; + else if (header.indexOf("GET /volume/") >= 0) { // yes, I know this is not RESTful + String str1 = header; + str1 = str1.substring(header.indexOf("GET /volume/") + 12); + 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); } - 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("

LeWe Türklingel

"); + client.println("

Volume: " + String(volume) + "

"); + client.println("

"); + client.println("

dfplayer status: " + printDetail(dfPlayer.readType(), dfPlayer.read()) + "

"); + client.println(""); + + // The HTTP response ends with another blank line + client.println(); + + // Break out of the while loop + break; + } else { // if you got a newline, then clear currentLine + currentLine = ""; } - - // Display the HTML web page - client.println(""); - client.println(""); - client.println(""); - client.println("Doorbell"); - client.println(""); - - client.println("

LeWe Türklingel

"); - client.println("

Volume: " + String(volume) + "

"); - client.println("

"); - client.println("

dfplayer status: " + printDetail(dfPlayer.readType(), dfPlayer.read()) + "

"); - client.println("

LED - State " + ledState + "

"); - client.println(""); - - // The HTTP response ends with another blank line - client.println(); - - // Break out of the while loop - break; - } else { // if you got a newline, then clear currentLine - currentLine = ""; + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine } - } else if (c != '\r') { // if you got anything else but a carriage return character, - currentLine += c; // add it to the end of the currentLine } } + // Clear the header variable + header = ""; + // Close the connection + client.stop(); + Serial.println("Client disconnected"); + Serial.println(""); } - // Clear the header variable - header = ""; - // Close the connection - client.stop(); - Serial.println("Client disconnected."); - Serial.println(""); + } + else { + Serial.println("Reconnecting..."); + setupServer(); } }