jbudd
Posts: 945
Joined: Mon Dec 16, 2013 10:23 am

Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 9:01 pm

Can anyone recommend an end-to-end tutorial to setting up an MQTT broker on the Raspberry Pi 2 with Raspbian Jessie?

I have tried following
http://owntracks.org/booklet/guide/broker/
and
http://jpmens.net/2013/09/01/installing ... pberry-pi/
but so far I can't get it running and connecting from beyond my home network.

Thanks!
jb

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 9:47 pm

How hard is it to install mosquitto? You can get a MQTT broker running in less than ten minutes.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jbudd
Posts: 945
Joined: Mon Dec 16, 2013 10:23 am

Re: Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 9:51 pm

Maybe you can Dougie, I only asked for help because I can't.

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 10:20 pm

See below viewtopic.php?p=1072534#p1072534 for the latest and greatest way to get Mosquitto 1.4.10 (which includes websockets) installed.


mosquitto_sub -h localhost -v -t "\$SYS/#"
[CTRL]+[C]
mosquitto_sub -h localhost -v -t test/topic &
mosquitto_pub -h localhost -t test/topic -m "My first hello world for Mosquitto"

Gets something like this

Code: Select all

[email protected] ~ $ mosquitto_sub -h localhost -v -t "\$SYS/#"
$SYS/broker/version mosquitto version 1.3.4
$SYS/broker/timestamp 2014-08-22 06:10:51+0000
$SYS/broker/changeset abbbb585edc5b6b9bb35f2563b957144e5368037
$SYS/broker/uptime 861168 seconds
$SYS/broker/clients/total 4
$SYS/broker/clients/inactive 1
$SYS/broker/clients/active 3
$SYS/broker/clients/maximum 4
$SYS/broker/clients/expired 0
$SYS/broker/messages/stored 68
$SYS/broker/messages/received 28721
$SYS/broker/messages/sent 28764
$SYS/broker/subscriptions/count 4
$SYS/broker/retained messages/count 46
$SYS/broker/heap/current 15284
$SYS/broker/heap/maximum 15568
$SYS/broker/publish/messages/dropped 0
$SYS/broker/publish/messages/received 0
$SYS/broker/publish/messages/sent 43
$SYS/broker/publish/bytes/received 4
$SYS/broker/publish/bytes/sent 223
$SYS/broker/bytes/received 58138
$SYS/broker/bytes/sent 59226
$SYS/broker/connection/pi.apollo/state 1
$SYS/broker/connection/eagle.apollo/state 1
$SYS/broker/load/messages/received/1min 5.04
$SYS/broker/load/messages/received/5min 3.02
$SYS/broker/load/messages/received/15min 2.37
$SYS/broker/load/messages/sent/1min 44.33
$SYS/broker/load/messages/sent/5min 11.46
$SYS/broker/load/messages/sent/15min 5.22
$SYS/broker/load/bytes/received/1min 76.82
$SYS/broker/load/bytes/received/5min 27.65
$SYS/broker/load/bytes/received/15min 12.68
$SYS/broker/load/bytes/sent/1min 1586.80
$SYS/broker/load/bytes/sent/5min 345.81
$SYS/broker/load/bytes/sent/15min 119.47
$SYS/broker/load/sockets/1min 1.58
$SYS/broker/load/sockets/5min 0.52
$SYS/broker/load/sockets/15min 0.19
$SYS/broker/load/connections/1min 1.58
$SYS/broker/load/connections/5min 0.52
$SYS/broker/load/connections/15min 0.19
$SYS/broker/load/publish/sent/1min 39.29
$SYS/broker/load/publish/sent/5min 8.44
$SYS/broker/load/publish/sent/15min 2.85
^C
[email protected] ~ $ mosquitto_sub -h localhost -v -t test/topic &
[1] 6213
[email protected] ~ $ mosquitto_pub -h localhost -t test/topic -m "My first hello world for Mosquitto"
test/topic My first hello world for Mosquitto
[email protected] ~ $
Last edited by DougieLawson on Thu Nov 24, 2016 11:48 am, edited 1 time in total.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jbudd
Posts: 945
Joined: Mon Dec 16, 2013 10:23 am

Re: Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 10:59 pm

Sorry, quite a long post. This is what I've done, from the beginning:
Write a new SD card image with Raspbian Jessie.
Edit /etc/network/interfaces and /etc/wpa_supplicant/wpa_supplicant .conf so that it can connect to my WiFi with a static IP address (192.168.1.140). Everything from now on is headless and wifi.
Correction - that's /etc/dhcpcd.conf and /etc/wpa_supplicant/wpa_supplicant.conf
Using raspi-config, expand the filesystem, change the hostname to RedPi, memory split 16.
Reboot
sudo apt-get update, sudo apt-get upgrade and sudo apt-get dist-upgrade.
Reboot.

Now, following the instructions in http://jpmens.net/2013/09/01/installing ... pberry-pi/:

Code: Select all

[email protected] ~ $ curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
[email protected] ~ $ sudo apt-key add mosquitto-repo.gpg.key
[email protected] ~ $ rm mosquitto-repo.gpg.key
[email protected] ~ $ cd /etc/apt/sources.list.d/
[email protected] /etc/apt/sources.list.d $ sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list  ### NB  CHANGED FILENAME
[email protected] ~ $ sudo apt-get update
[email protected] ~ $ sudo apt-get install mosquitto mosquitto-clients python-mosquitto
[email protected] ~ $ sudo /etc/init.d/mosquitto stop
[email protected] ~ $ cd
[email protected] ~ $ git clone https://github.com/owntracks/tools mosquitto
[email protected] ~ $ cd mosquitto
[email protected] ~ $ sudo ./mosquitto-setup.sh
[email protected] ~ $ sudo vi /etc/mosquitto/conf.d/mosquitto.conf
(Remove double slashes in last 4 lines, change server.crt to RedPi.crt and server.key to RedPi.key in last 3 lines)
Now, your commands

Code: Select all

[email protected] ~ $ mosquitto_sub -h localhost -v -t "\$SYS/#"
gives
$SYS/broker/version mosquitto version 1.4.5
$SYS/broker/timestamp Sun, 08 Nov 2015 21:43:12 +0000
$SYS/broker/uptime 1490 seconds
$SYS/broker/clients/total 0
$SYS/broker/clients/inactive 0
$SYS/broker/clients/disconnected 0
$SYS/broker/clients/active 0
$SYS/broker/clients/connected 0
$SYS/broker/clients/expired 0
$SYS/broker/messages/stored 93
$SYS/broker/messages/received 30
$SYS/broker/messages/sent 88
$SYS/broker/subscriptions/count 0
$SYS/broker/retained messages/count 47
$SYS/broker/heap/current 14680
$SYS/broker/heap/maximum 18048
$SYS/broker/publish/messages/dropped 0
$SYS/broker/publish/messages/received 2
$SYS/broker/publish/messages/sent 62
$SYS/broker/publish/bytes/received 22
$SYS/broker/publish/bytes/sent 287
$SYS/broker/bytes/received 256
$SYS/broker/bytes/sent 2411
$SYS/broker/load/messages/received/1min 0.11
$SYS/broker/load/messages/received/5min 0.65
$SYS/broker/load/messages/received/15min 0.82
$SYS/broker/load/messages/sent/1min 0.11
$SYS/broker/load/messages/sent/5min 0.75
$SYS/broker/load/messages/sent/15min 1.57
$SYS/broker/load/publish/sent/1min 0.06
$SYS/broker/load/publish/sent/5min 0.27
$SYS/broker/load/publish/sent/15min 0.85
$SYS/broker/load/publish/received/1min 0.06
$SYS/broker/load/publish/received/5min 0.27
$SYS/broker/load/publish/received/15min 0.13
$SYS/broker/load/bytes/received/1min 0.22
$SYS/broker/load/bytes/received/5min 1.74
$SYS/broker/load/bytes/received/15min 4.48
$SYS/broker/load/bytes/sent/1min 0.22
$SYS/broker/load/bytes/sent/5min 5.36
$SYS/broker/load/bytes/sent/15min 33.70
$SYS/broker/load/sockets/1min 0.05
$SYS/broker/load/sockets/5min 0.27
$SYS/broker/load/sockets/15min 0.25
$SYS/broker/load/connections/1min 0.05
$SYS/broker/load/connections/5min 0.27
$SYS/broker/load/connections/15min 0.25
$SYS/broker/log/M/subscribe 1447109721: mosqsub/827-RedPi 0 $SYS/#

Code: Select all

[email protected] ~ $ mosquitto_sub -h localhost -t test/topic &
[1] 779
[email protected] ~ $ mosquitto_pub -h localhost -t test/topic -m "Hello world"

gives Hello world
Promising!

Code: Select all

[email protected] ~ $ mosquitto_pub -h 127.0.0.1 -t test/topic -m "Hello world"

also gives Hello world but

Code: Select all

[email protected] ~ $ mosquitto_pub -h 192.168.1.140 -t test/topic -m "Hello world"

(192.168.1.140 is RedPi's static IP address) gives Error: Connection refused

I presume this is because Jan-Piet Mens' mosquitto-setup.sh creates and imposes a requirement for the TLS encryption keys, which certainly sounds like a good idea (I want eventually to be able to send MQTT messages over the internet).
Unfortunately I don't know how to use them.

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Mon Nov 09, 2015 11:33 pm

Nope. It's because you mosquitto server is not listening on the inaddr-any address.

Take a look at your mosquitto.conf, is there a "listener" line in there? Change

Code: Select all

listener 1883 127.0.0.1
to

Code: Select all

listener 1883
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 12:11 am

jbudd wrote:but so far I can't get it running and connecting from beyond my home network.
To allow connections into your home network, you'll need to configure your internet router to direct incoming connections on port 1883 to the ip of the pi that is running the broker (on my router it is under NAT > Port redirection).

jbudd
Posts: 945
Joined: Mon Dec 16, 2013 10:23 am

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 12:24 am

Nope. It's because you mosquitto server is not listening on the inaddr-any address.
After deleting the 127.0.0.1 bit, things do look better.

Now I can publish a Hello world message from another Raspberry Pi, and also from the MQTT Client app on my mobile phone (connected to my WiFi)
To allow connections into your home network, you'll need to configure your internet router to direct incoming connections on port 1883 to the ip of the pi
Yes, my router is set to pass traffic on port 1883 or 8883 to IP address 192.168.1.140

If I disconnect my phone from wifi and change MQTT Client settings to my public IP address, port 1883 -
It works!
Thanks for the advice.

Tomorrow's task then will be to try and see why I still can't connect via duckdns.org, and to try and understand how to use SSL

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

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 12:43 am

jbudd wrote:[

If I disconnect my phone from wifi and change MQTT Client settings to my public IP address, port 1883 -
It works!
Thanks for the advice.

Tomorrow's task then will be to try and see why I still can't connect via duckdns.org, and to try and understand how to use SSL
I've found it's very difficult to test stuff without* your network from within, I tested mine by using a mobile phone as a modem so the router didn't try and be 'helpful' (tested other stuff from work network).

I've not used duckdns, but with paid No-Ip I can contact my local mqtt broker over a mobile (cell) connection.
* without used as the opposite of within - outside, not as 'lacking something'

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 1:04 am

asandford wrote: I've found it's very difficult to test stuff without* your network from within, I tested mine by using a mobile phone as a modem so the router didn't try and be 'helpful' (tested other stuff from work network).
This is why Starbucks, McD's and Costa's have free WiFi. For the cost of a cup of coffee you can test your external system. You can test your DynDNS (or equivalent). You can test your port forwarding (my network has 22 (ssh), 80 (http), 443 (https with a self-signed certificate) and 1194 (vpn) open). With some clever configuration (using gogoc.net) I can even test my IPv6 connectivity.

If you see me in any of those emporia with a laptop and a coffee I'm probably doing some penetration testing of my home network.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 1:11 am

DougieLawson wrote: This is why Starbucks, McD's and Costa's have free WiFi. For the cost of a cup of coffee you can test your external system. You can test your DynDNS (or equivalent). You can test your port forwarding (my network has 22 (ssh), 80 (http), 443 (https with a self-signed certificate) and 1194 (vpn) open). With some clever configuration (using gogoc.net) I can even test my IPv6 connectivity.

If you see me in any of those emporia with a laptop and a coffee I'm probably doing some penetration testing of my home network.
I prefer to keep my stuff under my control / private and not use the data slurpling corps you have listed (they are free for a reason...).

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Tue Nov 10, 2015 8:20 am

asandford wrote: I prefer to keep my stuff under my control / private and not use the data slurpling corps you have listed (they are free for a reason...).
With OpenVPN running with a self-signed certificate and ssh with a key pair they aren't getting any data from me.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rdelgado
Posts: 1
Joined: Sun Apr 03, 2016 6:35 pm

Re: Tutorial for MQTT on Raspbian Jessie?

Sun Apr 03, 2016 6:39 pm

Hello, I have a similar problem, my program works fine within my network and a test program I wrote works fine publishing in test.mosquitto.org but not in my remote mosquitto server, I'm wondering if there is a way to test if the port forwarding is working ( question 1)

I've installed MqttFx and it can connect to my remote server (so port forwarding dhould be OK) and publish/subscribe correctly, so I have no idea what could be wrong.

I'm quite new to rasp, openhab and mosquitto.

Can you point me somewhere?

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Tutorial for MQTT on Raspbian Jessie?

Wed Apr 06, 2016 7:48 pm

I'm not sure what your setup is. You say "but not in my remote mosquito server". What do you mean? What is your remote server? I assume you have a RPi at home? Do you have an MQTT broker on that as well?

Accessing a site like mosquitto.org from inside your network at home will not need port forwarding, but if you want to sit in Starbucks and access a service running at home then you will need to set up port forwarding on your router.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

mknoblock
Posts: 16
Joined: Tue Jan 07, 2014 10:20 pm

Re: Tutorial for MQTT on Raspbian Jessie?

Sun May 15, 2016 3:46 pm

Jan-Piet Mens refers to a mosquitto repository here:

sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list

but this file doesn't exist (any longer?) on that site, so his setup fails.

DirkS
Posts: 9898
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Tutorial for MQTT on Raspbian Jessie?

Sun May 15, 2016 3:49 pm

mknoblock wrote:Jan-Piet Mens refers to a mosquitto repository here:

sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list

but this file doesn't exist (any longer?) on that site, so his setup fails.
There are separate lists for wheezy and jessie.
See http://mosquitto.org/2013/01/mosquitto- ... epository/

mknoblock
Posts: 16
Joined: Tue Jan 07, 2014 10:20 pm

Re: Tutorial for MQTT on Raspbian Jessie?

Sun May 15, 2016 3:56 pm

DougieLawson wrote:sudo apt-get install mosquitto
I get this:

Code: Select all

    [email protected] ~ $ sudo apt-get install mosquitto
    Reading package lists... Error!
    E: Type '<html>' is not known on line 1 in source list /etc/apt/sources.list.d/mosquitto-repo.list
    E: The list of sources could not be read.
    E: The package lists or status file could not be parsed or opened.
because the list file contents are:

Code: Select all

    [email protected] /etc/apt/sources.list.d $ cat mos*.list
    <html>
    <head><title>404 Not Found</title></head>
    <body bgcolor="white">
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
So - no 10-minute setup for me either (poor me).

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Sun May 15, 2016 8:00 pm

mknoblock wrote:Jan-Piet Mens refers to a mosquitto repository here:

sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list

but this file doesn't exist (any longer?) on that site, so his setup fails.
That's the old way. Start here: http://mosquitto.org/2013/01/mosquitto- ... epository/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Tutorial for MQTT on Raspbian Jessie?

Sun May 15, 2016 9:32 pm

mknoblock wrote:Jan-Piet Mens refers to a mosquitto repository here:

sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list

but this file doesn't exist (any longer?) on that site, so his setup fails.
Depending on your needs, node-red has an mqtt client inbuilt, and is already installed.

If you require a broker, there are many which have an mqtt-AMQP bridge.

Maya_87
Posts: 1
Joined: Thu May 26, 2016 2:04 pm

Re: Tutorial for MQTT on Raspbian Jessie?

Thu May 26, 2016 4:29 pm

Can I change the configuration of the broker so that it waits (i.e certain time interval) for sometime before sending the data to subscribers ?

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Tutorial for MQTT on Raspbian Jessie?

Thu May 26, 2016 5:32 pm

I don't think so but with Node-Red it is possible.
MQTT-Delay.png
MQTT-Delay.png (12.02 KiB) Viewed 45415 times
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Thu May 26, 2016 8:13 pm

Maya_87 wrote:Can I change the configuration of the broker so that it waits (i.e certain time interval) for sometime before sending the data to subscribers ?
That's not possible. The broker's role is to deliver the published message to all subscribers as soon as possible after it's published.

You'd have to run an external subscriber process to read a published message, wait a while, then republish on another topic.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Tutorial for MQTT on Raspbian Jessie?

Thu May 26, 2016 11:04 pm

Maya_87 wrote:Can I change the configuration of the broker so that it waits (i.e certain time interval) for sometime before sending the data to subscribers ?
The mqtt broker is quite simple, have a look at more feature-rch brokers such as RabbitMQ.

mattmiller
Posts: 2096
Joined: Thu Feb 05, 2015 11:25 pm

Re: Tutorial for MQTT on Raspbian Jessie?

Thu Nov 24, 2016 10:49 am

@dougielawson
JFI googling for installing mosquitto on Pi leads to here but I had to

Code: Select all

sudo apt-get install mosquitto-clients
as well in order to use mosquitto_pub

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tutorial for MQTT on Raspbian Jessie?

Thu Nov 24, 2016 11:02 am

You should install Mosquitto using the instructions at: http://mosquitto.org/2013/01/mosquitto- ... epository/ the version in Raspbian is a piece of ancient history.

Don't forget to install the client samples.
sudo apt-get install mosquitto-clients
Last edited by DougieLawson on Thu Nov 24, 2016 12:25 pm, edited 1 time in total.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Troubleshooting”