Allocate necessary memory for struct members

This commit is contained in:
coolneng 2023-04-11 07:35:27 +02:00
parent 92eeb0a638
commit 36c83f8dee
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 45 additions and 57 deletions

View File

@ -3,21 +3,24 @@
#include "FS.h"
#include "LittleFS.h"
#include <ArduinoJson.h>
struct Config {
typedef struct {
const char *ssid;
const char *psk;
const char *mqtt_host;
const char *mqtt_user;
const char *mqtt_password;
int mqtt_port;
const char *topic;
const char *device_id;
int mqtt_port;
long sleep_time;
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);

View File

@ -1,22 +1,16 @@
#ifndef WLAN_H
#define WLAN_H
#include "config.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
void initial_connection(const char *ssid, const char *psk);
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
const int sleep_time);
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 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);
void mqtt_transfer(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, float *data);
void mqtt_transfer(PubSubClient &client, Config *config, float *data);
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
#endif /* WLAN_H */

View File

@ -1,7 +1,19 @@
#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())
return false;
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);
if (err)
return false;
config = {
.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"],
};
initialize_config(config, json);
return true;
}

View File

@ -9,7 +9,7 @@ DHT dht(DHTPIN, DHTTYPE);
const int fc28_pin = A0;
const char *config_file_path = "/config.json";
Config config;
Config *config;
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
@ -20,10 +20,10 @@ bool check_valid_value(float value) {
void setup() {
Serial.begin(9600);
dht.begin();
config = (Config *)malloc(sizeof(Config));
if (!load_config_file(config_file_path, config))
Serial.println("ERROR: The config file could not be loaded");
connect_wlan(config.ssid, config.psk, config.connection_attempts,
config.sleep_time);
connect_wlan(config);
}
void loop() {
@ -33,11 +33,8 @@ void loop() {
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
if (check_valid_value(temperature) && check_valid_value(humidity)) {
mqtt_transfer(mqtt_client, config.mqtt_host, config.mqtt_port,
config.mqtt_user, config.mqtt_password, config.device_id,
config.topic, data);
mqtt_transfer(mqtt_client, config, data);
}
disconnect_mqtt(mqtt_client, config.topic);
enter_deep_sleep(false, config.sleep_time);
delay(60000);
disconnect_mqtt(mqtt_client, config->topic);
enter_deep_sleep(false, config->sleep_time);
}

View File

@ -8,14 +8,13 @@ void initial_connection(const char *ssid, const char *psk) {
WiFi.setAutoReconnect(true);
}
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
const int sleep_time) {
if (WiFi.SSID() != ssid)
initial_connection(ssid, psk);
void connect_wlan(Config *config) {
if (WiFi.SSID() != config->ssid)
initial_connection(config->ssid, config->psk);
int retries = 0;
while (WiFi.status() != WL_CONNECTED) {
if (retries == max_retries)
enter_deep_sleep(true, sleep_time);
if (retries == config->connection_attempts)
enter_deep_sleep(true, config->sleep_time);
retries++;
delay(1000);
Serial.print(".");
@ -23,15 +22,13 @@ void connect_wlan(const char *ssid, const char *psk, const int max_retries,
Serial.println("WiFi connected");
}
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 connect_mqtt(PubSubClient &client, Config *config) {
if (!client.connected())
client.setServer(mqtt_host, mqtt_port);
if (client.connect(device_id, mqtt_user, mqtt_password)) {
client.setServer(config->mqtt_host, config->mqtt_port);
if (client.connect(config->device_id, config->mqtt_user,
config->mqtt_password)) {
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;
}
void mqtt_transfer(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, float *data) {
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
char buffer[100];
connect_mqtt(client, mqtt_host, mqtt_port, mqtt_user, mqtt_password,
device_id, topic);
connect_mqtt(client, config);
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");
}