Move includes to cpp files
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
#ifndef CONFIG_H_
|
||||
#define CONFIG_H_
|
||||
|
||||
#include "FS.h"
|
||||
#include "LittleFS.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
typedef struct {
|
||||
const char *ssid;
|
||||
const char *psk;
|
||||
const char *mqtt_host;
|
||||
const char *mqtt_user;
|
||||
const char *mqtt_password;
|
||||
const char *topic;
|
||||
const char *device_id;
|
||||
int mqtt_port;
|
||||
long sleep_time;
|
||||
int connection_attempts;
|
||||
const char *ssid;
|
||||
const char *psk;
|
||||
const char *mqtt_host;
|
||||
const char *mqtt_user;
|
||||
const char *mqtt_password;
|
||||
const char *topic;
|
||||
const char *device_id;
|
||||
int mqtt_port;
|
||||
long sleep_time;
|
||||
int connection_attempts;
|
||||
} Config;
|
||||
|
||||
void initialize_config(Config *config, JsonDocument json);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define WLAN_H
|
||||
|
||||
#include "config.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
void initial_connection(const char *ssid, const char *psk,
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
#include "config.h"
|
||||
#include "LittleFS.h"
|
||||
|
||||
void initialize_config(Config *config, JsonDocument 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"];
|
||||
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");
|
||||
if (!config_file)
|
||||
return false;
|
||||
JsonDocument json;
|
||||
DeserializationError err = deserializeJson(json, config_file);
|
||||
if (err)
|
||||
return false;
|
||||
initialize_config(config, json);
|
||||
return true;
|
||||
if (!LittleFS.begin())
|
||||
return false;
|
||||
File config_file = LittleFS.open(file_path, "r");
|
||||
if (!config_file)
|
||||
return false;
|
||||
JsonDocument json;
|
||||
DeserializationError err = deserializeJson(json, config_file);
|
||||
if (err)
|
||||
return false;
|
||||
initialize_config(config, json);
|
||||
return true;
|
||||
}
|
||||
|
||||
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "config.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <SensirionI2cScd4x.h>
|
||||
#include <Wire.h>
|
||||
|
||||
|
||||
66
src/wlan.cpp
66
src/wlan.cpp
@@ -1,53 +1,53 @@
|
||||
#include "wlan.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
void initial_connection(const char *ssid, const char *psk,
|
||||
const char *hostname) {
|
||||
WiFi.hostname(hostname);
|
||||
WiFi.begin(ssid, psk);
|
||||
WiFi.persistent(true);
|
||||
WiFi.setAutoConnect(true);
|
||||
WiFi.setAutoReconnect(true);
|
||||
WiFi.hostname(hostname);
|
||||
WiFi.begin(ssid, psk);
|
||||
WiFi.persistent(true);
|
||||
WiFi.setAutoConnect(true);
|
||||
WiFi.setAutoReconnect(true);
|
||||
}
|
||||
|
||||
void connect_wlan(Config *config) {
|
||||
if (WiFi.SSID() != config->ssid)
|
||||
initial_connection(config->ssid, config->psk, config->device_id);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("WiFi connected");
|
||||
if (WiFi.SSID() != config->ssid)
|
||||
initial_connection(config->ssid, config->psk, config->device_id);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("WiFi connected");
|
||||
}
|
||||
|
||||
void connect_mqtt(PubSubClient &client, Config *config) {
|
||||
if (!client.connected())
|
||||
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(config->topic);
|
||||
}
|
||||
if (!client.connected())
|
||||
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(config->topic);
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||
Serial.println("Disconnecting MQTT");
|
||||
client.unsubscribe(topic);
|
||||
client.disconnect();
|
||||
Serial.println("Disconnecting MQTT");
|
||||
client.unsubscribe(topic);
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||
JsonDocument json;
|
||||
json["temperature"] = data[0];
|
||||
json["humidity"] = data[1];
|
||||
size_t payload_size = serializeJson(json, buffer, buffer_size);
|
||||
return payload_size;
|
||||
JsonDocument json;
|
||||
json["temperature"] = data[0];
|
||||
json["humidity"] = data[1];
|
||||
size_t payload_size = serializeJson(json, buffer, buffer_size);
|
||||
return payload_size;
|
||||
}
|
||||
|
||||
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
|
||||
char buffer[100];
|
||||
connect_mqtt(client, config);
|
||||
size_t payload_size = construct_json(data, buffer, 100);
|
||||
client.publish(config->topic, buffer, payload_size);
|
||||
Serial.println("Data transferred successfully");
|
||||
char buffer[100];
|
||||
connect_mqtt(client, config);
|
||||
size_t payload_size = construct_json(data, buffer, 100);
|
||||
client.publish(config->topic, buffer, payload_size);
|
||||
Serial.println("Data transferred successfully");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user