Add update functionality and counter variables

This commit is contained in:
Sietse Jonker
2024-02-27 16:55:48 +01:00
parent 540d80a545
commit 984e5e748d

View File

@@ -32,6 +32,10 @@ float humidity = 0;
uint16_t eCO2 = 0; uint16_t eCO2 = 0;
uint16_t TVOC = 0; uint16_t TVOC = 0;
bool noise = false; bool noise = false;
bool update = false;
double counterBaseline = 15000;
int counterInterval = 5000;
#define USE_SERIAL Serial #define USE_SERIAL Serial
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
@@ -82,7 +86,7 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
} }
void websocketSetup(){ void websocketSetup(){
for(uint8_t t = 4; t > 0; t--) { for(uint8_t t = 1; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush(); USE_SERIAL.flush();
delay(1000); delay(1000);
@@ -174,18 +178,19 @@ void setup() {
void loop() { void loop() {
webSocket.loop(); webSocket.loop();
if (update){
webSocket.sendTXT("Temp: " + String(temperature)); webSocket.sendTXT("Temp: " + String(temperature));
webSocket.sendTXT("Humi: " + String(humidity)); webSocket.sendTXT("Humi: " + String(humidity));
webSocket.sendTXT("eCO2: " + String(sgp.eCO2)); webSocket.sendTXT("eCO2: " + String(sgp.eCO2));
webSocket.sendTXT("TVOC: " + String(sgp.TVOC)); webSocket.sendTXT("TVOC: " + String(sgp.TVOC));
}
// if sensor doesnt work print in serialmonitor // if sensor doesnt work print in serialmonitor
if (!sgp.IAQmeasure()) { if (!sgp.IAQmeasure()) {
Serial.println("SGP30: BAD"); // Serial.println("SGP30: BAD");
return; return;
} else { } else {
Serial.println("SGP30: OK"); // Serial.println("SGP30: OK");
} }
// read dht11 sensor // read dht11 sensor
@@ -196,8 +201,21 @@ void loop() {
displayData(); displayData();
// get new baseline every 30 seconds // get new baseline every 30 seconds
if (counter == 30) { if (update) {
sgp.getIAQBaseline(&eCO2_base, &TVOC_base); sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
} }
counter++;
counter = millis();
Serial.println("baseline" + String(counterBaseline));
Serial.println("counter" + String(counter));
if (counter >= counterBaseline){
counterBaseline = counterBaseline + counterInterval;
update = true;
Serial.println("update");
} else {
update = false;
Serial.println("no update");
}
} }