Installing MQTT for C++ on Raspberry Pi

Easier method:

sudo apt-get install libmosquitto-dev libmosquittopp-dev libssl-dev

You will now be able to include the following in your c++ code.

#include "mosquittopp.h"

As long as you make your code with the following flags.

-l mosquittopp

Setting up websockets protocol for MQTT: (Without this you will not be able to send messages between the webserver and the nodes)

sudo vim /etc/mosquitto/conf.d/websocket.conf

Add the following here:

listener 1883
listener 1884
protocol websockets

Then restart mosquitto

sudo service mosquitto restart

On the web/client side, you can use javascript MQTT to handle the messages.

Follow the tutorial in the js mqtt git but replace the Location.port with 1884 (same as the one you set in the websocket.conf)


More complicated method:

Note that this was done for Raspberry Pi 3 Model B with Jessie Lite, but i’m sure it will work for other Pi models and Debian OS versions too.

Follow the previous post’s setup for: Installing mosquitto MQTT protocol. We just need to install the mosquitto protocol.

Here we try to build the paho.mqtt.c and paho.mqtt.cpp.

  1. First we need to build and install paho.mqtt.c since this will be used by the cpp build. Follow the folder structure since this is the standard setup and this will be used for the cpp setup as well. If you do place it in a non-standard folder, then later on there will be a ./configure where you can set the location.
    1. Install the needed packages: sudo apt-get install cmake make gcc libssl-dev
    2. mkdir ~/home/pi/git && cd ~/git
    3. git clone https://github.com/eclipse/paho.mqtt.c.git
    4. cd paho.mqtt.c
    5. *if you want to build static libraries and smaples : cmake -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SAMPLES=TRUE -DPAHO_WITH_SSL=TRUE
    6. make
    7. sudo make install
    8. You can try the sample codes to see if they work
  2. Installing paho.mqtt.cpp
    1. cd ~/git
    2. cd paho.mqtt.cpp
    3. Install needed packages: sudo apt-get install autoconf automake libtool make gcc -y
    4. ./bootstrap
    5. export PAHO_DIR=$PWD
    6. mkdir mybuild && cd mybuild
    7. $PAHO_DIR/configure –enable-samples (see page for more options)
    8. make #this may take a few minutes
    9. sudo make install
    10. Note: The samples will be built inside the mybuild directory you can try running it now. ./sample
    11. For more tests go to src/samples and try to make or build the samples:
    12. sudo ldconfig and then enter the command
    13. g++ -DOPENSSL -I.. -I/usr/local/include -D_NDEBUG -Wall -std=c++11 -O2 -o async_publish async_publish.cpp -L../../lib -L/usr/local/lib -lpaho-mqttpp3 -lpaho-mqtt3a

Running built in samples:

running tests
open new window for same shell
./async_publish
in other one
./async_subscribe
You should see the messages being sent by the publisher on the subscriber page.

Possible Errors:

error cannot find -lpaho-mqttpp3: you forgot to sudo make install

./async_publish: error while loading shared libraries: libpaho-mqttpp3.so.1: cannot open shared object file: No such file or directory: sudo ldconfig to update the libraries

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s