Payload information from HiveMQ broker

Hi guys, I am working on a project where I am receiving a currently playing song on Spotify from Node-RED. This song gets sent over to my Serial monitor but here's what I want. I want it to only display the "playingsong" once instead of every 4 seconds for example. And as soon as there is a new song playing it has to print out the name of that song. Here you can see that it just prints it over and over again;

I was thinking of using two strings called "Playingsong" and "Nextsong" and if there's and make the code detect if there's a change. But sadly it does not work. Would anyone have an idea on how to code this? Any help will be greatly apppreciated. Thanks in adcance!
Here's my code:

#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>

const char* ssid = "Vonderlynck";
const char* password = "wifi2377";
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 playingsong = "";
String nextsong = "";

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) {
  for (int i = 0; i < length; i++) {
    playingsong = ((char)payload[i]);
    nextsong = playingsong;
    if (nextsong == playingsong) {
      Serial.print(playingsong);
    }
    playingsong != nextsong;
    if (nextsong != playingsong) {
      Serial.print(nextsong);
      playingsong = nextsong;
    }
  }
  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("ThiboSpotifyLCD");
    }
    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();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.