OOP made in arduino 👍
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
#ifndef headerFile_h
|
||||
#define headerFile_h
|
||||
|
||||
// include these libraries
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_SH110X.h>
|
||||
#include <Adafruit_SGP30.h>
|
||||
#include "DHT.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>
|
||||
// #include <nodeCodeHeader.h>
|
||||
// #include <websockets.h>
|
||||
|
||||
// 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;
|
||||
bool noise = false;
|
||||
#endif
|
@@ -1,6 +1,8 @@
|
||||
#include <nodeCodeHeader.h>
|
||||
#include <websockets.h>
|
||||
#include <WebSocketsClient.h>
|
||||
#include "websockets.h"
|
||||
// #include <WebSocketsClient.h>
|
||||
|
||||
|
||||
|
||||
nodeReadings esp32Node;
|
||||
websockets webSocketClass;
|
||||
|
@@ -1,41 +1,19 @@
|
||||
#include "arduino.h"
|
||||
// #include <arduino.h>
|
||||
#include "nodeCodeHeader.h"
|
||||
#include <WebSocketsClient.h>
|
||||
// #include <DHT.h>
|
||||
// #include <Adafruit_SH110X.h>
|
||||
|
||||
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 {
|
||||
|
@@ -1,22 +1,60 @@
|
||||
#ifndef nodeReading_h
|
||||
#define nodeReading_h
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "headerFile.h"
|
||||
#include <Wire.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:
|
||||
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
|
@@ -1,13 +1,12 @@
|
||||
#include "arduino.h"
|
||||
#include <WebSocketsClient.h>
|
||||
#include <WiFiMulti.h>
|
||||
#include <WiFi.h>
|
||||
// #include "arduino.h"
|
||||
// #include <WebSocketsClient.h>
|
||||
// #include <WiFiMulti.h>
|
||||
// #include <WiFi.h>
|
||||
#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:
|
||||
|
@@ -1,12 +1,16 @@
|
||||
#ifndef websockerts_h
|
||||
#define websockerts_h
|
||||
#ifndef websockets_h
|
||||
#define websockets_h
|
||||
|
||||
#include <WiFiMulti.h>
|
||||
#include <WiFi.h>
|
||||
#include <WebSocketsClient.h>
|
||||
#include "Arduino.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
#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
|
Reference in New Issue
Block a user