Configure MQTT from SD card
This commit is contained in:
parent
1988b2957b
commit
5849638f1c
43
src/main.cpp
43
src/main.cpp
@ -21,6 +21,23 @@ struct wifi_config_struct
|
|||||||
|
|
||||||
wifi_config_struct wifi_config;
|
wifi_config_struct wifi_config;
|
||||||
|
|
||||||
|
struct mqtt_topic_struct
|
||||||
|
{
|
||||||
|
char title[32];
|
||||||
|
char topic[64];
|
||||||
|
char unit[16];
|
||||||
|
};
|
||||||
|
|
||||||
|
mqtt_topic_struct mqtt_topics;
|
||||||
|
|
||||||
|
struct mqtt_config_struct
|
||||||
|
{
|
||||||
|
char server[24];
|
||||||
|
unsigned int port;
|
||||||
|
char clientID[16];
|
||||||
|
};
|
||||||
|
|
||||||
|
mqtt_config_struct mqtt_config;
|
||||||
|
|
||||||
// Load configuration file from SD card
|
// Load configuration file from SD card
|
||||||
void loadconfig(const char* filename) {
|
void loadconfig(const char* filename) {
|
||||||
@ -30,20 +47,28 @@ void loadconfig(const char* filename) {
|
|||||||
StaticJsonDocument<512> jsonBuffer;
|
StaticJsonDocument<512> jsonBuffer;
|
||||||
|
|
||||||
DeserializationError error = deserializeJson(jsonBuffer, file);
|
DeserializationError error = deserializeJson(jsonBuffer, file);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print(F("Unable to deserialize JSON config file."));
|
Serial.print(F("Unable to deserialize JSON config file."));
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
} else {
|
return;
|
||||||
strcpy(wifi_config.ssid, jsonBuffer["wifi"]["ssid"] | "defualtSSID");
|
|
||||||
strcpy(wifi_config.password, jsonBuffer["wifi"]["password"] | "");
|
|
||||||
strcpy(wifi_config.ip, jsonBuffer["wifi"]["ip"] | "");
|
|
||||||
strcpy(wifi_config.mask, jsonBuffer["wifi"]["mask"] | "");
|
|
||||||
strcpy(wifi_config.gateway, jsonBuffer["wifi"]["gateway"] | "");
|
|
||||||
strcpy(wifi_config.dns1, jsonBuffer["wifi"]["dns1"] | "");
|
|
||||||
strcpy(wifi_config.dns2, jsonBuffer["wifi"]["dns2"] | "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
JsonObject wifi = jsonBuffer["wifi"];
|
||||||
|
strcpy(wifi_config.ssid, wifi["ssid"] | "defualtSSID");
|
||||||
|
strcpy(wifi_config.password, wifi["password"] | "");
|
||||||
|
strcpy(wifi_config.ip, wifi["ip"] | "");
|
||||||
|
strcpy(wifi_config.mask, wifi["mask"] | "");
|
||||||
|
strcpy(wifi_config.gateway, wifi["gateway"] | "");
|
||||||
|
strcpy(wifi_config.dns1, wifi["dns1"] | "");
|
||||||
|
strcpy(wifi_config.dns2, wifi["dns2"] | "");
|
||||||
|
|
||||||
|
JsonObject mqtt = jsonBuffer["mqtt"];
|
||||||
|
strcpy(mqtt_config.server, mqtt["server"] | "");
|
||||||
|
strcpy(mqtt_config.clientID, mqtt["clientID"] | "");
|
||||||
|
mqtt_config.port = mqtt["port"];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user