Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Arduino Arduino Library for ESP8266, ESP32, Portenta_H7, STM32 and RP2040W asynchronous MQTT client implementation. This library, ported to support ESP32, WT32_ETH01 (ESP32 + LAN8720), ESP8266, Portenta_H7 (Ethernet or WiFi) and STM32 (LAN8742A or LAN8720 Ethernet), Teensy 4.1 using QNEthernet, RASPBERRY_PI_PICO_W with CYW43439 WiFi. Currently s…

License

Notifications You must be signed in to change notification settings

khoih-prog/AsyncMQTT_Generic

Repository files navigation

AsyncMQTT_Generic Library (Async MQTT Client for ESP8266, ESP32, etc.)

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Important Notes for ESP8266

The new ESP8266 cores v3.0.2+ introduces many new breaking features, such as replacing axtls with bearssl, as well as good ones such as LwIP Ethernet W5500lwIP, W5100lwIP and ENC28J60lwIP libraries.

Because the original ESPAsyncTCP library is not well maintained to cope with dynamic environment of new cores, features, etc., this library has to use the forked ESPAsyncTCP library, which has been created to keep up with the fast changes and avoid compile errors in future cores.

Hopefully the bearssl feature of the new ESP8266 cores will be added to the forked ESPAsyncTCP library in the near future to support SSL.



Why do we need this AsyncMQTT_Generic library

Features

This AsyncMQTT_Generic library is based on and modified from Marvin Roger's async-mqtt-client Library, to provide support to many more boards besides ESP32/ESP8266, such as STM32F, Portenta_H7, Teensy 4.1, etc. boards. Those supported boards can be used with ESP8266/ESP32’s WiFi, LAN8742A Ethernet, Portenta_H7 WiFi/Ethernet, Teensy 4.1 QNEthernet, RASPBERRY_PI_PICO_W with CYW43439 WiFi, ESP32 with LwIP W5500 or ENC28J60.

Why Async is better

  • Using asynchronous network means that you can handle more than one connection at the same time
  • You are called once the request is ready and parsed
  • When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background
  • Speed is OMG
  • Easy to use API, HTTP Basic and Digest MD5 Authentication (default), ChunkedResponse
  • Easily extensible to handle any type of content
  • Supports Continue 100
  • Async WebSocket plugin offering different locations without extra servers or ports
  • Async EventSource (Server-Sent Events) plugin to send events to the browser
  • URL Rewrite plugin for conditional and permanent url rewrites
  • ServeStatic plugin that supports cache, Last-Modified, default index and more
  • Simple template processing engine to handle templates

Currently supported Boards

This AsyncMQTT_Generic library currently supports these following boards:

  1. ESP32

  2. ESP8266

  3. WT32_ETH01 (ESP32 + LAN8720A)

  4. STM32F/L/H/G/WB/MP1 boards with built-in Ethernet LAN8742A such as :

  • Nucleo-144 (F429ZI, F767ZI)
  • Discovery (STM32F746G-DISCOVERY)
  • All STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet
  1. STM32F4/F7 boards using Ethernet LAN8720 such as :
  • Nucleo-144 (F429ZI, NUCLEO_F746NG, NUCLEO_F746ZG, NUCLEO_F756ZG)
  • Discovery (DISCO_F746NG)
  • STM32F4 boards (BLACK_F407VE, BLACK_F407VG, BLACK_F407ZE, BLACK_F407ZG, BLACK_F407VE_Mini, DIYMORE_F407VGT, FK407M1)

Note: Must use with STM32 core v2.2.0-

  1. Portenta_H7 using either Murata WiFi or Vision-shield Ethernet

  2. Teensy 4.1 using QNEthernet Library

  3. RASPBERRY_PI_PICO_W with CYW43439 WiFi using arduino-pico core v2.4.0+

  4. ESP32 boards using LwIP ENC28J60 Ethernet

  5. ESP32 boards using LwIP W5500 Ethernet


To-be-supported Boards

  1. Any future board with supporting AsyncTCP library
  2. ESP32 boards using LwIP W6100 Ethernet


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. ESP32 Core 2.0.6+ for ESP32-based boards. Latest release
  3. ESP8266 Core 3.1.1+ for ESP8266-based boards. Latest release.
  4. Arduino Core for STM32 v2.4.0+ for STM32F/L/H/G/WB/MP1 boards using built-in LAN8742A. GitHub release. Use core v2.2.0- for LAN8720.
  5. Arduino mbed_portenta core 3.5.4+ for Arduino (Use Arduino Board Manager) Portenta_H7-based boards. GitHub release
  6. Earle Philhower's arduino-pico core v2.7.1+ for RASPBERRY_PI_PICO_W with CYW43439 WiFi, etc. GitHub release
  7. Teensy core v1.57+ for Teensy 4.1. GitHub release
  8. KH_Forked ESPAsyncTCP v1.3.0+ for ESP8266. To install manually for Arduino IDE
  9. AsyncTCP v1.1.1+ for ESP32.
  10. STM32Ethernet library v1.3.0+ for STM32 using built-in Ethernet LAN8742A on (Nucleo-144, Discovery). GitHub release
  11. LwIP library v2.1.2+ for STM32 using built-in Ethernet LAN8742A on (Nucleo-144, Discovery). GitHub release
  12. KH_Forked STM32AsyncTCP library v1.0.1+ for built-in Ethernet on (Nucleo-144, Discovery). To install manually for Arduino IDE
  13. WebServer_WT32_ETH01 library v1.5.1+ to use WT32_ETH01 (ESP32 + LAN8720). To install, check arduino-library-badge
  14. WebServer_ESP32_W5500 library v1.5.3+ to use (ESP32 + LwIP W5500). To install, check arduino-library-badge
  15. WebServer_ESP32_ENC library v1.5.3+ to use (ESP32 + LwIP ENC28J60). To install, check arduino-library-badge
  16. Portenta_H7_AsyncTCP library v1.4.0+ to use Portenta_H7 using either Murata WiFi or Vision-shield Ethernet. To install, check arduino-library-badge.
  17. Teensy41_AsyncTCP library v1.1.0+ to use Teensy 4.1 using QNEthernet Library. To install, check arduino-library-badge.
  18. AsyncTCP_RP2040W library v1.1.0+ to use RASPBERRY_PI_PICO_W with CYW43439 WiFi. To install, check arduino-library-badge.
  19. AsyncTCP_SSL library v1.3.1+ to use TLS/SSL for ESP32. To install, check arduino-library-badge.
  20. ESPAsync_WiFiManager library v1.15.1+ for ESP32/ESP8266 using some examples. GitHub release
  21. AsyncWebServer_RP2040W library v1.4.1+ for RP2040W using some examples. GitHub release


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for AsyncMQTT_Generic, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to AsyncMQTT_Generic page.
  2. Download the latest release AsyncMQTT_Generic-main.zip.
  3. Extract the zip file to AsyncMQTT_Generic-main directory
  4. Copy whole AsyncMQTT_Generic-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncMQTT_Generic library by using Library Manager. Search for AsyncMQTT_Generic in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Packages' Patches

1. For STM32 boards

1.1 For STM32 boards to use LAN8720

For Generic STM32F4 series boards, such as STM32F407VE, using LAN8720, please use STM32 core v2.2.0 as breaking core v2.3.0 creates the compile error.


To use LAN8720 on some STM32 boards

  • Nucleo-144 (F429ZI, NUCLEO_F746NG, NUCLEO_F746ZG, NUCLEO_F756ZG)
  • Discovery (DISCO_F746NG)
  • STM32F4 boards (BLACK_F407VE, BLACK_F407VG, BLACK_F407ZE, BLACK_F407ZG, BLACK_F407VE_Mini, DIYMORE_F407VGT, FK407M1)

you have to copy the files stm32f4xx_hal_conf_default.h and stm32f7xx_hal_conf_default.h into STM32 stm32 directory (~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system) to overwrite the old files.

Supposing the STM32 stm32 core version is 2.2.0. These files must be copied into the directory:

  • ~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system/STM32F4xx/stm32f4xx_hal_conf_default.h for STM32F4.
  • ~/.arduino15/packages/STM32/hardware/stm32/2.2.0/system/STM32F7xx/stm32f7xx_hal_conf_default.h for Nucleo-144 STM32F7.

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz, these files must be copied into the corresponding directory:

  • ~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/system/STM32F4xx/stm32f4xx_hal_conf_default.h
  • `~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/system/STM32F7xx/stm32f7xx_hal_conf_default.h

1.2 For STM32 boards to use Serial1

To use Serial1 on some STM32 boards without Serial1 definition (Nucleo-144 NUCLEO_F767ZI, Nucleo-64 NUCLEO_L053R8, etc.) boards, you have to copy the files STM32 variant.h into STM32 stm32 directory (~/.arduino15/packages/STM32/hardware/stm32/2.3.0). You have to modify the files corresponding to your boards, this is just an illustration how to do.

Supposing the STM32 stm32 core version is 2.3.0. These files must be copied into the directory:

  • ~/.arduino15/packages/STM32/hardware/stm32/2.3.0/variants/STM32F7xx/F765Z(G-I)T_F767Z(G-I)T_F777ZIT/NUCLEO_F767ZI/variant.h for Nucleo-144 NUCLEO_F767ZI.
  • ~/.arduino15/packages/STM32/hardware/stm32/2.3.0/variants/STM32L0xx/L052R(6-8)T_L053R(6-8)T_L063R8T/NUCLEO_L053R8/variant.h for Nucleo-64 NUCLEO_L053R8.

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz, these files must be copied into the corresponding directory:

  • ~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/variants/STM32F7xx/F765Z(G-I)T_F767Z(G-I)T_F777ZIT/NUCLEO_F767ZI/variant.h
  • ~/.arduino15/packages/STM32/hardware/stm32/x.yy.zz/variants/STM32L0xx/L052R(6-8)T_L053R(6-8)T_L063R8T/NUCLEO_L053R8/variant.h

2. For Teensy boards

To be able to compile and run on Teensy boards, you have to copy the files in Packages_Patches for Teensy directory into Teensy hardware directory (./arduino-1.8.19/hardware/teensy/avr/boards.txt).

Supposing the Arduino version is 1.8.19. These files must be copied into the directory:

  • ./arduino-1.8.19/hardware/teensy/avr/boards.txt
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy/Stream.h
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy3/Stream.h
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy4/Stream.h

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz These files must be copied into the directory:

  • ./arduino-x.yy.zz/hardware/teensy/avr/boards.txt
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy/Stream.h
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy3/Stream.h
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy4/Stream.h

3. For Portenta_H7 boards using Arduino IDE in Linux

To be able to upload firmware to Portenta_H7 using Arduino IDE in Linux (Ubuntu, etc.), you have to copy the file portenta_post_install.sh into mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh).

Then run the following command using sudo

$ cd ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1
$ chmod 755 portenta_post_install.sh
$ sudo ./portenta_post_install.sh

This will create the file /etc/udev/rules.d/49-portenta_h7.rules as follows:

# Portenta H7 bootloader mode UDEV rules

SUBSYSTEMS=="usb", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="035b", GROUP="plugdev", MODE="0666"

Supposing the ArduinoCore-mbed core version is 3.4.1. Now only one file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh

Whenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz

This file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/x.yy.zz/portenta_post_install.sh


HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)

Please have a look at ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to have more detailed description and solution of the issue.

1. ESP32 has 2 ADCs, named ADC1 and ADC2

2. ESP32 ADCs functions

  • ADC1 controls ADC function for pins GPIO32-GPIO39
  • ADC2 controls ADC function for pins GPIO0, 2, 4, 12-15, 25-27

3.. ESP32 WiFi uses ADC2 for WiFi functions

Look in file adc_common.c

In ADC2, there're two locks used for different cases:

  1. lock shared with app and Wi-Fi: ESP32: When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed. ESP32S2: The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.

  2. lock shared between tasks: when several tasks sharing the ADC2, we want to guarantee all the requests will be handled. Since conversions are short (about 31us), app returns the lock very soon, we use a spinlock to stand there waiting to do conversions one by one.

adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.

  • In order to use ADC2 for other functions, we have to acquire complicated firmware locks and very difficult to do
  • So, it's not advisable to use ADC2 with WiFi/BlueTooth (BT/BLE).
  • Use ADC1, and pins GPIO32-GPIO39
  • If somehow it's a must to use those pins serviced by ADC2 (GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27), use the fix mentioned at the end of ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to work with ESP32 WiFi/BlueTooth (BT/BLE).


Basic Usage

Please check API documentation.



HOWTO use STM32F4 with LAN8720

1. Wiring

This is the Wiring for STM32F4 (BLACK_F407VE, etc.) using LAN8720

LAN8720 PHY <---> STM32F4
TX1 <---> PB_13
TX_EN <---> PB_11
TX0 <---> PB_12
RX0 <---> PC_4
RX1 <---> PC_5
nINT/RETCLK <---> PA_1
CRS <---> PA_7
MDIO <---> PA_2
MDC <---> PC_1
GND <---> GND
VCC <---> +3.3V

2. HOWTO program using STLink V-2 or V-3

Connect as follows. To program, use STM32CubeProgrammer or Arduino IDE with

  • U(S)ART Support: "Enabled (generic Serial)"

  • Upload Method : "STM32CubeProgrammer (SWD)"

  • Jumper settings to program via STLInk

From <---> To
BOOT0 <---> 3.3V
  • Jumper settings when running normally to avoid accidentally reprogram
From <---> To
BOOT0 <---> GND

STLink <---> STM32F4
SWCLK <---> SWCLK
SWDIO <---> SWDIO
RST <---> NRST
GND <---> GND
5v <---> 5V


3. HOWTO use Serial Port for Debugging

Connect FDTI (USB to Serial) as follows:

FDTI <---> STM32F4
RX <---> TX=PA_9
TX <---> RX=PA_10
GND <---> GND


HOWTO use ESP8266 with W5x00 or ENC28J60 Ethernet

1. ESP8266 Wiring

This is the wiring for EP8266 W5x00 or ENC28J60 Ethernet when using SS = GPIO16

W5x00/ENC28J60 Ethernet <---> ESP8266
MOSI <---> MOSI = GPIO13
MISO <---> MISO = GPIO12
SCK <---> SCK = GPIO14
SS <---> GPIO16
GND <---> GND
VCC <---> +3.3V


HOWTO use ESP32 with LwIP W5500 or ENC28J60 Ethernet

1. ESP32 Wiring

This is the wiring for ESP8266 W5500 or ENC28J60 Ethernet when using SS = GPIO5

// Must connect INT to GPIOxx or not working
//#define INT_GPIO 4
//#define MISO_GPIO 19
//#define MOSI_GPIO 23
//#define SCK_GPIO 18
//#define CS_GPIO 5

W5x00/ENC28J60 Ethernet <---> ESP32
MOSI <---> MOSI = GPIO23
MISO <---> MISO = GPIO19
SCK <---> SCK = GPIO18
SS <---> GPIO5
INT <---> GPIO4
GND <---> GND
VCC <---> +3.3V


Examples

1. For ESP32

  1. FullyFeatured_ESP32
  2. FullyFeaturedSSL_ESP32
  3. AsyncWebServer_MQTT New

2. For ESP8266

  1. FullyFeatured_ESP8266
  2. FullyFeatured_ESP8266_Ethernet Use with core v3.0.2+

3. For WT32_ETH01

  1. FullyFeatured_WT32_ETH01
  2. FullyFeaturedSSL_WT32_ETH01

4. For STM32 using LAN8742A

  1. FullyFeatured_STM32

5. For STM32 using LAN8720

  1. FullyFeatured_STM32_LAN8720 Use with core v2.2.0-

6. For Portenta_H7 using Murata WiFi

  1. FullyFeatured_PortentaH7_WiFi

7. For Portenta_H7 using built-in Ethernet

  1. FullyFeatured_PortentaH7_Ethernet

8. For Teensy 4.1 using QNEthernet Library

  1. FullyFeatured_QNEthernet

9. or RASPBERRY_PI_PICO_W with CYW43439 WiFi

  1. FullyFeatured_RP2040W
  2. AsyncWebServer_MQTT_RP2040W New

10. For ESP32_ENC

  1. FullyFeatured_ESP32_ENC
  2. FullyFeaturedSSL_ESP32_ENC

11. For ESP32_W5500

  1. FullyFeatured_ESP32_W5500
  2. FullyFeaturedSSL_ESP32_W5500


// Example project which can be built with SSL enabled or disabled.
// The espressif8266_stage platform must be installed.
// Refer to platformio.ini for the build configuration and platform installation.
#include "defines.h"
#include <WiFi.h>
extern "C"
{
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#define ASYNC_TCP_SSL_ENABLED true
//#define ASYNC_TCP_SSL_ENABLED false
#include <AsyncMqtt_Generic.h>
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
#define MQTT_HOST "broker.emqx.io" // Broker address
#if ASYNC_TCP_SSL_ENABLED
#define MQTT_SECURE true
const uint8_t MQTT_SERVER_FINGERPRINT[] = {0x7e, 0x36, 0x22, 0x01, 0xf9, 0x7e, 0x99, 0x2f, 0xc5, 0xdb, 0x3d, 0xbe, 0xac, 0x48, 0x67, 0x5b, 0x5d, 0x47, 0x94, 0xd2};
const char *PubTopic = "async-mqtt/ESP32_SSL_Pub"; // Topic to publish
#define MQTT_PORT 8883
#else
const char *PubTopic = "async-mqtt/ESP32_Pub"; // Topic to publish
#define MQTT_PORT 1883
#endif
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
void connectToWifi()
{
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt()
{
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi ready");
break;
case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("WiFi STA starting");
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("WiFi STA connected");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.print("IP address: "); Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
Serial.println("WiFi lost IP");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#endif
default:
break;
}
}
void printSeparationLine()
{
Serial.println("************************************************");
}
void onMqttConnect(bool sessionPresent)
{
Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST);
Serial.print(", port: "); Serial.println(MQTT_PORT);
Serial.print("PubTopic: "); Serial.println(PubTopic);
printSeparationLine();
Serial.print("Session present: "); Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub);
mqttClient.publish(PubTopic, 0, true, "ESP32 Test");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2);
printSeparationLine();
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
(void) reason;
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected())
{
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
{
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: "); Serial.println(packetId);
Serial.print(" qos: "); Serial.println(qos);
}
void onMqttUnsubscribe(const uint16_t& packetId)
{
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: "); Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
const size_t& len, const size_t& index, const size_t& total)
{
(void) payload;
Serial.println("Publish received.");
Serial.print(" topic: "); Serial.println(topic);
Serial.print(" qos: "); Serial.println(properties.qos);
Serial.print(" dup: "); Serial.println(properties.dup);
Serial.print(" retain: "); Serial.println(properties.retain);
Serial.print(" len: "); Serial.println(len);
Serial.print(" index: "); Serial.println(index);
Serial.print(" total: "); Serial.println(total);
}
void onMqttPublish(const uint16_t& packetId)
{
Serial.println("Publish acknowledged");
Serial.print(" packetId: "); Serial.println(packetId);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStarting FullyFeatureSSL_ESP32 on "); Serial.println(ARDUINO_BOARD);
Serial.println(ASYNC_MQTT_GENERIC_VERSION);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
#if ASYNC_TCP_SSL_ENABLED
mqttClient.setSecure(MQTT_SECURE);
if (MQTT_SECURE)
{
//mqttClient.addServerFingerprint((const uint8_t[])MQTT_SERVER_FINGERPRINT);
mqttClient.addServerFingerprint((const uint8_t *)MQTT_SERVER_FINGERPRINT);
}
#endif
connectToWifi();
}
void loop()
{
}

2. File defines.h

#ifndef defines_h
#define defines_h
#define _ASYNC_MQTT_LOGLEVEL_ 1
#define WIFI_SSID "yourSSID"
#define WIFI_PASSWORD "yourpass"
#endif //defines_h



Debug Terminal Output Samples

1. FullyFeature_ESP8266 on ESP8266_NODEMCU_ESP12E

This is terminal debug output when running FullyFeatured_ESP8266 on ESP8266_NODEMCU_ESP12E connecting to broker.emqx.io MQTT server.

Starting FullyFeature_ESP8266 on ESP8266_NODEMCU_ESP12E
AsyncMQTT_Generic v1.8.1 for ESP8266
Connecting to Wi-Fi...
Connected to Wi-Fi. IP address: 192.168.2.82
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP8266_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 13
  index: 0
  total: 13
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish received.
  topic: async-mqtt/ESP8266_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish received.
  topic: async-mqtt/ESP8266_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish acknowledged.
  packetId: 3

2. FullyFeature_ESP32 on ESP32_DEV

This is terminal debug output when running FullyFeatured_ESP32 on ESP32_DEV connecting to broker.emqx.io MQTT server.

Starting FullyFeature_ESP32 on ESP32_DEV
AsyncMQTT_Generic v1.8.1 for ESP32 core v2.0.0+
Connecting to Wi-Fi...
WiFi ready
WiFi STA starting
WiFi STA connected
WiFi connected
IP address: 192.168.2.81
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP32_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP32_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 10
  index: 0
  total: 10
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged.
  packetId: 3

3. FullyFeatureSSL_ESP32 on ESP32_DEV

This is terminal debug output when running FullyFeaturedSSL_ESP32 on ESP32_DEV connecting to broker.emqx.io secured MQTT server (port 8883).

Starting FullyFeatureSSL_ESP32 on ESP32_DEV
AsyncMQTT_Generic v1.8.1 for ESP32 core v2.0.0+
Connecting to Wi-Fi...
WiFi ready
WiFi STA starting
WiFi STA connected
WiFi connected
IP address: 192.168.2.81
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_SSL_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 10
  index: 0
  total: 10
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3

4. FullyFeatureSSL_ESP32 on ESP32_DEV with ASYNC_MQTT_LOGLEVEL

This is terminal debug output when running FullyFeaturedSSL_ESP32 on ESP32_DEV connecting to broker.emqx.io secured MQTT server (port 8883). The ASYNC_MQTT_LOGLEVEL is set at 4 to show all the debugging messages.

Starting FullyFeatureSSL_ESP32 on ESP32_DEV
AsyncMQTT_Generic v1.8.1 for ESP32 core v2.0.0+
Connecting to Wi-Fi...
WiFi ready
WiFi STA starting
WiFi STA connected
WiFi connected
IP address: 192.168.2.81
Connecting to MQTT...
[AMQTT] CONNECTING
[AMQTT] _onAck: ack len = 303
[AMQTT] _onAck: ack len = 75
[AMQTT] _onAck: ack len = 51
[AMQTT] TCP conn, MQTT CONNECT
[AMQTT] _addFront: new front, packetType = CONNECT
[AMQTT] _handleQueue: snd, packetType # CONNECT , tls: realSent = 61
[AMQTT] _handleQueue: sent / _headsize = 32 / 32
[AMQTT] _handleQueue: released packetType # CONNECT
[AMQTT] _onAck: ack len = 61
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv CONNACK
[AMQTT] CONNACK
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_SSL_Pub
************************************************
Session present: 0
[AMQTT] SUBSCRIBE
[AMQTT] _addBack: new back, packetType = SUBSCRIBE
[AMQTT] _handleQueue: snd, packetType # SUBSCRIBE , tls: realSent = 60
[AMQTT] _handleQueue: sent / _headsize = 31 / 31
Subscribing at QoS 2, packetId: 1
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 0
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 1, packetId: 2
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 2, packetId: 3
************************************************
[AMQTT] _onAck: ack len = 60
[AMQTT] _onData : data rcv len = 5
[AMQTT] _onData: rcv SUBACK
[AMQTT] SUBACK
[AMQTT] SUB released
Subscribe acknowledged.
  packetId: 1
  qos: 2
[AMQTT] _handleQueue: released packetType # SUBSCRIBE
[AMQTT] _handleQueue: snd, packetType # PUBLISH , tls: realSent = 67
[AMQTT] _handleQueue: sent / _headsize = 38 / 38
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBLISH , tls: realSent = 65
[AMQTT] _handleQueue: sent / _headsize = 36 / 36
[AMQTT] _onData : data rcv len = 36
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
[AMQTT] _addBack: new back, packetType = PUBREC
[AMQTT] _onAck: ack len = 67
[AMQTT] _onData : data rcv len = 38
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 10
  index: 0
  total: 10
[AMQTT] _onAck: ack len = 65
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBACK
[AMQTT] PUB released
Publish acknowledged
  packetId: 2
[AMQTT] _onData : data rcv len = 36
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
[AMQTT] _addBack: new back, packetType = PUBACK
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBLISH , tls: realSent = 65
[AMQTT] _handleQueue: sent / _headsize = 36 / 36
[AMQTT] _onAck: ack len = 65
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREC
[AMQTT] snd PUBREL
[AMQTT] PUB released
[AMQTT] _insert: new insert, packetType = PUBREL
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBREL , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _onData : data rcv len = 36
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/ESP32_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
[AMQTT] _addBack: new back, packetType = PUBREC
[AMQTT] _onAck: ack len = 33
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBCOMP
[AMQTT] PUBREL released
Publish acknowledged
  packetId: 3
[AMQTT] _handleQueue: released packetType # PUBREL
[AMQTT] _handleQueue: snd, packetType # PUBREC , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _onAck: ack len = 33
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREL
[AMQTT] _insert: new insert, packetType = PUBCOMP
[AMQTT] _handleQueue: released packetType # PUBREC
[AMQTT] _handleQueue: snd, packetType # PUBCOMP , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBCOMP
[AMQTT] _handleQueue: snd, packetType # PUBACK , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBACK
[AMQTT] _handleQueue: snd, packetType # PUBREC , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] PUBREC released
[AMQTT] _onAck: ack len = 33
[AMQTT] _onAck: ack len = 66
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREL
[AMQTT] _insert: new insert, packetType = PUBCOMP
[AMQTT] _handleQueue: released packetType # PUBREC
[AMQTT] _handleQueue: snd, packetType # PUBCOMP , tls: realSent = 33
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBCOMP
[AMQTT] PUBREC released
[AMQTT] _onAck: ack len = 33
[AMQTT] PING
[AMQTT] _addBack: new back, packetType = PINGREQ
[AMQTT] _handleQueue: snd, packetType # PINGREQ , tls: realSent = 31
[AMQTT] _handleQueue: sent / _headsize = 2 / 2
[AMQTT] _handleQueue: released packetType # PINGREQ
[AMQTT] _onAck: ack len = 31
[AMQTT] _onData : data rcv len = 2
[AMQTT] _onData: rcv PINGRESP
[AMQTT] PINGRESP

5. FullyFeatureSSL_WT32_ETH01 on WT32-ETH01 with ETH_PHY_LAN8720

This is terminal debug output when running FullyFeaturedSSL_WT32_ETH01 on WT32-ETH01 connecting to broker.emqx.io secured MQTT server (port 8883).

Starting FullyFeatureSSL_WT32_ETH01 on WT32-ETH01 with ETH_PHY_LAN8720
WebServer_WT32_ETH01 v1.5.1 for core v2.0.0+
AsyncMQTT_Generic v1.8.1 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.97
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/WT32_ETH01_SSL_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/WT32_ETH01_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 15
  index: 0
  total: 15
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/WT32_ETH01_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/WT32_ETH01_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3

6. FullyFeature_STM32 on NUCLEO_F767ZI

This is terminal debug output when running FullyFeature_STM32 on STM32F7 NUCLEO_F767ZI connecting to broker.emqx.io MQTT server. The Ethernet loss was tested by disconnecting, then reconnecting the Ethernet cable.

Starting FullyFeature_STM32 on NUCLEO_F767ZI
AsyncMQTT_Generic v1.8.1 for STM32
Connected to network. IP = 192.168.2.118
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/STM32_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 1
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test1
  qos: 0
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test2
  qos: 1
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 3
Ethernet disconnected   <==== Test disconnect Ethernet very short time
Ethernet reconnected.
Ethernet disconnected   <==== Test disconnect Ethernet long enough time to force MQTT disconnection
Ethernet reconnected.
Disconnected from MQTT. <==== MQTT disconnected and reconnected
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/STM32_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 4
Publishing at QoS 0
Publishing at QoS 1, packetId: 5
Publishing at QoS 2, packetId: 6
************************************************
Subscribe acknowledged.
  packetId: 4
  qos: 2
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 1
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 5
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test1
  qos: 0
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test2
  qos: 1
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 6

7. FullyFeature_STM32 on NUCLEO_F767ZI with ASYNC_MQTT_LOGLEVEL

Starting FullyFeature_STM32 on NUCLEO_F767ZI
AsyncMQTT_Generic v1.8.1 for STM32
Connected to network. IP = 192.168.2.126
Connecting to MQTT...
[AMQTT] CONNECTING
[AMQTT] ClientID = stm32-DEADBEEF3201
[AMQTT] TCP conn, MQTT CONNECT
[AMQTT] _addFront: new front, packetType = CONNECT
[AMQTT] _handleQueue: snd, packetType # CONNECT
[AMQTT] _handleQueue: sent / _headsize = 32 / 32
[AMQTT] _handleQueue: released packetType # CONNECT
[AMQTT] _onAck: ack len = 32
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv CONNACK
[AMQTT] CONNACK
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/STM32_Pub
************************************************
Session present: 0
[AMQTT] SUBSCRIBE
[AMQTT] _addBack: new back, packetType = SUBSCRIBE
[AMQTT] _handleQueue: snd, packetType # SUBSCRIBE
[AMQTT] _handleQueue: sent / _headsize = 27 / 27
Subscribing at QoS 2, packetId: 1
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 0
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 1, packetId: 2
[AMQTT] PUBLISH
[AMQTT] _addBack: new back, packetType = PUBLISH
Publishing at QoS 2, packetId: 3
************************************************
[AMQTT] _onAck: ack len = 27
[AMQTT] _onData : data rcv len = 5
[AMQTT] _onData: rcv SUBACK
[AMQTT] SUBACK
[AMQTT] SUB released
Subscribe acknowledged.
  packetId: 1
  qos: 2
[AMQTT] _handleQueue: released packetType # SUBSCRIBE
[AMQTT] _handleQueue: snd, packetType # PUBLISH
[AMQTT] _handleQueue: sent / _headsize = 35 / 35
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBLISH
[AMQTT] _handleQueue: sent / _headsize = 37 / 37
[AMQTT] _onData : data rcv len = 37
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 1
  len: 11
  index: 0
  total: 11
[AMQTT] _addBack: new back, packetType = PUBREC
[AMQTT] _onAck: ack len = 72
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBACK
[AMQTT] PUB released
Publish acknowledged.
  packetId: 2
[AMQTT] _onData : data rcv len = 72
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test1
  qos: 0
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test2
  qos: 1
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
[AMQTT] _addBack: new back, packetType = PUBACK
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBLISH
[AMQTT] _handleQueue: sent / _headsize = 37 / 37
[AMQTT] _onAck: ack len = 37
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREC
[AMQTT] snd PUBREL
[AMQTT] PUB released
[AMQTT] _insert: new insert, packetType = PUBREL
[AMQTT] _handleQueue: released packetType # PUBLISH
[AMQTT] _handleQueue: snd, packetType # PUBREL
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _onData : data rcv len = 37
[AMQTT] _onData: rcv PUBLISH
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
[AMQTT] _addBack: new back, packetType = PUBREC
[AMQTT] _onAck: ack len = 4
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBCOMP
[AMQTT] PUBREL released
Publish acknowledged.
  packetId: 3
[AMQTT] _handleQueue: released packetType # PUBREL
[AMQTT] _handleQueue: snd, packetType # PUBREC
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _onAck: ack len = 4
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREL
[AMQTT] _insert: new insert, packetType = PUBCOMP
[AMQTT] _handleQueue: released packetType # PUBREC
[AMQTT] _handleQueue: snd, packetType # PUBCOMP
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBCOMP
[AMQTT] _handleQueue: snd, packetType # PUBACK
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBACK
[AMQTT] _handleQueue: snd, packetType # PUBREC
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] PUBREC released
[AMQTT] _onAck: ack len = 12
[AMQTT] _onData : data rcv len = 4
[AMQTT] _onData: rcv PUBREL
[AMQTT] _insert: new insert, packetType = PUBCOMP
[AMQTT] _handleQueue: released packetType # PUBREC
[AMQTT] _handleQueue: snd, packetType # PUBCOMP
[AMQTT] _handleQueue: sent / _headsize = 4 / 4
[AMQTT] _handleQueue: released packetType # PUBCOMP
[AMQTT] PUBREC released
[AMQTT] _onAck: ack len = 4
[AMQTT] PING
[AMQTT] _addBack: new back, packetType = PINGREQ
[AMQTT] _handleQueue: snd, packetType # PINGREQ
[AMQTT] _handleQueue: sent / _headsize = 2 / 2
[AMQTT] _handleQueue: released packetType # PINGREQ
[AMQTT] _onAck: ack len = 2
[AMQTT] _onData : data rcv len = 2
[AMQTT] _onData: rcv PINGRESP
[AMQTT] PINGRESP
[AMQTT] PING
[AMQTT] _addBack: new back, packetType = PINGREQ
[AMQTT] _handleQueue: snd, packetType # PINGREQ
[AMQTT] _handleQueue: sent / _headsize = 2 / 2
[AMQTT] _handleQueue: released packetType # PINGREQ
[AMQTT] _onAck: ack len = 2
[AMQTT] _onData : data rcv len = 2
[AMQTT] _onData: rcv PINGRESP
[AMQTT] PINGRESP

8. FullyFeatured_STM32_LAN8720 on BLACK_F407VE

This is terminal debug output when running FullyFeatured_STM32_LAN8720 on STM32F4 BLACK_F407VE connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_STM32_LAN8720 on BLACK_F407VE
AsyncMQTT_Generic v1.8.1 for STM32
Connected to network. IP = 192.168.2.132
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/STM32_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 1
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test1
  qos: 0
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test2
  qos: 1
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish received.
  topic: async-mqtt/STM32_Pub
  message: STM32 Test3
  qos: 2
  dup: 0
  retain: 0
  len: 11
  index: 0
  total: 11
Publish acknowledged.
  packetId: 3

9. FullyFeatured_PortentaH7_WiFi on PORTENTA_H7_M7

This is terminal debug output when running FullyFeatured_PortentaH7_WiFi on STM32H7 PORTENTA_H7_M7 using Murata WiFi, connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_PortentaH7_WiFi on PORTENTA_H7_M7
AsyncMQTT_Generic v1.8.1 for Portenta_H7_M7
Connecting to SSID: HueNet1
Connected to SSID: HueNet1
Local IP Address: 192.168.2.134
Signal strength (RSSI):-37 dBm
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/Portenta_H7_WiFi_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/Portenta_H7_WiFi_Pub
  message: Portenta_H7_WiFi Test 3
  qos: 2
  dup: 0
  retain: 1
  len: 23
  index: 0
  total: 23
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/Portenta_H7_WiFi_Pub
  message: Portenta_H7_WiFi Test1
  qos: 0
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish received.
  topic: async-mqtt/Portenta_H7_WiFi_Pub
  message: Portenta_H7_WiFi Test 2
  qos: 1
  dup: 0
  retain: 0
  len: 23
  index: 0
  total: 23
Publish received.
  topic: async-mqtt/Portenta_H7_WiFi_Pub
  message: Portenta_H7_WiFi Test 3
  qos: 2
  dup: 0
  retain: 0
  len: 23
  index: 0
  total: 23
Publish acknowledged.
  packetId: 3

10. FullyFeatured_Portenta_H7_Ethernet on PORTENTA_H7_M7

This is terminal debug output when running FullyFeatured_PortentaH7_Ethernet on STM32H7 PORTENTA_H7_M7, using built-in Ethernet, connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_PortentaH7_Ethernet on PORTENTA_H7_M7
AsyncMQTT_Generic v1.8.1 for Portenta_H7_M7
Connected to network. IP = 192.168.2.133
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/Portenta_H7_Ethernet_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test3
  qos: 2
  dup: 0
  retain: 1
  len: 26
  index: 0
  total: 26
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test1
  qos: 0
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test2
  qos: 1
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test3
  qos: 2
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish acknowledged.
  packetId: 3
Ethernet disconnected   <==== Test disconnect Ethernet very short time
Ethernet reconnected.
Ethernet disconnected   <==== Test disconnect Ethernet long enough time to force MQTT disconnection
Ethernet reconnected.
[ATCP] setCloseError() to: Connection reset => -14
Disconnected from MQTT. <==== MQTT disconnected and reconnected
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/Portenta_H7_Ethernet_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 4
Publishing at QoS 0
Publishing at QoS 1, packetId: 5
Publishing at QoS 2, packetId: 6
************************************************
Subscribe acknowledged.
  packetId: 4
  qos: 2
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test3
  qos: 2
  dup: 0
  retain: 1
  len: 26
  index: 0
  total: 26
Publish acknowledged.
  packetId: 5
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test1
  qos: 0
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test2
  qos: 1
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish received.
  topic: async-mqtt/Portenta_H7_Ethernet_Pub
  message: Portenta_H7_Ethernet Test3
  qos: 2
  dup: 0
  retain: 0
  len: 26
  index: 0
  total: 26
Publish acknowledged.
  packetId: 6

11. FullyFeatured_QNEthernet on TEENSY 4.1 using QNEthernet

This is terminal debug output when running FullyFeatured_QNEthernet on TEENSY 4.1, using built-in Ethernet with QNEthernet Library, connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_QNEthernet on TEENSY 4.1
AsyncMQTT_Generic v1.8.1 for Teensy 4.1 QNEthernet
Initialize Ethernet using static IP => Connected! IP address:192.168.2.222
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/Teensy41_QNEthernet_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/Teensy41_QNEthernet_Pub
  message: Teensy41_QNEthernet_Pub Test3
  qos: 2
  dup: 0
  retain: 1
  len: 29
  index: 0
  total: 29
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/Teensy41_QNEthernet_Pub
  message: Teensy41_QNEthernet_Pub Test1
  qos: 0
  dup: 0
  retain: 0
  len: 29
  index: 0
  total: 29
Publish received.
  topic: async-mqtt/Teensy41_QNEthernet_Pub
  message: Teensy41_QNEthernet_Pub Test2
  qos: 1
  dup: 0
  retain: 0
  len: 29
  index: 0
  total: 29
Publish received.
  topic: async-mqtt/Teensy41_QNEthernet_Pub
  message: Teensy41_QNEthernet_Pub Test3
  qos: 2
  dup: 0
  retain: 0
  len: 29
  index: 0
  total: 29
Publish acknowledged.
  packetId: 3

12. FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E using ESP8266_ENC28J60 Ethernet

This is terminal debug output when running FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E, using ENC28J60 Ethernet with lwIP_enc28j60 Library, connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E using ESP8266_ENC28J60 Ethernet
AsyncMQTT_Generic v1.8.1 for ESP8266
Connecting to network : .....................................
Ethernet DHCP IP address: 192.168.2.187
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP8266_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test3
  qos: 2
  dup: 0
  retain: 1
  len: 22
  index: 0
  total: 22
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test1
  qos: 0
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test2
  qos: 1
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test3
  qos: 2
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish acknowledged.
  packetId: 3

13. FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E using ESP8266_W5500 Ethernet

This is terminal debug output when running FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E, using W5500 Ethernet with lwIP_w5500 Library, connecting to broker.emqx.io MQTT server.

Starting FullyFeatured_ESP8266_Ethernet on ESP8266_NODEMCU_ESP12E using ESP8266_W5500 Ethernet
AsyncMQTT_Generic v1.8.1 for ESP8266
Connecting to network : .
Ethernet DHCP IP address: 192.168.2.188
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP8266_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test3
  qos: 2
  dup: 0
  retain: 1
  len: 22
  index: 0
  total: 22
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test1
  qos: 0
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test2
  qos: 1
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish received.
  topic: async-mqtt/ESP8266_Pub
  message: ESP8266_Ethernet Test3
  qos: 2
  dup: 0
  retain: 0
  len: 22
  index: 0
  total: 22
Publish acknowledged.
  packetId: 3

14. FullyFeature_RP2040W on RASPBERRY_PI_PICO_W

This is terminal debug output when running FullyFeature_RP2040W on RASPBERRY_PI_PICO_W, using CYW43439 WiFi, connecting to broker.emqx.io MQTT server.

Starting FullyFeature_RP2040W on RASPBERRY_PI_PICO_W
AsyncMQTT_Generic v1.8.1 for RP2040W CYW43439 WiFi
Connecting to SSID: HueNet2
Connected to SSID: HueNet2
Local IP Address: 192.168.2.180
Signal strength (RSSI):0 dBm
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/RP2040W_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/RP2040W_Pub
  message: RP2040W Test3
  qos: 2
  dup: 0
  retain: 1
  len: 13
  index: 0
  total: 13
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/RP2040W_Pub
  message: RP2040W Test1
  qos: 0
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish received.
  topic: async-mqtt/RP2040W_Pub
  message: RP2040W Test2
  qos: 1
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish received.
  topic: async-mqtt/RP2040W_Pub
  message: RP2040W Test3
  qos: 2
  dup: 0
  retain: 0
  len: 13
  index: 0
  total: 13
Publish acknowledged.
  packetId: 3

15. FullyFeatureSSL_ESP32_ENC on ESP32_DEV with ESP32_ENC28J60

This is terminal debug output when running FullyFeatureSSL_ESP32_ENC on ESP32_DEV with ESP32_ENC28J60, connecting to broker.emqx.io MQTTS server

Starting FullyFeatureSSL_ESP32_ENC on ESP32_DEV with ESP32_ENC28J60
WebServer_ESP32_ENC v1.5.3 for core v2.0.0+
AsyncMQTT_Generic v1.8.1 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.96
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_ENC_SSL_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP32_ENC_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 14
  index: 0
  total: 14
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_ENC_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_ENC_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3

16. FullyFeatureSSL_ESP32_W5500 on ESP32_DEV with ESP32_W5500

This is terminal debug output when running FullyFeatureSSL_ESP32_W5500 on ESP32_DEV with W5500, connecting to broker.emqx.io MQTTS server

Starting FullyFeatureSSL_ESP32_W5500 on ESP32_DEV with ESP32_W5500
WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+
AsyncMQTT_Generic v1.7.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.103
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_W5500_SSL_Pub
************************************************
Session present: 0
Subscribing at QoS 2, packetId: 1
Publishing at QoS 0
Publishing at QoS 1, packetId: 2
Publishing at QoS 2, packetId: 3
************************************************
Subscribe acknowledged.
  packetId: 1
  qos: 2
Publish received.
  topic: async-mqtt/ESP32_W5500_SSL_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_W5500_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 16
  index: 0
  total: 16
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_W5500_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_W5500_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3


Debug

Debug is enabled by default on Serial. Debug Level from 0 to 4. To disable, change the MYSQL_LOGLEVEL to 0

#define ASYNC_MQTT_DEBUG_PORT               Serial

// Debug Level from 0 to 4
#define _ASYNC_MQTT_LOGLEVEL_               1

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: AsyncMQTT_Generic issues



TO DO

  1. Support ESP8266, STM32, Portenta_H7, Teensy 4.1, RASPBERRY_PI_PICO_W using SSL/TLS
  2. Bug fixing.

DONE

  1. Add support to ESP32 (SSL and non-SSL)
  2. Add support to ESP8266 (non-SSL)
  3. Add support to WT32_ETH01 (SSL and non-SSL)
  4. Add support to Ethernet LAN8742A (non-SSL), using STM32Ethernet library and STM32duino_LwIP library.
  5. Add support to many STM32F4 and STM32F7 (without TLS/SSL) using LAN8720 Ethernet, such as F407xx, NUCLEO_F429ZI, DISCO_F746NG, NUCLEO_F746ZG, NUCLEO_F756ZG, etc.
  6. Add support to Portenta_H7, using either Murata WiFi or Vision-shield Ethernet
  7. Add support to Teensy 4.1 using QNEthernet Library
  8. Add support to ESP8266 W5x00 using lwIP_w5100 or lwIP_w5500 library
  9. Add support to ESP8266 ENC28J60 using lwIP_enc28j60 library
  10. Add support RASPBERRY_PI_PICO_W with CYW43439 WiFi, using arduino-pico core v2.4.0+
  11. Change to new Forked ESPAsyncTCP library for ESP8266 boards, using WiFi or LwIP Ethernet, with ESP8266 core v3.0.2+ or WiFi with ESP8266 core v2.7.4 to avoid compile errors
  12. Add astyle using allman style. Restyle the library
  13. Add support to ESP32 boards using LwIP W5500 Ethernet, (SSL and non-SSL)
  14. Add support to ESP32 boards using LwIP ENC28J60 Ethernet, (SSL and non-SSL)
  15. Add AsyncWebServer_MQTT for ESP32 example to demo how to use AsyncWebServer and AsyncMQTT_Generic together
  16. Add AsyncWebServer_MQTT_RP2040W example to demo how to use AsyncWebServer_RP2040W and AsyncMQTT_Generic together
  17. Improve README.md so that links can be used in other sites, such as PIO


Contributions and Thanks

  1. Based on and modified from Marvin Roger's async-mqtt-client Library
  2. Thanks to Jens Chr Brynildsen for reporting Missing default case in switch causes #10 leading to v1.7.0
marvinroger
⭐️ Marvin Roger

jenschr
Jens Chr Brynildsen


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • Most of the credits go to original author Marvin Roger

  • The library is licensed under MIT


Copyright

  1. Copyright (c) 2017- Marvin Roger
  2. Copyright (c) 2022- Khoi Hoang

About

Arduino Arduino Library for ESP8266, ESP32, Portenta_H7, STM32 and RP2040W asynchronous MQTT client implementation. This library, ported to support ESP32, WT32_ETH01 (ESP32 + LAN8720), ESP8266, Portenta_H7 (Ethernet or WiFi) and STM32 (LAN8742A or LAN8720 Ethernet), Teensy 4.1 using QNEthernet, RASPBERRY_PI_PICO_W with CYW43439 WiFi. Currently s…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages