174 lines
6.5 KiB
Arduino
174 lines
6.5 KiB
Arduino
/***************************************************
|
|
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:
|
|
https://www.youtube.com/watch?v=kq2RLz65_w0
|
|
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/
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include "DFRobotDFPlayerMini.h"
|
|
#include <WiFi.h>
|
|
|
|
HardwareSerial mySoftwareSerial(1);
|
|
DFRobotDFPlayerMini myDFPlayer;
|
|
int volume = 8;
|
|
|
|
const char* ssid = "LeWe";
|
|
const char* password = "5275274365464843";
|
|
WiFiServer server(80); // Set web server port number to 80
|
|
|
|
String header;
|
|
String ledState = "on";
|
|
const int ledPin = 26;
|
|
|
|
unsigned long timestamp = 0;
|
|
|
|
|
|
void setup()
|
|
{
|
|
mySoftwareSerial.begin(9600, SERIAL_8N1, 18, 19); // speed, type, TX, RX
|
|
Serial.begin(115200);
|
|
btStop(); // turn off bluetooth
|
|
|
|
// WiFi & LED ==================================================================
|
|
pinMode(ledPin, OUTPUT);
|
|
digitalWrite(ledPin, LOW);
|
|
|
|
// Connect to Wi-Fi network with SSID and password
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
// Print local IP address and start web server
|
|
Serial.println("");
|
|
Serial.println("WiFi connected.");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
server.begin();
|
|
|
|
|
|
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
|
|
}
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
WiFiClient client = server.available(); // Listen for incoming clients
|
|
|
|
if (ledState == "on" && (millis() - timestamp) > 2000) {
|
|
ledState = "off";
|
|
digitalWrite(ledPin, LOW);
|
|
}
|
|
|
|
if (client) {
|
|
String currentLine = ""; // make a String to hold incoming data from the client
|
|
while (client.connected()) { // loop while the client's connected
|
|
if (client.available()) { // if there's bytes to read from the client,
|
|
char c = client.read(); // read a byte, then
|
|
Serial.write(c); // print it out the serial monitor
|
|
header += c;
|
|
if (c == '\n') { // if the byte is a newline character
|
|
// 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:
|
|
if (currentLine.length() == 0) {
|
|
// 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:
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-type:text/html");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
// turns the GPIOs on and off
|
|
if (header.indexOf("GET /bell/on") >= 0) {
|
|
ledState = "on";
|
|
digitalWrite(ledPin, HIGH);
|
|
timestamp = millis();
|
|
myDFPlayer.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) {
|
|
volume = 30;
|
|
}
|
|
|
|
myDFPlayer.volume(volume);
|
|
Serial.print("volume set to ");
|
|
Serial.println(volume);
|
|
}
|
|
|
|
// Display the HTML web page
|
|
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:,\">");
|
|
// CSS to style the on/off buttons
|
|
// Feel free to change the background-color and font-size attributes to fit your preferences
|
|
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
|
|
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
|
|
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
|
|
client.println(".button2 {background-color: #555555;}</style></head>");
|
|
|
|
client.println("<body><h1>LeWe Türklingel</h1>");
|
|
client.println("<p>LED - State " + ledState + "</p>");
|
|
client.println("<p>Volume " + String(volume) + "</p>");
|
|
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Klingeln</button></a></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("");
|
|
}
|
|
}
|