MQTT Callback error
having issue pubsubclient throwing compile errors in ide.
tried different versions of pubsubclient, (imroy , knolleary), ethernet version, nightly build of ide , still same.
looked online , tried variations of different sketches (example ones included) should work getting same error.
pretty stuck , tearing hair out cannot figure out issue appreciated.
// running arduino uno ethernet shield stacked on velleman ka-01 stacked on top of that. plan send mqtt messages openhab.
full script code:
tried different versions of pubsubclient, (imroy , knolleary), ethernet version, nightly build of ide , still same.
code: [select]
pubsubclient client(server, 1883, callback, ethclient);
throws 'callback' not declared in scope error.looked online , tried variations of different sketches (example ones included) should work getting same error.
pretty stuck , tearing hair out cannot figure out issue appreciated.
// running arduino uno ethernet shield stacked on velleman ka-01 stacked on top of that. plan send mqtt messages openhab.
full script code:
code: [select]
#include <spi.h>
#include <ethernet.h>
#include <pubsubclient.h>
// set mac address
byte mac[] = { 0x90, 0xa2, 0xda, 0x0f, 0x5e, 0x69 };
// set fallback ip address if dhcp fails
// set 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 hard-coded address:
serial.println("assigning static ip address:");
ethernet.begin(mac, ip);
serial.print("my address:");
serial.println(ethernet.localip());
// connect broker, give arduino name
if (client.connect("arduino_led2")) {
// publish message status topic
client.publish("status/arduino_led","arduino led2 online");
// listen messages on control topic
client.subscribe("control/arduino_led/#");
}
}
void loop()
{
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
// check messages on subscribed topics
payload[length] = '\0';
serial.print("topic: ");
serial.println(string(topic));
// check topic identify type of content
if(string(topic) == "control/arduino_led/livingroom/color") {
// convert payload string
string value = string((char*)payload);
//value.trim();
serial.print (value);
//serial.flush();
// split string @ every "," , store in proper variable
// convert final result 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 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();
}
}
perhaps need forward declaration of callback function? see this stackoverflow question
eg (didnt try compile syntax may not totally correct)
or move code callback() above pubsubclient line.
eg (didnt try compile syntax may not totally correct)
code: [select]
ethernetclient ethclient;
void callback(char* topic, byte* payload, unsigned int length);
pubsubclient client(server, 1883, callback, ethclient);
or move code callback() above pubsubclient line.
Arduino Forum > Topics > Home Automation and Networked Objects > MQTT Callback error
arduino
Comments
Post a Comment