Almost done with the OOP

This commit is contained in:
Dano van den Bosch
2024-03-19 16:15:02 +01:00
parent a8f6491736
commit a4b8ae0dca
8 changed files with 114 additions and 27 deletions

View File

@@ -19,13 +19,19 @@
#define SCREEN_HEIGHT 64
#define i2c_adress 0x3c
#define OLED_RESET -1 // QT-PY / XIAO
#define USE_SERIAL Serial
// make new objects
// 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);
WiFiMulti WiFiMulti;
// WiFiMulti WiFiMulti;
Adafruit_SGP30 sgp;
// WebSocketsClient webSocket;

View File

@@ -1,19 +1,20 @@
#include <nodeCodeHeader.h>
#include <websockets.h>
#include <WebSocketsClient.h>
nodeReadings esp32Node();
websockets webSocket();
nodeReadings esp32Node;
websockets webSocketClass;
void setup()
{
void setup() {
// put your setup code here, to run once:
esp32Node.setup();
webSocket.websocketSetup();
webSocketClass.websocketSetup();
esp32Node.resetValues();
}
void loop()
{
void loop() {
// put your main code here, to run repeatedly:
esp32Node.loop();
webSocketClass.loop();
}

View File

@@ -1,5 +1,8 @@
#include "arduino.h"
#include "nodeCodeHeader.h"
#include <WebSocketsClient.h>
WebSocketsClient webSocket;
nodeReadings::nodeReadings() {
}
@@ -23,7 +26,7 @@ void nodeReadings::setup(){
void nodeReadings::loop() {
// loop the websocket connection so it stays alive
webSocket.loop();
// webSocket.loop();
// update when interval is met
if (currentMillis - lastMillis >= interval){
@@ -65,4 +68,37 @@ void nodeReadings::update(){
// check if any errors occured when reading sensors
checkForError();
}
void nodeReadings::displayData() {
// clear display for new info
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();
}
void nodeReadings::checkForError(){
if (!sgp.IAQmeasure()) {
Serial.println("SGP30: BAD");
errorSGP30 = true;
} else {
Serial.println("SGP30: OK");
errorSGP30 = false;
}
if (isnan(temperature) || isnan(humidity)){
Serial.println("DHT11: BAD");
errorDHT11 = true;
} else {
Serial.println("DHT11: OK");
errorDHT11 = false;
}
}

View File

@@ -13,7 +13,8 @@ class nodeReadings {
void loop();
void resetValues();
void update();
void checkForError();
void displayData();
private:
};

View File

@@ -1,8 +1,17 @@
#include "arduino.h"
#include <WebSocketsClient.h>
#include <WiFiMulti.h>
#include <WiFi.h>
#include "websockets.h"
WebSocketsClient webSocket;
WiFiMulti WiFiMulti;
websockets::websockets(){
}
// hexdump function for websockets binary handler
void websockets::hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
void websockets::hexdump(const void *mem, uint32_t len, uint8_t cols) {
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);
for(uint32_t i = 0; i < len; i++) {
@@ -28,8 +37,48 @@ void websockets::websocketSetup(){
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);
}
void websockets::loop(){
webSocket.loop();
}
// handler for websocket events
void websockets::webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when 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");
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);
break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
break;
}
}

View File

@@ -1,13 +1,21 @@
#ifndef websockerts_h
#define websockerts_h
#include <WebSocketsClient.h>
#include "Arduino.h"
#include "websocketsHeader.h"
#define USE_SERIAL Serial
// WiFiMulti WiFiMulti;
class websockets {
public:
websockets();
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16);
void websocketSetup();
void loop();
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
private:
};

View File

@@ -1,14 +0,0 @@
#include <Wire.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_SGP30.h>
#include "DHT.h"
#include <WiFiMulti.h>
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <nodeCodeHeader.h>
#include <websockets.h>
// WebSocketsClient webSocket;