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;
|
||||
|
||||
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
|
||||
void loadconfig(const char* filename) {
|
||||
@ -30,20 +47,28 @@ void loadconfig(const char* filename) {
|
||||
StaticJsonDocument<512> jsonBuffer;
|
||||
|
||||
DeserializationError error = deserializeJson(jsonBuffer, file);
|
||||
|
||||
file.close();
|
||||
|
||||
if (error) {
|
||||
Serial.print(F("Unable to deserialize JSON config file."));
|
||||
Serial.println(error.f_str());
|
||||
} else {
|
||||
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"] | "");
|
||||
return;
|
||||
}
|
||||
|
||||
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