diff --git a/arduino/node-code/node-code-final.ino b/arduino/node-code/node-code-final.ino index bfb827c..9b50484 100644 --- a/arduino/node-code/node-code-final.ino +++ b/arduino/node-code/node-code-final.ino @@ -18,13 +18,16 @@ #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define i2c_adress 0x3c +#define USE_SERIAL Serial +// make new objects Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); DHT dht(DHTPIN, DHTTYPE); WiFiMulti WiFiMulti; Adafruit_SGP30 sgp; WebSocketsClient webSocket; +// define variables uint16_t TVOC_base, eCO2_base; uint16_t counter = 0; float temperature = 0; @@ -36,8 +39,7 @@ bool update = false; double counterBaseline = 15000; int counterInterval = 5000; -#define USE_SERIAL Serial - +// hexdump function for websockets binary handler void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { const uint8_t* src = (const uint8_t*) mem; USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); @@ -51,8 +53,8 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { USE_SERIAL.printf("\n"); } +// handler for websocket events void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { - switch(type) { case WStype_DISCONNECTED: USE_SERIAL.printf("[WSc] Disconnected!\n"); @@ -86,15 +88,8 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { } void websocketSetup(){ - for(uint8_t t = 1; t > 0; t--) { - USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); - } - WiFiMulti.addAP("iotroam", "vbK9gbDBIB"); - //WiFi.disconnect(); while(WiFiMulti.run() != WL_CONNECTED) { delay(100); } @@ -105,11 +100,8 @@ void websocketSetup(){ // event handler webSocket.onEvent(webSocketEvent); - // use HTTP Basic Authorization this is optional remove if not needed - - - // try ever 5000 again if connection has failed - webSocket.setReconnectInterval(5000); + // try ever 500 again if connection has failed + webSocket.setReconnectInterval(500); } void resetValues() { @@ -121,6 +113,7 @@ void resetValues() { eCO2 = 0; TVOC = 0; noise = false; + lastCounter = 0; } void displayData() { @@ -141,22 +134,6 @@ void displayData() { display.display(); } -// bool wifiConnect(){ -// while (WiFi.status() != WL_CONNECTED) -// { -// delay(250); - -// wifiMulti.addAP("LansanKPN-boven", "19sander71vlieland14"); -// wifiMulti.addAP("iotroam", "vbK9gbDBIB"); -// Serial.println("Connectie maken..."); - -// wifiMulti.run(); -// } -// Serial.println("Wifi Connected..."); - -// return true; -// } - void setup() { Serial.begin(115200); @@ -183,6 +160,8 @@ void loop() { webSocket.sendTXT("Humi: " + String(humidity)); webSocket.sendTXT("eCO2: " + String(sgp.eCO2)); webSocket.sendTXT("TVOC: " + String(sgp.TVOC)); + + sgp.getIAQBaseline(&eCO2_base, &TVOC_base); } // if sensor doesnt work print in serialmonitor @@ -199,23 +178,11 @@ void loop() { // display sensordata on oled screen displayData(); - - // get new baseline every 30 seconds - if (update) { - sgp.getIAQBaseline(&eCO2_base, &TVOC_base); - } - 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"); + if (currentCounter - lastCounter >= 5000){ + lastcounter = millis() + update(); } + + currentCounter = millis(); } \ No newline at end of file