2

I'm trying to set up a mosquitto client to collect and log mqtt messages from my iot device which constantly sends it's gps data.

I'm using eclipse mosquitto with cpp wraper for this task.

I am sure that my device delivers data to broker, because on server mosquitto_sub -t mqtt_test --cafile ca.crt --client.crt --key client.key -p 8883 -q1 command returns exact same data which i'm sending. And on the device I get these logs:

...
[16] Client mosq/LDb;eF^;<P7fZd0d>f sending PUBLISH (d0, q1, r0, m5, 'mqtt_test', ... (175 bytes))
[16] Client mosq/LDb;eF^;<P7fZd0d>f received PUBACK (Mid: 5)
[16] Client mosq/LDb;eF^;<P7fZd0d>f sending PUBLISH (d0, q1, r0, m6, 'mqtt_test', ... (175 bytes))
[16] Client mosq/LDb;eF^;<P7fZd0d>f received PUBACK (Mid: 6)
...

Here's my server client code

main.cpp:

#include "mqtt_client.h"
#include <string.h>

#define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "127.0.0.1"
#define MQTT_PORT 8883

int main(int argc, char *argv[]) {
    std::cout << "gps logging app has started\n" << std::endl;
    class mqtt_client *iot_client;
    char client_id[] = CLIENT_ID;
    char host[] = BROKER_ADDRESS;
    int port = MQTT_PORT;

    if (argc > 1) {
        strcpy (host, argv[1]);
    }

    mosqpp::lib_init();
    iot_client = new mqtt_client(client_id, host, port, true);
    iot_client->loop_forever();

    mosqpp::lib_cleanup();

    return 0;
}

mqtt_client.cpp:

#include "mqtt_client.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>

#define MQTT_TOPIC "mqtt_test"

mqtt_client::mqtt_client(const char *id, const char *host, int port, bool clean) : mosquittopp(id, clean) {
    int ret;

    ret = tls_set("../ssl/ca.crt", nullptr, "../ssl/client.crt", "../ssl/client.key", nullptr);
    printf("tls_set ret %d %s\n", ret, strerror(ret));
    tls_insecure_set(true);

    ret = connect(host, port, DEFAULT_KEEP_ALIVE);
    printf("connect ret %d %s\n", ret, strerror(ret));

    ret = subscribe(nullptr, MQTT_TOPIC, 1);
    printf("subscribe ret %d %s\n", ret, strerror(ret));
}

void mqtt_client::on_subscribe(int mid, int qos_count, const int *granted_qos) {
    printf("on_subscribe mid %d qos_count %d\n", mid, qos_count);
}

void mqtt_client::on_message(const struct mosquitto_message *message) {
    int payload_size = MAX_PAYLOAD + 1;
    char buf[payload_size];
    memset(buf, 0, payload_size * sizeof(char));
    memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char));
    std::cout << "message recieved:\n" << buf << std::endl;
}

void mqtt_client::on_log(int level, const char *str) {
    printf("[%d] %s\n", level, str);
}

mqtt_client.h:

#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H
#include <cpp/mosquittopp.h>
#include <thread>

const int MAX_PAYLOAD = 50;
const int DEFAULT_KEEP_ALIVE = 60;

class mqtt_client : public mosqpp::mosquittopp {
public:
    mqtt_client (const char *id, const char *host, int port, bool clean);
    ~mqtt_client() override;

    void on_message(const struct mosquitto_message *message) override;
    void on_subscribe(int mid, int qos_count, const int *granted_qos) override;
    void on_log(int level, const char *str) override;
};

#endif //MQTT_CLIENT_H

tls_set, connect and subscribe returns 0.

Now the weird part. Everytime my iot devices publishes message, on_subscribe method gets called. This is how my output looks:

gps logging app has started

tls_set ret 0 Success
connect ret 0 Success
subscribe ret 0 Success
on_subscribe mid 40401736 qos_count 40401736
on_subscribe mid 40426056 qos_count 40426056
on_subscribe mid 40401128 qos_count 40401128
on_subscribe mid 40429016 qos_count 40429016
...

To my mind on_subscribe should be called in mqtt_client constructor when calling subscribe, but instead it does so everytime broker recieves message. And it seems that on_log, with on_message methods just doesn't work.

Please, help me find out why this doesn't work, or at least point in the right direction where to look for a problem.

KugisMugis
  • 143
  • 1
  • 4

0 Answers0