Skip to content

benjenq/BeMqttSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT client for c++ Sample (BeMqttClient)

A customize MQTT Client for C++ based on Paho.mqtt.cpp. Tested on macOS & Linux & Windows

Feature:

  • Class BeMqttClient.h implemented from mqtt/async_client. More easy to use.

Dependency libraries:

  • paho.mqtt.c : Eclipse Paho C Client Library for the MQTT Protocol
  • paho.mqtt.cpp : Eclipse Paho MQTT C++ Client Library

For Ubuntu / Debian (Linux)

Install script:

sudo apt install git build-essential gcc make cmake cmake-gui cmake-curses-gui libssl-dev doxygen graphviz libcppunit-dev -y
cd ~
git clone https://github.com/eclipse/paho.mqtt.cpp
cd paho.mqtt.cpp
./install_paho_mqtt_c.sh
cmake -Bbuild -H. -DPAHO_BUILD_DOCUMENTATION=TRUE -DPAHO_BUILD_SAMPLES=FALSE
sudo cmake --build build/ --target install
sudo ldconfig

For Windows

  1. Install CMake (GUI), MinGW-w64,OpenSSL, and add [installed]/bin to PATH parameter.
  2. Install Eclipse Paho C/C++ Client Library via CMake,
  3. Put include\*.* | lib\*.* into [MinGW-w64 insatlled]/ include | lib directories.
  • TODO: Detail step of 1.2.3...

How to use:

  1. Put your code file(s) .cpp in src/ directory.
  2. Just add #include <BeMqttClient.h> or #include "../include/BeMqttClient.h"
  3. make run in console to complie & launch your code.
  4. (Optinal)* Recommand to open this project via Microsoft Visual Studio Code because this project was configured for this IDE tool.

Sample Code:

#include <iostream>
#include "../include/BeMqttClient.h"

void v_MsgArrived(BeMqttClient *client, mqtt::const_message_ptr msg)
{
   //implement 
   std::cout << "Message arrived:" << endl;
   std::cout << "\ttopic: '" << msg->get_topic() << "' "
             << "\tmessage: '" << msg->to_string() << "'" << std::endl;
}
int main()
{
   std::string BrokerAddr = "tcp://localhost:1883"; //Your wrking MQTT broker IP or DNS with port number.
   std::string clientid = "ClientTest";

   BeMqttClient client1(clientid,BrokerAddr);
   client1.setMessageArrived(v_MsgArrived); //Message arrived callback.
   client1.connect();
   client1.subscribe("#", QosLevel::QoSLv1);
   client1.publish("TestTpoic","Test");

   std::cout << "Press 'q' [Enter] to quit" << std::endl;
   while (std::tolower(std::cin.get()) != 'q');
   client1.disConnect();
}

Console output:

<0x563ee1e02330> BeMqttCallBack created.
<0x7ffc962b13b0> [ClientTest] BeMqttClient created.
[ClientTest] BeMqttClient Connecting to [tcp://localhost:1883]....
[ClientTest] BeMqttClient Subscribe [#].....OK!
Press 'q' [Enter] to quit
Message arrived:
        topic: 'TestTpoic'      message: 'Test'
q
[ClientTest] BeMqttClient disConnect()...
<0x563ee1e02330> ~BeMqttCallBack
<0x7ffc962b13b0> [ClientTest] ~BeMqttClient

For more detail, see src/SampleCode.cpp

About

A customize MQTT Client for C++ based on Paho.mqtt.cpp.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published