Page 1 of 2

Simple MQTT with C++ and IDF

Posted: Wed Feb 07, 2018 11:51 pm
by Koulwa
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!

Re: Simple MQTT with C++ and IDF

Posted: Fri Feb 09, 2018 12:12 pm
by Nespressif
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/0 ... qtt-e-ssl/

Greetings

Re: Simple MQTT with C++ and IDF

Posted: Fri Feb 09, 2018 9:41 pm
by Koulwa
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.

Re: Simple MQTT with C++ and IDF

Posted: Sat Feb 10, 2018 1:45 am
by meowsqueak
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.

Re: Simple MQTT with C++ and IDF

Posted: Sat Feb 10, 2018 12:12 pm
by Nespressif
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,

Re: Simple MQTT with C++ and IDF

Posted: Sun Feb 11, 2018 5:51 pm
by Koulwa
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?

Re: Simple MQTT with C++ and IDF

Posted: Mon Feb 12, 2018 9:33 am
by Nespressif
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/tr ... ls/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/l ... -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,

Re: Simple MQTT with C++ and IDF

Posted: Mon Feb 12, 2018 8:53 pm
by Koulwa
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/tr ... ls/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/l ... -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. :(

Re: Simple MQTT with C++ and IDF

Posted: Tue Feb 13, 2018 8:04 am
by Nespressif
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,

Re: Simple MQTT with C++ and IDF

Posted: Tue Feb 13, 2018 8:15 am
by permal
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.