diff --git a/.gitignore b/.gitignore index 6c9187e..937a21a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .pio src/credentials.h +data/config.json diff --git a/data/example.json b/data/example.json new file mode 100644 index 0000000..9ad222c --- /dev/null +++ b/data/example.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 +} diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..fd9d693 --- /dev/null +++ b/include/config.h @@ -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_ diff --git a/include/wlan.h b/include/wlan.h index 37797d1..76d996a 100644 --- a/include/wlan.h +++ b/include/wlan.h @@ -4,17 +4,19 @@ #include #include -const int SLEEP_TIME = 900000000; -const int WIFI_TIMEOUT = 10000; - -void initial_connection(); -void connect_wlan(const int max_retries); -void connect_mqtt(PubSubClient &client, const char *device_id, +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 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 *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); -void enter_deep_sleep(bool wifi_timeout); +void enter_deep_sleep(bool wifi_timeout, int sleep_time); #endif /* WLAN_H */ diff --git a/platformio.ini b/platformio.ini index 9522f4d..5f9dd80 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,6 +11,7 @@ [env:d1_mini] platform = espressif8266 board = d1_mini +board_build.filesystem = littlefs framework = arduino lib_deps = adafruit/DHT sensor library@^1.4.4 diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..61d4677 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,29 @@ +#include "config.h" +#include + +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); } diff --git a/src/main.cpp b/src/main.cpp index 8e1e2ef..316fc9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "config.h" #include "wlan.h" #include #include @@ -7,9 +8,8 @@ DHT dht(DHTPIN, DHTTYPE); const int fc28_pin = A0; -const int max_connection_attempts = 60; -const char *topic = "homeostasis/room"; -const char *device_id = "homeostasis"; +const char *config_file_path = "/config.json"; +Config config; WiFiClient wifi_client; PubSubClient mqtt_client(wifi_client); @@ -20,7 +20,10 @@ bool check_valid_value(float value) { void setup() { Serial.begin(9600); 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() { @@ -30,8 +33,11 @@ void loop() { int soil_percentage = map(analog_val, 0, 1023, 0, 100); float data[3] = {temperature, humidity, static_cast(soil_percentage)}; 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); - enter_deep_sleep(false); + disconnect_mqtt(mqtt_client, config.topic); + enter_deep_sleep(false, config.sleep_time); + delay(60000); } diff --git a/src/wlan.cpp b/src/wlan.cpp index 7ed1d09..20aa94a 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -1,21 +1,21 @@ #include "wlan.h" -#include "credentials.h" #include -void initial_connection() { - WiFi.begin(SSID, PSK); +void initial_connection(const char *ssid, const char *psk) { + WiFi.begin(ssid, psk); WiFi.persistent(true); WiFi.setAutoConnect(true); WiFi.setAutoReconnect(true); } -void connect_wlan(const int max_retries) { - if (WiFi.SSID() != SSID) - initial_connection(); +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); int retries = 0; while (WiFi.status() != WL_CONNECTED) { if (retries == max_retries) - enter_deep_sleep(true); + enter_deep_sleep(true, sleep_time); retries++; delay(1000); Serial.print("."); @@ -23,11 +23,13 @@ void connect_wlan(const int max_retries) { 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) { if (!client.connected()) - client.setServer(MQTT_HOST, MQTT_PORT); - if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) { + client.setServer(mqtt_host, mqtt_port); + if (client.connect(device_id, mqtt_user, mqtt_password)) { Serial.println("MQTT connected"); client.subscribe(topic); } @@ -48,18 +50,21 @@ size_t construct_json(float *data, char *buffer, int buffer_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) { 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); client.publish(topic, buffer, payload_size); 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"); if (wifi_timeout) WiFi.disconnect(); - ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT); + ESP.deepSleep(sleep_time, WAKE_RF_DEFAULT); }