Doorbell-Speaker now automatically reconnects to wifi
This commit is contained in:
+29
-24
@@ -7,23 +7,26 @@ https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP3
|
|||||||
https://randomnerdtutorials.com/esp32-web-server-arduino-ide/
|
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 <Arduino.h>
|
||||||
#include "DFRobotDFPlayerMini.h"
|
#include "DFRobotDFPlayerMini.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
HardwareSerial hwSerial(1);
|
HardwareSerial hwSerial(1);
|
||||||
DFRobotDFPlayerMini dfPlayer;
|
DFRobotDFPlayerMini dfPlayer;
|
||||||
int volume = 5;
|
int volume = 22;
|
||||||
|
|
||||||
const char* ssid = "LeWe";
|
const char* ssid = "LeWe";
|
||||||
const char* password = "5275274365464843";
|
const char* password = "5275274365464843";
|
||||||
WiFiServer server(80); // Set web server port number to 80
|
WiFiServer server(80); // Set web server port number to 80
|
||||||
|
|
||||||
String header;
|
String header;
|
||||||
String ledState = "";
|
|
||||||
const int ledPin = 26;
|
const int ledPin = 26;
|
||||||
|
|
||||||
unsigned long timestamp = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
@@ -32,10 +35,22 @@ void setup()
|
|||||||
hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
|
hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
// WiFi & LED ==================================================================
|
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
digitalWrite(ledPin, LOW);
|
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
|
// Connect to Wi-Fi network with SSID and password
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
@@ -52,27 +67,17 @@ void setup()
|
|||||||
server.begin();
|
server.begin();
|
||||||
delay(500);
|
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
|
dfPlayer.play(1); //Play the first mp3
|
||||||
|
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
Serial.println("Setup completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
WiFiClient client = server.available(); // Listen for incoming clients
|
if ((WiFi.status() == WL_CONNECTED)) {
|
||||||
|
|
||||||
if (ledState == "on" && (millis() - timestamp) > 2000) {
|
WiFiClient client = server.available(); // Listen for incoming clients
|
||||||
ledState = "off";
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client) {
|
if (client) {
|
||||||
String currentLine = ""; // make a String to hold incoming data from the 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
|
// turns the GPIOs on and off
|
||||||
if (header.indexOf("GET /bell/on") >= 0) {
|
if (header.indexOf("GET /bell/on") >= 0) {
|
||||||
ledState = "on";
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
timestamp = millis();
|
|
||||||
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
|
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("<!DOCTYPE html><html>");
|
||||||
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||||
client.println("<link rel=\"icon\" href=\"data:,\">");
|
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("<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(".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>");
|
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\">−</button></a> " + String(volume) + " <a href=\"/volume/" + String(volume+1) + "\"><button class=\"button2\">+</button></a></p>");
|
client.println("<p>Volume: <a href=\"/volume/" + String(volume-1) + "\"><button class=\"button2\">−</button></a> " + String(volume) + " <a href=\"/volume/" + String(volume+1) + "\"><button class=\"button2\">+</button></a></p>");
|
||||||
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Klingeln</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='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>");
|
client.println("</body></html>");
|
||||||
|
|
||||||
// The HTTP response ends with another blank line
|
// The HTTP response ends with another blank line
|
||||||
@@ -149,10 +149,15 @@ void loop() {
|
|||||||
header = "";
|
header = "";
|
||||||
// Close the connection
|
// Close the connection
|
||||||
client.stop();
|
client.stop();
|
||||||
Serial.println("Client disconnected.");
|
Serial.println("Client disconnected");
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Reconnecting...");
|
||||||
|
setupServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String printDetail(uint8_t type, int value){
|
String printDetail(uint8_t type, int value){
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user