OOP made in arduino 👍
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
|
#ifndef headerFile_h
|
||||||
|
#define headerFile_h
|
||||||
|
|
||||||
// include these libraries
|
// include these libraries
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <Adafruit_SH110X.h>
|
// #include <Adafruit_SH110X.h>
|
||||||
#include <Adafruit_SGP30.h>
|
// #include <Adafruit_SGP30.h>
|
||||||
#include "DHT.h"
|
// #include "DHT.h"
|
||||||
#include <WiFiMulti.h>
|
#include <WiFiMulti.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
// #include <WebSocketsClient.h>
|
// #include <WebSocketsClient.h>
|
||||||
#include <nodeCodeHeader.h>
|
// #include <nodeCodeHeader.h>
|
||||||
#include <websockets.h>
|
// #include <websockets.h>
|
||||||
|
|
||||||
// define pins on esp32
|
// define pins on esp32
|
||||||
#define MICPIN 6
|
#define MICPIN 6
|
||||||
@@ -27,12 +30,12 @@
|
|||||||
// Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
// 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 = Adafruit_SH110X(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
// Adafruit_SH110X display(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);
|
// Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
DHT dht(DHTPIN, DHTTYPE);
|
// DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
// WiFiMulti WiFiMulti;
|
// WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
Adafruit_SGP30 sgp;
|
// Adafruit_SGP30 sgp;
|
||||||
// WebSocketsClient webSocket;
|
// WebSocketsClient webSocket;
|
||||||
|
|
||||||
// define variables
|
// define variables
|
||||||
@@ -47,4 +50,5 @@ unsigned long currentMillis;
|
|||||||
unsigned long lastMillis;
|
unsigned long lastMillis;
|
||||||
bool errorSGP30 = false;
|
bool errorSGP30 = false;
|
||||||
bool errorDHT11 = false;
|
bool errorDHT11 = false;
|
||||||
bool noise = false;
|
bool noise = false;
|
||||||
|
#endif
|
@@ -1,6 +1,8 @@
|
|||||||
#include <nodeCodeHeader.h>
|
#include <nodeCodeHeader.h>
|
||||||
#include <websockets.h>
|
#include "websockets.h"
|
||||||
#include <WebSocketsClient.h>
|
// #include <WebSocketsClient.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
nodeReadings esp32Node;
|
nodeReadings esp32Node;
|
||||||
websockets webSocketClass;
|
websockets webSocketClass;
|
||||||
|
@@ -1,41 +1,19 @@
|
|||||||
#include "arduino.h"
|
// #include <arduino.h>
|
||||||
#include "nodeCodeHeader.h"
|
#include "nodeCodeHeader.h"
|
||||||
#include <WebSocketsClient.h>
|
// #include <DHT.h>
|
||||||
|
// #include <Adafruit_SH110X.h>
|
||||||
|
|
||||||
WebSocketsClient webSocket;
|
|
||||||
|
|
||||||
nodeReadings::nodeReadings() {
|
nodeReadings::nodeReadings() {
|
||||||
}
|
|
||||||
|
|
||||||
void nodeReadings::setup(){
|
dht = new DHT(DHTPIN, DHTTYPE);
|
||||||
// make serial connection at 115200 baud
|
display = new Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
Serial.begin(115200);
|
|
||||||
|
webSocket = new WebSocketsClient();
|
||||||
|
sgp = new Adafruit_SGP30();
|
||||||
|
|
||||||
// tell display what settings to use
|
resetValues();
|
||||||
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::resetValues() {
|
void nodeReadings::resetValues() {
|
||||||
@@ -51,20 +29,53 @@ void nodeReadings::resetValues() {
|
|||||||
noise = false;
|
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(){
|
void nodeReadings::update(){
|
||||||
|
|
||||||
// display sensordata on oled screen
|
// display sensordata on oled screen
|
||||||
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->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
|
// read dht11 sensor
|
||||||
temperature = float(dht.readTemperature());
|
temperature = float(dht->readTemperature());
|
||||||
humidity = float(dht.readHumidity());
|
humidity = float(dht->readHumidity());
|
||||||
|
|
||||||
// check if any errors occured when reading sensors
|
// check if any errors occured when reading sensors
|
||||||
checkForError();
|
checkForError();
|
||||||
@@ -72,21 +83,21 @@ void nodeReadings::update(){
|
|||||||
|
|
||||||
void nodeReadings::displayData() {
|
void nodeReadings::displayData() {
|
||||||
// clear display for new info
|
// clear display for new info
|
||||||
display.clearDisplay();
|
display->clearDisplay();
|
||||||
|
|
||||||
// display the data on the oled screen
|
// display the data on the oled screen
|
||||||
display.setTextSize(1);
|
display->setTextSize(1);
|
||||||
display.setTextColor(SH110X_WHITE);
|
display->setTextColor(SH110X_WHITE);
|
||||||
display.setCursor(0,0);
|
display->setCursor(0,0);
|
||||||
display.println("Temperature: " + String(temperature) + " C");
|
display->println("Temperature: " + String(temperature) + " C");
|
||||||
display.println("Humidity: " + String(humidity) + " %");
|
display->println("Humidity: " + String(humidity) + " %");
|
||||||
display.println("eCO2: " + String(sgp.eCO2) + " ppm");
|
display->println("eCO2: " + String(sgp->eCO2) + " ppm");
|
||||||
display.println("TVOC: " + String(sgp.TVOC) + " ppb");
|
display->println("TVOC: " + String(sgp->TVOC) + " ppb");
|
||||||
display.display();
|
display->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
void nodeReadings::checkForError(){
|
void nodeReadings::checkForError(){
|
||||||
if (!sgp.IAQmeasure()) {
|
if (!sgp->IAQmeasure()) {
|
||||||
Serial.println("SGP30: BAD");
|
Serial.println("SGP30: BAD");
|
||||||
errorSGP30 = true;
|
errorSGP30 = true;
|
||||||
} else {
|
} else {
|
||||||
|
@@ -1,22 +1,60 @@
|
|||||||
#ifndef nodeReading_h
|
#ifndef nodeReading_h
|
||||||
#define nodeReading_h
|
#define nodeReading_h
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include <Wire.h>
|
||||||
#include "headerFile.h"
|
|
||||||
|
|
||||||
|
// #include "Arduino.h"
|
||||||
|
#include <Adafruit_SH110X.h>
|
||||||
|
// #include "headerFile.h"
|
||||||
|
#include <DHT.h>
|
||||||
|
#include <WebSocketsClient.h>
|
||||||
|
#include <Adafruit_SGP30.h>
|
||||||
|
|
||||||
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:
|
#define USE_SERIAL Serial
|
||||||
nodeReadings();
|
|
||||||
void setup();
|
|
||||||
void loop();
|
|
||||||
void resetValues();
|
|
||||||
void update();
|
|
||||||
void checkForError();
|
|
||||||
void displayData();
|
|
||||||
|
|
||||||
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
|
#endif
|
@@ -1,13 +1,12 @@
|
|||||||
#include "arduino.h"
|
// #include "arduino.h"
|
||||||
#include <WebSocketsClient.h>
|
// #include <WebSocketsClient.h>
|
||||||
#include <WiFiMulti.h>
|
// #include <WiFiMulti.h>
|
||||||
#include <WiFi.h>
|
// #include <WiFi.h>
|
||||||
#include "websockets.h"
|
#include "websockets.h"
|
||||||
|
|
||||||
WebSocketsClient webSocket;
|
|
||||||
WiFiMulti WiFiMulti;
|
|
||||||
|
|
||||||
websockets::websockets(){
|
websockets::websockets(){
|
||||||
|
webSocket = new WebSocketsClient();
|
||||||
|
_WiFiMulti = new WiFiMulti();
|
||||||
}
|
}
|
||||||
|
|
||||||
// hexdump function for websockets binary handler
|
// 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
|
// special function to setup websocket
|
||||||
void websockets::websocketSetup(){
|
void websockets::websocketSetup(){
|
||||||
WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
|
_WiFiMulti->addAP("iotroam", "vbK9gbDBIB");
|
||||||
WiFiMulti.addAP("LansanKPN-boven", "19sander71vlieland14");
|
_WiFiMulti->addAP("LansanKPN-boven", "19sander71vlieland14");
|
||||||
|
|
||||||
while(WiFiMulti.run() != WL_CONNECTED) {
|
while(_WiFiMulti->run() != WL_CONNECTED) {
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// server address, port and URL
|
// server address, port and URL
|
||||||
webSocket.begin("145.92.8.114", 80, "/ws");
|
webSocket->begin("145.92.8.114", 80, "/ws");
|
||||||
|
|
||||||
// event handler
|
// event handler
|
||||||
// webSocket.onEvent(webSocketEvent);
|
// webSocket->onEvent(webSocketEvent);
|
||||||
|
|
||||||
// try ever 500 again if connection has failed
|
// try ever 500 again if connection has failed
|
||||||
webSocket.setReconnectInterval(500);
|
webSocket->setReconnectInterval(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
void websockets::loop(){
|
void websockets::loop(){
|
||||||
webSocket.loop();
|
webSocket->loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// handler for websocket events
|
// 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);
|
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
|
||||||
|
|
||||||
// send message to server when Connected
|
// send message to server when Connected
|
||||||
webSocket.sendTXT("{\"message\": \"Connected\"}");
|
webSocket->sendTXT("{\"message\": \"Connected\"}");
|
||||||
break;
|
break;
|
||||||
case WStype_TEXT:
|
case WStype_TEXT:
|
||||||
// USE_SERIAL.printf("[WSc] get text: %s\n", payload);
|
// USE_SERIAL.printf("[WSc] get text: %s\n", payload);
|
||||||
|
|
||||||
// send message to server
|
// send message to server
|
||||||
// webSocket.sendTXT("message here");
|
// webSocket->sendTXT("message here");
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
// USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
// USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
||||||
hexdump(payload, length);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send data to server
|
// send data to server
|
||||||
// webSocket.sendBIN(payload, length);
|
// webSocket->sendBIN(payload, length);
|
||||||
break;
|
break;
|
||||||
case WStype_ERROR:
|
case WStype_ERROR:
|
||||||
case WStype_FRAGMENT_TEXT_START:
|
case WStype_FRAGMENT_TEXT_START:
|
||||||
|
@@ -1,12 +1,16 @@
|
|||||||
#ifndef websockerts_h
|
#ifndef websockets_h
|
||||||
#define websockerts_h
|
#define websockets_h
|
||||||
|
|
||||||
|
#include <WiFiMulti.h>
|
||||||
|
#include <WiFi.h>
|
||||||
#include <WebSocketsClient.h>
|
#include <WebSocketsClient.h>
|
||||||
#include "Arduino.h"
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define USE_SERIAL Serial
|
#define USE_SERIAL Serial
|
||||||
// WiFiMulti WiFiMulti;
|
// WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
// WebSocketsClient webSocket;
|
||||||
|
// WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
class websockets {
|
class websockets {
|
||||||
public:
|
public:
|
||||||
@@ -17,6 +21,8 @@ class websockets {
|
|||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
WebSocketsClient *webSocket;
|
||||||
|
WiFiMulti *_WiFiMulti;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user