2024-02-03 10:46:26 +07:00
|
|
|
/*
|
2024-02-06 10:41:10 +07:00
|
|
|
This is the code for the AirGradient DIY BASIC Air Quality Monitor with an D1
|
|
|
|
ESP8266 Microcontroller.
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-06 10:41:10 +07:00
|
|
|
It is an air quality monitor for PM2.5, CO2, Temperature and Humidity with a
|
|
|
|
small display and can send data over Wifi.
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
Open source air quality monitors and kits are available:
|
|
|
|
Indoor Monitor: https://www.airgradient.com/indoor/
|
|
|
|
Outdoor Monitor: https://www.airgradient.com/outdoor/
|
|
|
|
|
|
|
|
Build Instructions:
|
|
|
|
https://www.airgradient.com/documentation/diy-v4/
|
|
|
|
|
|
|
|
Following libraries need to be installed:
|
|
|
|
“WifiManager by tzapu, tablatronix” tested with version 2.0.16-rc.2
|
|
|
|
"Arduino_JSON" by Arduino version 0.2.0
|
|
|
|
"U8g2" by oliver version 2.34.22
|
|
|
|
|
2024-02-06 10:41:10 +07:00
|
|
|
Please make sure you have esp8266 board manager installed. Tested with
|
|
|
|
version 3.1.2.
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
Set board to "LOLIN(WEMOS) D1 R2 & mini"
|
|
|
|
|
2024-02-06 10:41:10 +07:00
|
|
|
Configuration parameters, e.g. Celsius / Fahrenheit or PM unit (US AQI vs ug/m3)
|
|
|
|
can be set through the AirGradient dashboard.
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
If you have any questions please visit our forum at
|
|
|
|
https://forum.airgradient.com/
|
|
|
|
|
|
|
|
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AirGradient.h>
|
|
|
|
#include <Arduino_JSON.h>
|
|
|
|
#include <ESP8266HTTPClient.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WiFiClient.h>
|
|
|
|
#include <WiFiManager.h>
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
#define WIFI_CONNECT_COUNTDOWN_MAX 180 /** sec */
|
|
|
|
#define WIFI_CONNECT_RETRY_MS 10000 /** ms */
|
|
|
|
#define LED_BAR_COUNT_INIT_VALUE (-1) /** */
|
|
|
|
#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
|
|
|
|
#define DISP_UPDATE_INTERVAL 5000 /** ms */
|
|
|
|
#define SERVER_CONFIG_UPDATE_INTERVAL 30000 /** ms */
|
|
|
|
#define SERVER_SYNC_INTERVAL 60000 /** ms */
|
|
|
|
#define SENSOR_CO2_CALIB_COUNTDOWN_MAX 5 /** sec */
|
|
|
|
#define SENSOR_TVOC_UPDATE_INTERVAL 1000 /** ms */
|
|
|
|
#define SENSOR_CO2_UPDATE_INTERVAL 5000 /** ms */
|
|
|
|
#define SENSOR_PM_UPDATE_INTERVAL 5000 /** ms */
|
2024-02-29 14:00:19 +07:00
|
|
|
#define SENSOR_TEMP_HUM_UPDATE_INTERVAL 2000 /** ms */
|
2024-02-16 13:39:33 +07:00
|
|
|
#define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */
|
2024-02-04 15:04:38 +07:00
|
|
|
#define WIFI_HOTSPOT_PASSWORD_DEFAULT \
|
|
|
|
"cleanair" /** default WiFi AP password \
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Use use LED bar state
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
UseLedBarOff, /** Don't use LED bar */
|
|
|
|
UseLedBarPM, /** Use LED bar for PMS */
|
|
|
|
UseLedBarCO2, /** Use LED bar for CO2 */
|
|
|
|
} UseLedBar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Schedule handle with timing period
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class AgSchedule {
|
|
|
|
public:
|
|
|
|
AgSchedule(int period, void (*handler)(void))
|
|
|
|
: period(period), handler(handler) {}
|
|
|
|
void run(void) {
|
|
|
|
uint32_t ms = (uint32_t)(millis() - count);
|
|
|
|
if (ms >= period) {
|
|
|
|
/** Call handler */
|
|
|
|
handler();
|
|
|
|
|
2024-02-17 12:47:51 +07:00
|
|
|
// Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
|
|
|
// (unsigned int)handler, period);
|
2024-02-04 15:04:38 +07:00
|
|
|
|
|
|
|
/** Update period time */
|
|
|
|
count = millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void (*handler)(void);
|
|
|
|
int period;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief AirGradient server configuration and sync data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class AgServer {
|
|
|
|
public:
|
|
|
|
void begin(void) {
|
|
|
|
inF = false;
|
|
|
|
inUSAQI = false;
|
|
|
|
configFailed = false;
|
|
|
|
serverFailed = false;
|
|
|
|
memset(models, 0, sizeof(models));
|
|
|
|
memset(mqttBroker, 0, sizeof(mqttBroker));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get server configuration
|
|
|
|
*
|
|
|
|
* @param id Device ID
|
|
|
|
* @return true Success
|
|
|
|
* @return false Failure
|
|
|
|
*/
|
2024-02-29 15:20:19 +07:00
|
|
|
bool fetchServerConfiguration(String id) {
|
2024-02-04 15:04:38 +07:00
|
|
|
String uri =
|
|
|
|
"http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config";
|
|
|
|
|
|
|
|
/** Init http client */
|
|
|
|
WiFiClient wifiClient;
|
|
|
|
HTTPClient client;
|
|
|
|
if (client.begin(wifiClient, uri) == false) {
|
|
|
|
configFailed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get */
|
|
|
|
int retCode = client.GET();
|
|
|
|
if (retCode != 200) {
|
|
|
|
client.end();
|
|
|
|
configFailed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** clear failed */
|
|
|
|
configFailed = false;
|
|
|
|
|
|
|
|
/** Get response string */
|
|
|
|
String respContent = client.getString();
|
|
|
|
client.end();
|
|
|
|
Serial.println("Get server config: " + respContent);
|
|
|
|
|
|
|
|
/** Parse JSON */
|
|
|
|
JSONVar root = JSON.parse(respContent);
|
|
|
|
if (JSON.typeof(root) == "undefined") {
|
|
|
|
/** JSON invalid */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get "country" */
|
|
|
|
if (JSON.typeof_(root["country"]) == "string") {
|
|
|
|
String country = root["country"];
|
|
|
|
if (country == "US") {
|
|
|
|
inF = true;
|
|
|
|
} else {
|
|
|
|
inF = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:48:58 +07:00
|
|
|
/** Get "pmStandard" */
|
|
|
|
if (JSON.typeof_(root["pmStandard"]) == "string") {
|
|
|
|
String standard = root["pmStandard"];
|
2024-02-04 15:04:38 +07:00
|
|
|
if (standard == "ugm3") {
|
|
|
|
inUSAQI = false;
|
|
|
|
} else {
|
|
|
|
inUSAQI = true;
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/** Get "co2CalibrationRequested" */
|
|
|
|
if (JSON.typeof_(root["co2CalibrationRequested"]) == "boolean") {
|
|
|
|
co2Calib = root["co2CalibrationRequested"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get "ledBarMode" */
|
|
|
|
if (JSON.typeof_(root["ledBarMode"]) == "string") {
|
|
|
|
String mode = root["ledBarMode"];
|
|
|
|
if (mode == "co2") {
|
|
|
|
ledBarMode = UseLedBarCO2;
|
|
|
|
} else if (mode == "pm") {
|
|
|
|
ledBarMode = UseLedBarPM;
|
|
|
|
} else if (mode == "off") {
|
|
|
|
ledBarMode = UseLedBarOff;
|
|
|
|
} else {
|
|
|
|
ledBarMode = UseLedBarOff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get model */
|
|
|
|
if (JSON.typeof_(root["model"]) == "string") {
|
|
|
|
String model = root["model"];
|
|
|
|
if (model.length()) {
|
|
|
|
int len =
|
|
|
|
model.length() < sizeof(models) ? model.length() : sizeof(models);
|
|
|
|
memset(models, 0, sizeof(models));
|
|
|
|
memcpy(models, model.c_str(), len);
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/** Get "mqttBrokerUrl" */
|
|
|
|
if (JSON.typeof_(root["mqttBrokerUrl"]) == "string") {
|
|
|
|
String mqtt = root["mqttBrokerUrl"];
|
|
|
|
if (mqtt.length()) {
|
|
|
|
int len = mqtt.length() < sizeof(mqttBroker) ? mqtt.length()
|
|
|
|
: sizeof(mqttBroker);
|
|
|
|
memset(mqttBroker, 0, sizeof(mqttBroker));
|
|
|
|
memcpy(mqttBroker, mqtt.c_str(), len);
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-06 10:41:10 +07:00
|
|
|
/** Get 'abcDays' */
|
|
|
|
if (JSON.typeof_(root["abcDays"]) == "number") {
|
|
|
|
co2AbcCalib = root["abcDays"];
|
|
|
|
} else {
|
|
|
|
co2AbcCalib = -1;
|
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/** Show configuration */
|
|
|
|
showServerConfig();
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool postToServer(String id, String payload) {
|
|
|
|
/**
|
|
|
|
* @brief Only post data if WiFi is connected
|
|
|
|
*/
|
|
|
|
if (WiFi.isConnected() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.printf("Post payload: %s\r\n", payload.c_str());
|
|
|
|
|
|
|
|
String uri =
|
|
|
|
"http://hw.airgradient.com/sensors/airgradient:" + id + "/measures";
|
|
|
|
|
|
|
|
WiFiClient wifiClient;
|
|
|
|
HTTPClient client;
|
|
|
|
if (client.begin(wifiClient, uri.c_str()) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
client.addHeader("content-type", "application/json");
|
|
|
|
int retCode = client.POST(payload);
|
|
|
|
client.end();
|
|
|
|
|
|
|
|
if ((retCode == 200) || (retCode == 429)) {
|
|
|
|
serverFailed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
serverFailed = true;
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get temperature configuration unit
|
|
|
|
*
|
|
|
|
* @return true F unit
|
|
|
|
* @return false C Unit
|
|
|
|
*/
|
|
|
|
bool isTemperatureUnitF(void) { return inF; }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get PMS standard unit
|
|
|
|
*
|
|
|
|
* @return true USAQI
|
|
|
|
* @return false ugm3
|
|
|
|
*/
|
|
|
|
bool isPMSinUSAQI(void) { return inUSAQI; }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
2024-02-17 13:02:24 +07:00
|
|
|
* @brief Get status of get server configuration is failed
|
2024-02-04 15:04:38 +07:00
|
|
|
*
|
|
|
|
* @return true Failed
|
|
|
|
* @return false Success
|
|
|
|
*/
|
|
|
|
bool isConfigFailed(void) { return configFailed; }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get status of post server configuration is failed
|
|
|
|
*
|
|
|
|
* @return true Failed
|
|
|
|
* @return false Success
|
|
|
|
*/
|
|
|
|
bool isServerFailed(void) { return serverFailed; }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get request calibration CO2
|
|
|
|
*
|
|
|
|
* @return true Requested. If result = true, it's clear after function call
|
|
|
|
* @return false Not-requested
|
|
|
|
*/
|
|
|
|
bool isCo2Calib(void) {
|
|
|
|
bool ret = co2Calib;
|
|
|
|
if (ret) {
|
|
|
|
co2Calib = false;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-06 10:41:10 +07:00
|
|
|
/**
|
|
|
|
* @brief Get the Co2 auto calib period
|
|
|
|
*
|
|
|
|
* @return int days, -1 if invalid.
|
|
|
|
*/
|
2024-02-17 12:04:11 +07:00
|
|
|
int getCo2AbcDaysConfig(void) { return co2AbcCalib; }
|
2024-02-06 10:41:10 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get device configuration model name
|
|
|
|
*
|
|
|
|
* @return String Model name, empty string if server failed
|
|
|
|
*/
|
|
|
|
String getModelName(void) { return String(models); }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get mqttBroker url
|
|
|
|
*
|
|
|
|
* @return String Broker url, empty if server failed
|
|
|
|
*/
|
|
|
|
String getMqttBroker(void) { return String(mqttBroker); }
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Show server configuration parameter
|
|
|
|
*/
|
|
|
|
void showServerConfig(void) {
|
|
|
|
Serial.println("Server configuration: ");
|
2024-02-06 10:41:10 +07:00
|
|
|
Serial.printf(" inF: %s\r\n", inF ? "true" : "false");
|
|
|
|
Serial.printf(" inUSAQI: %s\r\n", inUSAQI ? "true" : "false");
|
|
|
|
Serial.printf(" useRGBLedBar: %d\r\n", (int)ledBarMode);
|
|
|
|
Serial.printf(" Model: %s\r\n", models);
|
|
|
|
Serial.printf(" Mqtt Broker: %s\r\n", mqttBroker);
|
|
|
|
Serial.printf(" S8 calib period: %d\r\n", co2AbcCalib);
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/**
|
|
|
|
* @brief Get server config led bar mode
|
|
|
|
*
|
|
|
|
* @return UseLedBar
|
|
|
|
*/
|
|
|
|
UseLedBar getLedBarMode(void) { return ledBarMode; }
|
|
|
|
|
|
|
|
private:
|
2024-02-06 10:41:10 +07:00
|
|
|
bool inF; /** Temperature unit, true: F, false: C */
|
|
|
|
bool inUSAQI; /** PMS unit, true: USAQI, false: ugm3 */
|
|
|
|
bool configFailed; /** Flag indicate get server configuration failed */
|
|
|
|
bool serverFailed; /** Flag indicate post data to server failed */
|
|
|
|
bool co2Calib; /** Is co2Ppmcalibration requset */
|
|
|
|
int co2AbcCalib = -1; /** update auto calibration number of day */
|
2024-02-04 15:04:38 +07:00
|
|
|
UseLedBar ledBarMode = UseLedBarCO2; /** */
|
|
|
|
char models[20]; /** */
|
|
|
|
char mqttBroker[256]; /** */
|
|
|
|
};
|
|
|
|
AgServer agServer;
|
|
|
|
|
|
|
|
/** Create airgradient instance for 'DIY_BASIC' board */
|
|
|
|
AirGradient ag = AirGradient(DIY_BASIC);
|
|
|
|
|
|
|
|
static int co2Ppm = -1;
|
|
|
|
static int pm25 = -1;
|
2024-02-18 12:43:37 +07:00
|
|
|
static float temp = -1001;
|
2024-02-04 15:04:38 +07:00
|
|
|
static int hum = -1;
|
|
|
|
static long val;
|
|
|
|
static String wifiSSID = "";
|
|
|
|
static bool wifiHasConfig = false; /** */
|
|
|
|
|
|
|
|
static void boardInit(void);
|
|
|
|
static void failedHandler(String msg);
|
|
|
|
static void co2Calibration(void);
|
2024-02-29 15:20:19 +07:00
|
|
|
static void updateServerConfiguration(void);
|
2024-02-29 15:07:23 +07:00
|
|
|
static void co2Update(void);
|
|
|
|
static void pmUpdate(void);
|
|
|
|
static void tempHumUpdate(void);
|
2024-02-04 15:04:38 +07:00
|
|
|
static void sendDataToServer(void);
|
|
|
|
static void dispHandler(void);
|
|
|
|
static String getDevId(void);
|
|
|
|
static void updateWiFiConnect(void);
|
2024-02-17 12:11:44 +07:00
|
|
|
static void showNr(void);
|
2024-02-04 15:04:38 +07:00
|
|
|
|
2024-02-18 12:43:37 +07:00
|
|
|
bool hasSensorS8 = true;
|
|
|
|
bool hasSensorPMS = true;
|
|
|
|
bool hasSensorSHT = true;
|
2024-02-20 20:36:06 +07:00
|
|
|
int pmFailCount = 0;
|
2024-02-29 15:20:19 +07:00
|
|
|
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, updateServerConfiguration);
|
2024-02-04 15:04:38 +07:00
|
|
|
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
|
|
|
AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler);
|
2024-02-29 15:07:23 +07:00
|
|
|
AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update);
|
|
|
|
AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmUpdate);
|
|
|
|
AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate);
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
2024-02-17 12:11:44 +07:00
|
|
|
showNr();
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
/** Init I2C */
|
|
|
|
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
2024-02-16 13:39:33 +07:00
|
|
|
delay(1000);
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
/** Board init */
|
|
|
|
boardInit();
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/** Init AirGradient server */
|
|
|
|
agServer.begin();
|
|
|
|
|
2024-02-03 10:46:26 +07:00
|
|
|
/** Show boot display */
|
2024-02-04 15:04:38 +07:00
|
|
|
displayShowText("DIY basic", "Lib:" + ag.getVersion(), "");
|
2024-02-03 10:46:26 +07:00
|
|
|
delay(2000);
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
/** WiFi connect */
|
|
|
|
connectToWifi();
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
wifiHasConfig = true;
|
|
|
|
sendPing();
|
|
|
|
|
2024-02-29 15:20:19 +07:00
|
|
|
agServer.fetchServerConfiguration(getDevId());
|
2024-02-04 15:04:38 +07:00
|
|
|
if (agServer.isCo2Calib()) {
|
|
|
|
co2Calibration();
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-07 21:00:13 +07:00
|
|
|
/** Show serial number display */
|
2024-02-07 09:43:52 +07:00
|
|
|
ag.display.clear();
|
|
|
|
ag.display.setCursor(1, 1);
|
2024-02-07 21:00:13 +07:00
|
|
|
ag.display.setText("Warm Up");
|
|
|
|
ag.display.setCursor(1, 15);
|
2024-02-07 09:43:52 +07:00
|
|
|
ag.display.setText("Serial#");
|
|
|
|
ag.display.setCursor(1, 29);
|
|
|
|
String id = getNormalizedMac();
|
2024-02-07 21:00:13 +07:00
|
|
|
Serial.println("Device id: " + id);
|
|
|
|
String id1 = id.substring(0, 9);
|
2024-02-07 09:43:52 +07:00
|
|
|
String id2 = id.substring(9, 12);
|
2024-02-16 13:39:33 +07:00
|
|
|
ag.display.setText("\'" + id1);
|
2024-02-07 09:43:52 +07:00
|
|
|
ag.display.setCursor(1, 40);
|
|
|
|
ag.display.setText(id2 + "\'");
|
|
|
|
ag.display.show();
|
|
|
|
|
2024-02-07 21:00:13 +07:00
|
|
|
delay(5000);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2024-02-04 15:04:38 +07:00
|
|
|
configSchedule.run();
|
|
|
|
serverSchedule.run();
|
|
|
|
dispSchedule.run();
|
2024-02-18 12:43:37 +07:00
|
|
|
if (hasSensorS8) {
|
|
|
|
co2Schedule.run();
|
|
|
|
}
|
|
|
|
if (hasSensorPMS) {
|
|
|
|
pmsSchedule.run();
|
|
|
|
}
|
|
|
|
if (hasSensorSHT) {
|
|
|
|
tempHumSchedule.run();
|
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
|
|
|
|
updateWiFiConnect();
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void sendPing() {
|
|
|
|
JSONVar root;
|
|
|
|
root["wifi"] = WiFi.RSSI();
|
|
|
|
root["boot"] = 0;
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
// delay(1500);
|
|
|
|
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
|
|
|
// Ping Server succses
|
|
|
|
} else {
|
|
|
|
// Ping server failed
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
// delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void displayShowText(String ln1, String ln2, String ln3) {
|
|
|
|
char buf[9];
|
|
|
|
ag.display.clear();
|
|
|
|
|
|
|
|
ag.display.setCursor(1, 1);
|
|
|
|
ag.display.setText(ln1);
|
|
|
|
ag.display.setCursor(1, 19);
|
|
|
|
ag.display.setText(ln2);
|
|
|
|
ag.display.setCursor(1, 37);
|
|
|
|
ag.display.setText(ln3);
|
|
|
|
|
|
|
|
ag.display.show();
|
2024-02-17 13:56:07 +07:00
|
|
|
delay(100);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wifi Manager
|
|
|
|
void connectToWifi() {
|
|
|
|
WiFiManager wifiManager;
|
2024-02-04 15:04:38 +07:00
|
|
|
wifiSSID = "AG-" + String(ESP.getChipId(), HEX);
|
2024-02-03 10:46:26 +07:00
|
|
|
wifiManager.setConfigPortalBlocking(false);
|
2024-02-04 15:04:38 +07:00
|
|
|
wifiManager.setConfigPortalTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
|
|
|
|
wifiManager.autoConnect(wifiSSID.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT);
|
2024-02-03 10:46:26 +07:00
|
|
|
|
|
|
|
uint32_t lastTime = millis();
|
2024-02-04 15:04:38 +07:00
|
|
|
int count = WIFI_CONNECT_COUNTDOWN_MAX;
|
|
|
|
displayShowText(String(WIFI_CONNECT_COUNTDOWN_MAX) + " sec",
|
|
|
|
"SSID:", wifiSSID);
|
2024-02-03 10:46:26 +07:00
|
|
|
while (wifiManager.getConfigPortalActive()) {
|
|
|
|
wifiManager.process();
|
|
|
|
uint32_t ms = (uint32_t)(millis() - lastTime);
|
|
|
|
if (ms >= 1000) {
|
|
|
|
lastTime = millis();
|
2024-02-04 15:04:38 +07:00
|
|
|
displayShowText(String(count) + " sec", "SSID:", wifiSSID);
|
2024-02-03 10:46:26 +07:00
|
|
|
count--;
|
|
|
|
|
|
|
|
// Timeout
|
|
|
|
if (count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!WiFi.isConnected()) {
|
|
|
|
displayShowText("Booting", "offline", "mode");
|
|
|
|
Serial.println("failed to connect and hit timeout");
|
2024-02-04 15:04:38 +07:00
|
|
|
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void boardInit(void) {
|
2024-02-03 10:46:26 +07:00
|
|
|
/** Init SHT sensor */
|
2024-02-16 13:39:33 +07:00
|
|
|
if (ag.sht.begin(Wire) == false) {
|
2024-02-18 12:43:37 +07:00
|
|
|
hasSensorSHT = false;
|
|
|
|
Serial.println("SHT sensor not found");
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** CO2 init */
|
|
|
|
if (ag.s8.begin(&Serial) == false) {
|
2024-02-18 12:43:37 +07:00
|
|
|
Serial.println("CO2 S8 snsor not found");
|
|
|
|
hasSensorS8 = false;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** PMS init */
|
|
|
|
if (ag.pms5003.begin(&Serial) == false) {
|
2024-02-18 12:43:37 +07:00
|
|
|
Serial.println("PMS sensor not found");
|
|
|
|
hasSensorPMS = false;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Display init */
|
|
|
|
ag.display.begin(Wire);
|
|
|
|
ag.display.setTextColor(1);
|
2024-02-17 13:50:22 +07:00
|
|
|
ag.display.clear();
|
2024-02-17 13:56:07 +07:00
|
|
|
ag.display.show();
|
|
|
|
delay(100);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void failedHandler(String msg) {
|
|
|
|
while (true) {
|
|
|
|
Serial.println(msg);
|
|
|
|
delay(1000);
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void co2Calibration(void) {
|
|
|
|
/** Count down for co2CalibCountdown secs */
|
|
|
|
for (int i = 0; i < SENSOR_CO2_CALIB_COUNTDOWN_MAX; i++) {
|
|
|
|
displayShowText("CO2 calib", "after",
|
|
|
|
String(SENSOR_CO2_CALIB_COUNTDOWN_MAX - i) + " sec");
|
|
|
|
delay(1000);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
if (ag.s8.setBaselineCalibration()) {
|
|
|
|
displayShowText("Calib", "success", "");
|
|
|
|
delay(1000);
|
|
|
|
displayShowText("Wait for", "finish", "...");
|
|
|
|
int count = 0;
|
|
|
|
while (ag.s8.isBaseLineCalibrationDone() == false) {
|
|
|
|
delay(1000);
|
|
|
|
count++;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
displayShowText("Finish", "after", String(count) + " sec");
|
|
|
|
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
|
|
|
} else {
|
|
|
|
displayShowText("Calib", "failure!!!", "");
|
|
|
|
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-29 15:20:19 +07:00
|
|
|
static void updateServerConfiguration(void) {
|
|
|
|
if (agServer.fetchServerConfiguration(getDevId())) {
|
2024-02-04 15:04:38 +07:00
|
|
|
if (agServer.isCo2Calib()) {
|
2024-02-18 12:43:37 +07:00
|
|
|
if (hasSensorS8) {
|
|
|
|
co2Calibration();
|
|
|
|
} else {
|
|
|
|
Serial.println("CO2 S8 not available, calib ignored");
|
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-17 12:04:11 +07:00
|
|
|
if (agServer.getCo2AbcDaysConfig() > 0) {
|
2024-02-18 12:43:37 +07:00
|
|
|
if (hasSensorS8) {
|
|
|
|
int newHour = agServer.getCo2AbcDaysConfig() * 24;
|
|
|
|
Serial.printf("abcDays config: %d days(%d hours)\r\n",
|
|
|
|
agServer.getCo2AbcDaysConfig(), newHour);
|
|
|
|
int curHour = ag.s8.getAbcPeriod();
|
|
|
|
Serial.printf("Current config: %d (hours)\r\n", ag.s8.getAbcPeriod());
|
|
|
|
if (curHour == newHour) {
|
|
|
|
Serial.println("set 'abcDays' ignored");
|
2024-02-18 10:49:06 +07:00
|
|
|
} else {
|
2024-02-18 12:43:37 +07:00
|
|
|
if (ag.s8.setAbcPeriod(agServer.getCo2AbcDaysConfig() * 24) ==
|
|
|
|
false) {
|
|
|
|
Serial.println("Set S8 abcDays period calib failed");
|
|
|
|
} else {
|
|
|
|
Serial.println("Set S8 abcDays period calib success");
|
|
|
|
}
|
2024-02-18 10:49:06 +07:00
|
|
|
}
|
2024-02-18 12:43:37 +07:00
|
|
|
} else {
|
|
|
|
Serial.println("CO2 S8 not available, set 'abcDays' ignored");
|
2024-02-06 10:41:10 +07:00
|
|
|
}
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-29 15:07:23 +07:00
|
|
|
static void co2Update() {
|
2024-02-04 15:04:38 +07:00
|
|
|
co2Ppm = ag.s8.getCo2();
|
|
|
|
Serial.printf("CO2 index: %d\r\n", co2Ppm);
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-29 15:07:23 +07:00
|
|
|
void pmUpdate() {
|
2024-02-04 15:04:38 +07:00
|
|
|
if (ag.pms5003.readData()) {
|
|
|
|
pm25 = ag.pms5003.getPm25Ae();
|
|
|
|
Serial.printf("PMS2.5: %d\r\n", pm25);
|
2024-02-20 20:36:06 +07:00
|
|
|
pmFailCount = 0;
|
2024-02-04 15:04:38 +07:00
|
|
|
} else {
|
2024-02-20 21:05:04 +07:00
|
|
|
Serial.printf("PM read failed, %d", pmFailCount);
|
2024-02-20 20:36:06 +07:00
|
|
|
pmFailCount++;
|
|
|
|
if (pmFailCount >= 3) {
|
|
|
|
pm25 = -1;
|
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-29 15:07:23 +07:00
|
|
|
static void tempHumUpdate() {
|
2024-02-16 13:39:33 +07:00
|
|
|
if (ag.sht.measure()) {
|
|
|
|
temp = ag.sht.getTemperature();
|
|
|
|
hum = ag.sht.getRelativeHumidity();
|
|
|
|
Serial.printf("Temperature: %0.2f\r\n", temp);
|
|
|
|
Serial.printf(" Humidity: %d\r\n", hum);
|
|
|
|
} else {
|
|
|
|
Serial.println("Meaure SHT failed");
|
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void sendDataToServer() {
|
|
|
|
JSONVar root;
|
|
|
|
root["wifi"] = WiFi.RSSI();
|
|
|
|
if (co2Ppm >= 0) {
|
|
|
|
root["rco2"] = co2Ppm;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
if (pm25 >= 0) {
|
|
|
|
root["pm02"] = pm25;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-18 12:43:37 +07:00
|
|
|
if (temp > -1001) {
|
2024-02-17 12:47:51 +07:00
|
|
|
root["atmp"] = ag.round2(temp);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
if (hum >= 0) {
|
|
|
|
root["rhum"] = hum;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
if (agServer.postToServer(getDevId(), JSON.stringify(root)) == false) {
|
|
|
|
Serial.println("Post to server failed");
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
}
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static void dispHandler() {
|
|
|
|
String ln1 = "";
|
|
|
|
String ln2 = "";
|
|
|
|
String ln3 = "";
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
if (agServer.isPMSinUSAQI()) {
|
2024-02-20 21:05:04 +07:00
|
|
|
if (pm25 < 0) {
|
|
|
|
ln1 = "AQI: -";
|
|
|
|
} else {
|
|
|
|
ln1 = "AQI:" + String(ag.pms5003.convertPm25ToUsAqi(pm25));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pm25 < 0) {
|
|
|
|
ln1 = "PM :- ug";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ln1 = "PM :" + String(pm25) + " ug";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (co2Ppm > -1001) {
|
|
|
|
ln2 = "CO2:" + String(co2Ppm);
|
2024-02-04 15:04:38 +07:00
|
|
|
} else {
|
2024-02-20 21:05:04 +07:00
|
|
|
ln2 = "CO2: -";
|
|
|
|
}
|
|
|
|
|
|
|
|
String _hum = "-";
|
|
|
|
if (hum > 0) {
|
|
|
|
_hum = String(hum);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-20 21:05:04 +07:00
|
|
|
|
|
|
|
String _temp = "-";
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
if (agServer.isTemperatureUnitF()) {
|
2024-02-20 21:05:04 +07:00
|
|
|
if (temp > -1001) {
|
|
|
|
_temp = String((temp * 9 / 5) + 32).substring(0, 4);
|
|
|
|
}
|
|
|
|
ln3 = _temp + " " + _hum + "%";
|
2024-02-04 15:04:38 +07:00
|
|
|
} else {
|
2024-02-20 21:05:04 +07:00
|
|
|
if (temp > -1001) {
|
|
|
|
_temp = String(temp).substring(0, 4);
|
|
|
|
}
|
|
|
|
ln3 = _temp + " " + _hum + "%";
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
displayShowText(ln1, ln2, ln3);
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
static String getDevId(void) { return getNormalizedMac(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief WiFi reconnect handler
|
|
|
|
*/
|
|
|
|
static void updateWiFiConnect(void) {
|
|
|
|
static uint32_t lastRetry;
|
|
|
|
if (wifiHasConfig == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (WiFi.isConnected()) {
|
|
|
|
lastRetry = millis();
|
|
|
|
return;
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
uint32_t ms = (uint32_t)(millis() - lastRetry);
|
|
|
|
if (ms >= WIFI_CONNECT_RETRY_MS) {
|
|
|
|
lastRetry = millis();
|
|
|
|
WiFi.reconnect();
|
2024-02-03 10:46:26 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
Serial.printf("Re-Connect WiFi\r\n");
|
2024-02-03 10:46:26 +07:00
|
|
|
}
|
|
|
|
}
|
2024-02-04 15:04:38 +07:00
|
|
|
|
2024-02-17 13:50:22 +07:00
|
|
|
static void showNr(void) {
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("Serial nr: " + getDevId());
|
|
|
|
}
|
2024-02-17 12:11:44 +07:00
|
|
|
|
2024-02-04 15:04:38 +07:00
|
|
|
String getNormalizedMac() {
|
|
|
|
String mac = WiFi.macAddress();
|
|
|
|
mac.replace(":", "");
|
|
|
|
mac.toLowerCase();
|
|
|
|
return mac;
|
|
|
|
}
|