From 8824b48a02ecfaef8f309a8bd692914ca85185ed Mon Sep 17 00:00:00 2001 From: Tilman Date: Thu, 27 Sep 2018 21:18:00 +0000 Subject: [PATCH] Update Doorbell-Speaker.ino --- Doorbell-Speaker.ino | 118 ++++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/Doorbell-Speaker.ino b/Doorbell-Speaker.ino index 91be72d..d9c3ccb 100644 --- a/Doorbell-Speaker.ino +++ b/Doorbell-Speaker.ino @@ -1,11 +1,7 @@ -/*************************************************** - DFPlayer - A Mini MP3 Player For Arduino - - - ***************************************************/ - -/* Sources: +/* +Sources: 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/setup.png https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ @@ -15,16 +11,16 @@ https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ #include "DFRobotDFPlayerMini.h" #include -HardwareSerial mySoftwareSerial(1); -DFRobotDFPlayerMini myDFPlayer; -int volume = 8; +HardwareSerial hwSerial(1); +DFRobotDFPlayerMini dfPlayer; +int volume = 5; const char* ssid = "LeWe"; const char* password = "5275274365464843"; WiFiServer server(80); // Set web server port number to 80 String header; -String ledState = "on"; +String ledState = ""; const int ledPin = 26; unsigned long timestamp = 0; @@ -32,9 +28,9 @@ unsigned long timestamp = 0; void setup() { - mySoftwareSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX - Serial.begin(115200); btStop(); // turn off bluetooth + hwSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX + Serial.begin(115200); // WiFi & LED ================================================================== pinMode(ledPin, OUTPUT); @@ -54,31 +50,18 @@ void setup() Serial.println("IP address: "); 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); - - if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3 - 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); - delay(500); - digitalWrite(ledPin, LOW); - 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 - } + ledState = "on"; + digitalWrite(ledPin, HIGH); + timestamp = millis(); + dfPlayer.play(1); //Play the first mp3 } @@ -114,7 +97,7 @@ void loop() { ledState = "on"; digitalWrite(ledPin, HIGH); 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 String str1 = header; @@ -128,7 +111,7 @@ void loop() { volume = 30; } - myDFPlayer.volume(volume); + dfPlayer.volume(volume); Serial.print("volume set to "); Serial.println(volume); } @@ -141,10 +124,11 @@ void loop() { 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;}"); - client.println("

LeWe Türklingel

"); - client.println("

LED - State " + ledState + "

"); + 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 @@ -168,3 +152,55 @@ void loop() { 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; + } +}