Hello everyone,
I'm working on my project but I am facing an issue...
I have a network of two Arduinos MKR WiFi 1010 and a Raspberry Pi, which communicate through WiFi and MQTT protocol. Everything is working fine.
I'm trying now to add a DFPlayer Mini to one of the Arduinos in order to play an MP3 file as soon as it receives a trigger from the Raspberry through MQTT protocol.
Since the MKR WiFi 1010 has an Hardware Serial Port (Serial1) I'm using it to connect the DFPlayer Mini, instead of the Software Serial.
I believe this issue is the Serial1 which could be used also by the WiFi connection.
As soon as the Arduino calls a function from the DFPlayer Library, it crashes...
I tried the exact same hardware connection without initialising the WiFi and it works fine...
Do you have any suggestion?
#include "secrets.h"
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
////////// LED setup //////////
#define PIN 3
#define NUMPIXELS 80
int staticMode = 0;
int currentTime;
int startTime;
int currentColor; //0 = white, 1 = blue, 2 = red
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
////////// Speakers setup //////////
DFRobotDFPlayerMini mp3;
////////// WIFI setup //////////
WiFiClient wifi;
int status = WL_IDLE_STATUS;
IPAddress server(192, 168, 1, 171);
PubSubClient client(wifi);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("OK");
Serial.println("Connecting...");
while (status != WL_CONNECTED) {
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print(".");
delay(1000);
}
Serial.println("Connected to WiFi!\n");
client.setServer(server, 1883);
client.setCallback(callback);
if (client.connect("nemo")) {
Serial.println("mqtt connected");
client.subscribe("/clod/module/temperature");
client.subscribe("/clod/module/expiration");
client.subscribe("/clod/module/status");
} else {
Serial.println("mqtt not connected");
Serial.println("failed, rc=");
Serial.println(client.state());
}
strip.begin();
strip.show(); // Initialize all pixels to 'off'
mp3.volume(30);
}
void loop() {
client.loop();
if (staticMode == 0){
colorWipe(strip.Color(255, 255, 255), 50);
staticMode = 1;
currentColor = 0;
}
if(currentColor != 0) {
currentTime = millis();
if (currentTime - startTime > 5000) {
if (currentColor == 1) {
fadeOut(50, 0, 0, 255);
fadeIn(50, 255, 255, 255);
} else if (currentColor == 2) {
fadeOut(50, 255, 0, 0);
fadeIn(50, 255, 255, 255);
}
currentColor = 0;
}
}
}
void callback(char* topic, byte* payload, unsigned int length){
String msg;
for (int i = 0; i < length; i++) {
msg += (char)payload[i];
}
if (strcmp(topic, "/clod/module/temperature") == 0) {
if (msg == "newFood") {
startTime = millis();
fadeOut(50, 255, 255, 255);
fadeIn(50, 0, 0, 255);
mp3.play(1);
Serial.println("on");
currentColor = 1;
}
}
if(strcmp(topic, "/clod/module/expiration") == 0) {
if (msg == "expiringFood") {
startTime = millis();
fadeOut(50, 255, 255, 255);
fadeIn(50, 255, 0, 0);
mp3.play(2);
Serial.println("Expired Food!");
currentColor = 2;
}
}
if(strcmp(topic, "/clod/module/status") == 0) {
if (msg == "moduleOff") {
fadeOut(50, 255, 255, 255);
} else if (msg == "moduleOn") {
colorWipe(strip.Color(255, 255, 255), 50);
}
}
}
////////// LED functions //////////
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void fadeIn(int steps, byte R, byte G, byte B) {
int tmpR, tmpG, tmpB; // Temp values
// Fade up
for (int s=1; s<=steps; s++) {
tmpR = (R * s) / steps; // Values of single LEDs components
tmpG = (G * s) / steps;
tmpB = (B * s) / steps;
for (int i=0; i<NUMPIXELS; i++) {
strip.setPixelColor(i,tmpR,tmpG,tmpB);
}
strip.show();
delay(10);
}
}
void fadeOut(int steps, byte R, byte G, byte B) {
int tmpR, tmpG, tmpB; // Temp values
//fade down
for (int s=steps; s>=0; s--) {
tmpR = (R * s) / steps; // Values of single LEDs components
tmpG = (G * s) / steps;
tmpB = (B * s) / steps;
for (int i=0; i<NUMPIXELS; i++) {
strip.setPixelColor(i,tmpR,tmpG,tmpB);
}
strip.show();
delay(10);
}
}