OK, I just canāt get itā¦
Iāll look at it from the example code perspective:
Using very slight changes to the example code - I have added the broker info to reflect my local broker address, added the integer that I want to publish, and the publish command:
byte server[] = { 10,240,140,72 };
MQTT client(server, 1883, callback);
uint8_t rpm = 99;
client.publish("Speed", &rpm, sizeof(rpm));
If I run the example code (complete code below), I think I should be able to see the integer 99 - (Iām using a third party MQTT explorer on my Mac to view this data, subscribed to everything.) Am I not declaring the variable correctly? Maybe the full code will be helpful so I can get pointed in the right direction to send/receive the integers⦠I have the same result when I view the data on a different type of device, and If I instruct that device to send the integer 99 via mqtt, it arrives as expected.
What can I do to see the integer? It seems like the particle code isnāt recognizing that Iām sending an integer, even though it looks like the uint8_t data type should be expected by the library, it seems like it should be simple.
full code:
#include "MQTT.h"
void callback(char* topic, byte* payload, unsigned int length);
/**
* if want to use IP address,
* byte server[] = { XXX,XXX,XXX,XXX };
* MQTT client(server, 1883, callback);
* want to use domain name,
* exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started
* MQTT client("mqtt.eclipse.org", 1883, callback);
**/
byte server[] = { 10,240,140,72 };
MQTT client(server, 1883, callback);
uint8_t rpm = 99;
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
if (!strcmp(p, "RED"))
RGB.color(255, 0, 0);
else if (!strcmp(p, "GREEN"))
RGB.color(0, 255, 0);
else if (!strcmp(p, "BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
RGB.control(true);
// connect to the server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
client.publish("outTopic/message","hello world");
client.publish("Speed", &rpm, sizeof(rpm));
client.subscribe("inTopic/message");
}
}
void loop() {
if (client.isConnected())
client.loop();
}