Skip to content

Commit

Permalink
Calibrate and improve stability/filter erros when reading sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed May 16, 2024
1 parent 90d916f commit 426cabc
Show file tree
Hide file tree
Showing 17 changed files with 1,667 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/
*.log
compile_commands.json
.cache/
.env*
2 changes: 1 addition & 1 deletion cli/src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ pub async fn find_sensor(
}

Err("No devices found".into())
}
}
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ async fn main() -> Result<(), Box<dyn Error>> {

terminal.clear()?;
}
}
}
15 changes: 0 additions & 15 deletions core/.clangd

This file was deleted.

2 changes: 1 addition & 1 deletion core/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ board = nodemcu-32s
framework = arduino
monitor_speed = 9600
extra_scripts = pre:extra_script.py
board_build.f_cpu = 10000000L
board_build.f_cpu = 160000000L
lib_deps =
adafruit/Adafruit SGP30 Sensor@^2.0.0
adafruit/DHT sensor library@^1.4.4
Expand Down
156 changes: 84 additions & 72 deletions core/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Adafruit_BMP280.h" // include main library for BMP280 - Sensor
#include "Adafruit_Si7021.h" // include main library for SI7021 - Sensor
#include "HardwareSerial.h"
#include "MHZ19.h"
#include "ccs811.h"
#include "esp_sleep.h"
#include "i2c_scanner.h"
#include <Adafruit_Sensor.h>
#include <Arduino.h>
Expand Down Expand Up @@ -47,10 +47,9 @@ void setup() {
Serial.println("------------------------------------");

Wire.begin(21, 22);
Wire1.begin(25, 26);
mySerial.begin(BAUDRATE);

if (!lightSensor.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1)) {
if (!lightSensor.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23)) {
Serial.println("Error initializing BH1750");
}

Expand Down Expand Up @@ -89,11 +88,14 @@ void setup() {
Serial.print(SI702x.sernum_a, HEX);
Serial.println(SI702x.sernum_b, HEX);

myMHZ19.begin(mySerial);
myMHZ19.autoCalibration(true);
// Wait for the sensors to stabilize delay(2000);

// Wait for the sensors to stabilize
delay(2000);
while (myMHZ19.errorCode != RESULT_OK) {
Serial.println("Estabilshing connection to MH-Z19");
myMHZ19.begin(mySerial);
};

myMHZ19.calibrate();

// Create the BLE Device
BLEDevice::init("CO2CICKA Sensor");
Expand All @@ -117,71 +119,81 @@ void setup() {
pAdvertising->start();
}

void loop() {
int CO2 = myMHZ19.getCO2();
int8_t Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)

Serial.print("CO2 (ppm): ");
Serial.println(CO2);
Serial.print("MHZ19 => Temperature (C): ");
Serial.println(Temp);

float lightIntensity = lightSensor.readLightLevel();
Serial.print("Light Intensity: ");
Serial.print(lightIntensity);
Serial.println(" lux");

float temperature = bmp280.readTemperature() - 4;
Serial.print("BMP280 => Temperature = ");
Serial.print(temperature);
Serial.print(" °C, ");

float pressure = bmp280.readPressure() / 100;
Serial.print("Pressure = ");
Serial.print(bmp280.readPressure() / 100);
Serial.println(" Pa, ");

float humidity = SI702x.readHumidity() + 5;
Serial.print("SI702x => Temperature = ");
Serial.print(SI702x.readTemperature(), 2);
Serial.print(" °C, ");
Serial.print("Humidity = ");
Serial.println(humidity, 2);

uint16_t eco2, etvoc, errstat, raw; // Read CCS811

ccs811.set_envdata(temperature, humidity);
ccs811.read(&eco2, &etvoc, &errstat, &raw);
if (errstat == CCS811_ERRSTAT_OK) {
Serial.print("CCS811 => CO2 = ");
Serial.print(eco2);
Serial.print("ppm, TVOC = ");
Serial.println(etvoc);
}

FirebaseJson json;
if (CO2 > -1 && Temp > -1) {
json.add("co2", CO2);
json.add("tempereture_of_co2_sensor", Temp);
}

if (lightIntensity > 0) {
json.add("light", lightIntensity);
}
int last_C02;
unsigned long sync_timer = 0;

if (temperature && pressure && humidity) {
json.add("temperature", temperature);
json.add("pressure", pressure);
json.add("humidity", humidity);
void loop() {
if (millis() - sync_timer > 5000) {
int CO2 = myMHZ19.getCO2(false);

float lightIntensity = lightSensor.readLightLevel();
Serial.print("Light Intensity: ");
Serial.print(lightIntensity);
Serial.println(" lux");

float temperature = bmp280.readTemperature() - 4;
Serial.print("BMP280 => Temperature = ");
Serial.print(temperature);
Serial.print(" °C, ");

float pressure = bmp280.readPressure() / 100;
Serial.print("Pressure = ");
Serial.print(bmp280.readPressure() / 100);
Serial.println(" Pa, ");

float humidity = SI702x.readHumidity() + 5;
Serial.print("SI702x => Temperature = ");
Serial.print(SI702x.readTemperature(), 2);
Serial.print(" °C, ");
Serial.print("Humidity = ");
Serial.println(humidity, 2);

uint16_t eco2, etvoc, errstat, raw; // Read CCS811

ccs811.set_envdata(temperature, humidity);
ccs811.read(&eco2, &etvoc, &errstat, &raw);
if (errstat == CCS811_ERRSTAT_OK) {
Serial.print("CCS811 => CO2 = ");
Serial.print(eco2);
Serial.print("ppm, TVOC = ");
Serial.println(etvoc);
}

FirebaseJson json;
if (myMHZ19.errorCode == RESULT_OK && CO2 > 0) {
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
/*Serial.print("MHZ19 => Temperature (C): ");*/
/*Serial.println(Temp);*/

json.add("co2", CO2);
last_C02 = CO2;
} else {
json.add("co2", last_C02);
json.add("mhz19_error_code", myMHZ19.errorCode);

Serial.print("MH-Z19 Error: ");
Serial.println(myMHZ19.errorCode);
}

if (lightIntensity > 0) {
json.add("light", lightIntensity);
}

if (temperature && pressure && humidity) {
json.add("temperature", temperature);
json.add("pressure", pressure);
json.add("humidity", humidity);
}

json.add("eco2", eco2);
json.add("etvoc", etvoc);

json.toString(Serial, true);

pCharacteristic->setValue(json.raw());
pCharacteristic->notify();

sync_timer = millis();
}

json.add("eco2", eco2);
json.add("etvoc", etvoc);

json.toString(Serial, true);

pCharacteristic->setValue(json.raw());
pCharacteristic->notify();

delay(5000);
}
16 changes: 16 additions & 0 deletions firmware/to2-firmware/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"


[env]
ESP_LOGLEVEL="INFO"

[build]
rustflags = [
"-C", "link-arg=-nostartfiles",
]

target = "xtensa-esp32-none-elf"

[unstable]
build-std = ["alloc", "core"]
10 changes: 10 additions & 0 deletions firmware/to2-firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
3 changes: 3 additions & 0 deletions firmware/to2-firmware/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.check.allTargets": false,
}
Loading

0 comments on commit 426cabc

Please sign in to comment.