moved websocket class to work with esp

This commit is contained in:
Dano van den Bosch
2024-03-19 22:54:58 +01:00
parent cf18d4ab4f
commit 11099a1af9
5 changed files with 27 additions and 11 deletions

View File

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

View File

@@ -9,7 +9,8 @@ nodeReadings::nodeReadings() {
dht = new DHT(DHTPIN, DHTTYPE); dht = new DHT(DHTPIN, DHTTYPE);
display = new Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); display = new Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
webSocket = new WebSocketsClient(); // webSocket = new WebSocketsClient();
webSocket = new websockets(); //nu naar eigen class
sgp = new Adafruit_SGP30(); sgp = new Adafruit_SGP30();
resetValues(); resetValues();
@@ -46,13 +47,15 @@ void nodeReadings::setup(){
pinMode(MICPIN, INPUT); pinMode(MICPIN, INPUT);
pinMode(DHTPIN, INPUT); pinMode(DHTPIN, INPUT);
webSocket->websocketSetup();
// webSocketClass.websocketSetup();
} }
void nodeReadings::loop() { void nodeReadings::loop() {
// loop the websocket connection so it stays alive
// webSocket->loop();
// update when interval is met // update when interval is met
if (currentMillis - lastMillis >= interval){ if (currentMillis - lastMillis >= interval){
lastMillis = millis(); lastMillis = millis();
@@ -61,6 +64,9 @@ void nodeReadings::loop() {
// update the counter // update the counter
currentMillis = millis(); currentMillis = millis();
// loop the websocket connection so it stays alive
webSocket->loop();
} }
void nodeReadings::update(){ void nodeReadings::update(){
@@ -69,7 +75,7 @@ void nodeReadings::update(){
displayData(); displayData();
// webSocket->sendTXT("{\"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) + "\"}"); webSocket->sendMyText("{\"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);

View File

@@ -7,8 +7,9 @@
#include <Adafruit_SH110X.h> #include <Adafruit_SH110X.h>
// #include "headerFile.h" // #include "headerFile.h"
#include <DHT.h> #include <DHT.h>
#include <WebSocketsClient.h> // #include <WebSocketsClient.h>
#include <Adafruit_SGP30.h> #include <Adafruit_SGP30.h>
#include "websockets.h"
// define pins on esp32 // define pins on esp32
#define MICPIN 6 #define MICPIN 6
@@ -40,7 +41,8 @@ public:
private: private:
DHT *dht; DHT *dht;
Adafruit_SH1106G *display; Adafruit_SH1106G *display;
WebSocketsClient *webSocket; // WebSocketsClient *webSocket;
websockets *webSocket;
Adafruit_SGP30 *sgp; Adafruit_SGP30 *sgp;
uint16_t TVOC_base, eCO2_base; uint16_t TVOC_base, eCO2_base;

View File

@@ -80,4 +80,11 @@ void websockets::webSocketEvent(WStype_t type, uint8_t * payload, size_t length)
} }
} }
void websockets::sendMyText(String message) {
webSocket->sendTXT(message);
}

View File

@@ -19,6 +19,7 @@ class websockets {
void websocketSetup(); void websocketSetup();
void loop(); void loop();
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length); void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
void sendMyText(String message);
private: private:
WebSocketsClient *webSocket; WebSocketsClient *webSocket;