MQTTPubSubClient_Generic icon indicating copy to clipboard operation
MQTTPubSubClient_Generic copied to clipboard

MQTT, MQTT_over_WebSockets and Secured MQTT_over_WebSockets Client for Arduino. Supporting nRF52, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, RP2040-based boards, besides ESP8266, ESP32 (ESP...

MQTTPubSubClient_Generic Library (MQTT Client for ESP8266, ESP32, etc.)

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents

  • Why do we need this MQTTPubSubClient_Generic library
    • Features
    • Currently supported Boards
    • Currently supported WiFi shields/modules
    • Currently supported Ethernet shields/modules
  • Changelog
  • Prerequisites
  • Installation
    • Use Arduino Library Manager
    • Manual Install
    • VS Code & PlatformIO
  • Packages' Patches
    • 1. For Adafruit nRF52840 and nRF52832 boards
    • 2. For Teensy boards
    • 3. For Arduino SAM DUE boards
    • 4. For Arduino SAMD boards
      • For core version v1.8.10+
      • For core version v1.8.9-
    • 5. For Adafruit SAMD boards
    • 6. For Seeeduino SAMD boards
    • 7. For STM32 boards
      • 7.1. For STM32 boards to use LAN8720
      • 7.2. For STM32 boards to use Serial1
    • 8. For RP2040-based boards using Earle Philhower arduino-pico core
      • 8.1. To use BOARD_NAME
      • 8.2. To avoid compile error relating to microsecondsToClockCycles
    • 9. For Portenta_H7 boards using Arduino IDE in Linux
    • 10. For RTL8720DN boards using AmebaD core
    • 11. For SAMD21 and SAMD51 boards using ArduinoCore-fab-sam core
    • 12. For Seeeduino RP2040 boards
    • 13. For Seeeduino nRF52840 boards
  • Libraries' Patches
    • 1. For application requiring 2K+ HTML page
    • 2. For Ethernet library
    • 3. For EthernetLarge library
    • 4. For Etherne2 library
    • 5. For Ethernet3 library
    • 6. For UIPEthernet library
    • 7. For fixing ESP32 compile error
    • 8. For fixing ESP8266 compile error
  • HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)
    • 1. ESP32 has 2 ADCs, named ADC1 and ADC2
    • 2. ESP32 ADCs functions
    • 3. ESP32 WiFi uses ADC2 for WiFi functions
  • Usage
    • MQTT
    • MQTT over WebSocket
    • MQTT with Secure Connection
    • Callback Signature
    • Send and Receive Buffer Size Management
  • APIs
  • HOWTO use STM32F4 with LAN8720
    • 1. Wiring
    • 2. HOWTO program using STLink V-2 or V-3
    • 3. HOWTO use Serial Port for Debugging
  • Examples
    • 1. For Ethernet
      • 1. EtherMQTT
      • 2. EtherMQTToverWebSocket
    • 2. For QNEthernet
      • 1. MQTToverWebSocket_QNEthernet
      • 2. MQTT_QNEthernet
    • 3. For STM32
      • 1. EtherMQTToverWebSocket_STM32
      • 2. EtherMQTT_STM32
    • 4. For WiFi
      • 1. WiFiMQTT
      • 2. WiFiMQTToverWebSocket
      • 3. WiFiMQTToverWebSocketSecure
      • 4. WiFiMQTTSecure
      • 5. WiFiMQTTSecureAWS
    • 5. For RP2040W New
      • 1. WiFiMQTT
      • 2. WiFiMQTToverWebSocket
      • 3. WiFiMQTTSecure
      • 4. WiFiMQTTSecureAWS
    • 6. For WiFiNINA
      • 1. WiFiNINA_MQTT
      • 2. WiFiNINA_MQTToverWebSocket
      • 3. WiFiNINA_MQTToverWebSocketSecure
      • 4. WiFiNINA_MQTTSecure
      • 5. WiFiNINA_MQTTSecureAWS
    • 7. For WiFi101
      • 1. WiFiMQTT
      • 2. WiFiMQTToverWebSocket
  • Example EtherMQTToverWebSocket
    • 1. File EtherMQTToverWebSocket.ino
    • 2. File defines.h
  • Debug Terminal Output Samples
    • 1. WiFiMQTToverWebSocket on ESP8266_NODEMCU_ESP12E
    • 2. WiFiMQTToverWebSocketSecure on ESP32_DEV
    • 3. WiFiMQTToverWebSocket on SAMD_NANO_33_IOT
    • 4. EtherMQTToverWebSocket on NUCLEO_F767ZI
    • 5. EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI
    • 6. MQTToverWebSocket_QNEthernet on TEENSY 4.1
    • 7. EtherMQTToverWebSocket on MBED RASPBERRY_PI_PICO
    • 8. EtherMQTToverWebSocket on RASPBERRY_PI_PICO
    • 9. EtherMQTToverWebSocket on TEENSY 4.0
    • 10. WiFiMQTT on RASPBERRY_PI_PICO_W New
    • 11. WiFiMQTTSecure on RASPBERRY_PI_PICO_W New
    • 12. WiFiMQTToverWebSocket on RASPBERRY_PI_PICO_W New
    • 13. WiFiNINA_MQTToverWebSocket on Nano_RP2040_Connect New
    • 14. WiFiNINA_MQTToverWebSocketSecure on Nano_RP2040_Connect New
    • 15. WiFiNINA_MQTToverWebSocketSecure on SAMD_NANO_33_IOT New
  • Debug
  • Troubleshooting
  • Issues
  • TO DO
  • DONE
  • Contributions and Thanks
  • Contributing
  • License
  • Copyright


Why do we need this MQTTPubSubClient_Generic library

Features

This MQTTPubSubClient_Generic library is based on and modified from

to provide support to many more boards besides ESP32/ESP8266, such as SAMD, SAM DUE, nRF52, RP2040, STM32, Teensy 4.1, etc. boards. Those supported boards can be used with ESP8266/ESP32’s WiFi, WiFiNINA, CYW43439 WiFi, LAN8742A Ethernet, Teensy 4.1 QNEthernet, W5x00 Ethernet, etc.


Currently supported Boards

This MQTTPubSubClient_Generic library currently supports these following boards:

  1. nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox, etc.

  2. SAMD21

  • Arduino SAMD21: ZERO, MKRs, NANO_33_IOT, etc.
  • Adafruit SAMD21 (M0): ItsyBitsy M0, Feather M0, Feather M0 Express, Metro M0 Express, Circuit Playground Express, Trinket M0, PIRkey, Hallowing M0, Crickit M0, etc.
  • Seeeduino: LoRaWAN, Zero, Femto M0, XIAO M0, Wio GPS Board, etc.
  1. SAMD51
  • Adafruit SAMD51 (M4): Metro M4, Grand Central M4, ItsyBitsy M4, Feather M4 Express, Trellis M4, Metro M4 AirLift Lite, MONSTER M4SK Express, Hallowing M4, etc.
  • Seeeduino: Wio Terminal, Grove UI Wireless
  1. Teensy (4.1, 4.0, 3.6, 3.5, 3,2, 3.1, 3.0)

  2. ESP32

  • ESP32 boards, such as ESP32_DEV, etc.
  • ESP32_S2-based boards, such as ESP32S2_DEV, ESP32_S2 Saola, etc.
  • ESP32_C3-based boards, such as ESP32C3_DEV, etc. New
  • ESP32_S3 (ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.) New
  1. ESP8266

  2. RP2040-based boards, such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.

  3. WT32_ETH01 boards using ESP32-based boards and LAN8720 Ethernet

  4. STM32 boards with built-in Ethernet LAN8742A such as :

  1. STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 or ENC28J60 shields)

    • Nucleo-144
    • Nucleo-64
    • Discovery
    • Generic STM32F0, STM32F1, STM32F2, STM32F3, STM32F4, STM32F7 (with 64+K Flash): x8 and up
    • STM32L0, STM32L1, STM32L4
    • STM32G0, STM32G4
    • STM32H7
    • STM32WB
    • STM32MP1
    • LoRa boards
    • 3-D printer boards
    • Generic Flight Controllers
    • Midatronics boards
  2. STM32 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)
  1. RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, using Ethernet or WiFiNINA

  2. RP2040W boards, such as RASPBERRY_PI_PICO_W, using Cyw43439 WiFi or Ethernet


Currently supported WiFi shields/modules

  1. WiFiNINA using WiFiNINA_Generic library
  2. WWiFi101 using WiFi101_Generic library New
  3. U-Blox W101, W102 using WiFiNINA_Generic library
  4. ESP8266-AT command using WiFiEspAT library
  5. ESP8266/ESP32-AT command using ESP_AT_Lib library and ESP8266_AT_WebServer
  6. ESP32 and ESP8266 WiFi
  7. RP2040W built-in CYW43439 WiFi

Currently supported Ethernet shields/modules

  1. W5x00 using Ethernet_Generic library
  2. ENC28J60 using EthernetENC or UIPEthernet library
  3. LAN8720 Ethernet used in WT32_ETH01 (ESP32 + LAN8720) boards
  4. Teensy 4.1 built-in Ethernet using NativeEthernet library
  5. Teensy 4.1 built-in Ethernet using QNEthernet library
  6. LAN8742A or LAN8720 using STM32Ethernet and LwIP libraries.


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release

  2. Teensy core v1.57+ for Teensy (4.1, 4.0, 3.6, 3.5, 3,2, 3.1, 3.0) boards.

  3. Arduino SAM DUE core v1.6.12+ for SAM DUE ARM Cortex-M3 boards. Not yet

  4. Arduino SAMD core 1.8.13+ for SAMD ARM Cortex-M0+ boards. GitHub release

  5. Adafruit SAMD core 1.7.11+ for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). GitHub release

  6. Seeeduino SAMD core 1.8.3+ for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). Latest release

  7. Adafruit nRF52 v1.3.0+ for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, NINA_B302_ublox, etc. GitHub release

  8. ESP32 Core 2.0.5+ for ESP32-based boards. Latest release

  9. ESP8266 Core 3.0.2+ for ESP8266-based boards. Latest release. To use ESP8266 core 2.7.1+ for LittleFS.

  10. ArduinoCore-mbed mbed_rp2040, mbed_nano, mbed_portenta core 3.4.1+ for Arduino (Use Arduino Board Manager) Portenta_H7, RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO. GitHub release

  11. Earle Philhower's arduino-pico core v2.6.3+ for RP2040-based boards such as RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. GitHub release

  12. Arduino Core for STM32 v2.3.0+ for STM32F/L/H/G/WB/MP1 boards. GitHub release. Use STM32 core v2.2.0 for LAN8720 or compile error.

  13. WiFiNINA_Generic library v1.8.15-1+ if for WiFiNINA. To install. check arduino-library-badge.

  14. EthernetWebServer library v2.3.0+ if necessary to use Ethernet modules/shields. To install, check arduino-library-badge

  15. EthernetWebServer_STM32 library v1.5.0+ if necessary to use Ethernet modules/shields. To install, check arduino-library-badge

  16. WebServer_WT32_ETH01 library v1.5.1+ if necessary to use WT32_ETH01 boards. To install, check arduino-library-badge

  17. Depending on which Ethernet card you're using:

  1. WiFiWebServer library v1.10.1+ if necessary to use certain WiFi/WiFiNINA features. To install, check arduino-library-badge
  2. WiFi101_Generic Library v1.0.0+ to use SAMD MKR1000, etc. boards with WiFi101 to send large data. To install. check arduino-library-badge.
  3. WebSockets_Generic library v2.16.1+ if necessary to use MQTT-over-WebSockets features. To install, check arduino-library-badge


Installation

Use Arduino Library Manager

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

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install MQTTPubSubClient_Generic library by using Library Manager. Search for MQTTPubSubClient_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 Adafruit nRF52840 and nRF52832 boards

To be able to compile, run and automatically detect and display BOARD_NAME on nRF52840/nRF52832 boards, you have to copy the whole nRF52 Packages_Patches directory into Adafruit nRF52 directory (~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0).

Supposing the Adafruit nRF52 version is 1.3.0. These files must be copied into the directory:

  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/platform.txt
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/boards.txt
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Udp.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Print.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Print.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B302_ublox/variant.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B302_ublox/variant.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B112_ublox/variant.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B112_ublox/variant.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Udp.h

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

  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/platform.txt
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/boards.txt
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/cores/nRF5/Udp.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/cores/nRF5/Print.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/cores/nRF5/Print.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B302_ublox/variant.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B302_ublox/variant.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B112_ublox/variant.h
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B112_ublox/variant.cpp
  • ~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/cores/nRF5/Udp.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 Arduino SAM DUE boards

To be able to compile and run on SAM DUE boards, you have to copy the whole SAM DUE directory into Arduino sam directory (~/.arduino15/packages/arduino/hardware/sam/1.6.12).

Supposing the Arduino SAM core version is 1.6.12. This file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/sam/1.6.12/platform.txt

Whenever a new version is installed, remember to copy this file 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/sam/x.yy.zz/platform.txt

4. For Arduino SAMD boards

To be able to compile, run and automatically detect and display BOARD_NAME on Arduino SAMD (Nano-33-IoT, etc) boards, you have to copy the whole Arduino SAMD Packages_Patches directory into Arduino SAMD directory (~/.arduino15/packages/arduino/hardware/samd/1.8.13).

For core version v1.8.10+

Supposing the Arduino SAMD version is 1.8.13. Now only one file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/samd/1.8.13/platform.txt

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/samd/x.yy.zz/platform.txt

For core version v1.8.9-

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

  • ~/.arduino15/packages/arduino/hardware/samd/1.8.9/platform.txt
  • ~/.arduino15/packages/arduino/hardware/samd/1.8.9/cores/arduino/Arduino.h

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

These files must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/samd/x.yy.z/platform.txt
  • ~/.arduino15/packages/arduino/hardware/samd/x.yy.z/cores/arduino/Arduino.h

This is mandatory to fix the notorious Arduino SAMD compiler error. See Improve Arduino compatibility with the STL (min and max macro)

 ...\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
     min(const _Tp& __a, const _Tp& __b, _Compare __comp)

Whenever the above-mentioned compiler error issue is fixed with the new Arduino SAMD release, you don't need to copy the Arduino.h file anymore.

5. For Adafruit SAMD boards

To be able to compile, run and automatically detect and display BOARD_NAME on Adafruit SAMD (Itsy-Bitsy M4, etc) boards, you have to copy the whole Adafruit SAMD Packages_Patches directory into Adafruit samd directory (~/.arduino15/packages/adafruit/hardware/samd/1.7.11).

Supposing the Adafruit SAMD core version is 1.7.11. These files must be copied into the directory:

  • ~/.arduino15/packages/adafruit/hardware/samd/1.7.11/platform.txt
  • ~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.h
  • ~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.cpp

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:

  • ~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/platform.txt
  • ~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.h
  • ~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.cpp

6. For Seeeduino SAMD boards

To be able to compile, run and automatically detect and display BOARD_NAME on Seeeduino SAMD (XIAO M0, Wio Terminal, etc) boards, you have to copy the whole Seeeduino SAMD Packages_Patches directory into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3).

Supposing the Seeeduino SAMD core version is 1.8.3. These files must be copied into the directory:

  • ~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/platform.txt
  • ~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Arduino.h
  • ~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.h
  • ~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.cpp

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:

  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/platform.txt
  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Arduino.h
  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.h
  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.cpp

7. For STM32 boards

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

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

8. For RP2040-based boards using Earle Philhower arduino-pico core

8.1 To use BOARD_NAME

To be able to automatically detect and display BOARD_NAME on RP2040-based boards (RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc) boards, you have to copy the file RP2040 platform.txt into rp2040 directory (~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0).

Supposing the rp2040 core version is 1.4.0. This file must be copied into the directory:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0/platform.txt

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

  • ~/.arduino15/packages/rp2040/hardware/rp2040/x.yy.zz/platform.txt

With core after v1.5.0, this step is not necessary anymore thanks to the PR Add -DBOARD_NAME="{build.board}" #136.

8.2 To avoid compile error relating to microsecondsToClockCycles

Some libraries, such as Adafruit DHT-sensor-library, require the definition of microsecondsToClockCycles(). To be able to compile and run on RP2040-based boards, you have to copy the files in RP2040 Arduino.h into rp2040 directory (~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0).

Supposing the rp2040 core version is 1.4.0. This file must be copied to replace:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0/cores/rp2040/Arduino.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 This file must be copied to replace:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/x.yy.zz/cores/rp2040/Arduino.h

With core after v1.5.0, this step is not necessary anymore thanks to the PR Add defs for compatibility #142.

9. 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

10. For RTL8720DN boards using AmebaD core

To avoid compile error relating to PROGMEM, you have to copy the file Realtek AmebaD core pgmspace.h into Realtek AmebaD directory (~/.arduino15/packages/realtek/hardware/AmebaD/3.1.4/cores/ambd/avr/pgmspace.h).

Supposing the Realtek AmebaD core version is 3.1.4. This file must be copied into the directory:

  • ~/.arduino15/packages/realtek/hardware/AmebaD/3.1.4/cores/ambd/avr/pgmspace.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 This file must be copied into the directory:

  • ~/.arduino15/packages/realtek/hardware/AmebaD/x.yy.zz/cores/ambd/avr/pgmspace.h

11. For SAMD21 and SAMD51 boards using ArduinoCore-fab-sam core

To avoid compile error relating to SAMD21/SAMD51, you have to copy the file ArduinoCore-fab-sam core pgmspace.h into ArduinoCore-fab-sam samd directory (~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.9.0/boards.txt).

Supposing the ArduinoCore-fab-sam samd core version is 1.9.0. This file must be copied into the directory:

  • ~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/1.9.0/boards.txt

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

  • ~/.arduino15/packages/Fab_SAM_Arduino/hardware/samd/x.yy.zz/boards.txt

12. For Seeeduino RP2040 boards

To be able to compile, run and automatically detect and display BOARD_NAME on Seeeduino RP2040 (XIAO RP2040, Wio RP2040 Mini) boards, you have to copy the whole Seeeduino RP2040 Packages_Patches directory into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2).

Supposing the Seeeduino RP2040 core version is 2.7.2. These files must be copied into the directory:

  • ~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2/boards.txt
  • ~/.arduino15/packages/Seeeduino/hardware/rp2040/2.7.2/variants/Seeed_XIAO_RP2040/pins_arduino.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:

  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/boards.txt
  • ~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/variants/Seeed_XIAO_RP2040/pins_arduino.h

13. For Seeeduino nRF52840 boards

To be able to compile and run on Xiao nRF52840 boards, you have to copy the whole nRF52 1.0.0 directory into Seeeduino nRF52 directory (~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0).

Supposing the Seeeduino nRF52 version is 1.0.0. These files must be copied into the directory:

  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/platform.txt
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Print.h
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Print.cpp
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/1.0.0/cores/nRF5/Udp.h

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

  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/platform.txt
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Print.h
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Print.cpp
  • ~/.arduino15/packages/Seeeduino/hardware/nrf52/x.yy.z/cores/nRF5/Udp.h


Libraries' Patches

1. For application requiring 2K+ HTML page

If your application requires 2K+ HTML page, the current Ethernet library must be modified if you are using W5200/W5500 Ethernet shields. W5100 is not supported for 2K+ buffer. If you use boards requiring different CS/SS pin for W5x00 Ethernet shield, for example ESP32, ESP8266, nRF52, etc., you also have to modify the following libraries to be able to specify the CS/SS pin correctly.

2. For Ethernet library

To fix Ethernet library, just copy these following files into the Ethernet library directory to overwrite the old files:

  • Ethernet.h
  • Ethernet.cpp
  • EthernetServer.cpp
  • w5100.h
  • w5100.cpp

You can also use the forked and modified library at Patched Ethernet

3. For EthernetLarge library

To fix EthernetLarge library, just copy these following files into the EthernetLarge library directory to overwrite the old files:

  • EthernetLarge.h
  • EthernetLarge.cpp
  • EthernetServer.cpp
  • w5100.h
  • w5100.cpp

You can also use the forked and modified library at Patched EthernetLarge

4. For Ethernet2 library

To fix Ethernet2 library, just copy these following files into the Ethernet2 library directory to overwrite the old files:

  • Ethernet2.h
  • Ethernet2.cpp

To add UDP Multicast support, necessary for the UPnP_Generic library:

  • EthernetUdp2.h
  • EthernetUdp2.cpp

You can also use the forked and modified library at Patched Ethernet2

5. For Ethernet3 library

  1. To fix Ethernet3 library, just copy these following files into the Ethernet3 library directory to overwrite the old files:
  • Ethernet3.h
  • Ethernet3.cpp

You can also use the forked and modified library at Patched Ethernet3

6. For UIPEthernet library

To be able to compile and run on nRF52 boards with ENC28J60 using UIPEthernet library, you have to copy these following files into the UIPEthernet utility directory to overwrite the old files:

  • UIPEthernet.h
  • UIPEthernet.cpp
  • Enc28J60Network.h
  • Enc28J60Network.cpp

7. For fixing ESP32 compile error

To fix ESP32 compile error, just copy the following file into the ESP32 cores/esp32 directory (e.g. ./arduino-1.8.19/hardware/espressif/cores/esp32) to overwrite the old file:

  • Server.h

8. For fixing ESP8266 compile error

To fix ESP8266 compile error such as

error: 'class EthernetClass' has no member named 'init'
Ethernet.init (USE_THIS_SS_PIN);

just rename the following file in ./arduino-1.8.19/hardware/esp8266com/esp8266/libraries/Ethernet directory

  • From Ethernet.h to Ethernet_ESP8266.h


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).


Usage

MQTT

#include <WiFi.h>
// or
// #include <Ethernet_Generic.h>
#include <MQTTPubSubClient_Generic.h>

WiFiClient client;
// or
// EthernetClient client;
MQTTPubSubClient mqttClient;

void setup()
{
  // start your network
  WiFi.begin("your-ssid", "your-password");
  // connect to host
  client.connect("public.cloud.shiftr.io", 1883);
  // initialize mqtt client
  mqttClient.begin(client);
  // connect to mqtt broker
  mqttClient.connect("arduino", "public", "public");

  // subscribe callback which is called when every packet has come
  mqttClient.subscribe([](const String & topic, const String & payload, const size_t size)
  {
    Serial.println("mqtt received: " + topic + " - " + payload);
  });

  // subscribe topic and callback which is called when /hello has come
  mqttClient.subscribe("/hello", [](const String & payload, const size_t size)
  {
    Serial.print("/hello ");
    Serial.println(payload);
  });
}

void loop()
{
  // should be called to trigger callbacks
  mqttClient.update();
  // publish message
  mqttClient.publish("/hello", "world");
  delay(1000);
}

MQTT over WebSocket

Just replace WiFiClient or EthernetClient to WebSocketsClient.

#include <WiFi.h>
#include <WebSocketsClient_Generic.h>  // include before MQTTPubSubClient_Generic.h
#include <MQTTPubSubClient_Generic.h>

WebSocketsClient client;
MQTTPubSubClient mqttClient;

void setup()
{
  // start your network
  WiFi.begin("your-ssid", "your-password");
  // connect to host with MQTT over WebSocket securely
  client.beginSSL("public.cloud.shiftr.io", 443);
  client.setReconnectInterval(2000);
  // initialize mqtt client
  mqttClient.begin(client);
  // connect to mqtt broker
  mqttClient.connect("arduino", "public", "public");

  // subscribe callback which is called when every packet has come
  mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) 
  {
    Serial.println("mqtt received: " + topic + " - " + payload);
  });
  
  // subscribe topic and callback which is called when /hello has come
  mqttClient.subscribe("/hello", [](const String & payload, const size_t size) 
  {
    Serial.print("/hello ");
    Serial.println(payload);
  });
}

void loop() 
{
  // should be called to trigger callbacks
  mqttClient.update();
  // publish message
  mqttClient.publish("/hello", "world");
  delay(1000);
}

MQTT with Secure Connection

If your board supports secure connection with certificates, it is also supported same as above. Please see WiFiMQTTSecureAWS example for the detail.

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <MQTTPubSubClient_Generic.h>

const char AWS_CERT_CA[] =
  "-----BEGIN CERTIFICATE-----\n"
  "-----END CERTIFICATE-----\n";
const char AWS_CERT_PRIVATE[] =
  "-----BEGIN RSA PRIVATE KEY-----\n"
  "-----END RSA PRIVATE KEY-----\n";
const char AWS_CERT_CRT[] =
  "-----BEGIN CERTIFICATE-----\n"
  "-----END CERTIFICATE-----\n";

WiFiClientSecure client;
MQTTPubSubClient mqttClient;

void setup() 
{
  // start your network
  WiFi.begin("your-ssid", "your-password");
  // connect to aws endpoint with certificates and keys
  client.setCACert(AWS_CERT_CA);
  client.setCertificate(AWS_CERT_CRT);
  client.setPrivateKey(AWS_CERT_PRIVATE);
  client.connect("your-endpoint.iot.ap-somewhre.amazonaws.com", 8883);
  // initialize mqtt client
  mqttClient.begin(client);
  // connect to mqtt broker
  mqttClient.connect("your-device-name");

  // subscribe callback which is called when every packet has come
  mqttClient.subscribe([](const String & topic, const String & payload, const size_t size) 
  {
    Serial.println("mqtt received: " + topic + " - " + payload);
  });
  
  // subscribe topic and callback which is called when /hello has come
  mqttClient.subscribe("/hello", [](const String & payload, const size_t size) 
  {
    Serial.print("/hello ");
    Serial.println(payload);
  });
}

void loop() 
{
  // should be called to trigger callbacks
  mqttClient.update();
  // publish message
  mqttClient.publish("/hello", "aws");
  delay(1000);
}

Callback Signature

The following signature are available for callbacks.

// callback for every packet
void callback(const String& topic, const String& payload, const size_t size);
void callback(const String& topic, const char* payload, const size_t size);
void callback(const char* topic, const String& payload, const size_t size);
void callback(const const char* topic, const char* payload, const size_t size);

// callback for each topic
void callback(const String& payload, const size_t size);
void callback(const char* payload, const size_t size);

Send and Receive Buffer Size Management

The default send / receive buffer size is 128 and defined as below.

using MQTTPubSubClient = MQTTPubSub::PubSubClient<128>;

If you want to change the buffer, please create instance like this.

MQTTPubSub::PubSubClient<256> mqtt;

APIs

void begin(ClientType& client);
bool connect(const String& client_id, const String& user = "", const String& pass = "");
bool disconnect();
bool update();

bool publish(const String& topic, const String& payload, const bool retained = false, int qos = 0);
bool publish(const String& topic, uint8_t* payload, const size_t length, const bool retained = false, const uint8_t qos = 0);

void subscribe(const global_callback_t& cb);
bool subscribe(const String& topic, const topic_callback_t& cb);
bool subscribe(const String& topic, const uint8_t qos, const topic_callback_t& cb);
bool unsubscribe(const String& topic);

void setClockSource(const MQTTClientClockSource& cb);
void setWill(const String& topic, const String& payload = "", const bool retained = false, const uint8_t qos = 0);
void clearWill();
void setKeepAliveSendInterval(const uint32_t ms);
void setKeepAliveTimeout(const uint16_t sec);
void setCleanSession(const bool b);
void setTimeout(const uint32_t ms);
void setOptions(const uint16_t keep_alive_timeout_sec, const bool b_clean_session, const uint32_t timeout_ms);

bool isConnected() const;
lwmqtt_err_t getLastError() const;
lwmqtt_return_code_t getReturnCode() const;


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


Examples

1. For Ethernet

  1. EtherMQTT
  2. EtherMQTToverWebSocket

2. For QNEthernet

  1. MQTToverWebSocket_QNEthernet
  2. MQTT_QNEthernet

3. For STM32

  1. EtherMQTToverWebSocket_STM32
  2. EtherMQTT_STM32

4. For WiFi

  1. WiFiMQTT
  2. WiFiMQTToverWebSocket
  3. WiFiMQTToverWebSocketSecure
  4. WiFiMQTTSecure
  5. WiFiMQTTSecureAWS

5. For RP2040W

  1. WiFiMQTT
  2. WiFiMQTToverWebSocket
  3. WiFiMQTTSecure
  4. WiFiMQTTSecureAWS

6. For WiFiNINA

  1. WiFiNINA_MQTT
  2. WiFiNINA_MQTToverWebSocket
  3. WiFiNINA_MQTToverWebSocketSecure
  4. WiFiNINA_MQTTSecure
  5. WiFiNINA_MQTTSecureAWS

7. For WiFi101

  1. WiFiMQTT
  2. WiFiMQTToverWebSocket


Example EtherMQTToverWebSocket

1. File EtherMQTToverWebSocket.ino

https://github.com/khoih-prog/MQTTPubSubClient_Generic/blob/da7f8b5c32a139b67e450d815dab9f8675459f81/examples/Ethernet/EtherMQTToverWebSocket/EtherMQTToverWebSocket.ino#L17-L326

2. File defines.h

https://github.com/khoih-prog/MQTTPubSubClient_Generic/blob/da7f8b5c32a139b67e450d815dab9f8675459f81/examples/Ethernet/EtherMQTToverWebSocket/defines.h#L16-L491



Debug Terminal Output Samples

1. WiFiMQTToverWebSocket on ESP8266_NODEMCU_ESP12E

This is terminal debug output when running WiFiMQTToverWebSocket on ESP8266_NODEMCU_ESP12E connecting to test.mosquitto.org MQTT server.

Start WiFiMQTToverWebSocket on ESP8266_NODEMCU_ESP12E with ESP WiFi using WiFi Library
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.145
SSID: HueNet1, Signal strength (RSSI):-39 dBm
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from ESP8266_NODEMCU_ESP12E
Subscribed to /mqttPubSub => Hello from ESP8266_NODEMCU_ESP12E

2. WiFiMQTToverWebSocketSecure on ESP32_DEV

This is terminal debug output when running WiFiMQTToverWebSocketSecure on ESP32_DEV connecting to public.cloud.shiftr.io:443 WebSockets server.

Start WiFiMQTToverWebSocketSecure on ESP32_DEV
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.93
SSID: HueNet1, Signal strength (RSSI):-35 dBm
Connecting to secured-host:port = public.cloud.shiftr.io:443
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from WiFiMQTToverWebSocketSecure on ESP32_DEV
Subscribed to /mqttPubSub => Hello from WiFiMQTToverWebSocketSecure on ESP32_DEV

3. WiFiMQTToverWebSocket on SAMD_NANO_33_IOT

This is terminal debug output when running WiFiMQTToverWebSocket on SAMD_NANO_33_IOT connecting to test.mosquitto.org MQTT server.

Start WiFiMQTToverWebSocket on SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library
WiFiWebServer v1.10.1
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.150
SSID: HueNet1, Signal strength (RSSI):-15 dBm
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library
Subscribed to /mqttPubSub => Hello from SAMD_NANO_33_IOT with WiFiNINA using WiFiNINA_Generic Library

4. EtherMQTToverWebSocket on NUCLEO_F767ZI

This is terminal debug output when running EtherMQTToverWebSocket on STM32F7 NUCLEO_F767ZI connecting to test.mosquitto.org MQTT server.

Start EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library
EthernetWebServer_STM32 v1.5.0
MQTTPubSubClient_Generic v1.2.1
[EWS] Board : NUCLEO_F767ZI , setCsPin: 10
[EWS] Default SPI pinout:
[EWS] MOSI: 11
[EWS] MISO: 12
[EWS] SCK: 13
[EWS] SS: 10
[EWS] =========================
Connected! IP address: 192.168.2.135
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library
Subscribed to /mqttPubSub => Hello from NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library
MQTT received: /mqttPubSub - Hello from NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library
Subscribed to /mqttPubSub => Hello from NUCLEO_F767ZI with W5x00 using default Ethernet_Generic Library

5. EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI

This is terminal debug output when running EtherMQTToverWebSocket_STM32 on STM32F7 NUCLEO_F767ZI-ETH01 with LAN8742A connecting to test.mosquitto.org MQTT server.

Start EtherMQTToverWebSocket_STM32 on NUCLEO_F767ZI with LAN8742A Ethernet & STM32Ethernet Library
EthernetWebServer_STM32 v1.5.0
MQTTPubSubClient_Generic v1.2.1
Connected! IP address: 192.168.2.88
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from NUCLEO_F767ZI with LAN8742A Ethernet & STM32Ethernet Library
Subscribed to /mqttPubSub => Hello from NUCLEO_F767ZI with LAN8742A Ethernet & STM32Ethernet Library

6. MQTToverWebSocket_QNEthernet on TEENSY 4.1

This is terminal debug output when running MQTToverWebSocket_QNEthernet on TEENSY 4.1 with QNEthernet connecting to test.mosquitto.org MQTT server.

Start MQTToverWebSocket_QNEthernet on TEENSY 4.1
MQTTPubSubClient_Generic v1.2.1
=========== USE_QN_ETHERNET ===========
Initialize Ethernet using static IP => Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from TEENSY 4.1 using QNEthernet
Subscribed to /mqttPubSub => Hello from TEENSY 4.1 using QNEthernet

7. EtherMQTToverWebSocket on MBED RASPBERRY_PI_PICO

This is terminal debug output when running EtherMQTToverWebSocket on MBED RASPBERRY_PI_PICO with W5500 Ethernet connecting to test.mosquitto.org MQTT server.

Start EtherMQTToverWebSocket on MBED RASPBERRY_PI_PICO
EthernetWebServer v2.3.0
MQTTPubSubClient_Generic v1.2.1
[EWS] =========== USE_ETHERNET_GENERIC ===========
[EWS] Default SPI pinout:
[EWS] MOSI: 19
[EWS] MISO: 16
[EWS] SCK: 18
[EWS] SS: 17
[EWS] =========================
[EWS] RPIPICO setCsPin: 17
_pinCS = 0
W5100 init, using SS_PIN_DEFAULT = 17, new ss_pin = 10, W5100Class::ss_pin = 17
W5100::init: W5500, SSIZE =8192
=========================
Currently Used SPI pinout:
MOSI:19
MISO:16
SCK:18
SS:17
=========================
Using mac index = 17
Connected! IP address: 192.168.2.103
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker...Done connected!
MQTT received: /mqttPubSub - Hello from MBED RASPBERRY_PI_PICO
Subscribed to /mqttPubSub => Hello from MBED RASPBERRY_PI_PICO

8. EtherMQTToverWebSocket on RASPBERRY_PI_PICO

This is terminal debug output when running EtherMQTToverWebSocket on RASPBERRY_PI_PICO with W5500 Ethernet connecting to test.mosquitto.org MQTT server.

Start EtherMQTToverWebSocket on RASPBERRY_PI_PICO with W5x00 using Ethernet_Generic Library on SPI1
EthernetWebServer v2.3.0
MQTTPubSubClient_Generic v1.2.1
[EWS] =========== USE_ETHERNET_GENERIC ===========
[EWS] Default SPI pinout:
[EWS] MOSI: 15
[EWS] MISO: 12
[EWS] SCK: 14
[EWS] SS: 13
[EWS] =========================
[EWS] RPIPICO setCsPin: 13
[EWS] =========================
[EWS] Currently Used SPI pinout:
[EWS] MOSI: 15
[EWS] MISO: 12
[EWS] SCK: 14
[EWS] SS: 13
[EWS] =========================
Using mac index = 9
Connected! IP address: 192.168.2.115
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO with W5x00 using Ethernet_Generic Library on SPI1
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO with W5x00 using Ethernet_Generic Library on SPI1

9. EtherMQTToverWebSocket on TEENSY 4.0

This is terminal debug output when running EtherMQTToverWebSocket on TEENSY 4.0 with W5500 Ethernet connecting to test.mosquitto.org MQTT server.

Start EtherMQTToverWebSocket on TEENSY 4.0 with W5x00 using Ethernet_Generic Library on SPI0/SPI
EthernetWebServer v2.3.0
MQTTPubSubClient_Generic v1.2.1
[EWS] =========== USE_ETHERNET_GENERIC ===========
[EWS] Default SPI pinout:
[EWS] MOSI: 11
[EWS] MISO: 12
[EWS] SCK: 13
[EWS] SS: 10
[EWS] =========================
[EWS] Board : TEENSY 4.0 , setCsPin: 10
[EWS] =========================
[EWS] Currently Used SPI pinout:
[EWS] MOSI: 11
[EWS] MISO: 12
[EWS] SCK: 13
[EWS] SS: 10
[EWS] =========================
Using mac index = 4
Connected! IP address: 192.168.2.118
Connecting to WebSockets Server @ test.mosquitto.org
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from TEENSY 4.0 with W5x00 using Ethernet_Generic Library on SPI0/SPI
Subscribed to /mqttPubSub => Hello from TEENSY 4.0 with W5x00 using Ethernet_Generic Library on SPI0/SPI

10. WiFiMQTT on RASPBERRY_PI_PICO_W

This is terminal debug output when running WiFiMQTT on RASPBERRY_PI_PICO_W connecting to public.cloud.shiftr.io MQTT server.

Start WiFiMQTT on RASPBERRY_PI_PICO_W
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.77
SSID: HueNet1, Signal strength (RSSI):0 dBm
Connecting to WebSockets Server @ public.cloud.shiftr.io, port 1883
Connected!
Connecting to mqtt broker... connected!
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W

11. WiFiMQTTSecure on RASPBERRY_PI_PICO_W

This is terminal debug output when running WiFiMQTTSecure on RASPBERRY_PI_PICO_W connecting to public.cloud.shiftr.io MQTT server.

Start WiFiMQTTSecure on RASPBERRY_PI_PICO_W
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.77
SSID: HueNet1, Signal strength (RSSI):0 dBm
Connecting to secured-host:port = public.cloud.shiftr.io:8883

Connected!
Connecting to mqtt broker... connected!
MQTT received: /mqttPubSub - Hello from WiFiMQTTSecure on RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from WiFiMQTTSecure on RASPBERRY_PI_PICO_W

12. WiFiMQTToverWebSocket on RASPBERRY_PI_PICO_W

This is terminal debug output when running WiFiMQTToverWebSocket on RASPBERRY_PI_PICO_W connecting to test.mosquitto.org MQTT server.

Start WiFiMQTToverWebSocket on RASPBERRY_PI_PICO_W
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.77
SSID: HueNet1, Signal strength (RSSI):0 dBm
Connecting to WebSockets Server @ test.mosquitto.org, port 8080
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from RASPBERRY_PI_PICO_W
Subscribed to /mqttPubSub => Hello from RASPBERRY_PI_PICO_W

13. WiFiNINA_MQTToverWebSocket on Nano_RP2040_Connect

This is terminal debug output when running WiFiNINA_MQTToverWebSocket on NANO_RP2040_CONNECT, with WiFiNINA using WiFiNINA_Generic Library and arduino-pico core, connecting to test.mosquitto.org MQTT server.

Start WiFiNINA_MQTToverWebSocket on NANO_RP2040_CONNECT with WiFiNINA using WiFiNINA_Generic Library
WiFiNINA_Generic v1.8.15-1
WiFiWebServer v1.9.5
WebSockets_Generic v2.16.1
MQTTPubSubClient_Generic v1.2.1
Please upgrade the firmware
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.105
SSID: HueNet1, Signal strength (RSSI):-27 dBm
Connecting to WebSockets Server @ test.mosquitto.org, port 8080
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from NANO_RP2040_CONNECT with WiFiNINA using WiFiNINA_Generic Library
Subscribed to /mqttPubSub => Hello from NANO_RP2040_CONNECT with WiFiNINA using WiFiNINA_Generic Library

14. WiFiNINA_MQTToverWebSocketSecure on Nano_RP2040_Connect

This is terminal debug output when running WiFiNINA_MQTToverWebSocketSecure on NANO_RP2040_CONNECT, with WiFiNINA using WiFiNINA_Generic Library and Arduino mbed_nano core, connecting to public.cloud.shiftr.io Secured MQTT server.

Start WiFiNINA_MQTToverWebSocketSecure on Nano RP2040 Connect
WiFiNINA_Generic v1.8.15-1
WebSockets_Generic v2.16.1
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.105
SSID: HueNet1, Signal strength (RSSI):-30 dBm
Connecting to secured-host:port = public.cloud.shiftr.io:443
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from WiFiMQTToverWebSocketSecure on Nano RP2040 Connect
Subscribed to /mqttPubSub => Hello from WiFiMQTToverWebSocketSecure on Nano RP2040 Connect

15. WiFiNINA_MQTToverWebSocketSecure on SAMD_NANO_33_IOT

This is terminal debug output when running WiFiNINA_MQTToverWebSocketSecure on SAMD_NANO_33_IOT, with WiFiNINA using WiFiNINA_Generic Library, connecting to public.cloud.shiftr.io Secured MQTT server.

Start WiFiNINA_MQTToverWebSocketSecure on Nano RP2040 Connect
WiFiNINA_Generic v1.8.15-1
WebSockets_Generic v2.16.1
MQTTPubSubClient_Generic v1.2.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.105
SSID: HueNet1, Signal strength (RSSI):-30 dBm
Connecting to secured-host:port = public.cloud.shiftr.io:443
Connecting to mqtt broker.... connected!
MQTT received: /mqttPubSub - Hello from WiFiMQTToverWebSocketSecure on Nano RP2040 Connect
Subscribed to /mqttPubSub => Hello from WiFiMQTToverWebSocketSecure on Nano RP2040 Connect


Debug

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

#define MQTT_PUBSUB_DEBUG_PORT              Serial

// Debug Level from 0 to 4
#define _MQTT_PUBSUB_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: MQTTPubSubClient_Generic issues



TO DO

  1. Support Portenta_H7, SAM-DUE, megaAVR, etc.
  2. Bug fixing.

DONE

  1. Add support to Arduino SAMD21 (ZERO, MKR, NANO_33_IOT, etc.)
  2. Add support to Adafruit SAMD21 (Itsy-Bitsy M0, Metro M0, Feather M0 Express, etc.).
  3. Add support to Adafruit SAMD51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
  4. Add support to Adafruit nRF52 ( Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox, etc..
  5. Add support to Ethernet W5x00, using either Ethernet, Ethernet2, Ethernet3 or EthernetLarge library
  6. Add support to Ethernet ENC28J60, using UIPEthernet library
  7. Add support to ESP32 and ESP8266
  8. Add support to Seeeduino SAMD21/SAMD51: LoRaWAN, Zero, Femto M0, XIAO M0, Wio GPS Board, Wio Terminal, Grove UI Wireless
  9. Add support to EthernetENC
  10. Add support to RP2040-based boards such as RASPBERRY_PI_PICO, using Arduino-mbed RP2040 core
  11. Add support to RP2040-based boards such as RASPBERRY_PI_PICO, using Earle Philhower's arduino-pico core
  12. Add support to Teensy 4.1 built-in Ethernet using NativeEthernet library
  13. Add support to Teensy 4.1 built-in Ethernet using QNEthernet library
  14. Add support to SAMD21/SAMD51 boards using Fab_SAM_Arduino core
  15. Use new Ethernet_Generic library as default for W5x00.
  16. Support SPI2 for ESP32
  17. Add support to SPI1 for RP2040 using arduino-pico core
  18. Use new waitForLink() function of QNEthernet
  19. Add support to RP2040W using MQTT, Secured MQTT or MQTT-over-WebSockets with arduino-pico core
  20. Add support to Nano_RP2040_Connect, Nano_33_IoT, using MQTT, Secured MQTT, MQTT-over-WebSockets or Secured MQTT-over-WebSockets with WiFiNINA_Generic library library
  21. Add support to MKRWiFi1010, etc., using MQTT, MQTT-over-WebSockets with new WiFi101_Generic library library


Contributions and Thanks

  1. Based on and modified from
256dpi
⭐️ Joël Gähwiler

hideakitai
⭐️ Hideaki Tai


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


Copyright

  1. Copyright (c) 2017- Joël Gähwiler
  2. Copyright (c) 2021- Hideaki Tai
  3. Copyright (c) 2022- Khoi Hoang