Boilerplate code to manage WiFi and MQTT for Espressif ESP32 and ESP8266 devices using the Arduino framework.
Based on espMqttClient
{
"SSID": "network-ssid",
"PSK": "network-psk",
"IPAddress": [0,0,0,0],
"hostname" : "mqtt.domain.tld",
"port": 1883,
"devicename": "testdevice"
}
- You don't have to specify both an IP address and hostname of the MQTT broker. The Broker's hostname takes precedence over IP.
port
anddevicename
are optional.
Use pio run -t uploadfs
to upload when using Platformio.
Below is a simple version of how to use espMqttManager. Just add the header, call setup, start and loop.
The methods are not wrapped in a class, only in a namespace. The MQTT client is available using espMqttManager::mqttClient
.
#include <Arduino.h>
#include <espMqttManager.h>
void setup() {
delay(10000);
Serial.begin(115200);
espMqttManager::setup();
espMqttManager::start();
}
void loop() {
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 10000) {
lastMillis = millis();
uint8_t signalQuality = espMqttManagerHelpers::signalQuality();
char sqStr[5];
snprintf(sqStr, sizeof(sqStr), "%d%", signalQuality);
espMqttManager::mqttClient.publish("test/topic", 0, false, sqStr);
}
espMqttManager::loop();
}
Setup espMqttManager. Call from Arduino setup.
void espMqttManager::start();
Start espMqttManager. Call from Arduino setup. After calling this, the device will try to connect to WiFi.
Make sure to set any custom properties of the MQTT client before calling espMqttManager::start()
.
Worker task of the manager. Call from Arduino loop().
See Event hooks, onSetupSession
Disconnect from MQTT. WiFi will not disconnect. espMqttManager will try to cleanly disconnect from MQTT but forcibly after 10 seconds.
When setting clearSession
to true
, espMqttManager will clear the session from the broker and disconnect.
onMqttDisconnected will be called when disconnected or onReset when disconnected when clearSession
is set to true
.
Note that espMqttManager will not reconnect automatically after calling disconnect.
There are a few functions -callbacks- defined but only weakly linked. You can override these in your code.
onSetupSession is called when a connection to the MQTT broker has been established but no session was found (clean session = true).
You can use this function to set up the session eg. make subscriptions etc. When done, make sure to call espMqttManager::sessionReady to complete the setup.
Called when the device is fully connected to MQTT and the session has been setup
Called whenever the device disconnects from MQTT. espMqttManager will automatically reconnect, you don't have to do this.
onReset is called when the device is fully disconnected from MQTT and the session has been deleted. This is useful after updating the firmware.
Returns the WiFi signal quality as a percentage.
void espMqttManagerHelpers::handleUpdate(const uint8_t* payload, size_t length, size_t index, size_t total);
Can be used to update the firmware of the device. The arguments match the ones from the onMessage callback from espMqttClient.
The updated
boolean is a flag that is set when the update has been (successfully) completed.
You can disconnect with clearSession
set to true
after the update.
This library is released under the MIT Licence. A copy is included in the repo.