Basic mqtt publisher exmaple using YunClient

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

Gregg:
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:
GitHub - knolleary/pubsubclient: A client library for the Arduino Ethernet Shield that provides support for MQTT.

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:
http://knolleary.net/arduino-client-for-mqtt/
http://jpmens.net/2013/10/15/mqtt-on-the-arduino-yun/

That's about it
Regards

Hi friend,
I am working on "Arduino Nano + Ethernet shield"

  • Firmware upload to Arduino is done (example code given in Arduino)
  • Could you please help me in what things (at PC side) to be done to subscribe/ publish the messages.

Thanks in advance

sreenivas:
...
I am working on "Arduino Nano + Ethernet shield"

  • Firmware upload to Arduino is done (example code given in Arduino)
  • Could you please help me in what things (at PC side) to be done to subscribe/ publish the messages.
    ...

You might have better luck repost this at:

Interfacing w/ Software on the Computer

http://forum.arduino.cc/index.php?board=12.0

I'm interested too in MQTT with arduino Yun.

I have read (here I think: http://blog.mongohq.com/building-mongodb-into-your-internet-of-things-a-tutorial/ ) that the matt library for arduino is not complete and this let me thought: ohu but I have an arduino yun that is much more!

Why do not implement the mqtt publisher inside the linux part? like with mosca ( GitHub - moscajs/mosca: MQTT broker as a module ) for node.js or mosquitto (http://mosquitto.org/documentation/python/ ) with python..
it should be easier (at list I think) also include some security layer (that as far as I got matt per-se does not provide)..

so.. why mqtt in the arduino side? to develop some kind of code usable also for an arduino uno plus ethernet shield?

If you type

opkg update
opkg list | grep -i mosquitto

You'll find that mosquitto is available for the yun as an additional package. I think there are topics on the forum where someone described her implementation of yun and mosquitto. Or maybe it's some blog I've read (can't remember really)