Compare commits
8 Commits
a9ca6bc28d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
163bb80680
|
|||
|
e72df056a3
|
|||
|
8dffe8747f
|
|||
|
517ed64d31
|
|||
|
f1a8ec6ed0
|
|||
|
8907700a8a
|
|||
|
8c74f99efc
|
|||
|
183882ee41
|
@@ -7,6 +7,5 @@
|
|||||||
"mqtt_port": 1883,
|
"mqtt_port": 1883,
|
||||||
"mqtt_topic": "",
|
"mqtt_topic": "",
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"sleep_time": 30,
|
"sleep_time": 30
|
||||||
"connection_attempts": 60
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,13 @@ typedef struct {
|
|||||||
const char *topic;
|
const char *topic;
|
||||||
const char *device_id;
|
const char *device_id;
|
||||||
int mqtt_port;
|
int mqtt_port;
|
||||||
long sleep_time;
|
unsigned long sleep_time;
|
||||||
int connection_attempts;
|
|
||||||
} Config;
|
} Config;
|
||||||
|
|
||||||
void initialize_config(Config *config, JsonDocument json);
|
void initialize_config(Config *config, JsonDocument json);
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, Config *config);
|
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_
|
#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,14 +2,33 @@
|
|||||||
#define WLAN_H
|
#define WLAN_H
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <PubSubClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
|
#include <ESP8266WiFi.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);
|
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 */
|
#endif /* WLAN_H */
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ framework = arduino
|
|||||||
monitor_filters = esp8266_exception_decoder
|
monitor_filters = esp8266_exception_decoder
|
||||||
lib_deps =
|
lib_deps =
|
||||||
sensirion/Sensirion I2C SCD4x@^1.1.0
|
sensirion/Sensirion I2C SCD4x@^1.1.0
|
||||||
knolleary/PubSubClient@^2.8
|
marvinroger/AsyncMqttClient@^0.9.0
|
||||||
bblanchon/ArduinoJson@^7.4.1
|
bblanchon/ArduinoJson@^7.4.1
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ void initialize_config(Config *config, JsonDocument json) {
|
|||||||
config->topic = strdup(json["mqtt_topic"]);
|
config->topic = strdup(json["mqtt_topic"]);
|
||||||
config->device_id = strdup(json["device_id"]);
|
config->device_id = strdup(json["device_id"]);
|
||||||
config->mqtt_port = json["mqtt_port"];
|
config->mqtt_port = json["mqtt_port"];
|
||||||
config->sleep_time = minutes_to_microseconds(json["sleep_time"]);
|
config->sleep_time = minutes_to_milliseconds(json["sleep_time"]);
|
||||||
config->connection_attempts = json["connection_attempts"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, Config *config) {
|
bool load_config_file(const char *file_path, Config *config) {
|
||||||
@@ -28,4 +27,4 @@ bool load_config_file(const char *file_path, Config *config) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }
|
long minutes_to_milliseconds(int minutes) { return (minutes * 6e4); }
|
||||||
|
|||||||
64
src/main.cpp
64
src/main.cpp
@@ -1,63 +1,37 @@
|
|||||||
|
#include "sensor.h"
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <PubSubClient.h>
|
|
||||||
#include <SensirionI2cScd4x.h>
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
SensirionI2cScd4x sensor;
|
SensirionI2cScd4x sensor;
|
||||||
|
|
||||||
int error;
|
int error_code;
|
||||||
static char errorMessage[64];
|
char error_msg[64];
|
||||||
|
|
||||||
const char *config_file_path = "/config.json";
|
const char *config_file_path = "/config.json";
|
||||||
long inactivity_delay;
|
|
||||||
Config *config;
|
Config *config;
|
||||||
WiFiClient wifi_client;
|
|
||||||
PubSubClient mqtt_client(wifi_client);
|
|
||||||
|
|
||||||
int check_error(int error) {
|
float data[3];
|
||||||
if (error) {
|
unsigned long previous_millis = 0;
|
||||||
Serial.println("Error while initializing sensor");
|
|
||||||
errorToString(error, errorMessage, sizeof errorMessage);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Wire.begin();
|
config = (Config *)malloc(sizeof(Config));
|
||||||
sensor.begin(Wire, SCD41_I2C_ADDR_62);
|
|
||||||
error = sensor.wakeUp();
|
if (!load_config_file(config_file_path, config))
|
||||||
error = sensor.stopPeriodicMeasurement();
|
Serial.println("ERROR: The config file could not be loaded");
|
||||||
error = sensor.reinit();
|
|
||||||
error = sensor.startPeriodicMeasurement();
|
initialize_wlan();
|
||||||
inactivity_delay = 60000;
|
initialize_mqtt();
|
||||||
|
connect_wlan();
|
||||||
|
initialize_sensor(sensor, error_code, error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
uint16_t co2_concentration = 0.0;
|
unsigned long current_millis = millis();
|
||||||
float temperature = 0.0;
|
|
||||||
float humidity = 0.0;
|
|
||||||
bool data_available = false;
|
|
||||||
|
|
||||||
error = sensor.getDataReadyStatus(data_available);
|
if (current_millis - previous_millis >= config->sleep_time) {
|
||||||
error = sensor.readMeasurement(co2_concentration, temperature, humidity);
|
previous_millis = current_millis;
|
||||||
|
read_values(sensor, data, error_code, error_msg);
|
||||||
if (data_available) {
|
mqtt_transfer(data);
|
||||||
Serial.print("CO2 concentration [ppm]: ");
|
|
||||||
Serial.print(co2_concentration);
|
|
||||||
Serial.println();
|
|
||||||
Serial.print("Temperature [°C]: ");
|
|
||||||
Serial.print(temperature);
|
|
||||||
Serial.println();
|
|
||||||
Serial.print("Relative Humidity [RH]: ");
|
|
||||||
Serial.print(humidity);
|
|
||||||
Serial.println();
|
|
||||||
} else {
|
|
||||||
Serial.println(".");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(inactivity_delay);
|
|
||||||
}
|
}
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
79
src/wlan.cpp
79
src/wlan.cpp
@@ -1,53 +1,64 @@
|
|||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
|
|
||||||
void initial_connection(const char *ssid, const char *psk,
|
void initialize_wlan() {
|
||||||
const char *hostname) {
|
connection_handler = WiFi.onStationModeGotIP(on_wlan_connection);
|
||||||
WiFi.hostname(hostname);
|
disconnection_handler =
|
||||||
WiFi.begin(ssid, psk);
|
WiFi.onStationModeDisconnected(on_wlan_disconnection);
|
||||||
WiFi.persistent(true);
|
|
||||||
WiFi.setAutoConnect(true);
|
|
||||||
WiFi.setAutoReconnect(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_wlan(Config *config) {
|
void initialize_mqtt() {
|
||||||
if (WiFi.SSID() != config->ssid)
|
mqtt_client.onConnect(on_mqtt_connection);
|
||||||
initial_connection(config->ssid, config->psk, config->device_id);
|
mqtt_client.onDisconnect(on_mqtt_disconnection);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
|
||||||
delay(1000);
|
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
|
||||||
Serial.print(".");
|
Serial.println("MQTT initialization complete");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
Serial.println("WiFi connected");
|
||||||
|
connect_mqtt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_mqtt(PubSubClient &client, Config *config) {
|
void on_wlan_disconnection(const WiFiEventStationModeDisconnected &event) {
|
||||||
if (!client.connected())
|
Serial.println("WiFi disconnected");
|
||||||
client.setServer(config->mqtt_host, config->mqtt_port);
|
mqtt_connection_timer.detach();
|
||||||
if (client.connect(config->device_id, config->mqtt_user,
|
wlan_connection_timer.once(2, connect_wlan);
|
||||||
config->mqtt_password)) {
|
}
|
||||||
Serial.println("MQTT connected");
|
|
||||||
client.subscribe(config->topic);
|
void on_mqtt_connection(bool session) {
|
||||||
|
Serial.println("MQTT connected");
|
||||||
|
mqtt_client.subscribe(config->topic, 2);
|
||||||
|
};
|
||||||
|
|
||||||
|
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
|
||||||
|
Serial.println("MQTT disconnected");
|
||||||
|
|
||||||
|
if (WiFi.isConnected()) {
|
||||||
|
mqtt_connection_timer.once(2, connect_mqtt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
|
||||||
Serial.println("Disconnecting MQTT");
|
|
||||||
client.unsubscribe(topic);
|
|
||||||
client.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
JsonDocument json;
|
JsonDocument json;
|
||||||
json["temperature"] = data[0];
|
json["co2_concentration"] = data[0];
|
||||||
json["humidity"] = data[1];
|
json["temperature"] = data[1];
|
||||||
|
json["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(PubSubClient &client, Config *config, float *data) {
|
void mqtt_transfer(float *data) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
connect_mqtt(client, config);
|
|
||||||
size_t payload_size = construct_json(data, buffer, 100);
|
size_t payload_size = construct_json(data, buffer, 100);
|
||||||
client.publish(config->topic, buffer, payload_size);
|
uint16_t response =
|
||||||
Serial.println("Data transferred successfully");
|
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
||||||
|
if (response)
|
||||||
|
Serial.println("Data transferred successfully");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user