Commit b65f217f authored by Jerome Courat's avatar Jerome Courat

Updated Mqtt Settings to manage published and subscribed topics in a generic way (std::map).

parent 4630a82f
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
\ No newline at end of file
{
"mqtt": {
"active": true,
"host": "192.168.19.8",
"port": 1883,
"client": {
"id": "iot-capteurX",
"username": "guest",
"password": "guest"
},
"topics": {
"sub": {
"subtopic1": "iot/example/subtopic1",
"subtopic2": "iot/example/subtopic2"
},
"pub": {
"pubtopic1": "iot/example/pubtopic1",
"pubtopic2": "iot/example/pubtopic2"
}
}
}
}
\ No newline at end of file
{
"wifi": {
"name": "iot-capteurX",
"ap": {
"enabled": true,
"ssid": "iot-lcNathan-wifi",
"password": "password"
},
"sta": {
"enabled": true,
"ssid": "FBX_IOT",
"password": "12345678",
"mode": "dhcp",
"network": {
"ip": "192.168.19.101",
"gateway": "192.168.19.1",
"subnet": "255.255.255.0"
}
}
}
}
\ No newline at end of file
......@@ -4,6 +4,11 @@
#include <ArduinoJson.h>
#include <FS.h>
#include <ESP8266WiFi.h>
#include <vector>
#include <map>
typedef std::map<String,String> TopicsMap;
typedef std::map<String,String>::iterator TopicsIterator;
struct MQTTConfiguration
{
......@@ -14,12 +19,8 @@ struct MQTTConfiguration
String client_id;
String client_username;
String client_password;
String temperature_topic;
String humidity_topic;
String pressure_topic;
String voltage_topic;
String command_topic;
String signal_topic;
TopicsMap publications;
TopicsMap subscriptions;
};
extern MQTTConfiguration mqtt_settings;
......
......@@ -39,8 +39,18 @@ void reconnect(PubSubClient &client)
{
Serial.println("connected");
// subscription
bool subSuccess = client.subscribe(mqtt_settings.command_topic.c_str());
// subscriptions
bool subSuccess = true;
TopicsMap subs = mqtt_settings.subscriptions;
TopicsIterator it = subs.begin();
while(it != subs.end())
{
Serial.print("Subscribing to ");
Serial.println(it->second);
subSuccess = subSuccess & client.subscribe(it->second.c_str());
it++;
}
if (subSuccess)
{
Serial.println("-> subscription OK");
......
#include "mqtt_settings.h"
#include "LittleFS.h"
#include <ArduinoJson.h>
String MQTT_CONFIG_FILE = "/mqtt.json";
......@@ -30,7 +31,7 @@ boolean readMQTTSettings()
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
// ArduinoJson 6 migration now...
StaticJsonDocument<384> doc;
StaticJsonDocument<500> doc;
auto error = deserializeJson(doc, buf.get());
......@@ -48,19 +49,33 @@ boolean readMQTTSettings()
mqtt_settings.active = doc["mqtt"]["active"].as<bool>();
mqtt_settings.server = doc["mqtt"]["host"].as<String>();
mqtt_settings.port = doc["mqtt"]["port"].as<int>();
mqtt_settings.command_topic = doc["mqtt"]["topics"]["command"].as<String>();
mqtt_settings.temperature_topic = doc["mqtt"]["topics"]["temperature"].as<String>();
mqtt_settings.humidity_topic = doc["mqtt"]["topics"]["humidity"].as<String>();
mqtt_settings.pressure_topic = doc["mqtt"]["topics"]["pressure"].as<String>();
mqtt_settings.voltage_topic = doc["mqtt"]["topics"]["voltage"].as<String>();
mqtt_settings.signal_topic = doc["mqtt"]["topics"]["signal"].as<String>();
mqtt_settings.client_id = doc["mqtt"]["client"]["id"].as<String>();
mqtt_settings.client_username =
doc["mqtt"]["client"]["username"].as<String>();
doc["mqtt"]["client"]["username"].as<String>();
mqtt_settings.client_password =
doc["mqtt"]["client"]["password"].as<String>();
doc["mqtt"]["client"]["password"].as<String>();
Serial.println("End of MQTT Config");
JsonObject pubSection = doc["mqtt"]["topics"]["publish"].as<JsonObject>();
Serial.println("Publications:");
for (JsonObject::iterator it = pubSection.begin(); it != pubSection.end(); ++it) {
Serial.print(" --> ");
Serial.print(it->key().c_str());
Serial.print(" :: ");
Serial.println(it->value().as<char*>());
mqtt_settings.publications[it->key().c_str()] = it->value().as<String>();
}
JsonObject subSection = doc["mqtt"]["topics"]["subscribe"].as<JsonObject>();
Serial.println("Subscriptions:");
for (JsonObject::iterator it = subSection.begin(); it != subSection.end(); ++it) {
Serial.print(" --> ");
Serial.print(it->key().c_str());
Serial.print(" :: ");
Serial.println(it->value().as<char*>());
mqtt_settings.subscriptions[it->key().c_str()] = it->value().as<String>();
}
LittleFS.end();
return true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment