Hi,
I have spent a few days trying to get my YUN to communicate with a MQTT Broker - I am a total Newbie. I thought I would create this brief thread to assist others.
I first installed a MQTT Broker - Mosquitto. Install using the packages created for your distro (Download | Eclipse Mosquitto)
Open two SSH sessions to the server hosting the broker:
In the one session start the broker:
mosquitto -v
In the second session start a subscriber to the broker:
mosquitto_sub -h localhost -t \# -v
For the subscriber you can subscribe to a specify topic:
mosquitto_sub -h localhost -t outTopic -v
You need to install the PubSubClient library, this can be downloaded from:
The sketch for the Yun is as follows:
#include <YunClient.h>
#include <PubSubClient.h>
#define MQTT_SERVER "x.x.x.x"
#define MQTT_CLIENTID "YUN-Sensor"
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
YunClient yun;
PubSubClient mqtt(MQTT_SERVER, 1883, callback, yun);
void setup()
{
Serial.begin(9600);
Bridge.begin();
if (mqtt.connect(MQTT_CLIENTID)) {
mqtt.publish("outTopic","hello world");
mqtt.subscribe("inTopic");
}
}
void loop()
{
mqtt.loop();
}
NB: If you subscribed to a specific topic using the mosquitto subcriber, make sure the topic in the YUN Sketch matches.
References:
That's about it
Regards