cleans up code and fixes counter
This commit is contained in:
@@ -18,13 +18,16 @@
|
|||||||
#define SCREEN_WIDTH 128
|
#define SCREEN_WIDTH 128
|
||||||
#define SCREEN_HEIGHT 64
|
#define SCREEN_HEIGHT 64
|
||||||
#define i2c_adress 0x3c
|
#define i2c_adress 0x3c
|
||||||
|
#define USE_SERIAL Serial
|
||||||
|
|
||||||
|
// make new objects
|
||||||
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
|
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
|
||||||
DHT dht(DHTPIN, DHTTYPE);
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
WiFiMulti WiFiMulti;
|
WiFiMulti WiFiMulti;
|
||||||
Adafruit_SGP30 sgp;
|
Adafruit_SGP30 sgp;
|
||||||
WebSocketsClient webSocket;
|
WebSocketsClient webSocket;
|
||||||
|
|
||||||
|
// define variables
|
||||||
uint16_t TVOC_base, eCO2_base;
|
uint16_t TVOC_base, eCO2_base;
|
||||||
uint16_t counter = 0;
|
uint16_t counter = 0;
|
||||||
float temperature = 0;
|
float temperature = 0;
|
||||||
@@ -36,8 +39,7 @@ bool update = false;
|
|||||||
double counterBaseline = 15000;
|
double counterBaseline = 15000;
|
||||||
int counterInterval = 5000;
|
int counterInterval = 5000;
|
||||||
|
|
||||||
#define USE_SERIAL Serial
|
// hexdump function for websockets binary handler
|
||||||
|
|
||||||
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
|
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
|
||||||
const uint8_t* src = (const uint8_t*) mem;
|
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);
|
USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
|
||||||
@@ -51,8 +53,8 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
|
|||||||
USE_SERIAL.printf("\n");
|
USE_SERIAL.printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handler for websocket events
|
||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
||||||
@@ -86,15 +88,8 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void websocketSetup(){
|
void websocketSetup(){
|
||||||
for(uint8_t t = 1; t > 0; t--) {
|
|
||||||
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
|
|
||||||
USE_SERIAL.flush();
|
|
||||||
delay(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
|
WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
|
||||||
|
|
||||||
//WiFi.disconnect();
|
|
||||||
while(WiFiMulti.run() != WL_CONNECTED) {
|
while(WiFiMulti.run() != WL_CONNECTED) {
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
@@ -105,11 +100,8 @@ void websocketSetup(){
|
|||||||
// event handler
|
// event handler
|
||||||
webSocket.onEvent(webSocketEvent);
|
webSocket.onEvent(webSocketEvent);
|
||||||
|
|
||||||
// use HTTP Basic Authorization this is optional remove if not needed
|
// try ever 500 again if connection has failed
|
||||||
|
webSocket.setReconnectInterval(500);
|
||||||
|
|
||||||
// try ever 5000 again if connection has failed
|
|
||||||
webSocket.setReconnectInterval(5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetValues() {
|
void resetValues() {
|
||||||
@@ -121,6 +113,7 @@ void resetValues() {
|
|||||||
eCO2 = 0;
|
eCO2 = 0;
|
||||||
TVOC = 0;
|
TVOC = 0;
|
||||||
noise = false;
|
noise = false;
|
||||||
|
lastCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void displayData() {
|
void displayData() {
|
||||||
@@ -141,22 +134,6 @@ void displayData() {
|
|||||||
display.display();
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool wifiConnect(){
|
|
||||||
// while (WiFi.status() != WL_CONNECTED)
|
|
||||||
// {
|
|
||||||
// delay(250);
|
|
||||||
|
|
||||||
// wifiMulti.addAP("LansanKPN-boven", "19sander71vlieland14");
|
|
||||||
// wifiMulti.addAP("iotroam", "vbK9gbDBIB");
|
|
||||||
// Serial.println("Connectie maken...");
|
|
||||||
|
|
||||||
// wifiMulti.run();
|
|
||||||
// }
|
|
||||||
// Serial.println("Wifi Connected...");
|
|
||||||
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
@@ -183,6 +160,8 @@ void loop() {
|
|||||||
webSocket.sendTXT("Humi: " + String(humidity));
|
webSocket.sendTXT("Humi: " + String(humidity));
|
||||||
webSocket.sendTXT("eCO2: " + String(sgp.eCO2));
|
webSocket.sendTXT("eCO2: " + String(sgp.eCO2));
|
||||||
webSocket.sendTXT("TVOC: " + String(sgp.TVOC));
|
webSocket.sendTXT("TVOC: " + String(sgp.TVOC));
|
||||||
|
|
||||||
|
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if sensor doesnt work print in serialmonitor
|
// if sensor doesnt work print in serialmonitor
|
||||||
@@ -199,23 +178,11 @@ void loop() {
|
|||||||
|
|
||||||
// display sensordata on oled screen
|
// display sensordata on oled screen
|
||||||
displayData();
|
displayData();
|
||||||
|
|
||||||
// get new baseline every 30 seconds
|
|
||||||
if (update) {
|
|
||||||
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
|
|
||||||
}
|
|
||||||
|
|
||||||
counter = millis();
|
if (currentCounter - lastCounter >= 5000){
|
||||||
Serial.println("baseline" + String(counterBaseline));
|
lastcounter = millis()
|
||||||
Serial.println("counter" + String(counter));
|
update();
|
||||||
|
|
||||||
if (counter >= counterBaseline){
|
|
||||||
counterBaseline = counterBaseline + counterInterval;
|
|
||||||
|
|
||||||
update = true;
|
|
||||||
Serial.println("update");
|
|
||||||
} else {
|
|
||||||
update = false;
|
|
||||||
Serial.println("no update");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentCounter = millis();
|
||||||
}
|
}
|
Reference in New Issue
Block a user