diff --git a/arduino/node-code/nodeCodeFinal/headerFile.h b/arduino/node-code/headerFile.h similarity index 74% rename from arduino/node-code/nodeCodeFinal/headerFile.h rename to arduino/node-code/headerFile.h index 7443590..94df6d0 100644 --- a/arduino/node-code/nodeCodeFinal/headerFile.h +++ b/arduino/node-code/headerFile.h @@ -1,13 +1,16 @@ +#ifndef headerFile_h +#define headerFile_h + // include these libraries #include -#include -#include -#include "DHT.h" +// #include +// #include +// #include "DHT.h" #include #include // #include -#include -#include +// #include +// #include // define pins on esp32 #define MICPIN 6 @@ -27,12 +30,12 @@ // Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Adafruit_SH110X display = Adafruit_SH110X(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Adafruit_SH110X display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -DHT dht(DHTPIN, DHTTYPE); +// Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); +// DHT dht(DHTPIN, DHTTYPE); // WiFiMulti WiFiMulti; -Adafruit_SGP30 sgp; +// Adafruit_SGP30 sgp; // WebSocketsClient webSocket; // define variables @@ -47,4 +50,5 @@ unsigned long currentMillis; unsigned long lastMillis; bool errorSGP30 = false; bool errorDHT11 = false; -bool noise = false; \ No newline at end of file +bool noise = false; +#endif \ No newline at end of file diff --git a/arduino/node-code/nodeCodeFinal/nodeCodeFinal.ino b/arduino/node-code/nodeCodeFinal/nodeCodeFinal.ino index 6def471..ea12094 100644 --- a/arduino/node-code/nodeCodeFinal/nodeCodeFinal.ino +++ b/arduino/node-code/nodeCodeFinal/nodeCodeFinal.ino @@ -1,6 +1,8 @@ #include -#include -#include +#include "websockets.h" +// #include + + nodeReadings esp32Node; websockets webSocketClass; diff --git a/arduino/node-code/nodeCodeFinal/nodeCodeHeader.cpp b/arduino/node-code/nodeCodeFinal/nodeCodeHeader.cpp index 36dbd8a..c8e6bb3 100644 --- a/arduino/node-code/nodeCodeFinal/nodeCodeHeader.cpp +++ b/arduino/node-code/nodeCodeFinal/nodeCodeHeader.cpp @@ -1,41 +1,19 @@ -#include "arduino.h" +// #include #include "nodeCodeHeader.h" -#include +// #include +// #include -WebSocketsClient webSocket; nodeReadings::nodeReadings() { -} -void nodeReadings::setup(){ - // make serial connection at 115200 baud - Serial.begin(115200); + dht = new DHT(DHTPIN, DHTTYPE); + display = new Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + + webSocket = new WebSocketsClient(); + sgp = new Adafruit_SGP30(); - // tell display what settings to use - display.begin(i2c_adress, true); - display.clearDisplay(); + resetValues(); - // tell sensors to start reading - dht.begin(); - sgp.begin(); - - pinMode(MICPIN, INPUT); - pinMode(DHTPIN, INPUT); - -} - -void nodeReadings::loop() { - // loop the websocket connection so it stays alive - // webSocket.loop(); - - // update when interval is met - if (currentMillis - lastMillis >= interval){ - lastMillis = millis(); - update(); - } - - // update the counter - currentMillis = millis(); } void nodeReadings::resetValues() { @@ -51,20 +29,53 @@ void nodeReadings::resetValues() { noise = false; } +void nodeReadings::setup(){ + // DHT dht(DHTPIN, DHTTYPE); + // make serial connection at 115200 baud + Serial.begin(115200); + + // tell display what settings to use + display->begin(i2c_adress, true); + display->clearDisplay(); + + // tell sensors to start reading + dht->begin(); + sgp->begin(); + + pinMode(MICPIN, INPUT); + pinMode(DHTPIN, INPUT); + +} + +void nodeReadings::loop() { + // loop the websocket connection so it stays alive + // webSocket->loop(); + + // update when interval is met + if (currentMillis - lastMillis >= interval){ + lastMillis = millis(); + update(); + } + + // update the counter + currentMillis = millis(); +} + void nodeReadings::update(){ + // display sensordata on oled screen displayData(); - // webSocket.sendTXT("{\"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp.eCO2) + "\",\"TVOC\":\"" + String(sgp.TVOC) + "\"}"); - webSocket.sendTXT("{\"node\": \"" + String(WiFi.macAddress()) + "\", \"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp.eCO2) + "\",\"TVOC\":\"" + String(sgp.TVOC) + "\"}"); + // webSocket->sendTXT("{\"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp->eCO2) + "\",\"TVOC\":\"" + String(sgp->TVOC) + "\"}"); + webSocket->sendTXT("{\"node\": \"" + String(WiFi.macAddress()) + "\", \"Temp\":\"" + String(temperature) + "\",\"Humi\":\"" + String(humidity) + "\",\"eCO2\":\"" + String(sgp->eCO2) + "\",\"TVOC\":\"" + String(sgp->TVOC) + "\"}"); - sgp.getIAQBaseline(&eCO2_base, &TVOC_base); + sgp->getIAQBaseline(&eCO2_base, &TVOC_base); // read dht11 sensor - temperature = float(dht.readTemperature()); - humidity = float(dht.readHumidity()); + temperature = float(dht->readTemperature()); + humidity = float(dht->readHumidity()); // check if any errors occured when reading sensors checkForError(); @@ -72,21 +83,21 @@ void nodeReadings::update(){ void nodeReadings::displayData() { // clear display for new info - display.clearDisplay(); + display->clearDisplay(); // display the data on the oled screen - display.setTextSize(1); - display.setTextColor(SH110X_WHITE); - display.setCursor(0,0); - display.println("Temperature: " + String(temperature) + " C"); - display.println("Humidity: " + String(humidity) + " %"); - display.println("eCO2: " + String(sgp.eCO2) + " ppm"); - display.println("TVOC: " + String(sgp.TVOC) + " ppb"); - display.display(); + display->setTextSize(1); + display->setTextColor(SH110X_WHITE); + display->setCursor(0,0); + display->println("Temperature: " + String(temperature) + " C"); + display->println("Humidity: " + String(humidity) + " %"); + display->println("eCO2: " + String(sgp->eCO2) + " ppm"); + display->println("TVOC: " + String(sgp->TVOC) + " ppb"); + display->display(); } void nodeReadings::checkForError(){ - if (!sgp.IAQmeasure()) { + if (!sgp->IAQmeasure()) { Serial.println("SGP30: BAD"); errorSGP30 = true; } else { diff --git a/arduino/node-code/nodeCodeFinal/nodeCodeHeader.h b/arduino/node-code/nodeCodeFinal/nodeCodeHeader.h index 710bf20..76e9f2e 100644 --- a/arduino/node-code/nodeCodeFinal/nodeCodeHeader.h +++ b/arduino/node-code/nodeCodeFinal/nodeCodeHeader.h @@ -1,22 +1,60 @@ #ifndef nodeReading_h #define nodeReading_h -#include "Arduino.h" -#include "headerFile.h" +#include +// #include "Arduino.h" +#include +// #include "headerFile.h" +#include +#include +#include -class nodeReadings { +// define pins on esp32 +#define MICPIN 6 +#define DHTPIN 7 +#define SCL 9 +#define SDA 8 +#define DHTTYPE DHT11 +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define i2c_adress 0x3c +#define OLED_RESET -1 // QT-PY / XIAO - public: - nodeReadings(); - void setup(); - void loop(); - void resetValues(); - void update(); - void checkForError(); - void displayData(); +#define USE_SERIAL Serial - private: +// WebSocketsClient webSocket; + +class nodeReadings +{ + +public: + nodeReadings(); + void setup(); + void loop(); + void resetValues(); + void update(); + void checkForError(); + void displayData(); + +private: + DHT *dht; + Adafruit_SH1106G *display; + WebSocketsClient *webSocket; + Adafruit_SGP30 *sgp; + + uint16_t TVOC_base, eCO2_base; + uint16_t counter; + uint16_t eCO2; + uint16_t TVOC; + uint16_t interval; + float temperature; + float humidity; + unsigned long currentMillis; + unsigned long lastMillis; + bool errorSGP30; + bool errorDHT11; + bool noise; }; #endif \ No newline at end of file diff --git a/arduino/node-code/nodeCodeFinal/websockets.cpp b/arduino/node-code/nodeCodeFinal/websockets.cpp index d715a68..f8aee50 100644 --- a/arduino/node-code/nodeCodeFinal/websockets.cpp +++ b/arduino/node-code/nodeCodeFinal/websockets.cpp @@ -1,13 +1,12 @@ -#include "arduino.h" -#include -#include -#include +// #include "arduino.h" +// #include +// #include +// #include #include "websockets.h" -WebSocketsClient webSocket; -WiFiMulti WiFiMulti; - websockets::websockets(){ + webSocket = new WebSocketsClient(); + _WiFiMulti = new WiFiMulti(); } // hexdump function for websockets binary handler @@ -26,25 +25,25 @@ void websockets::hexdump(const void *mem, uint32_t len, uint8_t cols) { // special function to setup websocket void websockets::websocketSetup(){ - WiFiMulti.addAP("iotroam", "vbK9gbDBIB"); - WiFiMulti.addAP("LansanKPN-boven", "19sander71vlieland14"); + _WiFiMulti->addAP("iotroam", "vbK9gbDBIB"); + _WiFiMulti->addAP("LansanKPN-boven", "19sander71vlieland14"); - while(WiFiMulti.run() != WL_CONNECTED) { + while(_WiFiMulti->run() != WL_CONNECTED) { delay(100); } // server address, port and URL - webSocket.begin("145.92.8.114", 80, "/ws"); + webSocket->begin("145.92.8.114", 80, "/ws"); // event handler - // webSocket.onEvent(webSocketEvent); + // webSocket->onEvent(webSocketEvent); // try ever 500 again if connection has failed - webSocket.setReconnectInterval(500); + webSocket->setReconnectInterval(500); } void websockets::loop(){ - webSocket.loop(); + webSocket->loop(); } // handler for websocket events @@ -57,20 +56,20 @@ void websockets::webSocketEvent(WStype_t type, uint8_t * payload, size_t length) USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); // send message to server when Connected - webSocket.sendTXT("{\"message\": \"Connected\"}"); + webSocket->sendTXT("{\"message\": \"Connected\"}"); break; case WStype_TEXT: // USE_SERIAL.printf("[WSc] get text: %s\n", payload); // send message to server - // webSocket.sendTXT("message here"); + // webSocket->sendTXT("message here"); break; case WStype_BIN: // USE_SERIAL.printf("[WSc] get binary length: %u\n", length); hexdump(payload, length); // send data to server - // webSocket.sendBIN(payload, length); + // webSocket->sendBIN(payload, length); break; case WStype_ERROR: case WStype_FRAGMENT_TEXT_START: diff --git a/arduino/node-code/nodeCodeFinal/websockets.h b/arduino/node-code/nodeCodeFinal/websockets.h index 2483d88..f2b1c81 100644 --- a/arduino/node-code/nodeCodeFinal/websockets.h +++ b/arduino/node-code/nodeCodeFinal/websockets.h @@ -1,12 +1,16 @@ -#ifndef websockerts_h -#define websockerts_h +#ifndef websockets_h +#define websockets_h +#include +#include #include -#include "Arduino.h" +#include #define USE_SERIAL Serial // WiFiMulti WiFiMulti; +// WebSocketsClient webSocket; +// WiFiMulti WiFiMulti; class websockets { public: @@ -17,6 +21,8 @@ class websockets { void webSocketEvent(WStype_t type, uint8_t * payload, size_t length); private: + WebSocketsClient *webSocket; + WiFiMulti *_WiFiMulti; }; #endif \ No newline at end of file