Hi this works great for pushing data to https://io.adafruit.com in my case. I'm trying to subscribe to a feed and get data from a feed.
If I want to let's say print the data from a subscribed feed, how do I go about doing that?
#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("iot.eclipse.org", 1883, callback);
**/
SYSTEM_MODE(MANUAL);
//-----
#include "creds.h"
// please enter your sensitive data in creds.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char password[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
//----
MQTT client("io.adafruit.com", 1883, callback);
// for QoS2 MQTTPUBREL message.
// this messageid maybe have store list or array structure.
uint16_t qos2messageid = 0;
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("HERE");
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);
}
// QOS ack callback.
// if application use QOS1 or QOS2, MQTT server sendback ack message id.
void qoscallback(unsigned int messageid) {
Serial.print("Ack Message Id:");
Serial.println(messageid);
}
void setup() {
Serial.begin(9600);
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.on();
WiFi.setCredentials(ssid, password);
WiFi.connect();
delay(200);
while ( WiFi.connecting()) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
IPAddress localIP = WiFi.localIP();
while (localIP[0] == 0) {
localIP = WiFi.localIP();
Serial.println("waiting for an IP address");
delay(1000);
}
Serial.println("\nIP Address obtained");
delay(1000);
RGB.control(true);
// connect to the server
client.connect("io.adafruit.com", IO_USERNAME, IO_KEY);
// add qos callback. If don't add qoscallback, ACK message from MQTT server is ignored.
client.addQosCallback(qoscallback);
// publish/subscribe
if (client.isConnected()) {
delay(200);
Serial.println("IN HERE");
// get the messageid from parameter at 4.
uint16_t messageid;
client.publish("benjgorman/feeds/welcome-feed", "hello world QOS1", MQTT::QOS1, &messageid);
Serial.println(messageid);
// if 4th parameter don't set or NULL, application can not check the message id to the ACK message from MQTT server.
client.publish("benjgorman/feeds/welcome-feed", "hello world QOS1(message is NULL)", MQTT::QOS1);
// QOS=2
client.publish("benjgorman/feeds/welcome-feed", "hello world QOS2", MQTT::QOS2, &messageid);
Serial.println(messageid);
// save QoS2 message id as global parameter.
qos2messageid = messageid;
// MQTT subscribe endpoint could have the QoS
client.subscribe("benjgorman/feeds/servo", MQTT::QOS2);
}
}
void loop() {
if (client.isConnected())
{
client.loop();
client.subscribe("benjgorman/feeds/servo", MQTT::QOS2);
}
else
{
Serial.println("Not Connected");
}
delay(5000);
}
Hi this works great for pushing data to https://io.adafruit.com in my case. I'm trying to subscribe to a feed and get data from a feed.
If I want to let's say print the data from a subscribed feed, how do I go about doing that?