Compare commits

..

1 Commits

Author SHA1 Message Date
2620d05f49 Use a robust asynchronous MQTT client 2026-03-11 20:49:38 +01:00
6 changed files with 25 additions and 40 deletions

View File

@@ -1,58 +1,44 @@
* Homeostasis * Homeostasis
Temperature and humidity sensor that communicates via MQTT. The project is implemented using a Wemos D1 mini ESP32 board with a DHT22 shield. 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:
This specific shield needs to have the data line connected to a pin different to D4, as explained in [[https://blog.zs64.net/2018/02/fixing-the-wemos-d1-mini-dht22-shield][this blog post]]. - 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% #+ATTR_HTML: :width 60%
[[./board.jpg]] [[./result.png]]
** Pinout of the board ** Pinout of the board
#+ATTR_HTML: :width 40% #+ATTR_HTML: :width 40%
[[./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]]
- [[https://github.com/adafruit/Adafruit_Sensor][Adafruit Unified Sensor Driver]] - [[https://github.com/adafruit/Adafruit_Sensor][Adafruit Unified Sensor Driver]]
- [[https://github.com/marvinroger/async-mqtt-client][Async MQTT client]] - [[https://github.com/knolleary/pubsubclient][PubSubClient]]
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]] - [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
** Configuration
In the data directory, there is a JSON file that allows the configuration of multiple values:
- ssid: WiFi network name
- psk: WiFi password
- mqtt_host: IP address of the MQTT broker
- mqtt_user: username that has write access on the MQTT broker
- mqtt_password: password for the user that has write access on the MQTT broker
- mqtt_port: port used by the MQTT broker
- mqtt_topic: channel name
- device_id: unique identifier of the sensor
- sleep_time: time interval between each data transmission
- connection_attempts: maximum connection attemps to the network
The file needs to be renamed to config.json
** Deployment ** Deployment
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tooling than the official Arduino IDE. 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:
*** Generate compilation database
#+begin_src sql
pio run -t compiledb
#+end_src
*** Upload the configuration file to the board
#+begin_src shell #+begin_src shell
pio run -t uploadfs pio run -t uploadfs
#+end_src #+end_src
*** Compile the project
2. Compile the project
#+begin_src shell #+begin_src shell
pio run -t compile pio run -t compile
#+end_src #+end_src
*** Upload firmware
3. Upload firmware
#+begin_src shell #+begin_src shell
pio run -t upload pio run -t upload
#+end_src #+end_src

BIN
board.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

View File

@@ -20,8 +20,6 @@ void connect_mqtt();
void wlan_connection_handler(WiFiEvent_t event); void wlan_connection_handler(WiFiEvent_t event);
void on_mqtt_connection(bool session);
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason); void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason);
size_t construct_json(float *data, char *buffer, int buffer_size); size_t construct_json(float *data, char *buffer, int buffer_size);

View File

@@ -4,7 +4,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <DHT.h> #include <DHT.h>
DHT dht(D0, DHT22); DHT dht(4, DHT22);
const char *config_file_path = "/config.json"; const char *config_file_path = "/config.json";
Config *config; Config *config;

View File

@@ -3,7 +3,11 @@
void initialize_sensor(DHT &sensor) { sensor.begin(); } void initialize_sensor(DHT &sensor) { sensor.begin(); }
void read_values(DHT &sensor, float *data) { void read_values(DHT &sensor, float *data) {
data[0] = sensor.readTemperature(); float temperature = sensor.readTemperature();
data[1] = sensor.readHumidity(); float humidity = sensor.readHumidity();
Serial.println("Data fetched from the sensor"); Serial.println("Data fetched from the sensor");
data[0] = temperature;
data[1] = humidity;
} }

View File

@@ -3,7 +3,6 @@
void initialize_wlan() { WiFi.onEvent(wlan_connection_handler); } void initialize_wlan() { WiFi.onEvent(wlan_connection_handler); }
void initialize_mqtt() { void initialize_mqtt() {
mqtt_client.onConnect(on_mqtt_connection);
mqtt_client.onDisconnect(on_mqtt_disconnection); mqtt_client.onDisconnect(on_mqtt_disconnection);
mqtt_client.setServer(config->mqtt_host, config->mqtt_port); mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password); mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
@@ -32,8 +31,6 @@ void wlan_connection_handler(WiFiEvent_t event) {
} }
} }
void on_mqtt_connection(bool session) { Serial.println("MQTT connected"); }
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) { void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
Serial.println("MQTT disconnected"); Serial.println("MQTT disconnected");