store self-calibration data on non volatile storage

This commit is contained in:
Cedric Girard 2019-07-10 17:04:33 +02:00
parent c62742f0f3
commit 845f7bb3f7
Signed by: X-dark
GPG key ID: E7D0E125DB9519E4

View file

@ -1,5 +1,6 @@
#include <Arduino.h>
#include <Wire.h>
#include <Preferences.h>
#include <WiFi.h>
#include "wifi_login.h"
@ -14,6 +15,8 @@
#include <Adafruit_SGP30.h>
Preferences preferences;
U8G2_SSD1327_WS_128X128_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 17, /* dc=*/ 3, /* reset=*/ 16);
@ -61,8 +64,15 @@ uint32_t getAbsoluteHumidity(float temperature, float humidity) {
return absoluteHumidityScaled;
}
uint16_t TVOC_base, eCO2_base;
void setup() {
//Init Non Volatile Storage
// Namespace name is limited to 15 chars.
// RW-mode (second parameter has to be false).
preferences.begin("env-monitor", false);
//Init Display
u8g2.begin();
u8g2.setFont(u8g2_font_t0_15_mf);
@ -129,8 +139,13 @@ void setup() {
u8g2log.print("Failed to init IAQ algorithm\n");
}
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
//sgp.setIAQBaseline(0x8E68, 0x8F41); // Will vary for each sensor!
TVOC_base = preferences.getUShort("TVOC_base",0);
eCO2_base = preferences.getUShort("eCO2_base",0);
if ( TVOC_base != 0 && eCO2_base != 0){
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
u8g2log.print("Found previous baseline\n");
sgp.setIAQBaseline(eCO2_base, TVOC_base);
}
//delay to allow setup output reading
delay(10000);
@ -180,7 +195,6 @@ void loop() {
if (counter == 60) {
counter = 0;
uint16_t TVOC_base, eCO2_base;
if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
u8g2log.print("Failed to get baseline readings\n");
return;
@ -190,6 +204,8 @@ void loop() {
u8g2log.print(" & TVOC: 0x");
u8g2log.print(TVOC_base, HEX);
u8g2log.print("\n");
preferences.putUShort("TVOC_base", TVOC_base);
preferences.putUShort("eCO2_base", eCO2_base);
}
//wait 1min