Commit 83be86d6 authored by Georg Vinogradov's avatar Georg Vinogradov

Create Mqtt example

parent 6ee63814
Pipeline #146533 passed with stage
in 46 seconds
# Created by Georg Vinogradov on 28.05.19
# Setting cmake version
cmake_minimum_required(VERSION 3.1...3.14)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
# Using C++ version 11 with threads for compiling
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
# Setting search paths for cmake modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
# Setting project name and description
project(MQTT VERSION 1.0
DESCRIPTION "MQTT demo"
LANGUAGES CXX)
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
# Adding executable target
add_executable(mqtt_demo main.cpp)
# Checking if everything was found
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
message(FATAL_ERROR "MQTT libraries not found!")
elseif (NOT MQTT_INCLUDE_DIR)
message(FATAL_ERROR "MQTT includes not found!")
else()
message("MQTT includes found in "${MQTT_INCLUDE_DIR})
message("MQTT libraries found in "${MQTT_LIBS})
endif()
# Adding include directory to a target
target_include_directories(mqtt_demo PUBLIC ${MQTT_INCLUDE_DIR})
# Linking libraries to target
target_link_libraries(mqtt_demo PUBLIC ${MQTT_LIBS})
// Created by Georg Vinogradov on 28.05.19.
//
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h> // Sleep on unix
#include "mqtt/client.h"
const std::string SERVER_ADDRESS = "tcp://localhost:1883";
const std::string PUB_ID = "publisher_cpp";
const std::string SUB_ID = "subscriber_cpp";
const std::string TOPIC = "topic/hello";
const std::string PAYLOAD = "265";
const int QOS = 1;
using namespace std;
class callback : public virtual mqtt::callback
{
mqtt::client& cli_;
// Callback for when connected
void connected(const std::string& cause) {}
// Callback for when the connection is lost.
// This will initiate the attempt to manually reconnect.
void connection_lost(const std::string& cause) {
cout << "\nConnection lost";
if (!cause.empty())
cout << ": " << cause << endl;
}
// Callback for when message is received
void message_arrived(mqtt::const_message_ptr msg) {
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
}
public:
callback(mqtt::client& cli) : cli_(cli) {}
};
void publish()
{
cout << "\nInitialzing publisher" << endl;
// Publisher
mqtt::client publisher(SERVER_ADDRESS, PUB_ID);
mqtt::connect_options pubConnOpts;
pubConnOpts.set_keep_alive_interval(20);
pubConnOpts.set_clean_session(true);
try {
cout << "\nPublisher connects to broker..." << endl;
publisher.connect(pubConnOpts);
cout << "\nPublisher prepares message: "<< PAYLOAD << endl;
auto pubmsg = mqtt::make_message(TOPIC, PAYLOAD);
sleep(2);
cout << "\nPublisher sends message: "<< PAYLOAD << endl;
pubmsg->set_qos(QOS);
publisher.publish(pubmsg);
cout << "\nPublisher disconnects..." << endl;
publisher.disconnect();
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void subscribe()
{
cout << "\nInitialzing subscriber" << endl;
//Subscriber
mqtt::client subscriber(SERVER_ADDRESS, SUB_ID);
callback cb(subscriber); // Subscriber needs callback
subscriber.set_callback(cb);
mqtt::connect_options subConnOpts;
subConnOpts.set_keep_alive_interval(20);
subConnOpts.set_clean_session(false);
subConnOpts.set_automatic_reconnect(true);
try {
cout << "\nSubscriber connects to broker..." << endl;
subscriber.connect(subConnOpts);
cout << "\nSubscriber subscribes to topic: "<< TOPIC << endl;
subscriber.subscribe(TOPIC, QOS);
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
while (tolower(cin.get()) != 'q'); // Listens for messages until user interrupts
}
int main(int argc, char* argv[])
{
thread sub (subscribe);
publish();
sub.join();
return 0;
}
# Created by Georg Vinogradov on 28.05.19
message("Searching for installed MQTT on your system...")
# Searching for include directory
if (NOT MQTT_INCLUDE_DIR)
find_path(MQTT_INCLUDE_DIR MQTTAsync.h HINTS "/usr/include/" "/usr/local/include/")
endif()
# Searching for libraries (without SSL support)
if (NOT MQTT_LIBS)
find_library(
MQTT_C_LIB
NAMES paho-mqtt3c)
find_library(
MQTT_A_LIB
NAMES paho-mqtt3a)
find_library(
MQTT_PP_LIB
NAMES paho-mqttpp3)
set(MQTT_LIBS ${MQTT_A_LIB} ${MQTT_C_LIB} ${MQTT_PP_LIB})
endif()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment