Introduction
In IoT applications, battery-powered devices often need to communicate with MQTT servers, but the high power consumption of WiFi connections can severely affect battery life. The ESPNow2Mqtt library was born to address this issue, utilizing ESP-Now technology to complete MQTT communication within 100 milliseconds, achieving low power consumption and efficient IoT data transmission.
Architecture Overview
The ESPNow2Mqtt library adopts a unique design architecture that bridges the communication between devices and the MQTT server through an intermediate layer called a “gateway”. The library provides two core objects:
-
• EspNow2MqttClient: The device-side object used to send data to the MQTT server and receive data from the MQTT server.
-
• EspNow2MqttGateway: The gateway-side object responsible for receiving data sent from devices and publishing it to the MQTT server, while listening to MQTT server subscriptions and forwarding data to the corresponding devices.
-
Design Philosophy
The design of the ESPNow2Mqtt library is based on the ESP-Now protocol and introduces the following key elements:
-
• Custom Encryption: To overcome the limitations of the ESP-Now protocol, the library employs a custom encryption algorithm based on ChaCha encryption to achieve more secure data transmission.
-
• Lightweight Protocol: A simple protocol based on nanopb is defined in the library, allowing efficient querying and writing of MQTT topics.
-
• Message Size Limitation: To accommodate the limitations of the ESP-Now protocol, the library restricts message sizes to 200 bytes and supports a maximum of 5 publish or subscribe operations per request.
MQTT Topic Design
The ESPNow2Mqtt library adopts a simplified MQTT topic naming convention to save bandwidth and ensure efficient and secure communication between devices and the gateway. The topic structure is as follows:
EspNow/clientId/name
Where:
-
• EspNow: A fixed name used to identify ESPNow communication.
-
• clientId: A unique identifier specified by the device during initialization.
-
• name: A custom topic name defined by the device.
Simple Example: Blinking LED
The following code demonstrates a simple example of controlling an LED to blink using the ESPNow2Mqtt library:
#include <Arduino.h>
#include <EspNow2MqttClient.hpp>
#define LED 2
byte sharedKey[16]={10,200,23,4,50,3,99,82,39,100,211,112,143,4,15,106};
byte sharedChannel =8;
uint8_t gatewayMac[6]={0xA4,0xCF,0x12,0x25,0x9A,0x30};
EspNow2MqttClient client =EspNow2MqttClient("testLib", sharedKey, gatewayMac, sharedChannel);
void setup() {
pinMode(LED, OUTPUT);
client.init();
}
void loop() {
digitalWrite(LED, HIGH);
client.doSend("ON","led");
delay(1000);
digitalWrite(LED, LOW);
client.doSend("OFF","led");
delay(1000);
}
Device-Side Functions
The EspNow2MqttClient object provides the following functionalities:
-
• Send Data: Use the
doSend()
method to send data to the specified MQTT topic. -
• Subscribe to Data: Use the
doSubscribe()
method to subscribe to the specified MQTT topic and receive updates on the topic content. -
• Ping Test: Use the
doPing()
method to test the ESP-Now connection. -
• Combine Messages: Use the
doMultiple()
method to send multiple messages, including sending data, subscribing to data, and ping testing.
Gateway-Side Functions
The EspNow2MqttGateway object provides the following functionalities:
-
• Receive Device Data: Receives data sent from devices and publishes it to the MQTT server.
-
• Listen to MQTT Subscriptions: Listens to subscriptions on the MQTT server and forwards subscription data to the corresponding devices.
Conclusion
The ESPNow2Mqtt library provides developers with a simple and efficient solution for achieving high-speed MQTT communication on low-power devices. The design philosophy, functional features, and example code can help users easily build IoT applications based on ESP-Now and significantly enhance the battery life of battery-powered devices.
Project Address: https://github.com/eccnil/ESPNow2Mqtt