Compare commits
8 Commits
ba6d31c798
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
b943a3f1c8
|
|||
|
409c7f6561
|
|||
|
7bdf50a34f
|
|||
|
8a61260bc0
|
|||
|
36c83f8dee
|
|||
|
92eeb0a638
|
|||
|
37e249a503
|
|||
|
672875b74d
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
.pio
|
||||
src/credentials.h
|
||||
data/config.json
|
||||
|
||||
37
README.org
37
README.org
@@ -1,13 +1,44 @@
|
||||
* 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 addons:
|
||||
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:
|
||||
|
||||
- DHT11 temperature and humidity
|
||||
- DHT22 temperature and humidity sensor
|
||||
- FC-28 soil hygrometer sensor
|
||||
- Battery shield
|
||||
- 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.
|
||||
|
||||
#+ATTR_HTML: :width 60%
|
||||
[[./result.png]]
|
||||
|
||||
** Pinout of the board
|
||||
|
||||
#+ATTR_HTML: :width 50%
|
||||
#+ATTR_HTML: :width 40%
|
||||
[[./pinout.png]]
|
||||
** Dependencies
|
||||
- [[https://github.com/adafruit/DHT-sensor-library][Adafruit DHT sensor library]]
|
||||
- [[https://github.com/adafruit/Adafruit_Sensor][Adafruit Unified Sensor Driver]]
|
||||
- [[https://github.com/knolleary/pubsubclient][PubSubClient]]
|
||||
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
||||
** Deployment
|
||||
|
||||
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tools than 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
|
||||
|
||||
12
data/example.json
Normal file
12
data/example.json
Normal file
@@ -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
|
||||
}
|
||||
27
include/config.h
Normal file
27
include/config.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CONFIG_H_
|
||||
#define CONFIG_H_
|
||||
|
||||
#include "FS.h"
|
||||
#include "LittleFS.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;
|
||||
long sleep_time;
|
||||
int connection_attempts;
|
||||
} Config;
|
||||
|
||||
void initialize_config(Config *config, StaticJsonDocument<512> json);
|
||||
|
||||
bool load_config_file(const char *file_path, Config *config);
|
||||
|
||||
long minutes_to_microseconds(int minutes);
|
||||
|
||||
#endif // CONFIG_H_
|
||||
16
include/wlan.h
Normal file
16
include/wlan.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef WLAN_H
|
||||
#define WLAN_H
|
||||
|
||||
#include "config.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
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);
|
||||
|
||||
#endif /* WLAN_H */
|
||||
@@ -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
|
||||
|
||||
BIN
result.png
Normal file
BIN
result.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.7 MiB |
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); }
|
||||
20
src/main.cpp
20
src/main.cpp
@@ -1,15 +1,15 @@
|
||||
#include "config.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <DHT.h>
|
||||
|
||||
#define DHTTYPE DHT11
|
||||
#define DHTTYPE DHT22
|
||||
#define DHTPIN 4
|
||||
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);
|
||||
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() {
|
||||
@@ -30,8 +33,9 @@ void loop() {
|
||||
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, device_id, topic, data);
|
||||
mqtt_transfer(mqtt_client, config, data);
|
||||
}
|
||||
disconnect_mqtt(mqtt_client, topic);
|
||||
enter_deep_sleep(false);
|
||||
disconnect_mqtt(mqtt_client, config->topic);
|
||||
free(config);
|
||||
enter_deep_sleep(false, config->sleep_time);
|
||||
}
|
||||
|
||||
36
src/wlan.cpp
36
src/wlan.cpp
@@ -1,21 +1,20 @@
|
||||
#include "wlan.h"
|
||||
#include "credentials.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
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(Config *config) {
|
||||
if (WiFi.SSID() != config->ssid)
|
||||
initial_connection(config->ssid, config->psk);
|
||||
int retries = 0;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
if (retries == max_retries)
|
||||
enter_deep_sleep(true);
|
||||
if (retries == config->connection_attempts)
|
||||
enter_deep_sleep(true, config->sleep_time);
|
||||
retries++;
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
@@ -23,13 +22,13 @@ void connect_wlan(const int max_retries) {
|
||||
Serial.println("WiFi connected");
|
||||
}
|
||||
|
||||
void connect_mqtt(PubSubClient &client, const char *device_id,
|
||||
const char *topic) {
|
||||
void connect_mqtt(PubSubClient &client, Config *config) {
|
||||
if (!client.connected())
|
||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||
if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) {
|
||||
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(topic);
|
||||
client.subscribe(config->topic);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,18 +47,17 @@ size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||
return payload_size;
|
||||
}
|
||||
|
||||
void mqtt_transfer(PubSubClient &client, const char *device_id,
|
||||
const char *topic, float *data) {
|
||||
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
|
||||
char buffer[100];
|
||||
connect_mqtt(client, device_id, topic);
|
||||
connect_mqtt(client, config);
|
||||
size_t payload_size = construct_json(data, buffer, 100);
|
||||
client.publish(topic, buffer, payload_size);
|
||||
client.publish(config->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);
|
||||
}
|
||||
|
||||
20
src/wlan.h
20
src/wlan.h
@@ -1,20 +0,0 @@
|
||||
#ifndef WLAN_H
|
||||
#define WLAN_H
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
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,
|
||||
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,
|
||||
const char *topic, float *data);
|
||||
void enter_deep_sleep(bool wifi_timeout);
|
||||
|
||||
#endif /* WLAN_H */
|
||||
Reference in New Issue
Block a user