Hi everyone, So I'm working on a project where I am receiving a "current playing" song from Spotify that gets sent over to the HiveMQ broker by publishing on a topic from Node-RED. So I want the song name to be displayed on my LCD screen.
Here are the 2 problems I'm facing:
-
It doesn't clear the screen everytime a new song is being played. So I was thinking of using variables called "CurrentSong" and "PlayingSong" and if there's a change I would clear the LCD screen but it doesn't seem to work
-
I also want to make my text scroll from left to right because sometimes the song title is too long to be fully displayed on the LCD. So I want to know how many characters are in the String of my song so I can use this number to make it scroll depending on what song is playing.
I used this code which works:
void callback(char* topic, byte* payload, unsigned int length) {
playingsong="";
//Serial.print("Weergave payload=");
for (int i = 0; i < length; i++) {
playingsong += String(((char)payload[i]));
//Serial.print(length);
}
Serial.println();
Serial.print( " String length is " );
Serial.print( playingsong.length() );
Serial.println();
}
But I'd have to combine this with the code in my first problem and also with the code to make my text scroll from left to right which is this code:
void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
In this example it moves it by 13 positions but it has to be whatever the string length is from my song.
If anyone would be able to help me out with this I would highly appreciate it.
This is my full code so far:
#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
const char* ssid = "Sint-Rembert Techniek";
const char* password = "Dt16Mc6UJa35";
const char* mqtt_server = "broker.mqtt-dashboard.com";
LiquidCrystal_I2C lcd(0x3F, 16, 2);
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
String CurrentSong = "";
String PreviousSong = "";
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
//Serial.print("Weergave payload=");
CurrentSong = PreviousSong;
for (int i = 0; i < length; i++) {
CurrentSong = ((char)payload[i]);
if (CurrentSong == PreviousSong) {
Serial.print(PreviousSong);
}
if (CurrentSong != PreviousSong) {
Serial.print(CurrentSong);
lcd.clear();
}
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// ... and resubscribe
client.subscribe("ThiboSpotify");
}
else {
Serial.print("failed, rc=");
Serial.println(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
}
Thanks in advance.