---
title: "W6100 khoih-prog / AsyncMQTT_ESP32"
url: "https://maker.wiznet.io/eric_g/projects/khoih-prog-asyncmqtt-esp32/"
markdown_url: "https://maker.wiznet.io/eric_g/projects/khoih-prog-asyncmqtt-esp32/md"
type: "UCC: User Created Content"
author: "khoih prog"
author_url: "https://github.com/khoih-prog/AsyncMQTT_ESP32"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "khoih prog"
original_url: "https://github.com/khoih-prog/AsyncMQTT_ESP32"
published: "2023-02-01"
language: "en"
hardware: ["WIZnet W6100"]
likes: 2
views: 377
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# W6100 khoih-prog / AsyncMQTT_ESP32

> This AsyncMQTT_ESP32 library is based on and modified from Marvin Roger's async-mqtt-client Library.

Original author: khoih prog (source: https://github.com/khoih-prog/AsyncMQTT_ESP32)

## Components

- **WIZnet W6100** x 1 ([docs](https://docs.wiznet.io/Product/iEthernet/W6100/))

## Article

#### 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

### Example [FullyFeaturedSSL_ESP32_SC_W5500](https://github.com/khoih-prog/AsyncMQTT_ESP32/blob/main/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500)

#### 1. File [FullyFeaturedSSL_ESP32_SC_W5500.ino](https://github.com/khoih-prog/AsyncMQTT_ESP32/blob/main/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500.ino)

[AsyncMQTT_ESP32/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500.ino](https://github.com/khoih-prog/AsyncMQTT_ESP32/blob/4a9f8bcdb04e6c0c329bda40896afd4d7d8e31c7/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500.ino#L14-L283)

Lines 14 to 283 in [4a9f8bc](https://github.com/khoih-prog/AsyncMQTT_ESP32/commit/4a9f8bcdb04e6c0c329bda40896afd4d7d8e31c7)

|  | #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 &lt;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() &lt; 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&lt;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](https://github.com/khoih-prog/AsyncMQTT_ESP32/blob/main/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/defines.h)

[AsyncMQTT_ESP32/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/defines.h](https://github.com/khoih-prog/AsyncMQTT_ESP32/blob/4a9f8bcdb04e6c0c329bda40896afd4d7d8e31c7/examples/ESP32_SC_W5500/FullyFeaturedSSL_ESP32_SC_W5500/defines.h#L13-L90)

Lines 13 to 90 in [4a9f8bc](https://github.com/khoih-prog/AsyncMQTT_ESP32/commit/4a9f8bcdb04e6c0c329bda40896afd4d7d8e31c7)

|  | #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 |

---

Source: https://maker.wiznet.io/eric_g/projects/khoih-prog-asyncmqtt-esp32/
