Doorbell-Speaker now automatically reconnects to wifi

This commit is contained in:
2020-01-23 14:46:23 +00:00
parent 20d53d41b6
commit b7828706e2
+29 -24
View File
@@ -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 <Arduino.h>
#include "DFRobotDFPlayerMini.h"
#include <WiFi.h>
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);
@@ -52,27 +67,17 @@ void setup()
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";
digitalWrite(ledPin, HIGH);
timestamp = millis();
dfPlayer.play(1); //Play the first mp3
digitalWrite(ledPin, HIGH);
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);
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
String currentLine = ""; // make a String to hold incoming data from the client
@@ -94,9 +99,6 @@ void loop() {
// 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
@@ -120,7 +122,6 @@ void loop() {
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<title>Doorbell</title>");
client.println("<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; }");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 { background-color: #4CAF50; border: none; color: white; padding: 4px 10px; text-decoration: none; margin: 1px; cursor: pointer;}</style></head>");
@@ -129,7 +130,6 @@ void loop() {
client.println("<p>Volume: <a href=\"/volume/" + String(volume-1) + "\"><button class=\"button2\">&minus;</button></a> " + String(volume) + " <a href=\"/volume/" + String(volume+1) + "\"><button class=\"button2\">&plus;</button></a></p>");
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Klingeln</button></a></p>");
client.println("<p style='margin-top: 40px; font-size: 8px;'>dfplayer status: " + printDetail(dfPlayer.readType(), dfPlayer.read()) + "</p>");
client.println("<p style='font-size: 8px;'>LED - State " + ledState + "</p>");
client.println("</body></html>");
// The HTTP response ends with another blank line
@@ -149,10 +149,15 @@ void loop() {
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("Client disconnected");
Serial.println("");
}
}
else {
Serial.println("Reconnecting...");
setupServer();
}
}
String printDetail(uint8_t type, int value){
switch (type) {