Update Doorbell-Speaker.ino
This commit is contained in:
+76
-40
@@ -1,11 +1,7 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<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:
|
||||
/*
|
||||
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 <WiFi.h>
|
||||
|
||||
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;}</style></head>");
|
||||
|
||||
client.println("<body><h1>LeWe Türklingel</h1>");
|
||||
client.println("<p>LED - State " + ledState + "</p>");
|
||||
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
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user