Adapt the project to the SCD41 sensor
This commit is contained in:
66
src/main.cpp
66
src/main.cpp
@@ -1,38 +1,62 @@
|
||||
#include "config.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <DHT.h>
|
||||
#include <SensirionI2cScd4x.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define DHTTYPE DHT22
|
||||
#define DHTPIN 4
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
SensirionI2cScd4x sensor;
|
||||
|
||||
int error;
|
||||
static char errorMessage[64];
|
||||
|
||||
const char *config_file_path = "/config.json";
|
||||
long inactivity_delay;
|
||||
Config *config;
|
||||
WiFiClient wifi_client;
|
||||
PubSubClient mqtt_client(wifi_client);
|
||||
|
||||
bool check_valid_value(float value) {
|
||||
return (!isnan(value) && value >= 0 && value <= 100);
|
||||
int check_error(int error) {
|
||||
if (error) {
|
||||
Serial.println("Error while initializing sensor");
|
||||
errorToString(error, errorMessage, sizeof errorMessage);
|
||||
return 1;
|
||||
}
|
||||
return 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);
|
||||
Wire.begin();
|
||||
sensor.begin(Wire, SCD41_I2C_ADDR_62);
|
||||
error = sensor.wakeUp();
|
||||
error = sensor.stopPeriodicMeasurement();
|
||||
error = sensor.reinit();
|
||||
error = sensor.startPeriodicMeasurement();
|
||||
inactivity_delay = 60000;
|
||||
}
|
||||
|
||||
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);
|
||||
uint16_t co2_concentration = 0.0;
|
||||
float temperature = 0.0;
|
||||
float humidity = 0.0;
|
||||
bool data_available = false;
|
||||
|
||||
error = sensor.getDataReadyStatus(data_available);
|
||||
error = sensor.readMeasurement(co2_concentration, temperature, humidity);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user