Implement the main program
This commit is contained in:
67
src/main.cpp
67
src/main.cpp
@@ -1,63 +1,36 @@
|
||||
#include "config.h"
|
||||
#include "sensor.h"
|
||||
#include "wlan.h"
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <SensirionI2cScd4x.h>
|
||||
#include <Wire.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
SensirionI2cScd4x sensor;
|
||||
|
||||
int check_error(int error) {
|
||||
if (error) {
|
||||
Serial.println("Error while initializing sensor");
|
||||
errorToString(error, errorMessage, sizeof errorMessage);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int error_code;
|
||||
char error_msg[64];
|
||||
const char *config_file_path = "/config.json";
|
||||
Config *config;
|
||||
|
||||
void setup() {
|
||||
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;
|
||||
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);
|
||||
|
||||
initialize_sensor(sensor, error_code, error_msg);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint16_t co2_concentration = 0.0;
|
||||
float temperature = 0.0;
|
||||
float humidity = 0.0;
|
||||
bool data_available = false;
|
||||
float data[3];
|
||||
|
||||
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);
|
||||
read_values(sensor, data, error_code, error_msg);
|
||||
mqtt_transfer(mqtt_client, config, data);
|
||||
delay(config->sleep_time);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user