DrKoi
Posts: 3
Joined: Fri Jan 23, 2015 12:32 am

Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 12:40 am

I haven't been able to get Mosquitto to work on my B+.

Here's what I've done, starting from a brand new latest Raspbian image (after the usual steps of expand filesystem, enable SPI, set timezone, change password, set up wifi, static IP, reboot, and run rpi-update/apt-get update/apt-get upgrade)

Install Mosquitto:

Code: Select all

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.list
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
Test Mosquitto:

Terminal window 1:

Code: Select all

mosquitto_sub -d -t hello/world
Terminal window 2:

Code: Select all

mosquitto_pub -d -t hello/world -m "Greetings from Terminal window 2!"
Problem:

Nothing happens on either terminal for a few minutes, and then both show:

Code: Select all

Error: Connection timed out
:?: What am I missing or doing wrong?

fruitoftheloom
Posts: 27225
Joined: Tue Mar 25, 2014 12:40 pm

Re: Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 7:47 am

https://packages.debian.org/wheezy/mosquitto

MQTT is available from the Raspbian Repository

Code: Select all

sudo apt-get update
sudo apt-get install mosquitto
[/s][/s]
Last edited by fruitoftheloom on Fri Jan 23, 2015 11:00 am, edited 1 time in total.
Take what I advise as advice not the utopian holy grail, and it is gratis !!

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 8:06 am

Ignore Fruity, I'll assume you've already got all of the mosquitto pieces installed. They are libmosquitto1, mosquitto, mosquitto-clients, python-mosquitto & python3-mosquitto

Is mosquitto running? Is it listening on port 1883?

Take a look with ps -ef | grep mosq && netstat -tln | grep 1883 which should give you something like

Code: Select all

pi@pi:~$ ps -ef | grep mosq && netstat -tln | grep 1883
mosquit+   642     1  0 Jan20 ?        00:05:55 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
pi       30194 30161  0 08:03 pts/1    00:00:00 grep mosq
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN
tcp6       0      0 :::1883                 :::*                    LISTEN
pi@pi:~$
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

DrKoi
Posts: 3
Joined: Fri Jan 23, 2015 12:32 am

Re: Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 12:26 pm

Thanks DougieLawson.

I already had mosquitto, mosquitto-clients and libmosquitto1 installed. Just to rule out the possibility I now added the two python ones as well, but that didn't change anything.

I tried your command and below is what I'm getting. Maybe the missing tcp6 line is a clue?

Code: Select all

pi@raspberrypi ~ $ ps -ef | grep mosq && netstat -tln | grep 1883
107       2273     1  0 Jan22 ?        00:01:03 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
pi       17010 16806  0 07:16 pts/0    00:00:00 grep --color=auto mosq
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 7:26 pm

What have you got in /etc/network/interfaces? Is the

Code: Select all

auto lo
iface lo inet loopback
stuff missing from there.?

BTW, ignore IPv6, I should have deleted that line to avoid confusing you, my network is 100% IPv6 ready.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

DrKoi
Posts: 3
Joined: Fri Jan 23, 2015 12:32 am

Re: Getting Mosquitto to work on Raspberry Pi

Fri Jan 23, 2015 7:40 pm

Thanks DougieLawson, that did it!

I had no idea this could influence Mosquitto - I just think of that file as something I need to edit to get my wifi and static IP working and then forget about.

What I had was:

Code: Select all

auto wlan0
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet static
etc...
Now I changed it to:

Code: Select all

auto lo
iface lo inet loopback

allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
etc...

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Sat Jan 24, 2015 12:15 am

:D :D :D :D
That was a good guess. Without a loopback interface you can't talk to yourself.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 1:45 pm

Hi,
I'm trying to talk to mosquitto broker on my raspberry pi via java client written on my PC (windows 8.1), I'have already installed libmosquitto1, mosquitto, mosquitto-clients, python-mosquitto & python3-mosquitto.
When I use terminal window for subscribing and publishing using:

Code: Select all

mosquitto_sub -d -t hello/world
and

Code: Select all

mosquitto_pub -d -t hello/world -m "Greetings from Terminal window 2"
everything works fine, but when I use java client on my PC I' can't get it to work, java client is:

Code: Select all

import java.util.*;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.*;

public class mqtt_mosquitto_proba {

	public static void main(String[] args) {
	
		System.out.println("pocetak");				// proba jeli radi 
		
	//	String topic        = "marko";
      //  String content      = "Message from MqttPublishSample";
        int qos             = 0;
        String broker       = "tcp:192.168.1.47:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();
        Scanner unos = new Scanner(System.in);
        
while(true){
        	System.out.println("unesi temu");
        	String topic = unos.nextLine();
        	System.out.println("Tema je: " + topic);
        	
        	System.out.println("Unesi poruku");
        	String content = unos.nextLine();
        	System.out.println("Poruka je: " + content);
        //	unos.close();
        	
        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
           // System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
        }
	}
	}
when I try

Code: Select all

ps -ef | grep mosq && netstat -tln | grep 1883
I get

Code: Select all

107   2216    1    0  12:52   ?    00:05:55 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
pi       2679 2622  0 13:30 pts/1    00:00:00 grep --color=auto mosq
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN
In my /etc/network/interfaces I have:

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
when i try to send some message on topic I subscribed on my terminal via putty i get this error in eclipse:

Code: Select all

Exception in thread "main" java.lang.NullPointerException
	at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457)
	at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
	at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
	at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
	at mqtt_mosquitto_proba.main(mqtt_mosquitto_proba.java:30)

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 3:11 pm

Is you RPi connected to your network? Is the default gateway correct?

ifconfig -a
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8
ping -c3 google.com

Can you ping your RPi from your windows system? Can you ping your windows system from your RPi?
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 6:38 pm

I can ping Ri through ma PC and I can ping PC via Rpi. I've found IP adress of my Rpi over Fing (mobile app), but when I use net view in cmd there is no Rpi.
After using

Code: Select all

if config -a
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8
ping -c3 google.com
I get this:
Attachments
ping -c3.gif
after ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3.gif (9.17 KiB) Viewed 43298 times
after ip route.gif
after ip route
after ip route.gif (3.7 KiB) Viewed 43298 times
ifconfig -a.gif
after ifconfig -a
ifconfig -a.gif (29.26 KiB) Viewed 43298 times

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 6:41 pm

can you tell me what means
ping -c3 8.8.8.8
ping -c3 $(ip route | awk '/default/ {print $3}')
ifconfig -a
ip route
Attachments
ping -c3 google.com.gif
after ping -c3 google.com
ping -c3 google.com.gif (10.76 KiB) Viewed 43296 times
ping -c3 8.8.8.8.gif
after ping -c3 8.8.8.8
ping -c3 8.8.8.8.gif (8.16 KiB) Viewed 43296 times

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 7:01 pm

Ping is a network diagnostic tool. It builds an ICMP packet with src_addr as your home system and dest_addr as the target. The TCP/IP stack at the remote system receives that packet and sends it straight back. Your system then reports it as a successful ping. If the ICMP packet times out before the destination receives it and replies the ping fails.

On the original ARPANET system it would also ping the bell on the console tty at the remote system. So it was an audible signal that someone remote was testing the network.

So now we're sure that your Raspberry is connected to your LAN and can reach the public internet you need to look at the firewall on your Windows machine. You need to "trust" you LAN or Windows will assume it's a hostile network and block every attempt to connect to machines on that network segment (except for port 80 & port 443).
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 8:56 pm

I'm opened port 1883 in my firewall, and I tried to send some message again but I get the same errors in eclipse. I'm checking if something went through in the terminal using the

Code: Select all

mosquitto_sub -d -t tema

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sun Oct 18, 2015 9:12 pm

sorry, I made a foolish mistake, in client code I used:

Code: Select all

String broker = "tcp:IPadress:1883";
but correct way is to use:

Code: Select all

String broker = "tcp://IPadress:1883";
Thank you so much for your help ;)

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Tue Oct 27, 2015 10:39 pm

Hi, I'm trying to connect raspberry pi to Cloudmqtt.com cloud. I made an account there and using their documentation to make a client, for now it's the same client as is shown here:https://www.cloudmqtt.com/docs-python.html
the problem is when I try to run python script with python3, I got
ImportantError: No module named mosquitto

I tried to solve this with installing python3-mosquitto,

Code: Select all

sudo apt-get install python3-mosquitto
,
but then I have:
Reading package lists... Done
Building depenency tree
Reading state information... Done
E: Unable to locate a package python3-mosquitto
Does this package even exists for python3, or the problem is in something else?

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Tue Oct 27, 2015 11:52 pm

The python stuff for Mosquitto is now part of paho.
https://pypi.python.org/pypi/paho-mqtt/1.1

You should be able to install that with pip and pip3.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Wed Oct 28, 2015 12:12 am

I already tried

Code: Select all

pip-3.2 (since i get error unable when try to instal pip) install paho-mqtt
which returns:
Requrement already satisfied(use --upgrade to upgrade) : paho -mqtt in /usr/local/lib/python3.2/dist-packages
Cleaning up...
and when I try

Code: Select all

apt-cache search mosquitto
, I'm missing python3-mosquitto

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Wed Oct 28, 2015 12:17 am

The attachment apt-cache search mosquitto.gif is no longer available
Attachments
apt-cache search mosquitto.gif
apt-cache search mosquitto.gif (21.22 KiB) Viewed 42849 times

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Wed Oct 28, 2015 10:03 am

You need to change your import for the paho-mqtt library

Code: Select all

#!/usr/bin/python3

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected "+str(rc))
    client.subscribe("$SYS/#")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
That gets lots of noise from the $SYS broker status topics.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Fri Nov 27, 2015 6:18 pm

Hi, me again. Is it possible to connect one client to two brokers, in example one broker is local mosquitto and another is remote Cloudmqtt broker?
These two brokers are totally independent and this client should be able to publish and subscribe to certain topics.

User avatar
DougieLawson
Posts: 42641
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Getting Mosquitto to work on Raspberry Pi

Fri Nov 27, 2015 11:41 pm

I've not tried it (yet) but it should work. I had some of my Mosquittos running with bridged connections, but the master machine (my old Viglen X86 server) got decommissioned. So at the moment each Mosquitto is stand-alone.

I won't get a chance to play this weekend.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Getting Mosquitto to work on Raspberry Pi

Sat Nov 28, 2015 1:00 am

mbajlo00 wrote:Hi, me again. Is it possible to connect one client to two brokers, in example one broker is local mosquitto and another is remote Cloudmqtt broker?
These two brokers are totally independent and this client should be able to publish and subscribe to certain topics.
As DL said, you need to set up bridging. You can only receive from (and send to) one broker per connection. With bridging, the local broker acts as a client to the remote broker and messages will be passed across it.

Have a look here for IBM's explanation (they created MQTT, so should know).

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sat Nov 28, 2015 2:22 pm

Thanks, I will try that ;)

mbajlo00
Posts: 30
Joined: Wed Nov 26, 2014 11:19 pm

Re: Getting Mosquitto to work on Raspberry Pi

Sat Nov 28, 2015 7:10 pm

I configured mosquitto.conf file

Code: Select all

connection cloudmqtt
try_private false
address broker.cloudmqtt.com:port
start_type automatic
username your_username_here
password your_password_here
clientid any_clientid_here
notifications true
Now cloudmqtt broker and local mosquitto broker are getting the same messages send by client I wrote few days ago for cloudmqtt. But the problem is that cloudmqtt broker haven't got messages sent by client that was used communicate with local mosquitto broker. Does anybody knows what me be wrong with this?

I'm using these comands in terminal:

Code: Select all

mosquitto_sub -d -h broker.cloudmqtt.com -p <port> -u <username> -P <password> -t topic

mosquitto_pub -h broker.cloudmqtt.com -p <port> -u <username> -P <password> -t topic -m message

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Getting Mosquitto to work on Raspberry Pi

Sat Nov 28, 2015 8:42 pm

mbajlo00 wrote: Now cloudmqtt broker and local mosquitto broker are getting the same messages send by client I wrote few days ago for cloudmqtt. But the problem is that cloudmqtt broker haven't got messages sent by client that was used communicate with local mosquitto broker. Does anybody knows what me be wrong with this?
How persistant are the messages you sent a few days ago with your local broker? If it's 'fling and forget', then they're lost.

Return to “Troubleshooting”