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 "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;
|
|
||||||
|
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user