Compare commits
2 Commits
b943a3f1c8
...
2620d05f49
| Author | SHA1 | Date | |
|---|---|---|---|
|
2620d05f49
|
|||
|
1dcd9c8f0a
|
28
flake.lock
generated
28
flake.lock
generated
@@ -1,37 +1,23 @@
|
||||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1676283394,
|
||||
"narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1677229269,
|
||||
"narHash": "sha256-awE2w6oi9rzQ8qj1lwKEDm6qIA0a239fiB+AyPjXR2w=",
|
||||
"lastModified": 1773046814,
|
||||
"narHash": "sha256-3CEw64UyzEk5QjfbcXNIl4TfmIpa2oY+duuo6aiawcU=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7d0ed7f2e5aea07ab22ccb338d27fbe347ed2f11",
|
||||
"rev": "0c6c0dd2469abaa216599bb19bbf77a328af6564",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"type": "indirect"
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable-small",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
|
||||
31
flake.nix
31
flake.nix
@@ -1,14 +1,25 @@
|
||||
{
|
||||
description = "";
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
|
||||
|
||||
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
flake-utils.lib.eachDefaultSystem
|
||||
(system:
|
||||
let pkgs = nixpkgs.legacyPackages.${system}; in
|
||||
outputs =
|
||||
{ self, nixpkgs, ... }:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
in
|
||||
{
|
||||
devShell = import ./shell.nix { inherit pkgs; };
|
||||
}
|
||||
);
|
||||
devShells."${system}".default =
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
clang
|
||||
platformio
|
||||
];
|
||||
|
||||
shellHook = "";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef CONFIG_H_
|
||||
#define CONFIG_H_
|
||||
|
||||
#include "FS.h"
|
||||
#include "LittleFS.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
typedef struct {
|
||||
@@ -14,14 +12,13 @@ typedef struct {
|
||||
const char *topic;
|
||||
const char *device_id;
|
||||
int mqtt_port;
|
||||
long sleep_time;
|
||||
int connection_attempts;
|
||||
unsigned long sleep_time;
|
||||
} Config;
|
||||
|
||||
void initialize_config(Config *config, StaticJsonDocument<512> json);
|
||||
void initialize_config(Config *config, JsonDocument json);
|
||||
|
||||
bool load_config_file(const char *file_path, Config *config);
|
||||
|
||||
long minutes_to_microseconds(int minutes);
|
||||
long minutes_to_milliseconds(int minutes);
|
||||
|
||||
#endif // CONFIG_H_
|
||||
|
||||
10
include/sensor.h
Normal file
10
include/sensor.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef SENSOR_H_
|
||||
#define SENSOR_H_
|
||||
|
||||
#include <DHT.h>
|
||||
|
||||
void initialize_sensor(DHT &sensor);
|
||||
|
||||
void read_values(DHT &sensor, float *data);
|
||||
|
||||
#endif // SENSOR_H_
|
||||
@@ -2,15 +2,28 @@
|
||||
#define WLAN_H
|
||||
|
||||
#include "config.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <Ticker.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
static AsyncMqttClient mqtt_client;
|
||||
static Ticker mqtt_connection_timer, wlan_connection_timer;
|
||||
extern Config *config;
|
||||
|
||||
void initialize_wlan();
|
||||
|
||||
void initialize_mqtt();
|
||||
|
||||
void connect_wlan();
|
||||
|
||||
void connect_mqtt();
|
||||
|
||||
void wlan_connection_handler(WiFiEvent_t event);
|
||||
|
||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason);
|
||||
|
||||
void initial_connection(const char *ssid, const char *psk);
|
||||
void connect_wlan(Config *config);
|
||||
void connect_mqtt(PubSubClient &client, Config *config);
|
||||
void disconnect_mqtt(PubSubClient &client, const char *topic);
|
||||
size_t construct_json(float *data, char *buffer, int buffer_size);
|
||||
void mqtt_transfer(PubSubClient &client, Config *config, float *data);
|
||||
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
|
||||
|
||||
void mqtt_transfer(float *data);
|
||||
|
||||
#endif /* WLAN_H */
|
||||
|
||||
@@ -9,12 +9,13 @@
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:d1_mini]
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
platform = espressif32
|
||||
board = wemos_d1_mini32
|
||||
board_build.filesystem = littlefs
|
||||
framework = arduino
|
||||
monitor_filters = esp32_exception_decoder
|
||||
lib_deps =
|
||||
adafruit/DHT sensor library@^1.4.4
|
||||
adafruit/Adafruit Unified Sensor@^1.1.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
bblanchon/ArduinoJson@^6.21.1
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
bblanchon/ArduinoJson@^7.4.1
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
{ pkgs ? import <nixpkgs> { } }:
|
||||
|
||||
with pkgs;
|
||||
|
||||
mkShell { buildInputs = [ platformio clang ]; }
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "config.h"
|
||||
#include "LittleFS.h"
|
||||
|
||||
void initialize_config(Config *config, StaticJsonDocument<512> json) {
|
||||
void initialize_config(Config *config, JsonDocument json) {
|
||||
config->ssid = strdup(json["ssid"]);
|
||||
config->psk = strdup(json["psk"]);
|
||||
config->mqtt_host = strdup(json["mqtt_host"]);
|
||||
@@ -9,8 +10,7 @@ void initialize_config(Config *config, StaticJsonDocument<512> json) {
|
||||
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->sleep_time = minutes_to_milliseconds(json["sleep_time"]);
|
||||
}
|
||||
|
||||
bool load_config_file(const char *file_path, Config *config) {
|
||||
@@ -19,7 +19,7 @@ bool load_config_file(const char *file_path, Config *config) {
|
||||
File config_file = LittleFS.open(file_path, "r");
|
||||
if (!config_file)
|
||||
return false;
|
||||
StaticJsonDocument<512> json;
|
||||
JsonDocument json;
|
||||
DeserializationError err = deserializeJson(json, config_file);
|
||||
if (err)
|
||||
return false;
|
||||
@@ -27,4 +27,4 @@ bool load_config_file(const char *file_path, Config *config) {
|
||||
return true;
|
||||
}
|
||||
|
||||
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }
|
||||
long minutes_to_milliseconds(int minutes) { return (minutes * 6e4); }
|
||||
|
||||
37
src/main.cpp
37
src/main.cpp
@@ -1,41 +1,36 @@
|
||||
#include "config.h"
|
||||
#include "sensor.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <DHT.h>
|
||||
|
||||
#define DHTTYPE DHT22
|
||||
#define DHTPIN 4
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
DHT dht(4, DHT22);
|
||||
|
||||
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);
|
||||
}
|
||||
float data[2];
|
||||
unsigned long previous_millis = 0;
|
||||
|
||||
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);
|
||||
|
||||
initialize_wlan();
|
||||
initialize_mqtt();
|
||||
connect_wlan();
|
||||
initialize_sensor(dht);
|
||||
}
|
||||
|
||||
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);
|
||||
unsigned long current_millis = millis();
|
||||
|
||||
if (current_millis - previous_millis >= config->sleep_time) {
|
||||
previous_millis = current_millis;
|
||||
read_values(dht, data);
|
||||
mqtt_transfer(data);
|
||||
}
|
||||
disconnect_mqtt(mqtt_client, config->topic);
|
||||
free(config);
|
||||
enter_deep_sleep(false, config->sleep_time);
|
||||
}
|
||||
|
||||
13
src/sensor.cpp
Normal file
13
src/sensor.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "sensor.h"
|
||||
|
||||
void initialize_sensor(DHT &sensor) { sensor.begin(); }
|
||||
|
||||
void read_values(DHT &sensor, float *data) {
|
||||
float temperature = sensor.readTemperature();
|
||||
float humidity = sensor.readHumidity();
|
||||
|
||||
Serial.println("Data fetched from the sensor");
|
||||
|
||||
data[0] = temperature;
|
||||
data[1] = humidity;
|
||||
}
|
||||
76
src/wlan.cpp
76
src/wlan.cpp
@@ -1,63 +1,57 @@
|
||||
#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 initialize_wlan() { WiFi.onEvent(wlan_connection_handler); }
|
||||
|
||||
void initialize_mqtt() {
|
||||
mqtt_client.onDisconnect(on_mqtt_disconnection);
|
||||
mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
|
||||
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
|
||||
Serial.println("MQTT initialization complete");
|
||||
}
|
||||
|
||||
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(".");
|
||||
void connect_wlan() { WiFi.begin(config->ssid, config->psk); }
|
||||
|
||||
void connect_mqtt() {
|
||||
Serial.println("Connecting to MQTT");
|
||||
mqtt_client.connect();
|
||||
}
|
||||
|
||||
void wlan_connection_handler(WiFiEvent_t event) {
|
||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
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);
|
||||
connect_mqtt();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.println("WiFi disconnected");
|
||||
mqtt_connection_timer.detach();
|
||||
wlan_connection_timer.once(2, connect_wlan);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||
Serial.println("Disconnecting MQTT");
|
||||
client.unsubscribe(topic);
|
||||
client.disconnect();
|
||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
|
||||
Serial.println("MQTT disconnected");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
mqtt_connection_timer.once(2, connect_mqtt);
|
||||
}
|
||||
}
|
||||
|
||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||
StaticJsonDocument<100> json;
|
||||
JsonDocument 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) {
|
||||
void mqtt_transfer(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);
|
||||
uint16_t response =
|
||||
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
||||
if (response)
|
||||
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