In IoT applications, the ESP32 has become the preferred choice for many developers due to its low power consumption and rich features. However, using WiFi to connect to MQTT in low-power scenarios brings additional power burdens, which is a significant challenge for battery-powered devices. The ESPNow2Mqtt library was born to address this issue, utilizing the ESP-Now protocol and MQTT protocol to build an efficient communication architecture that allows ESP32 devices to interact with the MQTT server quickly and with low power consumption without needing to connect to WiFi.
Advantages of ESPNow2Mqtt
-
• Low Power Consumption: ESPNow2Mqtt utilizes the ESP-Now protocol for communication, avoiding the power consumption of WiFi connections and significantly reducing device power consumption.
-
• Quick Response: The communication efficiency of ESPNow2Mqtt is high, capable of completing data transmission within 100 milliseconds for quick responses.
-
• Flexible Expansion: ESPNow2Mqtt supports multiple types of messages, including ping, send, subscribe, and multiple, meeting various application scenarios.
-
• Safe and Reliable: ESPNow2Mqtt uses the ChaCha encryption algorithm to ensure the security and reliability of data transmission.
Architecture of ESPNow2Mqtt
The architecture of ESPNow2Mqtt mainly consists of the following two parts:
1. ESPNow2MqttClient (Client):
-
• Used for ESP32 devices to send data to the MQTT server and retrieve data from the MQTT server.
-
• Supports sending four types of messages: ping, send, subscribe, and multiple.
2. ESPNow2MqttGateway (Gateway):
-
• Responsible for forwarding data sent by ESP32 devices via ESP-Now to the MQTT server.
-
• Also responsible for forwarding subscription messages from the MQTT server to ESP32 devices.
3. Simplified Message Protocol:
-
• A concise message protocol defined using the Nanopb library for communication between ESP32 devices and the gateway.
4. Custom Encryption:
-
• Since ESP-Now can connect to a maximum of 6 nodes when using encryption, ESPNow2Mqtt uses the ChaCha encryption algorithm for data encryption, which is not limited by the number of nodes.
Using ESPNow2Mqtt
1. Install the ESPNow2Mqtt Library:
-
• In the PlatformIO project, add the following content to the
platformio.ini
file:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
eccnil/ESPNow2Mqtt
2. Code Example:
-
• ESP32 Device (Client):
#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);
}
-
• Gateway:
#include <Arduino.h>
#include <EspNow2MqttGateway.hpp>
// ... (other code)
3. Using MQTT Server:
-
• Ensure your MQTT server is running and configured with the appropriate topics.
-
• The ESPNow2Mqtt library will automatically send data to the specified topic.
Application Scenarios of ESPNow2Mqtt
-
• Low Power IoT Devices: Such as battery-powered sensors, actuators, etc.
-
• Wireless Control: For example, using ESP32 devices to control other devices, such as lights, motors, etc.
-
• Data Collection: For example, using ESP32 devices to collect sensor data and send it to the cloud.
Conclusion
The ESPNow2Mqtt library provides developers with a solution for building low-power, quick-response ESP32 IoT applications. It simplifies communication between ESP32 devices and the MQTT server, enhances development efficiency, and offers greater possibilities for innovation in IoT applications.
Project Address: https://github.com/eccnil/ESPNow2Mqtt