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