Doorbell-Speaker now automatically reconnects to wifi
This commit is contained in:
+91
-86
@@ -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,105 +67,95 @@ 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
|
||||||
while (client.connected()) { // loop while the client's connected
|
while (client.connected()) { // loop while the client's connected
|
||||||
if (client.available()) { // if there's bytes to read from the client,
|
if (client.available()) { // if there's bytes to read from the client,
|
||||||
char c = client.read(); // read a byte, then
|
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;
|
header += c;
|
||||||
if (c == '\n') { // if the byte is a newline character
|
if (c == '\n') { // if the byte is a newline character
|
||||||
// if the current line is blank, you got two newline characters in a row.
|
// 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:
|
// that's the end of the client HTTP request, so send a response:
|
||||||
if (currentLine.length() == 0) {
|
if (currentLine.length() == 0) {
|
||||||
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
// 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:
|
// and a content-type so the client knows what's coming, then a blank line:
|
||||||
client.println("HTTP/1.1 200 OK");
|
client.println("HTTP/1.1 200 OK");
|
||||||
client.println("Content-type:text/html");
|
client.println("Content-type:text/html");
|
||||||
client.println("Connection: close");
|
client.println("Connection: close");
|
||||||
client.println();
|
client.println();
|
||||||
|
|
||||||
// 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";
|
dfPlayer.play(1); //Play the first mp3
|
||||||
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();
|
|
||||||
|
|
||||||
if (volume < 0) {
|
|
||||||
volume = 0;
|
|
||||||
}
|
}
|
||||||
else if (volume > 30) {
|
else if (header.indexOf("GET /volume/") >= 0) { // yes, I know this is not RESTful
|
||||||
volume = 30;
|
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);
|
// Display the HTML web page
|
||||||
Serial.print("volume set to ");
|
client.println("<!DOCTYPE html><html>");
|
||||||
Serial.println(volume);
|
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||||
|
client.println("<link rel=\"icon\" href=\"data:,\">");
|
||||||
|
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>");
|
||||||
|
|
||||||
|
client.println("<body><h1>LeWe Türklingel</h1>");
|
||||||
|
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 style='margin-top: 40px; font-size: 8px;'>dfplayer status: " + printDetail(dfPlayer.readType(), dfPlayer.read()) + "</p>");
|
||||||
|
client.println("</body></html>");
|
||||||
|
|
||||||
|
// 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,
|
||||||
// Display the HTML web page
|
currentLine += c; // add it to the end of the currentLine
|
||||||
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>");
|
|
||||||
|
|
||||||
client.println("<body><h1>LeWe Türklingel</h1>");
|
|
||||||
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 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
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Clear the header variable
|
||||||
|
header = "";
|
||||||
|
// Close the connection
|
||||||
|
client.stop();
|
||||||
|
Serial.println("Client disconnected");
|
||||||
|
Serial.println("");
|
||||||
}
|
}
|
||||||
// Clear the header variable
|
}
|
||||||
header = "";
|
else {
|
||||||
// Close the connection
|
Serial.println("Reconnecting...");
|
||||||
client.stop();
|
setupServer();
|
||||||
Serial.println("Client disconnected.");
|
|
||||||
Serial.println("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user