sudo apt-get install libmosquitto-dev libmosquittopp-dev libssl-dev
You will now be able to include the following in your c++ code.
As long as you make your code with the following flags.
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
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.
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.
Install the needed packages:
sudo apt-get install cmake make gcc libssl-dev mkdir ~/home/pi/git && cd ~/git git clone https://github.com/eclipse/paho.mqtt.c.git cd paho.mqtt.c
- *if you want to build static libraries and smaples :
cmake -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SAMPLES=TRUE -DPAHO_WITH_SSL=TRUE make sudo make install
You can try the sample codes to see if they work
cd ~/git git clone https://github.com/eclipse/paho.mqtt.cpp.git cd paho.mqtt.cpp sudo apt-get install autoconf automake libtool make gcc -y ./bootstrap export PAHO_DIR=$PWD mkdir mybuild && cd mybuild $PAHO_DIR/configure --enable-samples (see page for more options) make #this may take a few minutes sudo make install
Note: The samples will be built inside the mybuild directory you can try running it now.
For more tests go to src/samples and try to make or build the samples:
sudo ldconfig 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:
Open new window for same shell, running:
Open another one and run:
You should see the messages being sent by the publisher on the subscriber page.
error cannot find -lpaho-mqttpp3:
Means 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:
Run sudo ldconfig to update the libraries