N CR7
N CR7

Reputation: 41

Return message subscribe using Mosquitto?

I have below code of Mosquitto that subscribe to the particular topic in c++.

main.cpp

/*
     * main.cpp
     *
     *  Created on: Jul 28, 2016
     *      Author: nilav
     */
#include <iostream>
#include "myMosq.h"
#include <string.h>
#include <unistd.h>

using namespace std;
int main() {

    myMosq *mosq;
    mosq = new myMosq("unique","topic", "localhost",1883);
    int res;
    while(1) {
        char tstr[500] ;
//      cin.getline(tstr,sizeof(tstr));
                sleep(2);
//              mosq->send_message(tstr);
                mosq->receive_message(tstr);

            res = mosq->loop();                     // Keep MQTT connection
            if (res)
                mosq->reconnect();
        }
    }

myMosq.h

/*
 * myMosq.h
 *
 *  Created on: Jul 28, 2016
 *      Author: nilav
 */

#ifndef MYMOSQ_H_
#define MYMOSQ_H_

#include <mosquittopp.h>
#include <mosquitto.h>
class myMosq : public mosqpp::mosquittopp
{
private:
 const char     *     host;
 const char    *     id;
 const char    *     topic;
 int                port;
 int                keepalive;

 void on_connect(int rc);
 void on_disconnect(int rc);
 void on_subscribe(int mid, int qos_count, const int *granted_qos);
public:
 myMosq(const char *id, const char * _topic, const char *host, int port);
 ~myMosq();
 bool send_message(const char * _message);
 bool receive_message(const char * _message);
};

#endif

myMosq.cpp

#include <cstdio>
#include <cstring>
#include <iostream>

#include "myMosq.h"
#include <mosquittopp.h>

using namespace std;

myMosq::myMosq(const char * _id,const char * _topic, const char * _host, int _port) : mosquittopp(_id)
 {
 mosqpp::lib_init();        // Mandatory initialization for mosquitto library
 this->keepalive = 60;    // Basic configuration setup for myMosq class
 this->id = _id;
 this->port = _port;
 this->host = _host;
 this->topic = _topic;
 connect_async(host,     // non blocking connection to broker request
 port,
 keepalive);
 loop_start();            // Start thread managing connection / publish / subscribe
 };


myMosq::~myMosq() {
 loop_stop();            // Kill the thread
 mosqpp::lib_cleanup();    // Mosquitto library cleanup
 }

//bool myMosq::send_message(const  char * _message)
// {
// int ret = publish(NULL,this->topic,strlen(_message),_message,1,false);
// cout << ret;
// return ( ret == MOSQ_ERR_SUCCESS );
// }

bool myMosq::receive_message(const char * message)
 {
    int set = subscribe(NULL, this->topic,2);
    return set;
 }

void myMosq::on_disconnect(int rc) {
 std::cout << ">> myMosq - disconnection(" << rc << ")" << std::endl;
 }


void myMosq::on_connect(int rc)
 {
 if ( rc == 0 ) {
 std::cout << ">> myMosq - connected with server" << std::endl;
 } else {
 std::cout << ">> myMosq - Impossible to connect with server(" << rc << ")" << std::endl;
 }
 }

void myMosq::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
    std::cout << ">> subscription succeeded (" << mid << ") " << std::endl;
    printf("Subscription succeeded.\n");
}

Now when I issue following command from terminal of ubuntu

mosquitto_pub -h localhost -t "topic" -m "Hello MQTT"

nothing is displayed in the program output. But I want a code that display the particular message produced in particular topic when subscribed. Any help will be appreciated.

Upvotes: 0

Views: 4972

Answers (1)

Richard
Richard

Reputation: 11

There is an error in the way you set up the bool myMosq::receive_message(const char * message) function. As far as MQTT works, you initially subscribe once to a topic and then use the provided loop function of the mosquittopp wrapper to check for changed data on the corresponding topics. Now you only have to specify, what should happen on the void on_message(const struct mosquitto_message* message) callback and grab and format the message object according to the used data type.

For example, to grab some kind of char-related data my overload implementation looks like this:

void MQTTSubscriber::on_message(const mosquitto_message* message) { cout << "Subscriber " << id << " received message of topic: " << message->topic << " Data: " << reinterpret_cast<char*>(message->payload) << "\n"; }

The connect_async() does all the work for establishing and keeping your broker connection and the loop_start() function handles the thread-separate callback functions.

Best regards, Richard

Upvotes: 1

Related Questions