Compare commits
11 Commits
ab54ce8cfb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
163bb80680
|
|||
|
e72df056a3
|
|||
|
8dffe8747f
|
|||
|
517ed64d31
|
|||
|
f1a8ec6ed0
|
|||
|
8907700a8a
|
|||
|
8c74f99efc
|
|||
|
183882ee41
|
|||
|
a9ca6bc28d
|
|||
|
463757fa60
|
|||
|
dbd562e367
|
26
.clangd
Normal file
26
.clangd
Normal file
@@ -0,0 +1,26 @@
|
||||
CompileFlags:
|
||||
Add: [
|
||||
-DSSIZE_MAX,
|
||||
-DLWIP_NO_UNISTD_H=1,
|
||||
-Dssize_t=long,
|
||||
-D_SSIZE_T_DECLARED,
|
||||
-Wno-unknown-warning-option
|
||||
]
|
||||
Remove: [
|
||||
-mlong-calls,
|
||||
-fno-tree-switch-conversion,
|
||||
-mtext-section-literals,
|
||||
-mlongcalls,
|
||||
-fstrict-volatile-bitfields,
|
||||
-free,
|
||||
-fipa-pta,
|
||||
-march=*,
|
||||
-mabi=*,
|
||||
-mcpu=*
|
||||
]
|
||||
Diagnostics:
|
||||
Suppress:
|
||||
- pp_including_mainfile_in_preamble
|
||||
- pp_expr_bad_token_start_expr
|
||||
- redefinition_different_typedef
|
||||
- main_returns_nonint
|
||||
11
README.org
11
README.org
@@ -1,12 +1,6 @@
|
||||
* Muraqib
|
||||
|
||||
Temperature and humidity sensor that sends its data via MQTT. The project is implemented using a Wemos D1 mini board with the following components:
|
||||
|
||||
- DHT22 temperature and humidity sensor
|
||||
- Battery shield
|
||||
- 3.7V 1000mAh Lithium battery
|
||||
|
||||
The sensor captures the data every 5 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. This sensor is part of a project to grow mushrooms in a controlled environment.
|
||||
CO2 sensor that collects and sends its data via MQTT to a server. The board of the sensor is the Wemos D1 mini and the sensor is a SCD41.
|
||||
|
||||
** Pinout of the board
|
||||
|
||||
@@ -14,8 +8,7 @@ The sensor captures the data every 5 minutes and sends it to a MQTT broker and t
|
||||
[[./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/Sensirion/arduino-i2c-scd4x][Sensirion I²C SCD4X Arduino Library]]
|
||||
- [[https://github.com/knolleary/pubsubclient][PubSubClient]]
|
||||
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
||||
** Configuration
|
||||
|
||||
@@ -7,6 +7,5 @@
|
||||
"mqtt_port": 1883,
|
||||
"mqtt_topic": "",
|
||||
"device_id": "",
|
||||
"sleep_time": 30,
|
||||
"connection_attempts": 60
|
||||
"sleep_time": 30
|
||||
}
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
#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;
|
||||
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_microseconds(int minutes);
|
||||
long minutes_to_milliseconds(int minutes);
|
||||
|
||||
#endif // CONFIG_H_
|
||||
|
||||
14
include/sensor.h
Normal file
14
include/sensor.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef SENSOR_H_
|
||||
#define SENSOR_H_
|
||||
|
||||
#include <SensirionI2cScd4x.h>
|
||||
|
||||
void handle_error(int code, char *msg);
|
||||
|
||||
void initialize_sensor(SensirionI2cScd4x &sensor, int error_code,
|
||||
char *error_msg);
|
||||
|
||||
void read_values(SensirionI2cScd4x &sensor, float *data, int error_code,
|
||||
char *error_msg);
|
||||
|
||||
#endif // SENSOR_H_
|
||||
@@ -2,15 +2,33 @@
|
||||
#define WLAN_H
|
||||
|
||||
#include "config.h"
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
static AsyncMqttClient mqtt_client;
|
||||
static Ticker mqtt_connection_timer, wlan_connection_timer;
|
||||
static WiFiEventHandler connection_handler, disconnection_handler;
|
||||
extern Config *config;
|
||||
|
||||
void initialize_wlan();
|
||||
|
||||
void initialize_mqtt();
|
||||
|
||||
void connect_wlan();
|
||||
|
||||
void connect_mqtt();
|
||||
|
||||
void on_wlan_connection(const WiFiEventStationModeGotIP &event);
|
||||
|
||||
void on_wlan_disconnection(const WiFiEventStationModeDisconnected &event);
|
||||
|
||||
void on_mqtt_connection(bool session);
|
||||
|
||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason);
|
||||
|
||||
void initial_connection(const char *ssid, const char *psk,
|
||||
const char *hostname);
|
||||
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 mqtt_transfer(float *data);
|
||||
|
||||
#endif /* WLAN_H */
|
||||
|
||||
@@ -13,8 +13,9 @@ platform = espressif8266
|
||||
board = d1_mini
|
||||
board_build.filesystem = littlefs
|
||||
framework = arduino
|
||||
monitor_filters = esp8266_exception_decoder
|
||||
lib_deps =
|
||||
adafruit/DHT sensor library@^1.4.6
|
||||
adafruit/Adafruit Unified Sensor@^1.1.15
|
||||
knolleary/PubSubClient@^2.8
|
||||
sensirion/Sensirion I2C SCD4x@^1.1.0
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
bblanchon/ArduinoJson@^7.4.1
|
||||
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
#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_microseconds(json["sleep_time"]);
|
||||
config->connection_attempts = json["connection_attempts"];
|
||||
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;
|
||||
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_microseconds(int minutes) { return (minutes * 6e7); }
|
||||
long minutes_to_milliseconds(int minutes) { return (minutes * 6e4); }
|
||||
|
||||
49
src/main.cpp
49
src/main.cpp
@@ -1,38 +1,37 @@
|
||||
#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);
|
||||
SensirionI2cScd4x sensor;
|
||||
|
||||
int error_code;
|
||||
char error_msg[64];
|
||||
|
||||
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[3];
|
||||
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);
|
||||
Serial.begin(9600);
|
||||
config = (Config *)malloc(sizeof(Config));
|
||||
|
||||
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(sensor, error_code, error_msg);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float temperature = dht.readTemperature();
|
||||
float humidity = dht.readHumidity();
|
||||
float data[2] = {temperature, humidity};
|
||||
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);
|
||||
unsigned long current_millis = millis();
|
||||
|
||||
if (current_millis - previous_millis >= config->sleep_time) {
|
||||
previous_millis = current_millis;
|
||||
read_values(sensor, data, error_code, error_msg);
|
||||
mqtt_transfer(data);
|
||||
}
|
||||
}
|
||||
|
||||
49
src/sensor.cpp
Normal file
49
src/sensor.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "sensor.h"
|
||||
#include <Wire.h>
|
||||
|
||||
// TODO Trigger a reset when an error occurs
|
||||
void handle_error(int code, char *msg) {
|
||||
if (code) {
|
||||
errorToString(code, msg, sizeof msg);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void initialize_sensor(SensirionI2cScd4x &sensor, int error_code,
|
||||
char *error_msg) {
|
||||
Wire.begin();
|
||||
sensor.begin(Wire, SCD41_I2C_ADDR_62);
|
||||
Serial.println("Starting sensor initialization");
|
||||
|
||||
error_code = sensor.wakeUp();
|
||||
handle_error(error_code, error_msg);
|
||||
error_code = sensor.stopPeriodicMeasurement();
|
||||
handle_error(error_code, error_msg);
|
||||
error_code = sensor.reinit();
|
||||
handle_error(error_code, error_msg);
|
||||
error_code = sensor.startPeriodicMeasurement();
|
||||
handle_error(error_code, error_msg);
|
||||
|
||||
Serial.println("The sensor was initialized properly");
|
||||
};
|
||||
|
||||
void read_values(SensirionI2cScd4x &sensor, float *data, int error_code,
|
||||
char *error_msg) {
|
||||
bool data_available = false;
|
||||
short unsigned int co2_concentration = 0.0;
|
||||
|
||||
error_code = sensor.getDataReadyStatus(data_available);
|
||||
handle_error(error_code, error_msg);
|
||||
|
||||
while (!data_available) {
|
||||
delay(100);
|
||||
error_code = sensor.getDataReadyStatus(data_available);
|
||||
handle_error(error_code, error_msg);
|
||||
}
|
||||
|
||||
Serial.println("Data fetched from the sensor");
|
||||
|
||||
error_code = sensor.readMeasurement(co2_concentration, data[1], data[2]);
|
||||
data[0] = co2_concentration;
|
||||
handle_error(error_code, error_msg);
|
||||
}
|
||||
89
src/wlan.cpp
89
src/wlan.cpp
@@ -1,53 +1,64 @@
|
||||
#include "wlan.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void initial_connection(const char *ssid, const char *psk,
|
||||
const char *hostname) {
|
||||
WiFi.hostname(hostname);
|
||||
WiFi.begin(ssid, psk);
|
||||
WiFi.persistent(true);
|
||||
WiFi.setAutoConnect(true);
|
||||
WiFi.setAutoReconnect(true);
|
||||
void initialize_wlan() {
|
||||
connection_handler = WiFi.onStationModeGotIP(on_wlan_connection);
|
||||
disconnection_handler =
|
||||
WiFi.onStationModeDisconnected(on_wlan_disconnection);
|
||||
}
|
||||
|
||||
void connect_wlan(Config *config) {
|
||||
if (WiFi.SSID() != config->ssid)
|
||||
initial_connection(config->ssid, config->psk, config->device_id);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("WiFi connected");
|
||||
void initialize_mqtt() {
|
||||
mqtt_client.onConnect(on_mqtt_connection);
|
||||
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_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)) {
|
||||
void connect_wlan() { WiFi.begin(config->ssid, config->psk); }
|
||||
|
||||
void connect_mqtt() {
|
||||
Serial.println("Connecting to MQTT");
|
||||
mqtt_client.connect();
|
||||
}
|
||||
|
||||
void on_wlan_connection(const WiFiEventStationModeGotIP &event) {
|
||||
Serial.println("WiFi connected");
|
||||
connect_mqtt();
|
||||
}
|
||||
|
||||
void on_wlan_disconnection(const WiFiEventStationModeDisconnected &event) {
|
||||
Serial.println("WiFi disconnected");
|
||||
mqtt_connection_timer.detach();
|
||||
wlan_connection_timer.once(2, connect_wlan);
|
||||
}
|
||||
|
||||
void on_mqtt_connection(bool session) {
|
||||
Serial.println("MQTT connected");
|
||||
client.subscribe(config->topic);
|
||||
}
|
||||
}
|
||||
mqtt_client.subscribe(config->topic, 2);
|
||||
};
|
||||
|
||||
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) {
|
||||
JsonDocument json;
|
||||
json["temperature"] = data[0];
|
||||
json["humidity"] = data[1];
|
||||
size_t payload_size = serializeJson(json, buffer, buffer_size);
|
||||
return payload_size;
|
||||
JsonDocument json;
|
||||
json["co2_concentration"] = data[0];
|
||||
json["temperature"] = data[1];
|
||||
json["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 mqtt_transfer(float *data) {
|
||||
char buffer[100];
|
||||
size_t payload_size = construct_json(data, buffer, 100);
|
||||
uint16_t response =
|
||||
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
||||
if (response)
|
||||
Serial.println("Data transferred successfully");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user