Replace the MQTT client with a robust async option
This commit is contained in:
@@ -2,14 +2,33 @@
|
|||||||
#define WLAN_H
|
#define WLAN_H
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <PubSubClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
static AsyncMqttClient mqtt_client;
|
||||||
|
static Ticker mqtt_connection_timer, wlan_connection_timer;
|
||||||
|
static WiFiEventHandler connection_handler, disconnection_handler;
|
||||||
|
extern Config *config;
|
||||||
|
|
||||||
|
void initialize_wlan();
|
||||||
|
|
||||||
|
void initialize_mqtt();
|
||||||
|
|
||||||
|
void connect_wlan();
|
||||||
|
|
||||||
|
void connect_mqtt();
|
||||||
|
|
||||||
|
void on_wlan_connection(const WiFiEventStationModeGotIP &event);
|
||||||
|
|
||||||
|
void on_wlan_disconnection(const WiFiEventStationModeDisconnected &event);
|
||||||
|
|
||||||
|
void on_mqtt_connection(bool session);
|
||||||
|
|
||||||
|
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason);
|
||||||
|
|
||||||
void initial_connection(const char *ssid, const char *psk,
|
|
||||||
const char *hostname);
|
|
||||||
void connect_wlan(Config *config);
|
|
||||||
void connect_mqtt(PubSubClient &client, Config *config);
|
|
||||||
void disconnect_mqtt(PubSubClient &client, const char *topic);
|
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size);
|
size_t construct_json(float *data, char *buffer, int buffer_size);
|
||||||
void mqtt_transfer(PubSubClient &client, Config *config, float *data);
|
|
||||||
|
void mqtt_transfer(float *data);
|
||||||
|
|
||||||
#endif /* WLAN_H */
|
#endif /* WLAN_H */
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ framework = arduino
|
|||||||
monitor_filters = esp8266_exception_decoder
|
monitor_filters = esp8266_exception_decoder
|
||||||
lib_deps =
|
lib_deps =
|
||||||
sensirion/Sensirion I2C SCD4x@^1.1.0
|
sensirion/Sensirion I2C SCD4x@^1.1.0
|
||||||
knolleary/PubSubClient@^2.8
|
marvinroger/AsyncMqttClient@^0.9.0
|
||||||
bblanchon/ArduinoJson@^7.4.1
|
bblanchon/ArduinoJson@^7.4.1
|
||||||
|
|
||||||
|
|||||||
25
src/main.cpp
25
src/main.cpp
@@ -1,20 +1,18 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "sensor.h"
|
#include "sensor.h"
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <PubSubClient.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
WiFiClient wifi_client;
|
|
||||||
PubSubClient mqtt_client(wifi_client);
|
|
||||||
SensirionI2cScd4x sensor;
|
SensirionI2cScd4x sensor;
|
||||||
|
|
||||||
int error_code;
|
int error_code;
|
||||||
char error_msg[64];
|
char error_msg[64];
|
||||||
|
|
||||||
const char *config_file_path = "/config.json";
|
const char *config_file_path = "/config.json";
|
||||||
Config *config;
|
Config *config;
|
||||||
|
|
||||||
|
float data[3];
|
||||||
|
unsigned long previous_millis = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
config = (Config *)malloc(sizeof(Config));
|
config = (Config *)malloc(sizeof(Config));
|
||||||
@@ -22,15 +20,18 @@ void setup() {
|
|||||||
if (!load_config_file(config_file_path, config))
|
if (!load_config_file(config_file_path, config))
|
||||||
Serial.println("ERROR: The config file could not be loaded");
|
Serial.println("ERROR: The config file could not be loaded");
|
||||||
|
|
||||||
connect_wlan(config);
|
initialize_wlan();
|
||||||
connect_mqtt(mqtt_client, config);
|
initialize_mqtt();
|
||||||
|
connect_wlan();
|
||||||
initialize_sensor(sensor, error_code, error_msg);
|
initialize_sensor(sensor, error_code, error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
float data[3];
|
unsigned long current_millis = millis();
|
||||||
|
|
||||||
read_values(sensor, data, error_code, error_msg);
|
if (current_millis - previous_millis >= config->sleep_time) {
|
||||||
mqtt_transfer(mqtt_client, config, data);
|
previous_millis = current_millis;
|
||||||
delay(config->sleep_time);
|
read_values(sensor, data, error_code, error_msg);
|
||||||
|
mqtt_transfer(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
src/wlan.cpp
80
src/wlan.cpp
@@ -1,50 +1,50 @@
|
|||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
|
|
||||||
void initial_connection(const char *ssid, const char *psk,
|
void initialize_wlan() {
|
||||||
const char *hostname) {
|
connection_handler = WiFi.onStationModeGotIP(on_wlan_connection);
|
||||||
WiFi.hostname(hostname);
|
disconnection_handler =
|
||||||
WiFi.begin(ssid, psk);
|
WiFi.onStationModeDisconnected(on_wlan_disconnection);
|
||||||
WiFi.persistent(true);
|
|
||||||
WiFi.setAutoConnect(true);
|
|
||||||
WiFi.setAutoReconnect(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_wlan(Config *config) {
|
void initialize_mqtt() {
|
||||||
if (WiFi.SSID() != config->ssid)
|
mqtt_client.onConnect(on_mqtt_connection);
|
||||||
initial_connection(config->ssid, config->psk, config->device_id);
|
mqtt_client.onDisconnect(on_mqtt_disconnection);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
|
||||||
delay(1000);
|
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
|
||||||
Serial.print(".");
|
Serial.println("MQTT initialization complete");
|
||||||
}
|
}
|
||||||
Serial.println("");
|
|
||||||
|
void connect_wlan() { WiFi.begin(config->ssid, config->psk); }
|
||||||
|
|
||||||
|
void connect_mqtt() {
|
||||||
|
Serial.println("Connecting to MQTT");
|
||||||
|
mqtt_client.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_wlan_connection(const WiFiEventStationModeGotIP &event) {
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
|
connect_mqtt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_mqtt(PubSubClient &client, Config *config) {
|
void on_wlan_disconnection(const WiFiEventStationModeDisconnected &event) {
|
||||||
bool connection;
|
Serial.println("WiFi disconnected");
|
||||||
client.setServer(config->mqtt_host, config->mqtt_port);
|
mqtt_connection_timer.detach();
|
||||||
|
wlan_connection_timer.once(2, connect_wlan);
|
||||||
|
}
|
||||||
|
|
||||||
while (!client.connected()) {
|
void on_mqtt_connection(bool session) {
|
||||||
connection = client.connect(config->device_id, config->mqtt_user,
|
Serial.println("MQTT connected");
|
||||||
config->mqtt_password);
|
mqtt_client.subscribe(config->topic, 2);
|
||||||
if (connection) {
|
};
|
||||||
Serial.println("MQTT connected");
|
|
||||||
client.subscribe(config->topic);
|
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
|
||||||
} else {
|
Serial.println("MQTT disconnected");
|
||||||
Serial.println("MQTT failed to connect");
|
|
||||||
Serial.println(client.state());
|
if (WiFi.isConnected()) {
|
||||||
delay(5000);
|
mqtt_connection_timer.once(2, connect_mqtt);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
|
||||||
Serial.println("Disconnecting MQTT");
|
|
||||||
client.unsubscribe(topic);
|
|
||||||
client.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
JsonDocument json;
|
JsonDocument json;
|
||||||
json["co2_concentration"] = data[0];
|
json["co2_concentration"] = data[0];
|
||||||
@@ -54,9 +54,11 @@ size_t construct_json(float *data, char *buffer, int buffer_size) {
|
|||||||
return payload_size;
|
return payload_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
|
void mqtt_transfer(float *data) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
size_t payload_size = construct_json(data, buffer, 100);
|
size_t payload_size = construct_json(data, buffer, 100);
|
||||||
client.publish(config->topic, buffer, payload_size);
|
uint16_t response =
|
||||||
Serial.println("Data transferred successfully");
|
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
||||||
|
if (response)
|
||||||
|
Serial.println("Data transferred successfully");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user