Initial commit
This commit is contained in:
30
src/config.cpp
Normal file
30
src/config.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "config.h"
|
||||
|
||||
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");
|
||||
if (!config_file)
|
||||
return false;
|
||||
StaticJsonDocument<512> 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); }
|
||||
41
src/main.cpp
Normal file
41
src/main.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "config.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <DHT.h>
|
||||
|
||||
#define DHTTYPE DHT22
|
||||
#define DHTPIN 4
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
const int fc28_pin = A0;
|
||||
const char *config_file_path = "/config.json";
|
||||
Config *config;
|
||||
WiFiClient wifi_client;
|
||||
PubSubClient mqtt_client(wifi_client);
|
||||
|
||||
bool check_valid_value(float value) {
|
||||
return (!isnan(value) && value >= 0 && value <= 100);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float temperature = dht.readTemperature();
|
||||
float humidity = dht.readHumidity();
|
||||
int analog_val = analogRead(fc28_pin);
|
||||
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, data);
|
||||
}
|
||||
disconnect_mqtt(mqtt_client, config->topic);
|
||||
free(config);
|
||||
enter_deep_sleep(false, config->sleep_time);
|
||||
}
|
||||
63
src/wlan.cpp
Normal file
63
src/wlan.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "wlan.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
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(Config *config) {
|
||||
if (WiFi.SSID() != config->ssid)
|
||||
initial_connection(config->ssid, config->psk);
|
||||
int retries = 0;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
if (retries == config->connection_attempts)
|
||||
enter_deep_sleep(true, config->sleep_time);
|
||||
retries++;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||
Serial.println("Disconnecting MQTT");
|
||||
client.unsubscribe(topic);
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||
StaticJsonDocument<100> json;
|
||||
json["temperature"] = data[0];
|
||||
json["humidity"] = data[1];
|
||||
json["soil_humidity"] = data[2];
|
||||
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");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user