Code cleanup

This commit is contained in:
Admar Schoonen 2022-05-20 20:49:07 +02:00
parent 67760a854c
commit 64e4e6f5e4
1 changed files with 0 additions and 395 deletions

View File

@ -10,48 +10,10 @@
#include <Adafruit_NeoPixel.h>
#include <math.h>
#include "PMS.h" // PMS Library from Mariusz Kacki https://github.com/fu-hsi/pms
#include "ClosedCube_HDC1080.h" // ClosedCube_HDC1080 from closedcube https://github.com/closedcube/ClosedCube_HDC1080_Arduino
#include <Adafruit_BMP280.h> // BMP280 from Adafruit https://github.com/adafruit/Adafruit_BMP280_Library
#include "Adafruit_CCS811.h" // CCS811 from Adafruit https://github.com/adafruit/Adafruit_CCS811
#define PMSA003_RESET 18
#define PMSA003_RX 17
#define PMSA003_TX 16
#define PMSA003_SET 19
ClosedCube_HDC1080 hdc1080;
Adafruit_BMP280 bmp;
Adafruit_CCS811 ccs;
PMS pms(Serial2);
PMS::DATA PMSA003_data;
typedef enum {
PMS_state_sleeping,
PMS_state_stabilizing,
PMS_state_ready
} PMS_state_t;
PMS_state_t PMS_state;
unsigned long CCS811_new_state_time = 0;
#define NUM_LEDS 93
//#define NUM_LEDS 10
#define DATA_PIN 5
#define PMSA003_RESET 18
#define PMSA003_RX 17
#define PMSA003_TX 16
#define PMSA003_SET 19
#define BUIENRADAR_START_LED 32
#define BUIENRADAR_SKIP_LED 1
#define BUIENRADAR_NUM_LEDS 12
@ -246,47 +208,6 @@ void setup() {
Serial.print("dESPatch.configure() returned with code ");
Serial.println(x);
#endif
hdc1080.begin(0x40);
Serial.print("HDC1080 Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("HDC1080 Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
if (!bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID)) {
Serial.println(F("Could not find a valid BMP280 sensor. Check wiring or try different address."));
while (1) {
delay(10);
}
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
if (!ccs.begin()) {
Serial.println("Could not find CSS811 sensor.");
while (1) {
delay(10);
}
ccs.setDriveMode(CCS811_DRIVE_MODE_IDLE); // wait for at least 10 minutes before entering a new state!
CCS811_new_state_time = millis() + 600000;
// Wait for the sensor to be ready
while(!ccs.available());
}
Serial2.begin(9600, SERIAL_8N1, PMSA003_TX, PMSA003_RX); // arg 3: ESP32 RX pin, arg 4: ESP32 TX pin
pms.passiveMode();
Serial.println("Waking up and waiting 30 seconds for stable readings...");
pms.wakeUp();
PMS_state = PMS_state_stabilizing;
}
void interpolateValues(Adafruit_NeoPixel * leds, int numLeds, int startLed, int skipLed, \
@ -333,39 +254,6 @@ void interpolateValues(Adafruit_NeoPixel * leds, int numLeds, int startLed, int
}
}
/*
void getJson(const float * location) {
int httpCode;
//String url = baseUrl + "&lat=" + String(location[0]) + "&lon=" + String(location[1]);
String url, latS, lonS;
int tmp1, tmp2;
String payload = "";
tmp1 = int(location[0]);
tmp2 = int((location[0] - tmp1) * 1000000);
latS = String(tmp1) + "." + String(tmp2);
tmp1 = int(location[1]);
tmp2 = int((location[1] - tmp1) * 1000000);
lonS = String(tmp1) + "." + String(tmp2);
url = baseUrl + "&lat=" + latS + "&lon=" + lonS;
Serial.println(url);
http.begin(url);
httpCode = http.GET();
if (httpCode > 0) {
payload = http.getString();
parseJson(&payload);
} else {
Serial.print(__func__);
Serial.print("(): got http code ");
Serial.println(httpCode);
}
http.end();
}*/
// From https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/
String urlencode(String str)
{
@ -593,109 +481,6 @@ static float buienradarMap(float x) {
return y;
}
static int calculateIAQI(void) {
int IAQI[3] = {0};
int result = 0;
bool values_equal = true;
uint16_t eCO2 = ccs.geteCO2();
if (eCO2 < 400) {
IAQI[0] = 0;
} else if (eCO2 < 600) {
IAQI[0] = 1;
} else if (eCO2 < 800) {
IAQI[0] = 2;
} else if (eCO2 < 1500) {
IAQI[0] = 4;
} else if (eCO2 < 1800) {
IAQI[0] = 7;
} else if (eCO2 < 2500) {
IAQI[0] = 9;
} else {
IAQI[0] = 10;
}
if (PMSA003_data.PM_AE_UG_2_5 < 10) {
IAQI[1] = 0;
} else if (PMSA003_data.PM_AE_UG_2_5 < 15) {
IAQI[1] = 1;
} else if (PMSA003_data.PM_AE_UG_2_5 < 20) {
IAQI[1] = 2;
} else if (PMSA003_data.PM_AE_UG_2_5 < 30) {
IAQI[1] = 3;
} else if (PMSA003_data.PM_AE_UG_2_5 < 40) {
IAQI[1] = 4;
} else if (PMSA003_data.PM_AE_UG_2_5 < 50) {
IAQI[1] = 5;
} else if (PMSA003_data.PM_AE_UG_2_5 < 70) {
IAQI[1] = 6;
} else if (PMSA003_data.PM_AE_UG_2_5 < 90) {
IAQI[1] = 7;
} else if (PMSA003_data.PM_AE_UG_2_5 < 100) {
IAQI[1] = 8;
} else if (PMSA003_data.PM_AE_UG_2_5 < 140) {
IAQI[1] = 9;
} else {
IAQI[1] = 10;
}
if (PMSA003_data.PM_AE_UG_10_0 < 10) {
IAQI[1] = 0;
} else if (PMSA003_data.PM_AE_UG_10_0 < 20) {
IAQI[1] = 1;
} else if (PMSA003_data.PM_AE_UG_10_0 < 30) {
IAQI[1] = 2;
} else if (PMSA003_data.PM_AE_UG_10_0 < 45) {
IAQI[1] = 3;
} else if (PMSA003_data.PM_AE_UG_10_0 < 60) {
IAQI[1] = 4;
} else if (PMSA003_data.PM_AE_UG_10_0 < 75) {
IAQI[1] = 5;
} else if (PMSA003_data.PM_AE_UG_10_0 < 100) {
IAQI[1] = 6;
} else if (PMSA003_data.PM_AE_UG_10_0 < 125) {
IAQI[1] = 7;
} else if (PMSA003_data.PM_AE_UG_10_0 < 150) {
IAQI[1] = 8;
} else if (PMSA003_data.PM_AE_UG_10_0 < 200) {
IAQI[1] = 9;
} else {
IAQI[1] = 10;
}
result = 0;
for (int n = 0; n < sizeof(IAQI) / sizeof(IAQI[0]); n++) {
if (result < IAQI[n]) {
result = IAQI[n];
}
}
for (int n = 1; n < sizeof(IAQI) / sizeof(IAQI[0]); n++) {
if (IAQI[n] != IAQI[0]) {
values_equal = false;
break;
}
}
if ((values_equal) && (IAQI[0] != 0)) {
result = result + 1;
}
/*
Serial.print("IAQI data:");
for (int n = 0; n < sizeof(IAQI) / sizeof(IAQI[0]); n++) {
Serial.print(" ");
Serial.print(IAQI[n]);
}
Serial.println("");
Serial.print("IAQI score: ");
Serial.println(result);*/
return result;
}
static void updateLedsNoWifi(void) {
uint32_t ledIdx;
uint32_t ledCount;
@ -859,179 +644,12 @@ static void updateLeds(void) {
r = (color & 0xFF0000) >> 16;
g = (color & 0x00FF00) >> 8;
b = (color & 0x0000FF);
/*Serial.print("setting led ");
Serial.print(ledIdx);
Serial.print(" to value 0x");
Serial.println(color, HEX);*/
leds.setPixelColor(ledIdx, leds.Color(r, g, b));
}
color = colormap(calculateIAQI());
r = (color & 0xFF0000) >> 16;
g = (color & 0x00FF00) >> 8;
b = (color & 0x0000FF);
/* Serial.print("setting led ");
Serial.print(IAQI_LED);
Serial.print(" to value 0x");
Serial.println(color, HEX);*/
leds.setPixelColor(IAQI_LED, leds.Color(r, g, b));
}
void readSensors() {
static unsigned long t_start = 0;
static unsigned long bmp_time = 0;
static unsigned long hdc1080_time = 0;
static unsigned long ccs811_time = 0;
unsigned long t_now;
uint32_t pms_counter = 0;
double hdc1080_T_raw = -400;
double hdc1080_RH_raw = -400;
double ccs811_T_raw = -400;
t_now = millis();
switch (PMS_state) {
case PMS_state_sleeping:
if (t_now - t_start >= 300000) {
Serial.println("Waking up and waiting 30 seconds for stable readings...");
pms.wakeUp();
PMS_state = PMS_state_stabilizing;
t_start = t_now;
}
break;
case PMS_state_stabilizing:
if (t_now - t_start >= 30000) {
Serial.println("Sensor should be stable now...");
PMS_state = PMS_state_ready;
}
break;
case PMS_state_ready:
Serial.println("Reading sensor...");
pms.requestRead();
do {
if (pms.readUntil(PMSA003_data)) {
Serial.print("PM 1.0 (ug/m3): ");
Serial.println(PMSA003_data.PM_AE_UG_1_0);
Serial.print("PM 2.5 (ug/m3): ");
Serial.println(PMSA003_data.PM_AE_UG_2_5);
Serial.print("PM 10.0 (ug/m3): ");
Serial.println(PMSA003_data.PM_AE_UG_10_0);
Serial.println();
break;
} else {
Serial.println("No data :(");
pms_counter++;
if (pms_counter > 10) {
break;
}
}
} while(1);
Serial.println("Sleeping for ~ 270 seconds...");
pms.sleep();
PMS_state = PMS_state_sleeping;
break;
default:
break;
}
if (t_now - bmp_time > 5000) {
double bmp280_T_raw = bmp.readTemperature();
double bmp280_T_corrected = bmp280_T_raw - (25.5 - 18.4);
Serial.print(F("BMP280: temperature = "));
Serial.print(bmp280_T_raw);
Serial.print(", ");
Serial.print(bmp280_T_corrected);
Serial.println(" *C");
Serial.print(F("BMP280: pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("BMP280: approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
bmp_time = t_now;
}
if (t_now - hdc1080_time > 5000) {
hdc1080_T_raw = hdc1080.readTemperature();
hdc1080_RH_raw = hdc1080.readHumidity();
// Both HDC1080 and BMP280 temperature readings are off by ~ 7 degrees
double hdc1080_T_corrected = hdc1080_T_raw - (25.5 - 18.4);
/*
* Re-calculate humidity based on corrected temperature; formulas from
* https://www.thecalculator.co/others/Relative-Humidity-Calculator-682.html
*/
// double T_dew = (pow(hdc1080_RH_raw/100, 1/8)*(112+0.9*hdc1080_T_raw) )+(0.1*hdc1080_T_raw)-112;
// double hdc1080_RH_corrected = 100*((112-0.1*hdc1080_T_corrected+T_dew)/(112+0.9*hdc1080_T_corrected));
// http://hyperphysics.phy-astr.gsu.edu/hbase/Kinetic/relhum.html
double VD_sat_raw = 5.018 + 0.32321 * hdc1080_T_raw +
0.0081847 * hdc1080_T_raw * hdc1080_T_raw +
0.00031243 * hdc1080_T_raw * hdc1080_T_raw * hdc1080_T_raw;
double VD_act = hdc1080_RH_raw * VD_sat_raw / 100;
double VD_sat_corrected = 5.018 + 0.32321 * hdc1080_T_corrected +
0.0081847 * hdc1080_T_corrected * hdc1080_T_corrected +
0.00031243 * hdc1080_T_corrected * hdc1080_T_corrected * hdc1080_T_corrected;
// double hdc1080_RH_corrected = VD_act / VD_sat_corrected * 100;
double hdc1080_RH_corrected = (hdc1080_RH_raw + VD_act / VD_sat_corrected * 100) / 2;
Serial.print("HDC1080: temperature = ");
Serial.print(hdc1080_T_raw);
Serial.print(", ");
Serial.print(hdc1080_T_corrected);
Serial.println(" *C");
Serial.print("HDC1080: RH = ");
Serial.print(hdc1080_RH_raw);
Serial.print(", ");
Serial.print(hdc1080_RH_corrected);
Serial.println(" %");
//ccs.setEnvironmentalData(hdc1080_RH_corrected, hdc1080_T_corrected);
hdc1080_time = t_now;
}
if ((CCS811_new_state_time != 0) && (CCS811_new_state_time < millis())) {
Serial.println("");
Serial.println("SETTING CCS811 TO DRIVE MODE 10 SEC");
Serial.println("");
ccs.setDriveMode(CCS811_DRIVE_MODE_10SEC); // wait for at least 10 minutes before entering a new state!
CCS811_new_state_time = 0;
}
if ((ccs.available()) && (t_now - ccs811_time > 5000)) {
if (!ccs.readData()) {
Serial.print("CCS811: eCO2 = ");
Serial.print(ccs.geteCO2());
Serial.println("ppm");
Serial.print("CCS811: eTVOC = ");
Serial.println(ccs.getTVOC());
} else {
Serial.println("Error reading CCS811 data");
}
Serial.println();
ccs811_time = t_now;
}
}
void loop(void) {
static unsigned long t_prev = 0;
static bool ledOn = false;
@ -1045,9 +663,6 @@ void loop(void) {
if (firstTime || (t_now - t_prev >= 60000)) {
t_prev = t_now;
firstTime = false;
//buienradar((float *) location, 12, rainColor);
//luchtmeetnet((float *) location, 12, AQIColor, gmtOffset_sec, daylightOffset_sec);
#ifndef DISABLE_WIFI
getPrecipitation(address);
@ -1063,18 +678,8 @@ void loop(void) {
counter = 0;
}
#endif
/*Serial.println("precipitation:");
for (int n = 0; n < rainpickerData.precipitation.len; n++) {
Serial.print(" ");
Serial.print(rainpickerData.precipitation.time[n]);
Serial.print(": ");
Serial.println(rainpickerData.precipitation.value[n]);
}*/
}
//readSensors();
#ifdef DISABLE_WIFI
updateLedsNoWifi();
#else