Arduino, ESP 32 ,ESP 8266 With MQTT and MQTT-SN

Arduino, ESP32, and ESP8266 are popular micro controllers used in the maker community.

These devices can be integrated with MQTT and MQTT-SN to build efficient IoT systems.

  • Arduino: A popular microcontroller platform that is highly customizable and easy to use with sensors, actuators, and modules.
  • ESP32: A powerful microcontroller with built-in Wi-Fi and Bluetooth. It supports larger projects and high-performance IoT applications.
  • ESP8266: A compact, low-cost Wi-Fi microcontroller, ideal for simpler IoT projects.
  • MQTT is a lightweight, publish/subscribe messaging protocol designed for low-bandwidth, high-latency, or unreliable networks using TCP as the network protocol.
  • MQTT-SN (MQTT for Sensor Networks) is a variant of MQTT and generally uses UDP as the networking protocol.

Both MQTT and MQTT-SN require a broker.

There are many MQTT brokers available the most popular being mosquitto. However there are very few MQTT-SN brokers available and so MQTT-SN usually communicates via an MQTT-SN to MQTT Gateway.

MQTT Libraries

When working with Arduino for MQTT (Message Queuing Telemetry Transport) communication, two popular MQTT client libraries are often used: ArduinoMqttClient and PubSubClient.

Each library has its own strengths and trade-offs depending on your project’s requirements. Here’s a detailed comparison of the two:

  • ArduinoMqttClient:
    • Supports MQTT 3.1 and 3.1.1 versions.
    • Supports MQTT over Websockets
    • Integrates seamlessly with modern Arduino IDEs and boards.
    • Compatible with newer networking hardware like the Arduino MKR WiFi 1010, Nano 33 IoT, and similar devices.
    • Works directly with Arduino-compatible cloud services such as Arduino IoT Cloud.
    • Limited support for older boards like ESP8266 and more geared towards newer ones.
    • As it’s newer, it might have fewer resources or example codes available compared to PubSubClient.
  • PubSubClient:
    • Created by Nick O’Leary, this is a widely used and mature MQTT library.
    • Supports MQTT 3.1 and 3.1.1 versions.
    • No WebSockets support.
    • Very lightweight and optimized for low-memory devices like ESP8266 or ESP32.
    • Well-documented and widely used across various platforms and hardware setups, including ESP8266, ESP32, and Ethernet shields.

Other Libraries

  • WiFi (for ESP32/ESP8266): Connects the microcontroller to a Wi-Fi network.
  • WiFiClientSecure: For secure communication with TLS/SSL support (if needed).
  • MqttSnClient: for MQTT-SN

Note: full details are in the code examples.

Installing New Board Support

You will need to install additional boards for the ESP devices though the board manager.

  • In the Boards Manager window, you can search for specific board platforms (e.g., “ESP32”, “ESP8266”).
  • Click on the desired package and press “Install”. This will download and install the necessary files to support that platform.
  • After installation, you can select the board in the “Tools” > “Board” menu.

You can manage third-party boards by adding additional Board Manager URLs:

  1. Go to File > Preferences.
  2. In the “Additional Boards Manager URLs” field, you can enter URLs provided by third-party board developers (e.g., for ESP8266 or ESP32).
  3. Click OK and re-open the Boards Manager. You should now see third-party boards in the list.

Here are some popular URLs for additional boards:

  • ESP8266: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  • ESP32: https://dl.espressif.com/dl/package_esp32_index.json

arduino-boards-preferences

MQTT vs. MQTT-SN: Which One to Use?

  • MQTT: Ideal for devices with better power and bandwidth capabilities (e.g., ESP32, ESP8266). It’s a well-established protocol and supports reliable communication over TCP. Because of the widespread support it is usually the first choice.
  • MQTT-SN: Better suited for constrained devices like low-power wireless sensors. It uses UDP, which consumes less power and bandwidth than TCP, making it more appropriate for battery-operated devices. In addition is places less requirements on the broker hardware.

General Tutorials

Code Examples

The following tutorials illustrate using MQTT and MQTT-SN and include full code examples. Currently working on these examples

MQTT

For MQTT I will include examples using the ArduinoClient and the PubSubClient

MQTT-SN

  • Using MQTT-SN with Arduino
  • Using MQTT-SN with ESP8266
  • Using MQTT-SN with ESP32
Please rate? And use Comments to let me know more