Simple MQTT with C++ and IDF

Koulwa
Posts: 11
Joined: Wed Feb 07, 2018 11:12 pm

Simple MQTT with C++ and IDF

Postby Koulwa » Wed Feb 07, 2018 11:51 pm

So far, have loved the nkolban/esp32-snippets. many thanks to nkolban!

Really, just looking for the best way to include MQTT with C++ only using the IDF (NOT Arduino).

I do have an issue though, trying to get MQTT to work, while using the other C++ libraries to work as well. I really don't need anything super fancy for the time. I was trying to use the AWS cpp library.

I'm new to binding c/c++ code together, so i'm trying to udnerstand how to pull it off.

Following the logic BLEDevice::init("");
I implemented
AWS::init("192.168.0.132", 1883);

but get compiler errors:
CXX build/main/main.o
In file included from C:/msys32/home/tyler/Pure-ESP32/ESP32/BaseStationAlphaAWS/main/main.cpp:15:0:
C:/msys32/home/tyler/Pure-ESP32/ESP32/BaseStationAlphaAWS/components/cpp_utils/AWS.h:13:43: fatal error: aws_iot_mqtt_client_interface.h: No such file or directory
compilation terminated.
make[1]: *** [/home/ESP32/esp-idf/make/component_wrapper.mk:274: main.o] Error 1
make: *** [C:/msys32/home/ESP32/esp-idf/make/project.mk:450: component-main-build] Error 2

Also, was able to get another library for MQTT working on the ESP32, https://github.com/tuanpmt/esp32-mqtt
But again, need to call that code in C++. Not sure how to go about doing this, as I was immediately getting compile errors when switching to C++

Thanks!

Nespressif
Posts: 33
Joined: Tue Sep 12, 2017 11:25 am

Re: Simple MQTT with C++ and IDF

Postby Nespressif » Fri Feb 09, 2018 12:12 pm

Hi, I have the same problem, I've tried several mqtt libraries and managed to make espmqtt work but only with C code, not C++. But I have a problem, stop sending information to Thingspeak, about 15 minutes. Therefore, I still haven't been able to use the mqtt protocol because of this, I still use REST API.

I would like to get all the information I can about ESP32/MQTT (now I'm going to try AWS) and how to integrate C++ code with IDF.

This post may help you with mqtt/espmqtt http://www.lucadentella.it/en/2017/12/04/esp32-28-mqtt-e-ssl/

Greetings

Koulwa
Posts: 11
Joined: Wed Feb 07, 2018 11:12 pm

Re: Simple MQTT with C++ and IDF

Postby Koulwa » Fri Feb 09, 2018 9:41 pm

This post may help you with mqtt/espmqtt http://www.lucadentella.it/en/2017/12/0 ... qtt-e-ssl/


^ So this uses the same library that I mentioned before that was working, tuanpmt/esp32-mqtt https://github.com/tuanpmt/esp32-mqtt


As an update, I am able to get it to work with C++ by moving the settings struct w/in the mqtt_start() function, rather than passing it via pointer. Also, having the call back functions in the mqtt.c file.

Code: Select all

    mqtt_client *mqtt_start() {

    strcpy(p_settings.host , "172.20.10.3");
    p_settings.port = 1883;
    strcpy(p_settings.client_id , "espmqtt");
    strcpy(p_settings.username, "user");
    strcpy(p_settings.password, "pass");

    strcpy(p_settings.lwt_topic, "/test");
    strcpy(p_settings.lwt_msg, "offline");

    p_settings.clean_session = 0;
    p_settings.keepalive = 120;
    p_settings.connected_cb = mqtt_connected_callback;
    p_settings.disconnected_cb = mqtt_disconnected_callback;


    p_settings.lwt_qos = 0;
    p_settings.lwt_retain = 0;
    p_settings.lwt_msg_len = strlen(p_settings.lwt_msg);

    mqtt_settings* settings = &p_settings;


    terminate_mqtt = false;

    // ... rest of start code continues   


then when including the library, wrap it much like the app_main()

Code: Select all

extern "C" {
   #include "mqtt.h"
   void app_main(void);
}

// ... other code ... //

void app_main(void) {

   boot = new BootWiFi();   
   boot->boot();
   mqtt_start();
}



I have noticed some issues with the MQTT Library though, pretty sure its leaking memory somewhere because you eventually get a stack overflow like what you are seeing with your 15 min mark (just depends on how often you're calling publish, your connection status, and if it was successful, its a fragile library from what I can tell). I'm getting the ESP32 with JTAG next week and will debug that then, so I can get more info.


Very new with the ESP32 (like 2 days of work worth) but I think we can get this working. kolban seems like the authority on this (these libraries are so amazingly helpful) maybe he can give a few pointers.

Ideally, I'd like to get a well supported and well-tested MQTT code setup in C++, because well, it's going to be better in the long run. But the above helped me get past this point for now.

meowsqueak
Posts: 97
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Simple MQTT with C++ and IDF

Postby meowsqueak » Sat Feb 10, 2018 1:45 am

The problem with tuanpmt/espmqtt is that it's fundamentally broken. It treats TCP as a packet interface, not as a stream, so it only sends or receives the first message in a transmission. It will drop messages all over the place. I looked into fixing it, but in the end it was simpler to just use 256dpi/esp-mqtt instead, which doesn't have security support (or didn't when I last looked into it) but works much more reliably.

Nespressif
Posts: 33
Joined: Tue Sep 12, 2017 11:25 am

Re: Simple MQTT with C++ and IDF

Postby Nespressif » Sat Feb 10, 2018 12:12 pm

Very good contribution, this component did not know it (I have tried paho, mosquito... etc) but I'm going to try it, because I don't care that don't have security support. I've been testing mqtt on AWS IoT and it's great, being integrated with IDF.

Greetings,

Koulwa
Posts: 11
Joined: Wed Feb 07, 2018 11:12 pm

Re: Simple MQTT with C++ and IDF

Postby Koulwa » Sun Feb 11, 2018 5:51 pm

With the aws iot, do you have to use it on their platform or can you use the mqtt portion for another service IE: mosquitoe?

Also, did you implement it in c, or C++ with the snippets aws iot code?

Nespressif
Posts: 33
Joined: Tue Sep 12, 2017 11:25 am

Re: Simple MQTT with C++ and IDF

Postby Nespressif » Mon Feb 12, 2018 9:33 am

Koulwa wrote:With the aws iot, do you have to use it on their platform or can you use the mqtt portion for another service IE: mosquitoe?

Also, did you implement it in c, or C++ with the snippets aws iot code?


I've done some tests with the example of espressif https://github.com/espressif/esp-idf/tree/master/examples/protocols/aws_iot, I think you have to use AWS IoT Device SDK for Embedded C or AWS IoT C++ Device SDK, because the connection uses a certificate to validate itself. I don't think you can use mosquito nets or another library to connect to AWS IoT Core, but I don't know.

And as you can see, you have a C SDK and another one for C++.https://docs.aws.amazon.com/es_es/iot/latest/developerguide/iot-sdks.html

Although I'm not an expert on the subject, I've only done some initial testing. Let's see if anyone can bring us their experience with AWS, although it's impressive the amount of services Amazon offers, I'm not surprised that they are the number 1 in the cloud.

Greetings,

Koulwa
Posts: 11
Joined: Wed Feb 07, 2018 11:12 pm

Re: Simple MQTT with C++ and IDF

Postby Koulwa » Mon Feb 12, 2018 8:53 pm

Nespressif wrote:
Koulwa wrote:With the aws iot, do you have to use it on their platform or can you use the mqtt portion for another service IE: mosquitoe?

Also, did you implement it in c, or C++ with the snippets aws iot code?


I've done some tests with the example of espressif https://github.com/espressif/esp-idf/tree/master/examples/protocols/aws_iot, I think you have to use AWS IoT Device SDK for Embedded C or AWS IoT C++ Device SDK, because the connection uses a certificate to validate itself. I don't think you can use mosquito nets or another library to connect to AWS IoT Core, but I don't know.

And as you can see, you have a C SDK and another one for C++.https://docs.aws.amazon.com/es_es/iot/latest/developerguide/iot-sdks.html

Although I'm not an expert on the subject, I've only done some initial testing. Let's see if anyone can bring us their experience with AWS, although it's impressive the amount of services Amazon offers, I'm not surprised that they are the number 1 in the cloud.

Greetings,



Cool cool. Yeah was able to get the amazon IOT code to work, and did get everything pushing to their platform. Unfortunatly, i'm trying to send to a local server that will be kept on site.

I am interested in exploring amazon IOT in other contexts, as i was able to get it setup fairly easily. Man. I didn't think MQTT would be so hard with the IDF. :(

Nespressif
Posts: 33
Joined: Tue Sep 12, 2017 11:25 am

Re: Simple MQTT with C++ and IDF

Postby Nespressif » Tue Feb 13, 2018 8:04 am

Koulwa wrote: I didn't think MQTT would be so hard with the IDF. :(


Neither do I, with how easy it is with arduino IDE, for example. Last week I downloaded the 256dpi/esp-mqtt complement and it appears in the menuconfig, but I haven't tried it yet to send it to thingspeak.

Greetings,

permal
Posts: 226
Joined: Sun May 14, 2017 5:36 pm

Re: Simple MQTT with C++ and IDF

Postby permal » Tue Feb 13, 2018 8:15 am

If you want to go all-out C++, have a look at my Smooth framework for the IDF, and its accompanying test project. It has an implementation for MQTT (QoS 0-2, but no persistent storage) in C++. As it builds on on top of the Smooth framework, it's not something you can use as you would other Arduino-like libraries, such as PubSubClient, so you'd have to build your entire application based on Smooth. Smooth is currently GPL-licensed.

Who is online

Users browsing this forum: Baidu [Spider] and 5 guests