Mosquitto C++ sample code to publish message

I start using mosquitto in a C++ program and I think documentation could be a little bit improved by some examples. To use mosquitto, there is a mosquittopp class acting as a wrapper on top of the mosquitto lib. Basically to create your own program you have to herit from that one and it is easy then.

Mosquitto lib can be managed as a thread or directly by calling the different sub function. In my point of view the thread approach is the better as otherwize you have to manage connection / disconnection manually. Mosquitto lib is then event managed, so you can subscribe to the callback event by surcharging the existing function.

Here is an exemple for a program just needing to publish messages to a broker.

Continue reading

MQTT on Raspeberry PI

MQTT (MQ Telemetry Transport) is a Message Queuing system for machine to machine communications. It allows communication of mobile systems over high latency, low bandwidth and poor quality networks. It manage communications over TCP (not over Http) to optimize the size of messages and it manages different quality of services. Depending of it, messages can be dropped, received multiple times of you can have the insurance to receive the message one and only one time.

It makes this protocol really interesting for the communication between a raspberry PI and a server when this communication is event driven and the communication link looks like a Edge/3G channel.

Mosquitto is an open source implementation for MQTT.

Here are some tips on how I implemented it, for a demonstration purpose

Continue reading