Using Code provided by [federicobusero(Profile - federicobusero - Blynk Community)
• Hardware model + communication type.NODEMCU (ESP 32-12F dev kit V3) WWW.DOIT.COM
WIFI
• Smartphone OS Android 10
• Blynk server
• Blynk Library version 0.61
• Add your sketch code. Code should be formatted as example below.
I Hope I have the code formatted properly.
Below is the picture of my setup again.
I have tried a simple webserver setup on the ESP32 that turns on a Led with pin D6 and it works fine. So I am confident that the ESP32 is working, also the servo works.
I have a seperate power supply for the Mini Player and it ground goes to the ESP32.
/*************************************************************
This example shows how you can control a DFPlayer MP3 module
on ESP8266 using Blynk (music player widget) and a Wifi connection
Hardware:
- ESP8266 based module e.g. NodeMCU
- DFPlayer Mini MP3 player
App project setup:
- Button widget attached to V4 :Play track button: OFF value=0, ON value=tracknr
- Music Player widget attached to V5: shows current tracknr
- Slider widget attached to V6 : Volume slider: range 0-30
- Menu settings widget attached to V7: Equaliser selector: values
1 Normal
2 Pop
3 Rock
4 Jazz
5 Classic
6 Bass
Necessary libraries
- Blynk: https://github.com/blynkkk/blynk-library/releases/latest
- DFPlay: https://github.com/rwpalmer/DFPlay
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
//#include <SerialESP8266wifi.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <Servo.h>
// D5 is RX of ESP8266, connect to TX of DFPlayer
// D6 is TX of ESP8266, connect to RX of DFPlayer module
SoftwareSerial dfPlaySerial(12, 14);
#include "DFPlay.h"
DFPlay dfPlay;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxxxxxxxxxx";
Servo servo;
void cradle() {
//you begin your own personal code for servo here
int pos;
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
//your personal code for servo should end here
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt();
if (pinValue == 1) { // if Button sends 1
cradle(); // start the function cradle
Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
int pinValue = 0; // Set V3 status to 0 to quit, unless button is still pushed (as per below)
Blynk.syncVirtual(V3); // ...Then force BLYNK_WRITE(V3) function check of button status to determine if repeating or done.
}
}
#define PLAY_MODE_OFF 0
#define PLAY_MODE_TRACK 1
#define PLAY_MODE_ALL 2
int playmode = PLAY_MODE_OFF;
#define DFPLAY_MEDIA_USB 1
#define DFPLAY_MEDIA_SD 2
#define VPIN_PLAYTRACK V4
#define VPIN_MUSICPLAYER V5
#define VPIN_VOLUME V6
#define VPIN_EQUALIZER V7
int currentTrackCount = -1;
BLYNK_CONNECTED() {
// Synchronize volume & equalizer of DFPlayer with Blynk widgets
Blynk.syncVirtual(VPIN_VOLUME);
Blynk.syncVirtual(VPIN_EQUALIZER);
// Update music player widget (play/stop button and label) based on state DFPlayer
if (currentTrackCount >= 0)
{
String trackcountString(currentTrackCount + 1); // Start with 1 instead of 0 in user interface
Blynk.virtualWrite(VPIN_MUSICPLAYER, "play");
Blynk.setProperty(VPIN_MUSICPLAYER, "label", trackcountString);
}
else
{
Blynk.virtualWrite(VPIN_MUSICPLAYER, "stop");
Blynk.setProperty(VPIN_MUSICPLAYER, "label", " ");
}
}
BLYNK_WRITE(VPIN_PLAYTRACK)
{
int tracknr = param.asInt();
#ifdef BLYNK_PRINT
BLYNK_PRINT.print("BLYNK_WRITE(VPIN_PLAYTRACK): ");
BLYNK_PRINT.println(tracknr);
#endif
if (tracknr > 0)
{
String trackcountString(tracknr);
dfPlay.play(DFPLAY_MEDIA_SD, 0, tracknr); // Plays tracknr in root folder on SD-card
playmode = PLAY_MODE_TRACK;
currentTrackCount = -1;
Blynk.setProperty(VPIN_MUSICPLAYER, "label", trackcountString);
Blynk.virtualWrite(VPIN_MUSICPLAYER, "play");
}
}
BLYNK_WRITE(VPIN_MUSICPLAYER)
{
String action = param.asStr();
#ifdef BLYNK_PRINT
BLYNK_PRINT.print("BLYNK_WRITE(VPIN_MUSICPLAYER): ");
BLYNK_PRINT.println(action);
#endif
if (action == "play") {
dfPlay.play(DFPLAY_MEDIA_SD); // Plays all tracks in root folder of SD-card
playmode = PLAY_MODE_ALL;
}
if (action == "stop") {
dfPlay.stop();
}
if (action == "next") {
dfPlay.skip();
}
if (action == "prev") {
dfPlay.back();
}
}
BLYNK_WRITE(VPIN_VOLUME)
{
int paramVol = param.asInt();
uint8_t volume = constrain(paramVol, 0, 30);
#ifdef BLYNK_PRINT
BLYNK_PRINT.print("BLYNK_WRITE(VPIN_VOLUME): ");
BLYNK_PRINT.println(paramVol);
#endif
dfPlay.setVolume(volume);
}
BLYNK_WRITE(VPIN_EQUALIZER)
{
int paramEq = param.asInt();
uint8_t eq = constrain(paramEq, 1, 6) - 1; // Blynk starts with 1, setEqualizer starts with 0
#ifdef BLYNK_PRINT
BLYNK_PRINT.print("BLYNK_WRITE(VPIN_EQUALIZER): ");
BLYNK_PRINT.println(paramEq);
#endif
dfPlay.setEqualizer(eq);
}
void updatePlayerState()
{
int trackcount;
if (dfPlay.isPlaying())
{
trackcount = (int)dfPlay.getTrackCount();
}
else
{
trackcount = -1;
playmode = PLAY_MODE_OFF;
}
if (trackcount != currentTrackCount)
{
switch (playmode)
{
case PLAY_MODE_OFF:
Blynk.setProperty(VPIN_MUSICPLAYER, "label", " ");
Blynk.virtualWrite(VPIN_MUSICPLAYER, "stop");
break;
case PLAY_MODE_ALL:
String trackcountString(trackcount + 1); // Start with 1 instead of 0 in user interface
Blynk.setProperty(VPIN_MUSICPLAYER, "label", trackcountString);
Blynk.virtualWrite(VPIN_MUSICPLAYER, "play");
break;
}
currentTrackCount = trackcount;
}
}
void setup()
{
// Debug console
#ifdef BLYNK_PRINT
BLYNK_PRINT.begin(9600);
BLYNK_PRINT.println("Waiting for connections...");
#endif
dfPlaySerial.begin(9600);
dfPlay.begin(dfPlaySerial); // Prepares DFPlay for execution
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
servo.attach(4); //attaches servo to pin 4 (D2 on node mcu)
}
void loop()
{
Blynk.run();
dfPlay.manageDevice(); // Sends requests to DFPlayer & processes responses.
updatePlayerState();
}