RGB Strip Arduino MQTT control

Having an issue with Pubsubclient throwing up compile errors in the IDE.

Tried different versions of Pubsubclient, (imroy and knolleary), ethernet version, nightly build of the IDE and still all the same.

throws up a ‘callback’ was not declared in this scope error.

Looked online and tried variations of different sketches (example ones included) that should work but everything is getting the same error.

Pretty stuck and tearing my hair out as cannot figure out the issue so any help appreciated.

// Running an Arduino Uno with an ethernet shield stacked on that with the Velleman KA-01 stacked on top of that. Plan is to send MQTT messages to it from OpenHAB.

Full script code:

[code]#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };

// Set fallback IP address if DHCP fails

// Set the broker server IP
byte server[] = { 192,168,0,40 };

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 6;
int GREEN = 5;
int RED = 3;

void setup()
{
// Open serial communications
Serial.begin(9600);

// Start with a hard-coded address:
Serial.println("Assigning Static IP address:");
Ethernet.begin(mac, ip);

 Serial.print("My address:");

Serial.println(Ethernet.localIP());

// Connect to Broker, give it arduino as the name
if (client.connect(“arduino_LED2”)) {

// Publish a message to the status topic
client.publish("status/arduino_LED","Arduino LED2 is now online");

// Listen for messages on the control topic
client.subscribe("control/arduino_LED/#");

}
}

void loop()
{
client.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
// check for messages on subscribed topics
payload[length] = ‘\0’;
Serial.print("Topic: ");
Serial.println(String(topic));

// check topic to identify type of content
if(String(topic) == “control/arduino_LED/livingroom/color”) {
// convert payload to String
String value = String((char*)payload);
//value.trim();
Serial.print (value);
//Serial.flush();
// split string at every “,” and store in proper variable
// convert final result to integer
SoffitR = value.substring(0,value.indexOf(’,’)).toInt();
SoffitG = value.substring(value.indexOf(’,’)+1,value.lastIndexOf(’,’)).toInt();
SoffitB = value.substring(value.lastIndexOf(’,’)+1).toInt();

// print obtained values for debugging

Serial.print("RED: ");
Serial.println(SoffitR);
//client.publish(“status/arduino_LED”, SoffitR);

Serial.print("GREEN: ");
Serial.println(SoffitG);
//client.publish(“status/arduino_LED”, SoffitG);

Serial.print("BLUE: ");
Serial.println(SoffitB);
//client.publish(“status/arduino_LED/soffit/color/blue”, int SoffitB);
//Serial.flush();

analogWrite(GREEN, SoffitG);
analogWrite(RED, SoffitR);
analogWrite(BLUE, SoffitB);

while(Serial.available())
Serial.read();

}
}[/code]

I think you will have better luck posting this to an Arduino forum. While there are Arduino users on this forum, most are probably at the same or less skill level as you. In short, the number of people here who might be able help is very low.

This is my example using uno + ethernet + pubsubclient

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include "DHT.h"

char server[] = "mqtt_server_hostname";

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };

char message_buff[100];
char sensor_name[] = "Ethernet-WZ1500";

EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  int i = 0;

  for (i = 0; i < length; i++) {
    message_buff[i] = payload[i];

    Serial.print((char)payload[i]);
  }
  message_buff[i] = '\0';

  String msgString = String(message_buff);

  Serial.print(" - " + msgString);
  Serial.println();

}

void reconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect(sensor_name)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      mqttClient.publish("home/living/sensor/state", "CONNECTED - SENSOR REBOOT");
      mqttClient.subscribe("home/living/sensor/commands/#");
    } else {
      Serial.print(mqttClient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(57600);

  
  mqttClient.setServer(server, 1883);
  mqttClient.setCallback(callback);

  //Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway, myDNS);
  //Ethernet.begin(mac, ip, myDNS, gateway, subnet); // cu custom dns/ip etc
  Ethernet.begin(mac);  // cu dhcp
  // Allow the hardware to sort itself out
  delay(1500);

  Serial.print("ip = ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnet mask = ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gateway = ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dns = ");
  Serial.println(Ethernet.dnsServerIP());

  reconnect();
}


void loop()
{
  if (!mqttClient.connected()) {
    reconnect();
    mqttClient.connect(sensor_name);
    mqttClient.publish("home/living/sensor/state", "RECONECTED - CONNECTION LOST");
    mqttClient.subscribe("home/living/sensor/commands/#");

  } else {
   // do stuff as long as we are connected
  // publish , count etc

}
  mqttClient.loop();
}


Attempted to compile and get this:

Arduino: 1.6.13 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\joshu\AppData\Local\Temp\arduino_modified_sketch_183025\sketch_dec22a.ino: In function 'void reconnect()':

sketch_dec22a:50: error: 'class PubSubClient' has no member named 'state'

       Serial.print(mqttClient.state());

                               ^

C:\Users\joshu\AppData\Local\Temp\arduino_modified_sketch_183025\sketch_dec22a.ino: In function 'void setup()':

sketch_dec22a:64: error: 'class PubSubClient' has no member named 'setServer'

   mqttClient.setServer(server, 1883);

              ^

sketch_dec22a:65: error: 'class PubSubClient' has no member named 'setCallback'

   mqttClient.setCallback(callback);

              ^

Multiple libraries were found for "Ethernet.h"
 Used: C:\Users\joshu\OneDrive\Projects\Arduino\libraries\Ethernet
 Not used: C:\Program Files (x86)\Arduino\libraries\Ethernet
exit status 1
'class PubSubClient' has no member named 'state'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Wait - reloaded the PubSubClient from library manager (2.6) and your example now works! Fingers crossed I can go from here and get it working. Many thanks

Would be interested to see everybody’s examples of how they have achieved control over RGB strips whether they are analog ones or Neopixel-like strips.

Did you get the original example working? I couldn’t even with a reinstall of the Arduino IDE and reinstall of the PubSub library.

I did some poking around and discovered there are some issues with the Arduino IDE. It went way over my head… but I located some discussion about reworking of some MQTT examples to work with the newer versions…

Anyway, I messed with the code and came up with this:

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };

// Set fallback IP address if DHCP fails
IPAddress ip(192, 168, 10, 194);

// Set the broker server IP
byte server[] = { 192, 168, 10, 172 };

EthernetClient ethClient;
//PubSubClient client(server, 1883, callback, ethClient);
PubSubClient client(ethClient);

int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 6;
int GREEN = 5;
int RED = 3;


void setup()
{
  // Open serial communications
  Serial.begin(9600);
  client.setServer(server, 1883);
  client.setCallback(callback);

  // Start with a hard-coded address:
  Serial.println("Assigning Static IP address:");
  Ethernet.begin(mac, ip);

  Serial.print("My address:");
  Serial.println(Ethernet.localIP());

  // Connect to Broker, give it arduino as the name
  if (client.connect("arduino_LED2")) {

    // Publish a message to the status topic
    client.publish("status/arduino_LED", "Arduino LED2 is now online");

    // Listen for messages on the control topic
    client.subscribe("control/arduino_LED/#");
  }
}

void loop()
{
  client.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
  // check for messages on subscribed topics
  payload[length] = '\0';
  Serial.print("Topic: ");
  Serial.println(String(topic));



  // check topic to identify type of content
  if (String(topic) == "control/arduino_LED/livingroom/color") {
    // convert payload to String
    String value = String((char*)payload);
    //value.trim();
    Serial.print (value);
    //Serial.flush();
    // split string at every "," and store in proper variable
    // convert final result to integer
    SoffitR = value.substring(0, value.indexOf(',')).toInt();
    SoffitG = value.substring(value.indexOf(',') + 1, value.lastIndexOf(',')).toInt();
    SoffitB = value.substring(value.lastIndexOf(',') + 1).toInt();

    // print obtained values for debugging
    Serial.print("RED: ");
    Serial.println(SoffitR);
    //client.publish("status/arduino_LED", SoffitR);

    Serial.print("GREEN: ");
    Serial.println(SoffitG);
    //client.publish("status/arduino_LED", SoffitG);

    Serial.print("BLUE: ");
    Serial.println(SoffitB);
    //client.publish("status/arduino_LED/soffit/color/blue", int SoffitB);
    //Serial.flush();

    analogWrite(GREEN, SoffitG);
    analogWrite(RED, SoffitR);
    analogWrite(BLUE, SoffitB);

    while (Serial.available())
      Serial.read();

  }
}

It compiles and seems to do the trick. I have some tinkering to do, but it does control my RGB LEDs!

I just set up a version of this: https://github.com/bruhautomation/ESP-MQTT-Digital-LEDs with my NodeMCU breakout: Multi-purpose NodeMCU breakout PCB

I did have to put

#define FASTLED_ALLOW_INTERRUPTS 0

at the start of my sketch, and run the ESP8266 at 160Mhz to get smooth animations, but it works like a charm! I just use OH to send effect names to the MQTT topic for the LEDs to switch between different ones. Best of all, it’s wireless!

Awesome - that works!!! Have you got any rules/code for getting colorpicker to output rgb values to a string to send over MQTT?

Many thanks

How far did you get with the code? I’m in a similar situation…

For everybody who is still having this problem:
The void must be declared before you call it. That means you have to put the void callback above the decleration of PubSubClient. You can take look at the example which is with the library, they put it above as well.

I know it has been a while, but maybe someone else is helped by it. In my experience the knolleary and imroy pubsubs are not fully interchangeable using the one for a sketch meant for the other gives those ‘has no member…’ etcetera errors
If you have the Imroy lib installed and you would do an automatic update of yr libraries, Imroy gets replaced by Knolleary, so at a certain moment you might get confused about what library you are actually using

as Imroy’s version shows the most recent activity, I’d stick with his version.

1 Like

Hi Bartus,

Could you share your openHAB config that you used to have BruhAutomation nodeMCU working?

[UPDATE] Found it here.

Cheers,
Slaw

@slaw425 That should work too, but since then I’ve cleaned up my configs a bit, and created a dedicated forum post (including how-to videos) here: NodeMCU MQTT LED Strip Controller Build & Config How-To Videos

Check it out!