Compare commits
6 Commits
a9ca6bc28d
...
8dffe8747f
| Author | SHA1 | Date | |
|---|---|---|---|
|
8dffe8747f
|
|||
|
517ed64d31
|
|||
|
f1a8ec6ed0
|
|||
|
8907700a8a
|
|||
|
8c74f99efc
|
|||
|
183882ee41
|
@@ -20,6 +20,6 @@ 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_
|
||||||
@@ -10,7 +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"];
|
config->connection_attempts = json["connection_attempts"];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,4 +28,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); }
|
||||||
|
|||||||
67
src/main.cpp
67
src/main.cpp
@@ -1,63 +1,36 @@
|
|||||||
|
#include "config.h"
|
||||||
|
#include "sensor.h"
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <SensirionI2cScd4x.h>
|
#include <stdlib.h>
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
SensirionI2cScd4x sensor;
|
|
||||||
|
|
||||||
int error;
|
|
||||||
static char errorMessage[64];
|
|
||||||
|
|
||||||
const char *config_file_path = "/config.json";
|
|
||||||
long inactivity_delay;
|
|
||||||
Config *config;
|
|
||||||
WiFiClient wifi_client;
|
WiFiClient wifi_client;
|
||||||
PubSubClient mqtt_client(wifi_client);
|
PubSubClient mqtt_client(wifi_client);
|
||||||
|
SensirionI2cScd4x sensor;
|
||||||
|
|
||||||
int check_error(int error) {
|
int error_code;
|
||||||
if (error) {
|
char error_msg[64];
|
||||||
Serial.println("Error while initializing sensor");
|
const char *config_file_path = "/config.json";
|
||||||
errorToString(error, errorMessage, sizeof errorMessage);
|
Config *config;
|
||||||
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();
|
connect_wlan(config);
|
||||||
inactivity_delay = 60000;
|
connect_mqtt(mqtt_client, config);
|
||||||
|
initialize_sensor(sensor, error_code, error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
uint16_t co2_concentration = 0.0;
|
float data[3];
|
||||||
float temperature = 0.0;
|
|
||||||
float humidity = 0.0;
|
|
||||||
bool data_available = false;
|
|
||||||
|
|
||||||
error = sensor.getDataReadyStatus(data_available);
|
read_values(sensor, data, error_code, error_msg);
|
||||||
error = sensor.readMeasurement(co2_concentration, temperature, humidity);
|
mqtt_transfer(mqtt_client, config, data);
|
||||||
|
delay(config->sleep_time);
|
||||||
if (data_available) {
|
|
||||||
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);
|
||||||
|
}
|
||||||
21
src/wlan.cpp
21
src/wlan.cpp
@@ -17,16 +17,25 @@ void connect_wlan(Config *config) {
|
|||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_mqtt(PubSubClient &client, Config *config) {
|
void connect_mqtt(PubSubClient &client, Config *config) {
|
||||||
if (!client.connected())
|
bool connection;
|
||||||
client.setServer(config->mqtt_host, config->mqtt_port);
|
client.setServer(config->mqtt_host, config->mqtt_port);
|
||||||
if (client.connect(config->device_id, config->mqtt_user,
|
|
||||||
config->mqtt_password)) {
|
while (!client.connected()) {
|
||||||
|
connection = client.connect(config->device_id, config->mqtt_user,
|
||||||
|
config->mqtt_password);
|
||||||
|
if (connection) {
|
||||||
Serial.println("MQTT connected");
|
Serial.println("MQTT connected");
|
||||||
client.subscribe(config->topic);
|
client.subscribe(config->topic);
|
||||||
|
} else {
|
||||||
|
Serial.println("MQTT failed to connect");
|
||||||
|
Serial.println(client.state());
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,15 +47,15 @@ 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) {
|
||||||
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(PubSubClient &client, Config *config, 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);
|
client.publish(config->topic, buffer, payload_size);
|
||||||
Serial.println("Data transferred successfully");
|
Serial.println("Data transferred successfully");
|
||||||
|
|||||||
Reference in New Issue
Block a user