Allocate necessary memory for struct members
This commit is contained in:
parent
92eeb0a638
commit
36c83f8dee
|
@ -3,21 +3,24 @@
|
||||||
|
|
||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#include "LittleFS.h"
|
#include "LittleFS.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
struct Config {
|
typedef struct {
|
||||||
const char *ssid;
|
const char *ssid;
|
||||||
const char *psk;
|
const char *psk;
|
||||||
const char *mqtt_host;
|
const char *mqtt_host;
|
||||||
const char *mqtt_user;
|
const char *mqtt_user;
|
||||||
const char *mqtt_password;
|
const char *mqtt_password;
|
||||||
int mqtt_port;
|
|
||||||
const char *topic;
|
const char *topic;
|
||||||
const char *device_id;
|
const char *device_id;
|
||||||
|
int mqtt_port;
|
||||||
long sleep_time;
|
long sleep_time;
|
||||||
int connection_attempts;
|
int connection_attempts;
|
||||||
};
|
} Config;
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, struct Config &config);
|
void initialize_config(Config *config, StaticJsonDocument<512> json);
|
||||||
|
|
||||||
|
bool load_config_file(const char *file_path, Config *config);
|
||||||
|
|
||||||
long minutes_to_microseconds(int minutes);
|
long minutes_to_microseconds(int minutes);
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,16 @@
|
||||||
#ifndef WLAN_H
|
#ifndef WLAN_H
|
||||||
#define WLAN_H
|
#define WLAN_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
void initial_connection(const char *ssid, const char *psk);
|
void initial_connection(const char *ssid, const char *psk);
|
||||||
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
|
void connect_wlan(Config *config);
|
||||||
const int sleep_time);
|
void connect_mqtt(PubSubClient &client, Config *config);
|
||||||
void connect_mqtt(PubSubClient &client, const char *mqtt_host,
|
|
||||||
const int mqtt_port, const char *mqtt_user,
|
|
||||||
const char *mqtt_password, const char *device_id,
|
|
||||||
const char *topic);
|
|
||||||
void disconnect_mqtt(PubSubClient &client, const char *topic);
|
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, const char *mqtt_host,
|
void mqtt_transfer(PubSubClient &client, Config *config, float *data);
|
||||||
const int mqtt_port, const char *mqtt_user,
|
|
||||||
const char *mqtt_password, const char *device_id,
|
|
||||||
const char *topic, float *data);
|
|
||||||
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
|
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
|
||||||
|
|
||||||
#endif /* WLAN_H */
|
#endif /* WLAN_H */
|
||||||
|
|
|
@ -1,7 +1,19 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, struct Config &config) {
|
void initialize_config(Config *config, StaticJsonDocument<512> json) {
|
||||||
|
config->ssid = strdup(json["ssid"]);
|
||||||
|
config->psk = strdup(json["psk"]);
|
||||||
|
config->mqtt_host = strdup(json["mqtt_host"]);
|
||||||
|
config->mqtt_user = strdup(json["mqtt_user"]);
|
||||||
|
config->mqtt_password = strdup(json["mqtt_password"]);
|
||||||
|
config->topic = strdup(json["mqtt_topic"]);
|
||||||
|
config->device_id = strdup(json["device_id"]);
|
||||||
|
config->mqtt_port = json["mqtt_port"];
|
||||||
|
config->sleep_time = minutes_to_microseconds(json["sleep_time"]);
|
||||||
|
config->connection_attempts = json["connection_attempts"];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_config_file(const char *file_path, Config *config) {
|
||||||
if (!LittleFS.begin())
|
if (!LittleFS.begin())
|
||||||
return false;
|
return false;
|
||||||
File config_file = LittleFS.open(file_path, "r");
|
File config_file = LittleFS.open(file_path, "r");
|
||||||
|
@ -11,18 +23,7 @@ bool load_config_file(const char *file_path, struct Config &config) {
|
||||||
DeserializationError err = deserializeJson(json, config_file);
|
DeserializationError err = deserializeJson(json, config_file);
|
||||||
if (err)
|
if (err)
|
||||||
return false;
|
return false;
|
||||||
config = {
|
initialize_config(config, json);
|
||||||
.ssid = json["ssid"],
|
|
||||||
.psk = json["psk"],
|
|
||||||
.mqtt_host = json["mqtt_host"],
|
|
||||||
.mqtt_user = json["mqtt_user"],
|
|
||||||
.mqtt_password = json["mqtt_password"],
|
|
||||||
.mqtt_port = json["mqtt_port"],
|
|
||||||
.topic = json["mqtt_topic"],
|
|
||||||
.device_id = json["device_id"],
|
|
||||||
.sleep_time = minutes_to_microseconds(json["sleep_time"]),
|
|
||||||
.connection_attempts = json["connection_attempts"],
|
|
||||||
};
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
src/main.cpp
15
src/main.cpp
|
@ -9,7 +9,7 @@ DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
const int fc28_pin = A0;
|
const int fc28_pin = A0;
|
||||||
const char *config_file_path = "/config.json";
|
const char *config_file_path = "/config.json";
|
||||||
Config config;
|
Config *config;
|
||||||
WiFiClient wifi_client;
|
WiFiClient wifi_client;
|
||||||
PubSubClient mqtt_client(wifi_client);
|
PubSubClient mqtt_client(wifi_client);
|
||||||
|
|
||||||
|
@ -20,10 +20,10 @@ bool check_valid_value(float value) {
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
dht.begin();
|
dht.begin();
|
||||||
|
config = (Config *)malloc(sizeof(Config));
|
||||||
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.ssid, config.psk, config.connection_attempts,
|
connect_wlan(config);
|
||||||
config.sleep_time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -33,11 +33,8 @@ void loop() {
|
||||||
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
||||||
float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
|
float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
|
||||||
if (check_valid_value(temperature) && check_valid_value(humidity)) {
|
if (check_valid_value(temperature) && check_valid_value(humidity)) {
|
||||||
mqtt_transfer(mqtt_client, config.mqtt_host, config.mqtt_port,
|
mqtt_transfer(mqtt_client, config, data);
|
||||||
config.mqtt_user, config.mqtt_password, config.device_id,
|
|
||||||
config.topic, data);
|
|
||||||
}
|
}
|
||||||
disconnect_mqtt(mqtt_client, config.topic);
|
disconnect_mqtt(mqtt_client, config->topic);
|
||||||
enter_deep_sleep(false, config.sleep_time);
|
enter_deep_sleep(false, config->sleep_time);
|
||||||
delay(60000);
|
|
||||||
}
|
}
|
||||||
|
|
33
src/wlan.cpp
33
src/wlan.cpp
|
@ -8,14 +8,13 @@ void initial_connection(const char *ssid, const char *psk) {
|
||||||
WiFi.setAutoReconnect(true);
|
WiFi.setAutoReconnect(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
|
void connect_wlan(Config *config) {
|
||||||
const int sleep_time) {
|
if (WiFi.SSID() != config->ssid)
|
||||||
if (WiFi.SSID() != ssid)
|
initial_connection(config->ssid, config->psk);
|
||||||
initial_connection(ssid, psk);
|
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
if (retries == max_retries)
|
if (retries == config->connection_attempts)
|
||||||
enter_deep_sleep(true, sleep_time);
|
enter_deep_sleep(true, config->sleep_time);
|
||||||
retries++;
|
retries++;
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
@ -23,15 +22,13 @@ void connect_wlan(const char *ssid, const char *psk, const int max_retries,
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_mqtt(PubSubClient &client, const char *mqtt_host,
|
void connect_mqtt(PubSubClient &client, Config *config) {
|
||||||
const int mqtt_port, const char *mqtt_user,
|
|
||||||
const char *mqtt_password, const char *device_id,
|
|
||||||
const char *topic) {
|
|
||||||
if (!client.connected())
|
if (!client.connected())
|
||||||
client.setServer(mqtt_host, mqtt_port);
|
client.setServer(config->mqtt_host, config->mqtt_port);
|
||||||
if (client.connect(device_id, mqtt_user, mqtt_password)) {
|
if (client.connect(config->device_id, config->mqtt_user,
|
||||||
|
config->mqtt_password)) {
|
||||||
Serial.println("MQTT connected");
|
Serial.println("MQTT connected");
|
||||||
client.subscribe(topic);
|
client.subscribe(config->topic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,15 +47,11 @@ size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
return payload_size;
|
return payload_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_transfer(PubSubClient &client, const char *mqtt_host,
|
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
|
||||||
const int mqtt_port, const char *mqtt_user,
|
|
||||||
const char *mqtt_password, const char *device_id,
|
|
||||||
const char *topic, float *data) {
|
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
connect_mqtt(client, mqtt_host, mqtt_port, mqtt_user, mqtt_password,
|
connect_mqtt(client, config);
|
||||||
device_id, topic);
|
|
||||||
size_t payload_size = construct_json(data, buffer, 100);
|
size_t payload_size = construct_json(data, buffer, 100);
|
||||||
client.publish(topic, buffer, payload_size);
|
client.publish(config->topic, buffer, payload_size);
|
||||||
Serial.println("Data transferred successfully");
|
Serial.println("Data transferred successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue