Skip to content

Instantly share code, notes, and snippets.

@magurosan
Last active September 24, 2017 14:59
Show Gist options
  • Save magurosan/f51a45242f83f4974287955d924f8b9f to your computer and use it in GitHub Desktop.
Save magurosan/f51a45242f83f4974287955d924f8b9f to your computer and use it in GitHub Desktop.
ESPr Developer + DFPlayer mini sketch sample (for IoTLT nagoya vol7)
/*
MP3 webserver sample, using SSCI ESPr(R) Developer with DFRobots DFPlayer mini
Copyright (c) 2017 Masaki Ota / MagurosanTeam
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <ESP8266WiFi.h>
// for DFPlayer
#define BUSY_PIN 14
#define SOFT_RX 13
#define SOFT_TX 12
SoftwareSerial mySerial(SOFT_RX, SOFT_TX); // RX/TX emulation
// WiFi Server
WiFiServer server(80);
const char* ssid = "YOUR WI-FI SSID";
const char* password = "YOUR WI-FI PASSWORD";
void setup () {
Serial.begin (9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
delay(1000); // delay 1ms to set volume
mp3_set_volume (15);
delay(10);
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());
}
void loop() {
// 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);
}
String val = "";
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (getQuery(req, "/play/", &val) > 0) {
mp3_play(val.toInt());
} else if (getQuery(req, "/volume/", &val) > 0) {
mp3_set_volume(val.toInt());
} else if (getQuery(req, "/eq/", &val) > 0) {
mp3_set_EQ(val.toInt());
} else if (req.indexOf("/next") >= 0) {
mp3_next();
} else if (req.indexOf("/stop") >= 0) {
mp3_stop();
} else {
Serial.println("invalid request");
client.stop();
return;
}
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><body>hello!</body></html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
}
int getQuery(String req, String prefix, String* out) {
int pos = req.indexOf(prefix);
if (pos > 0) {
String val = req.substring(pos + prefix.length());
*out = val.substring(0, val.indexOf(" "));
return out->length();
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment