Skip to content

This repository provides a C++ wrapper designed to simplify integration with the Eclipse Paho MQTT C++ Client Library. It abstracts connection management, subscriptions, publishing, and callbacks, reducing boilerplate and improving code clarity.

License

Notifications You must be signed in to change notification settings

nap-it/paho.mqtt.cpp.wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

paho-mqtt-cpp-wrapper

This repository provides a C++ wrapper designed to simplify integration with the Eclipse Paho MQTT C++ Client Library (source code). It abstracts connection management, subscriptions, publishing, and callbacks, reducing boilerplate and improving code clarity.

If you find this code useful in your research, please consider citing:

@ARTICLE{10620279,
    author={Figueiredo, Andreia and Rito, Pedro and Luís, Miguel and Sargento, Susana},
    journal={IEEE Open Journal of Intelligent Transportation Systems}, 
    title={Enhancing Vehicular Network Efficiency: The Impact of Object Data Inclusion in the Collective Perception Service}, 
    year={2024},
    volume={5},
    number={},
    pages={454-468},
    keywords={Sensors;Road traffic;Vehicle dynamics;Sensor systems;Intelligent transportation systems;Intelligent sensors;Autonomous systems;Smart cities;Vehicular ad hoc networks;Autonomous mobility;collective perception;cooperative perception messages;smart city;vehicular network},
    doi={10.1109/OJITS.2024.3437206}}

Requirements

To use this wrapper, you’ll need some development tools and libraries.


1. Essential Tools

Make sure you have these installed on your system:

  • C++ Compiler (e.g., g++) — compiles your C++ code.
  • CMake — builds and manages your project.
  • Git — downloads source code from repositories.

Install on Ubuntu/Debian:

sudo apt update
sudo apt install build-essential cmake git

2. Required Libraries

These libraries are needed for MQTT functionality and secure connections:

  • libssl-dev — enables secure (SSL/TLS) MQTT connections.
  • libcurl4-openssl-dev — optional, only needed if you build Paho with CURL support.
  • pthread — for multi-threading (usually included by default).

Install system libraries (Ubuntu/Debian):

sudo apt install libssl-dev libcurl4-openssl-dev

Eclipse Paho MQTT Libraries

You need both the C and C++ versions of the Paho MQTT library:

Step-by-step installation:

  1. Install Paho MQTT C library:

    git clone https://github.com/eclipse/paho.mqtt.c.git
    cd paho.mqtt.c
    git checkout v1.3.13
    cmake -Bbuild -H. -DPAHO_ENABLE_TESTING=OFF -DPAHO_BUILD_STATIC=ON \
          -DPAHO_WITH_SSL=ON -DPAHO_HIGH_PERFORMANCE=ON
    cmake --build build/ --target install
    sudo ldconfig
    cd ..
  2. Install Paho MQTT C++ library:

    git clone https://github.com/eclipse/paho.mqtt.cpp.git
    cd paho.mqtt.cpp
    git checkout v1.3.2
    cmake -Bbuild -H. -DPAHO_BUILD_STATIC=ON \
          -DPAHO_BUILD_DOCUMENTATION=OFF -DPAHO_BUILD_SAMPLES=ON
    cmake --build build/ --target install
    sudo ldconfig
    cd ..

Tip: These commands will download, build, and install the required libraries.


3. Logging

This wrapper uses spdlog for logging.
spdlog is a header-only library, so you don’t need to compile or install it—just download or clone the repository and add the include folder to your project’s include path.

Version used: v1.11.0

How to add spdlog:

  1. Download or clone spdlog:
    git clone https://github.com/gabime/spdlog.git
    git checkout v1.11.0
  2. In your CMakeLists.txt, add:
    include_directories(${CMAKE_SOURCE_DIR}/spdlog/include)

CMake Integration

Add the wrapper to your project via CMakeLists.txt:

# Include directories
include_directories(${CMAKE_SOURCE_DIR}/spdlog/include)
include_directories(${CMAKE_SOURCE_DIR}/paho-mqtt-cpp-wrapper/include)
include_directories(${CMAKE_SOURCE_DIR})

# Add the wrapper subdirectory
add_subdirectory(paho-mqtt-cpp-wrapper)

# Link the wrapper and dependencies to your target
target_link_libraries(
    mqttwrapper-example
    paho-mqtt-cpp-wrapper
    pthread
    paho-mqttpp3
    paho-mqtt3as
)

Replace mqttwrapper-example with the name of your target.

Minimal Usage Example

This demonstrates:

  • Creating an MQTT configuration
  • Providing a message callback
  • Connecting via the wrapper
  • Publishing messages
#include "mqttwrapper.h"
#include <thread>
#include <chrono>
#include <spdlog/spdlog.h>

// Callback for incoming messages
void on_message(std::string topic, std::string message) {
    spdlog::info("Message received on {}: {}", topic, message);
}

int main() {
    // Optional: set logging level
    spdlog::set_level(spdlog::level::info);

    // Basic MQTT configuration
    data_mqtt_server config;
    config.address = "tcp://localhost:1883";
    config.client_id = "example_client_1";
    config.publish_topic = "test/topic";
    config.subscription_topic = {"test/topic"};

    // Optional authentication
    config.user_name = "";
    config.password = "";

    // Create the wrapper instance
    MqttWrapper mqtt(config, on_message);

    // Wait for connection
    while (!mqtt.is_connected()) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    // Publish periodically
    for (;;) {
        mqtt.publish(config.publish_topic, "Hello from wrapper");
        spdlog::info("Published message");
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

For NAP members, an example can be found in the repository code-examples (link)

👥 Authors & Contact

For bug reports, feature requests, or questions, please use the issue tracker or contact:

License

paho.mqtt.cpp.wrapper is licensed under LGPLv3, see license file for details.

About

This repository provides a C++ wrapper designed to simplify integration with the Eclipse Paho MQTT C++ Client Library. It abstracts connection management, subscriptions, publishing, and callbacks, reducing boilerplate and improving code clarity.

Resources

License

Stars

Watchers

Forks

Packages

No packages published