Using Mosquitto MQTT Broker with Arduino Yun

Dear all,

I am trying to use a local Mosquitto with my Arduino Yun. My project will be installed on a site without internet connection, so I need develop a system that runs only in the LAN.

I had installed Mosquitto with this:

opkg update

opkg install mosquitto mosquitto-client libmosquitto

My first goal is use the broker without authentication and no-SSL (port 1883). I am using default config file (/etc/mosquitto/mosquitto.conf).

Using the utilities mosquitto_sub and mosquitto_pub al works fine!!!

I open one SSH terminal and write:

mosquitto_sub -v -t 'test/test'

And open other SSH terminal and write:

mosquitto_pub -h 192.168.1.3 -t 'test/test' -m 'helloWord'

And works great!!! (192.168.1.3 is my Arduino LAN IP, but this command also works with 'localhost' and 'arduino.local' alias. When I run this command from outside Arduino (like my desktop PC) it works perfectly too (the PC are in the same LAN).

The big trouble is when I try to connect using the MQTT Dashboard app from my smartphone or PubSubClient library from my Arduino sketch. With public brokers like cloudmqtt or iot.eclipse.org works fine, but when I change the broker access/port to my local Mosquitto neither the Dashboard app and PubSubClient works!!!!! I had tried many many things and not!!!! I remember I had already got a successfully connection with app on my preliminary tests, but now I can not anymore. I don't know what else to do.

Can someone help me?

Thanks a lot!!!

After more and more tests, I did finally established a connection to Mosquitto broker installed on Arduino Yun from MQTT Dashboard app!!! And when I try from sketch I receive error code -4 from PubSubClient library:

-4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time

So, I think that's the problem is the broker timeout or similar also... Does someone know it? Have already this problem? Any suggestions for set up the timeout to try to connect? Certainly the solution will be in Mosquitto config file.. I think, I don't know...

At later I will try more and more... thanks if someone are reading my post.

I solved the question! For help other people I will tell the solution.

The Mosquitto opkg package available on OpenWRT/Linino OS system works only with version 3.1 of MQTT protocol and PubSubClient library default value is to work with version 3.1.1. This is the point!!!

With these configuration never will be possible establish a connection through PubSubClient, because the version of protocols are incompatible.

So, to resolve the question is necessary change the version 3.1.1 to 3.1 on the PubSubClient library. To do this edit the PubSubClient.h file (generally located on /home/userxxx/Arduino/libraries/PubSubClient/src) and change the line:

#define MQTT_VERSION MQTT_VERSION_3_1_1

to

#define MQTT_VERSION MQTT_VERSION_3_1

Just do this and the problem will be solved!

Congratulations on figuring it out, and thanks for posting it - it should help the next person who runs into that issue. (And that next person might be me! I'm thinking of trying MQTT for my next project.)

PubSubClient:

  • Arduino Ethernet
  • Arduino Ethernet Shield
  • Arduino YUN ...
  • Arduino WiFi Shield ...

Pubsubclient is designed for 8 bits MPU. However Yun has both 8 bits MPU as well as 32 bits MIPS CPU.

The code run at 32 bits MIPS CPU has following advantage:
high performance, high availability, better ability to debug, easy maintainability, flexible programming language selection.

Python sample code (32 bits MIPs CPU at Yun supports A-Z Languages)

http://ibuyopenwrt.com/index.php

opkg update
opkg install python-openssl #adds ssl support to python
opkg install distribute #it contains the easy_install command line tool (this can take some time)
easy_install pip #installs pip  (this can take some time)
pip install mosquitto
nano /mnt/sda1/temperature.py
#!/usr/bin/python
import mosquitto
import sys
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.connect("127.0.0.1", 1883, 60, True)
mqttc.publish("hello/temperature", sys.argv[1] )
chmod 755  /mnt/sda1/temperature.py

Test it by run:

/mnt/sda1/temperature.py 34

open second terminal windows:

mosquitto_sub -d -t hello/temperature

to confirm temperature sent.

8 bits MPU ATmega32u4 code:

#include <Process.h>
void setup() {
  Bridge.begin();   // Initialize Bridge
}
void loop() {
  int temperature = random(0, 100);
  Process p;        // Create a process and call it "p"
  p.begin("/mnt/sda1/temperature.py");   // Process that launch the  command
  p.addParameter(temperature); // pass  parameter 
  p.run();      // Run the process and wait for its termination
  delay(5000);
}

http://ibuyopenwrt.com/index.php/8-yun-compatible/53-mqtt-broker-on-yun

You're welcome!!!

After install and config this old version of Mosquitto (the only available on Arduino Yun/Linino OS) I used a bridge between local broker running on Linino OS and remote broker running on the cloud. And it works! Now I can use all the system locally (inside the LAN) if internet fails, or when internet is available I can use the same code for publishing and subscribing the same topics remotelly.

Now I am trying to use authentication on local Mosquitto broker but this version has another problem: the mosquitto_passwd utillity not exists!! So, my new question is: Does someone knows if I could use the mosquitto_passwd on other machine with other Mosquitto installation and copy the generated password file to Linino OS running another Mosquitto installation? Does someone had already tried this?

Thanks a lot!

Thanks Sonnyyu... It's a very interesting way... You didn't use PubSubClient library... All the heavy processing was concentrated on the 32-bit CPU. Very good!!! Thanks a lot! I will study the possibility of change my code to something like this!!!

Doing as much as possible on the Linux side is a good goal. The 8-bit Arduino sketch processor is very good at controlling I/O pins, while the 32-bit Linux processor is very good at heavy computing, and accessing the microSD card and the network. When I design a system for the Yun, I try to do as much as possible on the Linux side, and use the sketch as a dumb I/O processor: the sketch reads the sensors and other devices connected to the shield pins, and sends the raw data down to the Linux side using a Process object. The Linux process does all the heavy lifting, and sends raw output data back to the sketch. The sketch reads this output data from the Process object and sets the outputs.

For me, about the only feature I use from the Bridge Library is the Process class, sometimes multiple instances at once. Anything yo can do with the file access and network classes, you can do better and faster on the Linux side. The only downside is a steeper learning curve.

Thanks for the addtional information, ShapeShifter!

To finish this topic my answer to the question of use mosquitto_passwd utility from other machine and copy manually the shadow password file to Linino is POSITIVE! It's possible!

I did it and works fine! I created my password file on a PC running Ubuntu and Mosquitto (any version) and copied the file to Linino /etc/mosquitto folder. After this, I edited the mosquitto.conf file to use authentication (it's very easy) and restart mosquitto. Now I only can use my local Mosquitto with user/password login.