Load credentials and settings from config file
This commit is contained in:
parent
37e249a503
commit
92eeb0a638
|
@ -1,2 +1,3 @@
|
||||||
.pio
|
.pio
|
||||||
src/credentials.h
|
src/credentials.h
|
||||||
|
data/config.json
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"ssid": "",
|
||||||
|
"psk": "",
|
||||||
|
"mqtt_host": "",
|
||||||
|
"mqtt_user": "",
|
||||||
|
"mqtt_password": "",
|
||||||
|
"mqtt_port": 1883,
|
||||||
|
"mqtt_topic": "",
|
||||||
|
"device_id": "",
|
||||||
|
"sleep_time": 30,
|
||||||
|
"connection_attempts": 60
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef CONFIG_H_
|
||||||
|
#define CONFIG_H_
|
||||||
|
|
||||||
|
#include "FS.h"
|
||||||
|
#include "LittleFS.h"
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
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;
|
||||||
|
long sleep_time;
|
||||||
|
int connection_attempts;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool load_config_file(const char *file_path, struct Config &config);
|
||||||
|
|
||||||
|
long minutes_to_microseconds(int minutes);
|
||||||
|
|
||||||
|
#endif // CONFIG_H_
|
|
@ -4,17 +4,19 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
const int SLEEP_TIME = 900000000;
|
void initial_connection(const char *ssid, const char *psk);
|
||||||
const int WIFI_TIMEOUT = 10000;
|
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
|
||||||
|
const int sleep_time);
|
||||||
void initial_connection();
|
void connect_mqtt(PubSubClient &client, const char *mqtt_host,
|
||||||
void connect_wlan(const int max_retries);
|
const int mqtt_port, const char *mqtt_user,
|
||||||
void connect_mqtt(PubSubClient &client, const char *device_id,
|
const char *mqtt_password, const char *device_id,
|
||||||
const char *topic);
|
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 *device_id,
|
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);
|
const char *topic, float *data);
|
||||||
void enter_deep_sleep(bool wifi_timeout);
|
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
|
||||||
|
|
||||||
#endif /* WLAN_H */
|
#endif /* WLAN_H */
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
[env:d1_mini]
|
[env:d1_mini]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = d1_mini
|
board = d1_mini
|
||||||
|
board_build.filesystem = littlefs
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
adafruit/DHT sensor library@^1.4.4
|
adafruit/DHT sensor library@^1.4.4
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include "config.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
bool load_config_file(const char *file_path, struct Config &config) {
|
||||||
|
if (!LittleFS.begin())
|
||||||
|
return false;
|
||||||
|
File config_file = LittleFS.open(file_path, "r");
|
||||||
|
if (!config_file)
|
||||||
|
return false;
|
||||||
|
StaticJsonDocument<512> json;
|
||||||
|
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"],
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }
|
20
src/main.cpp
20
src/main.cpp
|
@ -1,3 +1,4 @@
|
||||||
|
#include "config.h"
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
|
@ -7,9 +8,8 @@
|
||||||
DHT dht(DHTPIN, DHTTYPE);
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
const int fc28_pin = A0;
|
const int fc28_pin = A0;
|
||||||
const int max_connection_attempts = 60;
|
const char *config_file_path = "/config.json";
|
||||||
const char *topic = "homeostasis/room";
|
Config config;
|
||||||
const char *device_id = "homeostasis";
|
|
||||||
WiFiClient wifi_client;
|
WiFiClient wifi_client;
|
||||||
PubSubClient mqtt_client(wifi_client);
|
PubSubClient mqtt_client(wifi_client);
|
||||||
|
|
||||||
|
@ -20,7 +20,10 @@ bool check_valid_value(float value) {
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
dht.begin();
|
dht.begin();
|
||||||
connect_wlan(max_connection_attempts);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -30,8 +33,11 @@ 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, device_id, topic, data);
|
mqtt_transfer(mqtt_client, config.mqtt_host, config.mqtt_port,
|
||||||
|
config.mqtt_user, config.mqtt_password, config.device_id,
|
||||||
|
config.topic, data);
|
||||||
}
|
}
|
||||||
disconnect_mqtt(mqtt_client, topic);
|
disconnect_mqtt(mqtt_client, config.topic);
|
||||||
enter_deep_sleep(false);
|
enter_deep_sleep(false, config.sleep_time);
|
||||||
|
delay(60000);
|
||||||
}
|
}
|
||||||
|
|
33
src/wlan.cpp
33
src/wlan.cpp
|
@ -1,21 +1,21 @@
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include "credentials.h"
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
void initial_connection() {
|
void initial_connection(const char *ssid, const char *psk) {
|
||||||
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(const int max_retries) {
|
void connect_wlan(const char *ssid, const char *psk, const int max_retries,
|
||||||
if (WiFi.SSID() != SSID)
|
const int sleep_time) {
|
||||||
initial_connection();
|
if (WiFi.SSID() != ssid)
|
||||||
|
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 == max_retries)
|
||||||
enter_deep_sleep(true);
|
enter_deep_sleep(true, sleep_time);
|
||||||
retries++;
|
retries++;
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
@ -23,11 +23,13 @@ void connect_wlan(const int max_retries) {
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_mqtt(PubSubClient &client, const char *device_id,
|
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) {
|
const char *topic) {
|
||||||
if (!client.connected())
|
if (!client.connected())
|
||||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
client.setServer(mqtt_host, mqtt_port);
|
||||||
if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) {
|
if (client.connect(device_id, mqtt_user, mqtt_password)) {
|
||||||
Serial.println("MQTT connected");
|
Serial.println("MQTT connected");
|
||||||
client.subscribe(topic);
|
client.subscribe(topic);
|
||||||
}
|
}
|
||||||
|
@ -48,18 +50,21 @@ size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
return payload_size;
|
return payload_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_transfer(PubSubClient &client, const char *device_id,
|
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) {
|
const char *topic, float *data) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
connect_mqtt(client, device_id, topic);
|
connect_mqtt(client, mqtt_host, mqtt_port, mqtt_user, mqtt_password,
|
||||||
|
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(topic, buffer, payload_size);
|
||||||
Serial.println("Data transferred successfully");
|
Serial.println("Data transferred successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
void enter_deep_sleep(bool wifi_timeout) {
|
void enter_deep_sleep(bool wifi_timeout, int sleep_time) {
|
||||||
Serial.println("Entering deep sleep");
|
Serial.println("Entering deep sleep");
|
||||||
if (wifi_timeout)
|
if (wifi_timeout)
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
|
ESP.deepSleep(sleep_time, WAKE_RF_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue