Page 1 of 1

[FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Tue Dec 12, 2017 7:09 pm
by Arkaik
Hi,

I'm using Kolban's esp32 port available here https://github.com/nkolban/esp32-snippe ... embedded_c

Everything compile fine and I'm able to publish into my topics on my remote MQTT server.

However after some time (around 5 minutes) I don't receive messages anymore. I investigated a little into paho sources and I realized that MQTTFreeRTOS.h is never included nor used anywhere when using MQTTClient-C.

Code: Select all

$ rgrep -n MQTTFreeRTOS.h
components/paho_mqtt_embedded_c/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c:18:#include "MQTTFreeRTOS.h"
Indeed high-level MQTTClient include linux sources but not freeRTOS ones.

MQTTClient.h

Code: Select all

/*******************************************************************************
 * Copyright (c) 2014, 2015 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
 *    Ian Craggs - documentation and platform specific header
 *******************************************************************************/

#if !defined(__MQTT_CLIENT_C_)
#define __MQTT_CLIENT_C_

#if defined(__cplusplus)
 extern "C" {
#endif

#if defined(WIN32_DLL) || defined(WIN64_DLL)
  #define DLLImport __declspec(dllimport)
  #define DLLExport __declspec(dllexport)
#elif defined(LINUX_SO)
  #define DLLImport extern
  #define DLLExport  __attribute__ ((visibility ("default")))
#else
  #define DLLImport
  #define DLLExport
#endif
#include "MQTTLinux.h"
#include "MQTTPacket.h"
#include "stdio.h"

#if defined(MQTTCLIENT_PLATFORM_HEADER)
/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value
 * into a string constant suitable for use with include.
 */
#define xstr(s) str(s)
#define str(s) #s
#include xstr(MQTTCLIENT_PLATFORM_HEADER)
#endif
So to use MQTT FreeRTOS sources I replaced

Code: Select all

#include "MQTTLinux.h"
with

Code: Select all

#include "MQTTFreeRTOS.h"
However I've got the following error when compiling

Code: Select all

$ make
CC MQTTClient-C/src/MQTTClient.o
In file included from <project_path>/components/paho_mqtt_embedded_c/MQTTClient-C/src/MQTTClient.h:35:0,
                 from <project_path>/components/paho_mqtt_embedded_c/MQTTClient-C/src/MQTTClient.c:16:
<project_path>/components/paho_mqtt_embedded_c/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.h:20:22: fatal error: FreeRTOS.h: No such file or directory
compilation terminated.
<esp-idf_path>/make/component_wrapper.mk:202 : la recette pour la cible « MQTTClient-C/src/MQTTClient.o » a échouée
make[1]: *** [MQTTClient-C/src/MQTTClient.o] Erreur 1
<esp-idf_path>/make/project.mk:386 : la recette pour la cible « paho_mqtt_embedded_c-build » a échouée
make: *** [paho_mqtt_embedded_c-build] Erreur 2
The linker doesn't seems to be able to find freertos directory. Indeed in my main app I use the following includes

Code: Select all

#include <esp_wifi.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <lwip/sockets.h>
#include <nvs_flash.h>
#include "driver/i2c.h"
When paho uses the following one

Code: Select all

#include "FreeRTOS.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP.h"
#include "semphr.h"
#include "task.h"
The header is in freertos path, however I don't want to modify all the paho sources to use the same include format.

How can I include freertos directory without having to modify esp-idf makefiles ?
Is that normal that paho doesn't use MQTTFreeRTOS.h by default ? Or at least is surrounded with #ifdef #endif to use freeRTOS sources when deployed on freeRTOS ?
Am I doing the good thing to use freeRTOS paho sources instead of linux ones ?

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Moreover, I also realized that FreeRTOS.h is including files that doesn't exit on my computer, so external sources which are FreeRTOS_Sockets.h FreeRTOS_IP.h.

Where can I find those files ?

Thanks in advance

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 4:27 am
by Gfast2
Hi Arkaik,

AFAIK FreeRTOS has its own IP stack, which is not cost free. So in ESP-IDF we use lwIP.

The included header you mensioned in the library looks like from the FreeRTOS stack but not lwIP. I just tell you want I think, but still not googling around about them. Perhaps it helps.

BTW, I have spent nearly half year to port a another library Open62541 (OPC UA protocol) to ESP-IDF. Its really not that simple as I expacted. But I've learned tons of stuff meanwhile.

Cheers

Gfast2

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 10:40 am
by Arkaik
Hi Gfast2,

In fact, I'm using lwIP to handle the wifi connection. I'm using the same method than for the example here : https://github.com/nkolban/esp32-snippe ... ple/main.c

My question is about low-level implementation of Timer and Network structures which MQTTClient-C uses.

You can see that both MQTTLinux.h and MQTTFreeRTOS.h are quite similar even if having differences. However MQTTFreeRTOS.h is never used or included anywhere so my question is : Why would there be a MQTTFreeRTOS.h if never used?

What I understand is I can either use MQTTLinux.h, MQTTCC3200.h or MQTTFreeRTOS.h depending on the targetted board/OS. Mine is ESP32DevKitC with FreeRTOS so I need to use MQTTFreeRTOS.h right ?

Now if I'm right I just don't understand how to integer paho using MQTTFreeRTOS.h (it works with linux one) because it's using headers that are not in the project architecture, so maybe I can find those external sources somewhere else but I'm surprised that it's using "out-of-project" files.

From what you say I could not use FreeRTOS IP stack because it's not cost free ? So why is this project using this IP stack instead of lwIP?

Thanks for your answer.

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 11:36 am
by Gfast2
From what you say I could not use FreeRTOS IP stack because it's not cost free ? So why is this project using this IP stack instead of lwIP?
I believe make FreeRTOS and lwIP works together just you've see on our ESP32s are also not easy. :lol:

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 11:49 am
by bcanustundag
Hi Arkaik,

For what it's worth, there is an example mqtt client implementation which employs esp32's tcp/ip socket implementation ( uses lwip of esp32-idf).

It can be the case that you've already seen it but here it is: https://github.com/tuanpmt/espmqtt

Baris

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 12:26 pm
by Arkaik
Damn, so you think the port has only be done for Linux and not for FreeRTOS ?

I though all the development libraries were given with esp-idf, so you mean that FreeRTOS wifi stack is not given by esp-idf and I should find it somewhere else and integrate it to my project ?

Can you give me some advices on how I could go further ?

I find strange that nobody ever used MQTT for FreeRTOS on ESP32...

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 12:53 pm
by WiFive
nkolban just grabbed paho sources, modified Linux version to work with esp32 because esp32 has socket api wrapper for lwip. then he left the rest of the paho files there even if they are not needed.

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Wed Dec 13, 2017 2:07 pm
by Gfast2
Kolban is incredible, He is trying to do REALLY the advanced things! Highly respect!

---

It would be much cooler if some of us can make tutorials about these things, or just make very nice README, or just somehow make them cleaner. But I do understand, if I've got these library up'n running, I would not have enough power/time to make them that perfect...

Cheers

Gfast2

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Thu Dec 14, 2017 2:38 am
by vonnieda
Arkaik wrote:Damn, so you think the port has only be done for Linux and not for FreeRTOS ?

I though all the development libraries were given with esp-idf, so you mean that FreeRTOS wifi stack is not given by esp-idf and I should find it somewhere else and integrate it to my project ?

Can you give me some advices on how I could go further ?

I find strange that nobody ever used MQTT for FreeRTOS on ESP32...

The AWS IOT component that comes with ESP-IDF includes paho and a working port. It has some bits in there to allow it to work with AWS IOT but the code is pretty close to what is needed to use paho by itself.

You can find the interesting bits at https://github.com/espressif/esp-idf/tr ... s_iot/port

Jason

Re: [FreeRTOS] How to use paho mqtt with MQTTFreeRTOS sources ?

Posted: Thu Dec 14, 2017 1:31 pm
by kurtzweber
bcanustundag wrote:It can be the case that you've already seen it but here it is: https://github.com/tuanpmt/espmqtt
Baris
+1, I found this tutorial that describes how to use that library... for me it works fine!
http://www.lucadentella.it/en/2017/12/0 ... qtt-e-ssl/