baxtex
Posts: 7
Joined: Tue Dec 22, 2015 2:32 pm

MQTT client in C on the RPi

Sat Dec 09, 2017 10:16 pm

I wanted to setup a MQTT client on my raspberry pi so it can publish to different topics. Now I googled alot and there seems to be many (old) libaries around with pretty bad documentation, no examples or anything. Is there a guide or some well documented libary for this out there?
Here are some links that I found: https://github.com/mqtt/mqtt.github.io/wiki/libraries


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

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 1:37 pm

The paho lib is what I use for Python and I'd imagine the C version would be OK as well

https://github.com/eclipse/paho.mqtt.c

baxtex
Posts: 7
Joined: Tue Dec 22, 2015 2:32 pm

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 6:10 pm

mattmiller wrote:
Sun Dec 10, 2017 1:37 pm
The paho lib is what I use for Python and I'd imagine the C version would be OK as well

https://github.com/eclipse/paho.mqtt.c
So I'm fairly new to C and is having some trouble installing it.

I cloned the git and then ran the commands

Code: Select all

make
and

Code: Select all

sudo make install
I then referenced

Code: Select all

#include "MQTTClient.h"
in my program.c file. Then I tried compiling it and it seems to work, I didn't get any errors. So I made a new method and copy pasted their sample for publishing, found here: https://github.com/eclipse/paho.mqtt.c/ ... _publish.c

Then I tried compiling again but I got serveral errors that methods that are in

Code: Select all

"MQTTClient.h"
isn't defined. What am I doing wrong here?

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

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 9:18 pm

Have you got libmqclient-dev installed?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3B*3, 3B+

Please post ALL technical questions on the forum. Do not send private messages.

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

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 9:39 pm

I'm no C expert but I had a go and after some trial and error I think its working

note: before I started I did

Code: Select all

sudo apt-get install libssl 
to install the OpenSSL development libraries (as mentioned in the git readme)

To actually then get one of the samples (MQTTClient_subscribe.c) to compile without a load of error message - I had to explicitly link it to the paho library

Code: Select all

gcc -o program MQTTClient_subscribe.c -l paho-mqtt3c
(I thought the whole point of doing the sudo make install was to install the library and make it available to all programs but as I said - I don't know much about C - hopefully an expert will come along and help us out on this point)

baxtex
Posts: 7
Joined: Tue Dec 22, 2015 2:32 pm

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 9:52 pm

mattmiller wrote:
Sun Dec 10, 2017 9:39 pm
I'm no C expert but I had a go and after some trial and error I think its working

note: before I started I did

Code: Select all

sudo apt-get install libssl 
to install the OpenSSL development libraries (as mentioned in the git readme)

To actually then get one of the samples (MQTTClient_subscribe.c) to compile without a load of error message - I had to explicitly link it to the paho library

Code: Select all

gcc -o program MQTTClient_subscribe.c -l paho-mqtt3c
(I thought the whole point of doing the sudo make install was to install the library and make it available to all programs but as I said - I don't know much about C - hopefully an expert will come along and help us out on this point)

Awesome! Adding -l paho-mqtt3c solved it! But just like you I would like to know if there is a better way to do it.
Now I just need to find out how to specify the password for the broker.

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

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 10:16 pm

if you look at
https://www.hivemq.com/blog/mqtt-client ... ahocclient
and scroll down to section about usernames/passwords that seems to show what to do

( i don't access any brokers that have usernames/passwords so I can't test it out)

baxtex
Posts: 7
Joined: Tue Dec 22, 2015 2:32 pm

Re: MQTT client in C on the RPi

Sun Dec 10, 2017 10:26 pm

mattmiller wrote:
Sun Dec 10, 2017 10:16 pm
if you look at
https://www.hivemq.com/blog/mqtt-client ... ahocclient
and scroll down to section about usernames/passwords that seems to show what to do

( i don't access any brokers that have usernames/passwords so I can't test it out)
Yep, found it, managed to configure it like this:

Code: Select all

  MQTTClient_deliveryToken token;
    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID,
    MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.username = USERNAME;
    conn_opts.password = PASSWORD;
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
When running the code, it seems to be able to connect and send the message beacuse I receive the delivery token and I can see on my broker that something is connected, just not any message. Will have to investigate further, but thanks for your help!

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 4 guests