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

Arduino Library for ESP32/S2/S3/C3 asynchronous MQTT client implementation. This library, ported to support ESP32/S2/S3/C3, WT32_ETH01 (ESP32 + LAN8720), ESP32 using LwIP ENC28J60, W5500, W6100 or LAN8720. Supporting TLS/SSL for MQTTS Client

License

Notifications You must be signed in to change notification settings

khoih-prog/AsyncMQTT_ESP32

Repository files navigation

AsyncMQTT_ESP32 Library

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need this AsyncMQTT_ESP32 library

Features

This AsyncMQTT_ESP32 library is based on and modified from Marvin Roger's async-mqtt-client Library, to provide support to ESP32/S2/S3/C3 boards using WiFi or LwIP W5500 / W6100 / ENC28J60 / LAN8720 Ethernet

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

1. ESP32/S2/S3/C3 using WiFi

  1. ESP32 (ESP32-DEV, etc.)

2. ESP32 using LwIP ENC28J60, W5500, W6100 or LAN8720

  1. ESP32 (ESP32-DEV, etc.)

3. WT32_ETH01 using ESP32-based boards and LAN8720 Ethernet

4. ESP32S3 using LwIP W5500, W6100 or ENC28J60

  1. ESP32-S3 (ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)

5. ESP32S2 using LwIP W5500, W6100 or ENC28J60

  1. ESP32-S2 (ESP32S2_DEV, etc.)

6. ESP32C3 using LwIP W5500, W6100 or ENC28J60

  1. ESP32-C3 (ESP32C3_DEV, etc.)

ESP32S3_DEV

ESP32S2_DEV

ESP32C3_DEV


W6100

FULL_DUPLEX, 100Mbps


W5500

FULL_DUPLEX, 100Mbps


ENC28J60

FULL_DUPLEX, 10Mbps


To-be Supported Boards

1. New ESP32 using LwIP W5500, W6100 or ENC28J60



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. ESP32 Core 2.0.6+ for ESP32-based boards. Latest release
  3. AsyncTCP v1.1.1+ for ESP32. To install manually for Arduno IDE
  4. AsyncTCP_SSL v1.3.1+ for ESP32. GitHub release
  5. WebServer_WT32_ETH01 v1.5.1+ for ESP32-based WT32_ETH01 using either ESP32 core v2.0.0+ or v1.0.6-. GitHub release
  6. WebServer_ESP32_ENC library v1.5.1+ if necessary to use ESP32 boards using LwIP ENC28J60 Ethernet. To install, check arduino-library-badge
  7. WebServer_ESP32_W5500 library v1.5.2+ if necessary to use ESP32 boards using LwIP W5500 Ethernet. To install, check arduino-library-badge
  8. WebServer_ESP32_SC_ENC library v1.2.0+ if necessary to use ESP32_S2/S3/C3 boards using LwIP ENC28J60 Ethernet. To install, check arduino-library-badge
  9. WebServer_ESP32_SC_W5500 library v1.2.1+ if necessary to use ESP32_S2/S3/C3 boards using LwIP W5500 Ethernet. To install, check arduino-library-badge
  10. WebServer_ESP32_W6100 library v1.5.2+ if necessary to use ESP32 boards using LwIP W6100 Ethernet. To install, check arduino-library-badge
  11. WebServer_ESP32_SC_W6100 library v1.2.1+ if necessary to use ESP32_S2/S3/C3 boards using LwIP W6100 Ethernet. To install, check arduino-library-badge


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for AsyncMQTT_ESP32, 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_ESP32 page.
  2. Download the latest release AsyncMQTT_ESP32-main.zip.
  3. Extract the zip file to AsyncMQTT_ESP32-main directory
  4. Copy whole AsyncMQTT_ESP32-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncMQTT_ESP32 library by using Library Manager. Search for AsyncMQTT_ESP32 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


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.



How to connect W5500, W6100 or ENC28J60 to ESP32_S2/S3/C3

W6100

FULL_DUPLEX, 100Mbps


W5500

FULL_DUPLEX, 100Mbps


ENC28J60

FULL_DUPLEX, 10Mbps


ESP32S3_DEV

You can change the INT pin to another one. Default is GPIO4

// Must connect INT to GPIOxx or not working
#define INT_GPIO            4
W5500, W6100 or ENC28J60 <---> ESP32_S3
MOSI <---> GPIO11
MISO <---> GPIO13
SCK <---> GPIO12
CS/SS <---> GPIO10
INT <---> GPIO4
RST <---> RST
GND <---> GND
3.3V <---> 3.3V

ESP32S2_DEV

You can change the INT pin to another one. Default is GPIO4

// Must connect INT to GPIOxx or not working
#define INT_GPIO            4
W5500, W6100 or ENC28J60 <---> ESP32_S2
MOSI <---> GPIO35
MISO <---> GPIO37
SCK <---> GPIO36
CS/SS <---> GPIO34
INT <---> GPIO4
RST <---> RST
GND <---> GND
3.3V <---> 3.3V

ESP32C3_DEV

You can change the INT pin to another one. Default is GPIO4

// Must connect INT to GPIOxx or not working
#define INT_GPIO            10
W5500, W6100 or ENC28J60 <---> ESP32_C3
MOSI <---> GPIO6
MISO <---> GPIO5
SCK <---> GPIO4
CS/SS <---> GPIO7
INT <---> GPIO10
RST <---> RST
GND <---> GND
3.3V <---> 3.3V


Examples

1. For ESP32

  1. FullyFeatured_ESP32
  2. FullyFeaturedSSL_ESP32

2. For WT32_ETH01

  1. FullyFeatured_WT32_ETH01
  2. FullyFeaturedSSL_WT32_ETH01

3. For ESP32_ENC

  1. FullyFeatured_ESP32_ENC
  2. FullyFeaturedSSL_ESP32_ENC

4. For ESP32_W5500

  1. FullyFeatured_ESP32_W5500
  2. FullyFeaturedSSL_ESP32_W5500

5. For ESP32_SC_ENC

  1. FullyFeatured_ESP32_SC_ENC
  2. FullyFeaturedSSL_ESP32_SC_ENC

6. For ESP32_SC_W5500

  1. FullyFeatured_ESP32_SC_W5500
  2. FullyFeaturedSSL_ESP32_SC_W5500

7. For ESP32_W6100

  1. FullyFeatured_ESP32_W6100
  2. FullyFeaturedSSL_ESP32_W6100

8. For ESP32_SC_W6100

  1. FullyFeatured_ESP32_SC_W6100
  2. FullyFeaturedSSL_ESP32_SC_W6100


#include "defines.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_ESP32.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_W5500_SSL_Pub"; // Topic to publish
#define MQTT_PORT 8883
#else
const char *PubTopic = "async-mqtt/ESP32_SC_W5500_Pub"; // Topic to publish
#define MQTT_PORT 1883
#endif
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
void connectToMqtt()
{
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void ETH_event(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH starting");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.println("ETH got IP");
Serial.print("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH stops");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_ETH_CONNECTED:
erial.println(F("ETH Connected"));
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.println("ETH connected");
Serial.println("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 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_W5500 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 (ESP32_W5500_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);
delay(500);
Serial.print("\nStarting FullyFeatureSSL_ESP32_SC_W5500 on ");
Serial.print(ARDUINO_BOARD);
Serial.println(" with " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_ESP32_SC_W5500_VERSION);
Serial.println(ASYNC_MQTT_ESP32_VERSION);
AMQTT_LOGWARN(F("Default SPI pinout:"));
AMQTT_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
AMQTT_LOGWARN1(F("MOSI:"), MOSI_GPIO);
AMQTT_LOGWARN1(F("MISO:"), MISO_GPIO);
AMQTT_LOGWARN1(F("SCK:"), SCK_GPIO);
AMQTT_LOGWARN1(F("CS:"), CS_GPIO);
AMQTT_LOGWARN1(F("INT:"), INT_GPIO);
AMQTT_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
AMQTT_LOGWARN(F("========================="));
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
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
///////////////////////////////////
// To be called before ETH.begin()
WiFi.onEvent(ETH_event);
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(myIP, myGW, mySN, myDNS);
ESP32_W5500_waitForConnect();
///////////////////////////////////
}
void loop()
{
}

2. File defines.h

#ifndef defines_h
#define defines_h
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
#define _ASYNC_MQTT_LOGLEVEL_ 1
//////////////////////////////////////////////////////////
// For ESP32-S3
// Optional values to override default settings
//#define ETH_SPI_HOST SPI2_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
//#define INT_GPIO 4
//#define MISO_GPIO 13
//#define MOSI_GPIO 11
//#define SCK_GPIO 12
//#define CS_GPIO 10
// For ESP32_C3
// Optional values to override default settings
// Don't change unless you know what you're doing
//#define ETH_SPI_HOST SPI2_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
//#define INT_GPIO 10
//#define MISO_GPIO 5
//#define MOSI_GPIO 6
//#define SCK_GPIO 4
//#define CS_GPIO 7
//////////////////////////////////////////////////////////
#include <WebServer_ESP32_SC_W5500.h>
/////////////////////////////////////////////
// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20
byte mac[][NUMBER_OF_MAC] =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
};
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
#endif //defines_h



Debug Terminal Output Samples

1. FullyFeature_ESP32_SC_W5500 on ESP32S3_DEV with ESP32_S3_W5500

This is terminal debug output when running FullyFeatured_ESP32_SC_W5500 on ESP32S3_DEV with LwIP W5500, connecting to broker.emqx.io MQTT server.

Starting FullyFeature_ESP32_SC_W5500 on ESP32S3_DEV with ESP32_S3_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.108
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP32_SC_W5500_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_SC_W5500_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 16
  index: 0
  total: 16
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_SC_W5500_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SC_W5500_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged.
  packetId: 3

2. FullyFeatureSSL_ESP32_SC_W5500 on ESP32S3_DEV with ESP32_S3_W5500

This is terminal debug output when running FullyFeaturedSSL_ESP32_SC_W5500 on ESP32S3_DEV with LwIP W5500, connecting to broker.emqx.io secured MQTTS server (port 8883)

Starting FullyFeatureSSL_ESP32_SC_W5500 on ESP32S3_DEV with ESP32_S3_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.125
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: 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

3. FullyFeature_ESP32_SC_ENC on ESP32S3_DEV with ESP32_S3_ENC28J60

This is terminal debug output when running FullyFeatured_ESP32_SC_ENC on ESP32S3_DEV with LwIP ENC28J60, connecting to broker.emqx.io MQTT server.

Starting FullyFeature_ESP32_SC_ENC on ESP32S3_DEV with ESP32_S3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.88
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP32_SC_ENC_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_SC_ENC_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 14
  index: 0
  total: 14
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged.
  packetId: 3

4. FullyFeatureSSL_ESP32_SC_ENC on ESP32S3_DEV with ESP32_S3_ENC28J60

This is terminal debug output when running FullyFeaturedSSL_ESP32_SC_ENC on ESP32S3_DEV with LwIP ENC28J60, connecting to broker.emqx.io secured MQTTS server (port 8883)

Starting FullyFeatureSSL_ESP32_SC_ENC on ESP32S3_DEV with ESP32_S3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.109
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_SC_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_SC_ENC_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 17
  index: 0
  total: 17
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3

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_ESP32 v1.10.0 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. FullyFeatureSSL_ESP32_SC_W5500 on ESP32S2_DEV with ESP32_S2_W5500

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

Starting FullyFeatureSSL_ESP32_SC_W5500 on ESP32S2_DEV with ESP32_S2_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
Using built-in mac_eth = 7E:DF:A1:08:32:C9
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.133
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

7. FullyFeatureSSL_ESP32_SC_W5500 on ESP32C3_DEV with ESP32_C3_W5500

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

Starting FullyFeatureSSL_ESP32_SC_W5500 on ESP32C3_DEV with ESP32_C3_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
Using built-in mac_eth = 7C:DF:A1:BC:BC:53
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.135
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

8. FullyFeatureSSL_ESP32_SC_ENC on ESP32C3_DEV with ESP32_C3_ENC28J60

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

Starting FullyFeatureSSL_ESP32_SC_ENC on ESP32C3_DEV with ESP32_C3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
Using built-in mac_eth = 7C:DF:A1:DA:66:87
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.136
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_SC_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_SC_ENC_SSL_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 17
  index: 0
  total: 17
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_SC_ENC_SSL_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged
  packetId: 3

9. FullyFeature_ESP32_W6100 on ESP32_DEV with ESP32_W6100

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

Starting FullyFeature_ESP32_W6100 on ESP32_DEV with ESP32_W6100
WebServer_ESP32_W6100 v1.5.2 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.158
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 1883
PubTopic: async-mqtt/ESP32_W6100_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_W6100_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 16
  index: 0
  total: 16
Publish acknowledged.
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_W6100_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_W6100_Pub
  qos: 2
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish acknowledged.
  packetId: 3

10. FullyFeatureSSL_ESP32_SC_W6100 on ESP32S3_DEV with ESP32_S3_W6100

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

Starting FullyFeatureSSL_ESP32_SC_W6100 on ESP32S3_DEV with ESP32_S3_W6100
WebServer_ESP32_SC_W6100 v1.2.1 for core v2.0.0+
AsyncMQTT_ESP32 v1.10.0 for ESP32 core v2.0.0+
ETH starting
ETH connected
ETH got IP
IP address: 192.168.2.92
Connecting to MQTT...
Connected to MQTT broker: broker.emqx.io, port: 8883
PubTopic: async-mqtt/ESP32_W6100_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_W6100_SSL_Pub
  qos: 2
  dup: 0
  retain: 1
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_W6100_SSL_Pub
  qos: 0
  dup: 0
  retain: 0
  len: 16
  index: 0
  total: 16
Publish acknowledged
  packetId: 2
Publish received.
  topic: async-mqtt/ESP32_W6100_SSL_Pub
  qos: 1
  dup: 0
  retain: 0
  len: 6
  index: 0
  total: 6
Publish received.
  topic: async-mqtt/ESP32_W6100_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_ESP32 issues



TO DO

  1. Bug fixing.

DONE

  1. Add support to any ESP32/S2/S3/C3 boards using WiFi with SSL/TLS
  2. Add support to ESP32 boards using LwIP W5500 / ENC28J60 / LAN8720 Ethernet with SSL/TLS
  3. Add support to ESP32S3 boards using LwIP W5500 / ENC28J60 / LAN8720 Ethernet with SSL/TLS
  4. Add support to ESP32S2 boards using LwIP W5500 / ENC28J60 / LAN8720 Ethernet with SSL/TLS
  5. Add support to ESP32C3 boards using LwIP W5500 / ENC28J60 / LAN8720 Ethernet with SSL/TLS
  6. Add support to ESP32 and ESP32S2/S3/C3 boards using LwIP W6100 Ethernet with SSL/TLS


Contributions and Thanks

  1. Based on and modified from Marvin Roger's async-mqtt-client Library
marvinroger
⭐️ Marvin Roger


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

  • Many 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