Update Doorbell-Speaker.ino

This commit is contained in:
2018-09-27 21:18:00 +00:00
parent ffa539c00f
commit 8824b48a02
+74 -38
View File
@@ -1,11 +1,7 @@
/*************************************************** /*
DFPlayer - A Mini MP3 Player For Arduino Sources:
<https://www.dfrobot.com/index.php?route=product/product&product_id=1121>
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
***************************************************/
/* Sources:
https://www.youtube.com/watch?v=kq2RLz65_w0 https://www.youtube.com/watch?v=kq2RLz65_w0
https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299
https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/ESP32_DFPlayer_full.ino https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/ESP32_DFPlayer_full.ino
https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/setup.png https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_DFPlayer_full/setup.png
https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ https://randomnerdtutorials.com/esp32-web-server-arduino-ide/
@@ -15,16 +11,16 @@ https://randomnerdtutorials.com/esp32-web-server-arduino-ide/
#include "DFRobotDFPlayerMini.h" #include "DFRobotDFPlayerMini.h"
#include <WiFi.h> #include <WiFi.h>
HardwareSerial mySoftwareSerial(1); HardwareSerial hwSerial(1);
DFRobotDFPlayerMini myDFPlayer; DFRobotDFPlayerMini dfPlayer;
int volume = 8; int volume = 5;
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 = "on"; String ledState = "";
const int ledPin = 26; const int ledPin = 26;
unsigned long timestamp = 0; unsigned long timestamp = 0;
@@ -32,9 +28,9 @@ unsigned long timestamp = 0;
void setup() void setup()
{ {
mySoftwareSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
Serial.begin(115200);
btStop(); // turn off bluetooth btStop(); // turn off bluetooth
hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
Serial.begin(115200);
// WiFi & LED ================================================================== // WiFi & LED ==================================================================
pinMode(ledPin, OUTPUT); pinMode(ledPin, OUTPUT);
@@ -54,31 +50,18 @@ void setup()
Serial.println("IP address: "); Serial.println("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
server.begin(); 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);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3 ledState = "on";
Serial.println(myDFPlayer.readType(), HEX);
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
for (int i=0; i < 5; i++){
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, HIGH);
delay(500); timestamp = millis();
digitalWrite(ledPin, LOW); dfPlayer.play(1); //Play the first mp3
delay(500);
}
}
else {
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); //Set serial communication time out 500ms
myDFPlayer.volume(volume); //Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
myDFPlayer.play(1); //Play the first mp3
}
} }
@@ -114,7 +97,7 @@ void loop() {
ledState = "on"; ledState = "on";
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, HIGH);
timestamp = millis(); timestamp = millis();
myDFPlayer.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
String str1 = header; String str1 = header;
@@ -128,7 +111,7 @@ void loop() {
volume = 30; volume = 30;
} }
myDFPlayer.volume(volume); dfPlayer.volume(volume);
Serial.print("volume set to "); Serial.print("volume set to ");
Serial.println(volume); Serial.println(volume);
} }
@@ -142,9 +125,10 @@ void loop() {
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>");
client.println("<body><h1>LeWe T&uuml;rklingel</h1>"); client.println("<body><h1>LeWe T&uuml;rklingel</h1>");
client.println("<p>LED - State " + ledState + "</p>");
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>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><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>"); client.println("</body></html>");
// The HTTP response ends with another blank line // The HTTP response ends with another blank line
@@ -168,3 +152,55 @@ void loop() {
Serial.println(""); Serial.println("");
} }
} }
String printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
return "Time Out!";
break;
case WrongStack:
return "Stack Wrong!";
break;
case DFPlayerCardInserted:
return "Card Inserted!";
break;
case DFPlayerCardRemoved:
return "Card Removed!";
break;
case DFPlayerCardOnline:
return "Card Online!";
break;
case DFPlayerPlayFinished:
return "Play Finished!";
break;
case DFPlayerError:
switch (value) {
case Busy:
return "Error: Card not found";
break;
case Sleeping:
return "Error: Sleeping";
break;
case SerialWrongStack:
return "Error: Get Wrong Stack";
break;
case CheckSumNotMatch:
return "Error: Check Sum Not Match";
break;
case FileIndexOut:
return "Error: File Index Out of Bound";
break;
case FileMismatch:
return "Error: Cannot Find File";
break;
case Advertise:
return "Error: In Advertise";
break;
default:
break;
}
break;
default:
break;
}
}