Compare commits
1 Commits
master
...
1a4411940f
| Author | SHA1 | Date | |
|---|---|---|---|
|
1a4411940f
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
|||||||
.pio
|
.pio
|
||||||
data/config.json
|
src/credentials.h
|
||||||
|
|||||||
28
README.org
28
README.org
@@ -1,20 +1,20 @@
|
|||||||
* Homeostasis
|
* Homeostasis
|
||||||
|
|
||||||
Temperature, air humidity and soil humidity sensor that communicates via MQTT. The project is implemented using a Wemos D1 mini board with the following components:
|
Temperature, air humidity and soil humidity sensor that communicates via MQTT. The project is implemented using a Wemos D1 mini board with the following addons:
|
||||||
|
|
||||||
- DHT22 temperature and humidity sensor
|
- DHT11 temperature and humidity
|
||||||
- FC-28 soil hygrometer sensor
|
- FC-28 soil hygrometer sensor
|
||||||
- Battery shield
|
- Battery shield
|
||||||
- 3.7V 1000mAh Lithium battery
|
- 3.7V 1000mAh Lithium battery
|
||||||
|
|
||||||
The sensor captures the data every 15 minutes and sends it to a MQTT broker and then enters deep sleep, it is important to save power as the device runs off a battery.
|
The sensor captures the data every 15 minutes and sends it to a MQTT broker and then enters deep sleep, it is important to save power as the device runs off a battery.
|
||||||
|
|
||||||
#+ATTR_HTML: :width 60%
|
#+ATTR_HTML: :width 50%
|
||||||
[[./result.png]]
|
[[./result.png]]
|
||||||
|
|
||||||
** Pinout of the board
|
** Pinout of the board
|
||||||
|
|
||||||
#+ATTR_HTML: :width 40%
|
#+ATTR_HTML: :width 50%
|
||||||
[[./pinout.png]]
|
[[./pinout.png]]
|
||||||
** Dependencies
|
** Dependencies
|
||||||
- [[https://github.com/adafruit/DHT-sensor-library][Adafruit DHT sensor library]]
|
- [[https://github.com/adafruit/DHT-sensor-library][Adafruit DHT sensor library]]
|
||||||
@@ -23,22 +23,4 @@ The sensor captures the data every 15 minutes and sends it to a MQTT broker and
|
|||||||
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
||||||
** Deployment
|
** Deployment
|
||||||
|
|
||||||
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tools than the Arduino IDE.
|
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tools that the Arduino IDE
|
||||||
|
|
||||||
1. Upload the configuration file to the board:
|
|
||||||
|
|
||||||
#+begin_src shell
|
|
||||||
pio run -t uploadfs
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
2. Compile the project
|
|
||||||
|
|
||||||
#+begin_src shell
|
|
||||||
pio run -t compile
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
3. Upload firmware
|
|
||||||
|
|
||||||
#+begin_src shell
|
|
||||||
pio run -t upload
|
|
||||||
#+end_src
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"ssid": "",
|
|
||||||
"psk": "",
|
|
||||||
"mqtt_host": "",
|
|
||||||
"mqtt_user": "",
|
|
||||||
"mqtt_password": "",
|
|
||||||
"mqtt_port": 1883,
|
|
||||||
"mqtt_topic": "",
|
|
||||||
"device_id": "",
|
|
||||||
"sleep_time": 30,
|
|
||||||
"connection_attempts": 60
|
|
||||||
}
|
|
||||||
32
flake.lock
generated
32
flake.lock
generated
@@ -1,23 +1,37 @@
|
|||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"flake-utils": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1773046814,
|
"lastModified": 1676283394,
|
||||||
"narHash": "sha256-3CEw64UyzEk5QjfbcXNIl4TfmIpa2oY+duuo6aiawcU=",
|
"narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=",
|
||||||
"owner": "NixOS",
|
"owner": "numtide",
|
||||||
"repo": "nixpkgs",
|
"repo": "flake-utils",
|
||||||
"rev": "0c6c0dd2469abaa216599bb19bbf77a328af6564",
|
"rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "numtide",
|
||||||
"ref": "nixos-unstable-small",
|
"repo": "flake-utils",
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1677229269,
|
||||||
|
"narHash": "sha256-awE2w6oi9rzQ8qj1lwKEDm6qIA0a239fiB+AyPjXR2w=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "7d0ed7f2e5aea07ab22ccb338d27fbe347ed2f11",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "nixpkgs",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
flake.nix
31
flake.nix
@@ -1,25 +1,14 @@
|
|||||||
{
|
{
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
|
description = "";
|
||||||
|
|
||||||
outputs =
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||||
{ self, nixpkgs, ... }:
|
|
||||||
let
|
outputs = { self, nixpkgs, flake-utils }:
|
||||||
system = "x86_64-linux";
|
flake-utils.lib.eachDefaultSystem
|
||||||
in
|
(system:
|
||||||
|
let pkgs = nixpkgs.legacyPackages.${system}; in
|
||||||
{
|
{
|
||||||
devShells."${system}".default =
|
devShell = import ./shell.nix { inherit pkgs; };
|
||||||
let
|
}
|
||||||
pkgs = import nixpkgs {
|
);
|
||||||
inherit system;
|
|
||||||
};
|
|
||||||
in
|
|
||||||
pkgs.mkShell {
|
|
||||||
packages = with pkgs; [
|
|
||||||
clang
|
|
||||||
platformio
|
|
||||||
];
|
|
||||||
|
|
||||||
shellHook = "";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
#ifndef CONFIG_H_
|
|
||||||
#define CONFIG_H_
|
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char *ssid;
|
|
||||||
const char *psk;
|
|
||||||
const char *mqtt_host;
|
|
||||||
const char *mqtt_user;
|
|
||||||
const char *mqtt_password;
|
|
||||||
const char *topic;
|
|
||||||
const char *device_id;
|
|
||||||
int mqtt_port;
|
|
||||||
unsigned long sleep_time;
|
|
||||||
} Config;
|
|
||||||
|
|
||||||
void initialize_config(Config *config, JsonDocument json);
|
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, Config *config);
|
|
||||||
|
|
||||||
long minutes_to_milliseconds(int minutes);
|
|
||||||
|
|
||||||
#endif // CONFIG_H_
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#ifndef SENSOR_H_
|
|
||||||
#define SENSOR_H_
|
|
||||||
|
|
||||||
#include <DHT.h>
|
|
||||||
|
|
||||||
void initialize_sensor(DHT &sensor);
|
|
||||||
|
|
||||||
void read_values(DHT &sensor, float *data);
|
|
||||||
|
|
||||||
#endif // SENSOR_H_
|
|
||||||
@@ -1,29 +1,20 @@
|
|||||||
#ifndef WLAN_H
|
#ifndef WLAN_H
|
||||||
#define WLAN_H
|
#define WLAN_H
|
||||||
|
|
||||||
#include "config.h"
|
#include <ESP8266WiFi.h>
|
||||||
#include <AsyncMqttClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <Ticker.h>
|
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
static AsyncMqttClient mqtt_client;
|
const int SLEEP_TIME = 900000000;
|
||||||
static Ticker mqtt_connection_timer, wlan_connection_timer;
|
const int WIFI_TIMEOUT = 10000;
|
||||||
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();
|
||||||
|
void connect_wlan(const int max_retries);
|
||||||
|
void connect_mqtt(PubSubClient &client, 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);
|
size_t construct_json(float *data, char *buffer, int buffer_size);
|
||||||
|
void mqtt_transfer(PubSubClient &client, const char *device_id,
|
||||||
void mqtt_transfer(float *data);
|
const char *topic, float *data);
|
||||||
|
void enter_deep_sleep(bool wifi_timeout);
|
||||||
|
|
||||||
#endif /* WLAN_H */
|
#endif /* WLAN_H */
|
||||||
|
|||||||
@@ -9,13 +9,11 @@
|
|||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:d1_mini]
|
[env:d1_mini]
|
||||||
platform = espressif32
|
platform = espressif8266
|
||||||
board = wemos_d1_mini32
|
board = d1_mini
|
||||||
board_build.filesystem = littlefs
|
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_filters = esp32_exception_decoder
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
adafruit/DHT sensor library@^1.4.4
|
adafruit/DHT sensor library@^1.4.4
|
||||||
adafruit/Adafruit Unified Sensor@^1.1.9
|
adafruit/Adafruit Unified Sensor@^1.1.9
|
||||||
marvinroger/AsyncMqttClient@^0.9.0
|
knolleary/PubSubClient@^2.8
|
||||||
bblanchon/ArduinoJson@^7.4.1
|
bblanchon/ArduinoJson@^6.21.1
|
||||||
|
|||||||
BIN
result.png
BIN
result.png
Binary file not shown.
|
Before Width: | Height: | Size: 3.7 MiB After Width: | Height: | Size: 4.4 MiB |
5
shell.nix
Normal file
5
shell.nix
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{ pkgs ? import <nixpkgs> { } }:
|
||||||
|
|
||||||
|
with pkgs;
|
||||||
|
|
||||||
|
mkShell { buildInputs = [ platformio clang ]; }
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "LittleFS.h"
|
|
||||||
|
|
||||||
void initialize_config(Config *config, JsonDocument 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_milliseconds(json["sleep_time"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
JsonDocument json;
|
|
||||||
DeserializationError err = deserializeJson(json, config_file);
|
|
||||||
if (err)
|
|
||||||
return false;
|
|
||||||
initialize_config(config, json);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
long minutes_to_milliseconds(int minutes) { return (minutes * 6e4); }
|
|
||||||
45
src/main.cpp
45
src/main.cpp
@@ -1,36 +1,37 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "sensor.h"
|
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
|
|
||||||
DHT dht(4, DHT22);
|
#define DHTTYPE DHT11
|
||||||
|
#define DHTPIN 4
|
||||||
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
const char *config_file_path = "/config.json";
|
const int fc28_pin = A0;
|
||||||
Config *config;
|
const int max_connection_attempts = 60;
|
||||||
|
const char *topic = "homeostasis/room";
|
||||||
|
const char *device_id = "homeostasis";
|
||||||
|
WiFiClient wifi_client;
|
||||||
|
PubSubClient mqtt_client(wifi_client);
|
||||||
|
|
||||||
float data[2];
|
bool check_valid_value(float value) {
|
||||||
unsigned long previous_millis = 0;
|
return (!isnan(value) && value >= 0 && value <= 100);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
config = (Config *)malloc(sizeof(Config));
|
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");
|
|
||||||
|
|
||||||
initialize_wlan();
|
|
||||||
initialize_mqtt();
|
|
||||||
connect_wlan();
|
|
||||||
initialize_sensor(dht);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long current_millis = millis();
|
float temperature = dht.readTemperature();
|
||||||
|
float humidity = dht.readHumidity();
|
||||||
if (current_millis - previous_millis >= config->sleep_time) {
|
int analog_val = analogRead(fc28_pin);
|
||||||
previous_millis = current_millis;
|
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
||||||
read_values(dht, data);
|
float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
|
||||||
mqtt_transfer(data);
|
if (check_valid_value(temperature) && check_valid_value(humidity)) {
|
||||||
|
mqtt_transfer(mqtt_client, device_id, topic, data);
|
||||||
}
|
}
|
||||||
|
disconnect_mqtt(mqtt_client, topic);
|
||||||
|
enter_deep_sleep(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
80
src/wlan.cpp
80
src/wlan.cpp
@@ -1,57 +1,65 @@
|
|||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
|
#include "credentials.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
void initialize_wlan() { WiFi.onEvent(wlan_connection_handler); }
|
void initial_connection() {
|
||||||
|
WiFi.begin(SSID, PSK);
|
||||||
void initialize_mqtt() {
|
WiFi.persistent(true);
|
||||||
mqtt_client.onDisconnect(on_mqtt_disconnection);
|
WiFi.setAutoConnect(true);
|
||||||
mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
|
WiFi.setAutoReconnect(true);
|
||||||
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
|
|
||||||
Serial.println("MQTT initialization complete");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_wlan() { WiFi.begin(config->ssid, config->psk); }
|
void connect_wlan(const int max_retries) {
|
||||||
|
if (WiFi.SSID() != SSID)
|
||||||
void connect_mqtt() {
|
initial_connection();
|
||||||
Serial.println("Connecting to MQTT");
|
int retries = 0;
|
||||||
mqtt_client.connect();
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
}
|
if (retries == max_retries)
|
||||||
|
enter_deep_sleep(true);
|
||||||
void wlan_connection_handler(WiFiEvent_t event) {
|
retries++;
|
||||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
delay(1000);
|
||||||
switch (event) {
|
Serial.print(".");
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
}
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
connect_mqtt();
|
}
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
void connect_mqtt(PubSubClient &client, const char *device_id,
|
||||||
Serial.println("WiFi disconnected");
|
const char *topic) {
|
||||||
mqtt_connection_timer.detach();
|
if (!client.connected())
|
||||||
wlan_connection_timer.once(2, connect_wlan);
|
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||||
break;
|
if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) {
|
||||||
|
Serial.println("MQTT connected");
|
||||||
|
client.subscribe(topic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
|
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||||
Serial.println("MQTT disconnected");
|
Serial.println("Disconnecting MQTT");
|
||||||
|
client.unsubscribe(topic);
|
||||||
if (WiFi.isConnected()) {
|
client.disconnect();
|
||||||
mqtt_connection_timer.once(2, connect_mqtt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
JsonDocument json;
|
StaticJsonDocument<100> json;
|
||||||
json["temperature"] = data[0];
|
json["temperature"] = data[0];
|
||||||
json["humidity"] = data[1];
|
json["humidity"] = data[1];
|
||||||
|
json["soil_humidity"] = data[2];
|
||||||
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(float *data) {
|
void mqtt_transfer(PubSubClient &client, const char *device_id,
|
||||||
|
const char *topic, float *data) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
|
connect_mqtt(client, device_id, topic);
|
||||||
size_t payload_size = construct_json(data, buffer, 100);
|
size_t payload_size = construct_json(data, buffer, 100);
|
||||||
uint16_t response =
|
client.publish(topic, buffer, payload_size);
|
||||||
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
|
||||||
if (response)
|
|
||||||
Serial.println("Data transferred successfully");
|
Serial.println("Data transferred successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void enter_deep_sleep(bool wifi_timeout) {
|
||||||
|
Serial.println("Entering deep sleep");
|
||||||
|
if (wifi_timeout)
|
||||||
|
WiFi.disconnect();
|
||||||
|
ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user