#include <Arduino.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <ESP8266WiFi.h>
//Распиновка
int button = D0;
int MP3BusyPin = D1;
int MP3Com1 = D3;
int MP3Com2 = D4;
int led1 = D5;
int led2 = D6;
int led3 = D5;
//Параметры wi-fi сети
const char* ssid = "WiFiVillage";
const char* password = "***";
//Параметры для светодиодов
const byte totalLEDs = 3; // Number of LEDS used -- number <255 so use a byte or uint8_t
const byte ledPin[totalLEDs] = {led1,led2,led3}; //Pins assigned to those LEDS -- number <255 so use a byte or uint8_t
boolean ledState[totalLEDs] = {0,0,0}; // What is the current LED state -- used a byte to store true/false as boolean since it is ON or OFF (0 or not 0)
unsigned int ledOn[totalLEDs] = {0,0,0}; // used to store ON times, using unsigned int to hold time up to 65535 ms) -- used INT (16 bits to store larger values
unsigned int ledOff[totalLEDs] ={0,0,0}; // used to store OFF times, using unsigned int to hold time up to 65535 ms) -- used INT (16 bits to store larger values
unsigned long previousMillis[totalLEDs] = {0,0,0}; // What time was the LED last tumned OFF -- Needs to store largest possible value (4 bytes used)
unsigned long currentMillis;
//Начальная громкость
int MP3Vol = 1;
WiFiServer server(80);
//Описание ответов вебсервера
int i;
char* Ans[]={"Volume +", "Volume -", "Play Next"};
SoftwareSerial mySoftwareSerial(MP3Com1, MP3Com2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
static unsigned long timerMP3;
void setup()
{
pinMode(button, OUTPUT);
pinMode(MP3BusyPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(MP3Vol); //Set volume value. From 0 to 30
myDFPlayer.next(); //Play the first mp3
timerMP3 = millis();
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
//Настройка светодиодов
for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
{
pinMode(ledPin[currentLED], OUTPUT); //initilize all used LED pins as outputs
digitalWrite(ledPin[currentLED], LOW); //Turn LEDs off to start
}
}
void loop()
{
if(digitalRead(button) == HIGH && digitalRead(MP3BusyPin) == HIGH)
{
PlayMP3:
Serial.println("play MP3");
myDFPlayer.next ();
timerMP3 = millis();
//delayplay = 0;
}
//Раздел выбора следующего MP3 при нажатии на кнопку во время проигрывания текущего
if (millis() - timerMP3 > 2000 && digitalRead(button) == HIGH && digitalRead(MP3BusyPin) == LOW)
{
myDFPlayer.next();
timerMP3 = millis();
}
//Раздел мигания светодиодами во время проигрывания MP3
if(digitalRead(MP3BusyPin) == LOW)
{
currentMillis = millis(); // Get the current time
for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
{
if (currentMillis - previousMillis[currentLED] > (ledState[currentLED] ? ledOff[currentLED]:ledOn[currentLED])){ // using a conditional operator
digitalWrite(ledPin[currentLED], ledState[currentLED] = !ledState[currentLED]); // Flip the state and output it
(ledState[currentLED] == 0) ? ledOn[currentLED]=random(400, 800) : ledOff[currentLED]=random(800, 1400);
previousMillis[currentLED] = currentMillis; // reset the blinker time
}
}
}
//Выключение светодиодов при тишине
if(digitalRead(MP3BusyPin) == HIGH && digitalRead(button) == LOW)
{
for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
{
digitalWrite(ledPin[currentLED], LOW); //Turn LEDs off to start
}
}
// if (myDFPlayer.available()) {
// printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
// }
//}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("/BeerukoffaMusicBox/1") != -1)
{
i=0;
MP3Vol++;
myDFPlayer.volume (MP3Vol);
}
else if (req.indexOf("/BeerukoffaMusicBox/0") != -1)
{
i=1;
MP3Vol--;
myDFPlayer.volume (MP3Vol);
}
else if (req.indexOf("/BeerukoffaMusicBox/2") != -1)
{
i=2;
myDFPlayer.next ();
}
else {
Serial.println("invalid request");
client.stop();
return;
}
Serial.println(Ans[i]);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\ ";
s += Ans[i];
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
}
void printDetail(uint8_t type, int value){
switch (type) {
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}