mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-06-29 17:50:58 +02:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
1a52c2d9f8 | |||
6afcf6d4c3 | |||
af139331b1 | |||
e79a798b88 | |||
14fb790e2a | |||
2aab02940d | |||
da07067661 | |||
b2091114b3 | |||
e09128572c | |||
e16966d092 | |||
26a8b065bc | |||
589b98d97e | |||
cb4d9372f8 | |||
6cb7fa8a1b | |||
6cdbb8a0a3 | |||
781fb51c6f | |||
17646f3067 | |||
7a4b665bb5 | |||
571b36d05f | |||
23513cf88c | |||
b475c5c1ec | |||
ee9f26ee04 | |||
8c94cea764 | |||
7c63af5ba9 | |||
7c1eae83e4 | |||
e48ff0e41c | |||
c9e3a2a9b4 |
@ -79,8 +79,8 @@ public:
|
|||||||
/** Call handler */
|
/** Call handler */
|
||||||
handler();
|
handler();
|
||||||
|
|
||||||
Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
// Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
||||||
(unsigned int)handler, period);
|
// (unsigned int)handler, period);
|
||||||
|
|
||||||
/** Update period time */
|
/** Update period time */
|
||||||
count = millis();
|
count = millis();
|
||||||
@ -271,7 +271,7 @@ public:
|
|||||||
bool isPMSinUSAQI(void) { return inUSAQI; }
|
bool isPMSinUSAQI(void) { return inUSAQI; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get status of get server coniguration is failed
|
* @brief Get status of get server configuration is failed
|
||||||
*
|
*
|
||||||
* @return true Failed
|
* @return true Failed
|
||||||
* @return false Success
|
* @return false Success
|
||||||
@ -305,7 +305,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return int days, -1 if invalid.
|
* @return int days, -1 if invalid.
|
||||||
*/
|
*/
|
||||||
int getCo2Abccalib(void) { return co2AbcCalib; }
|
int getCo2AbcDaysConfig(void) { return co2AbcCalib; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get device configuration model name
|
* @brief Get device configuration model name
|
||||||
@ -359,7 +359,7 @@ AirGradient ag = AirGradient(DIY_BASIC);
|
|||||||
|
|
||||||
static int co2Ppm = -1;
|
static int co2Ppm = -1;
|
||||||
static int pm25 = -1;
|
static int pm25 = -1;
|
||||||
static float temp = -1;
|
static float temp = -1001;
|
||||||
static int hum = -1;
|
static int hum = -1;
|
||||||
static long val;
|
static long val;
|
||||||
static String wifiSSID = "";
|
static String wifiSSID = "";
|
||||||
@ -376,7 +376,12 @@ static void sendDataToServer(void);
|
|||||||
static void dispHandler(void);
|
static void dispHandler(void);
|
||||||
static String getDevId(void);
|
static String getDevId(void);
|
||||||
static void updateWiFiConnect(void);
|
static void updateWiFiConnect(void);
|
||||||
|
static void showNr(void);
|
||||||
|
|
||||||
|
bool hasSensorS8 = true;
|
||||||
|
bool hasSensorPMS = true;
|
||||||
|
bool hasSensorSHT = true;
|
||||||
|
int pmFailCount = 0;
|
||||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
||||||
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
||||||
AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler);
|
AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler);
|
||||||
@ -386,6 +391,7 @@ AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumPoll);
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
showNr();
|
||||||
|
|
||||||
/** Init I2C */
|
/** Init I2C */
|
||||||
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
||||||
@ -436,9 +442,15 @@ void loop() {
|
|||||||
configSchedule.run();
|
configSchedule.run();
|
||||||
serverSchedule.run();
|
serverSchedule.run();
|
||||||
dispSchedule.run();
|
dispSchedule.run();
|
||||||
|
if (hasSensorS8) {
|
||||||
co2Schedule.run();
|
co2Schedule.run();
|
||||||
|
}
|
||||||
|
if (hasSensorPMS) {
|
||||||
pmsSchedule.run();
|
pmsSchedule.run();
|
||||||
|
}
|
||||||
|
if (hasSensorSHT) {
|
||||||
tempHumSchedule.run();
|
tempHumSchedule.run();
|
||||||
|
}
|
||||||
|
|
||||||
updateWiFiConnect();
|
updateWiFiConnect();
|
||||||
}
|
}
|
||||||
@ -469,6 +481,7 @@ void displayShowText(String ln1, String ln2, String ln3) {
|
|||||||
ag.display.setText(ln3);
|
ag.display.setText(ln3);
|
||||||
|
|
||||||
ag.display.show();
|
ag.display.show();
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wifi Manager
|
// Wifi Manager
|
||||||
@ -507,22 +520,28 @@ void connectToWifi() {
|
|||||||
static void boardInit(void) {
|
static void boardInit(void) {
|
||||||
/** Init SHT sensor */
|
/** Init SHT sensor */
|
||||||
if (ag.sht.begin(Wire) == false) {
|
if (ag.sht.begin(Wire) == false) {
|
||||||
failedHandler("SHT init failed");
|
hasSensorSHT = false;
|
||||||
|
Serial.println("SHT sensor not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** CO2 init */
|
/** CO2 init */
|
||||||
if (ag.s8.begin(&Serial) == false) {
|
if (ag.s8.begin(&Serial) == false) {
|
||||||
failedHandler("SenseAirS8 init failed");
|
Serial.println("CO2 S8 snsor not found");
|
||||||
|
hasSensorS8 = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** PMS init */
|
/** PMS init */
|
||||||
if (ag.pms5003.begin(&Serial) == false) {
|
if (ag.pms5003.begin(&Serial) == false) {
|
||||||
failedHandler("PMS5003 init failed");
|
Serial.println("PMS sensor not found");
|
||||||
|
hasSensorPMS = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Display init */
|
/** Display init */
|
||||||
ag.display.begin(Wire);
|
ag.display.begin(Wire);
|
||||||
ag.display.setTextColor(1);
|
ag.display.setTextColor(1);
|
||||||
|
ag.display.clear();
|
||||||
|
ag.display.show();
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void failedHandler(String msg) {
|
static void failedHandler(String msg) {
|
||||||
@ -560,11 +579,31 @@ static void co2Calibration(void) {
|
|||||||
static void serverConfigPoll(void) {
|
static void serverConfigPoll(void) {
|
||||||
if (agServer.pollServerConfig(getDevId())) {
|
if (agServer.pollServerConfig(getDevId())) {
|
||||||
if (agServer.isCo2Calib()) {
|
if (agServer.isCo2Calib()) {
|
||||||
|
if (hasSensorS8) {
|
||||||
co2Calibration();
|
co2Calibration();
|
||||||
|
} else {
|
||||||
|
Serial.println("CO2 S8 not available, calib ignored");
|
||||||
}
|
}
|
||||||
if (agServer.getCo2Abccalib() > 0) {
|
}
|
||||||
if (ag.s8.setAutoCalib(agServer.getCo2Abccalib() * 24) == false) {
|
if (agServer.getCo2AbcDaysConfig() > 0) {
|
||||||
Serial.println("Set S8 auto calib failed");
|
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");
|
||||||
|
} else {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("CO2 S8 not available, set 'abcDays' ignored");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -579,10 +618,15 @@ void pmPoll() {
|
|||||||
if (ag.pms5003.readData()) {
|
if (ag.pms5003.readData()) {
|
||||||
pm25 = ag.pms5003.getPm25Ae();
|
pm25 = ag.pms5003.getPm25Ae();
|
||||||
Serial.printf("PMS2.5: %d\r\n", pm25);
|
Serial.printf("PMS2.5: %d\r\n", pm25);
|
||||||
|
pmFailCount = 0;
|
||||||
} else {
|
} else {
|
||||||
|
Serial.printf("PM read failed, %d", pmFailCount);
|
||||||
|
pmFailCount++;
|
||||||
|
if (pmFailCount >= 3) {
|
||||||
pm25 = -1;
|
pm25 = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void tempHumPoll() {
|
static void tempHumPoll() {
|
||||||
if (ag.sht.measure()) {
|
if (ag.sht.measure()) {
|
||||||
@ -604,8 +648,8 @@ static void sendDataToServer() {
|
|||||||
if (pm25 >= 0) {
|
if (pm25 >= 0) {
|
||||||
root["pm02"] = pm25;
|
root["pm02"] = pm25;
|
||||||
}
|
}
|
||||||
if (temp >= 0) {
|
if (temp > -1001) {
|
||||||
root["atmp"] = temp;
|
root["atmp"] = ag.round2(temp);
|
||||||
}
|
}
|
||||||
if (hum >= 0) {
|
if (hum >= 0) {
|
||||||
root["rhum"] = hum;
|
root["rhum"] = hum;
|
||||||
@ -622,16 +666,42 @@ static void dispHandler() {
|
|||||||
String ln3 = "";
|
String ln3 = "";
|
||||||
|
|
||||||
if (agServer.isPMSinUSAQI()) {
|
if (agServer.isPMSinUSAQI()) {
|
||||||
|
if (pm25 < 0) {
|
||||||
|
ln1 = "AQI: -";
|
||||||
|
} else {
|
||||||
ln1 = "AQI:" + String(ag.pms5003.convertPm25ToUsAqi(pm25));
|
ln1 = "AQI:" + String(ag.pms5003.convertPm25ToUsAqi(pm25));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pm25 < 0) {
|
||||||
|
ln1 = "PM :- ug";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ln1 = "PM :" + String(pm25) + " ug";
|
ln1 = "PM :" + String(pm25) + " ug";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (co2Ppm > -1001) {
|
||||||
ln2 = "CO2:" + String(co2Ppm);
|
ln2 = "CO2:" + String(co2Ppm);
|
||||||
|
} else {
|
||||||
|
ln2 = "CO2: -";
|
||||||
|
}
|
||||||
|
|
||||||
|
String _hum = "-";
|
||||||
|
if (hum > 0) {
|
||||||
|
_hum = String(hum);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _temp = "-";
|
||||||
|
|
||||||
if (agServer.isTemperatureUnitF()) {
|
if (agServer.isTemperatureUnitF()) {
|
||||||
ln3 = String((temp * 9 / 5) + 32).substring(0, 4) + " " + String(hum) + "%";
|
if (temp > -1001) {
|
||||||
|
_temp = String((temp * 9 / 5) + 32).substring(0, 4);
|
||||||
|
}
|
||||||
|
ln3 = _temp + " " + _hum + "%";
|
||||||
} else {
|
} else {
|
||||||
ln3 = String(temp).substring(0, 4) + " " + String(hum) + "%";
|
if (temp > -1001) {
|
||||||
|
_temp = String(temp).substring(0, 4);
|
||||||
|
}
|
||||||
|
ln3 = _temp + " " + _hum + "%";
|
||||||
}
|
}
|
||||||
displayShowText(ln1, ln2, ln3);
|
displayShowText(ln1, ln2, ln3);
|
||||||
}
|
}
|
||||||
@ -659,6 +729,11 @@ static void updateWiFiConnect(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void showNr(void) {
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("Serial nr: " + getDevId());
|
||||||
|
}
|
||||||
|
|
||||||
String getNormalizedMac() {
|
String getNormalizedMac() {
|
||||||
String mac = WiFi.macAddress();
|
String mac = WiFi.macAddress();
|
||||||
mac.replace(":", "");
|
mac.replace(":", "");
|
||||||
|
@ -38,13 +38,17 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "mqtt_client.h"
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
|
|
||||||
|
#include "EEPROM.h"
|
||||||
#include <AirGradient.h>
|
#include <AirGradient.h>
|
||||||
#include <Arduino_JSON.h>
|
#include <Arduino_JSON.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
#include <U8g2lib.h>
|
#include <U8g2lib.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Application state machine state
|
* @brief Application state machine state
|
||||||
@ -52,7 +56,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|||||||
*/
|
*/
|
||||||
enum {
|
enum {
|
||||||
APP_SM_WIFI_MANAGER_MODE, /** In WiFi Manger Mode */
|
APP_SM_WIFI_MANAGER_MODE, /** In WiFi Manger Mode */
|
||||||
APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE, /** WiFi Manager has connected to mobile
|
APP_SM_WIFI_MANAGER_PORTAL_ACTIVE, /** WiFi Manager has connected to mobile
|
||||||
phone */
|
phone */
|
||||||
APP_SM_WIFI_MANAGER_STA_CONNECTING, /** After SSID and PW entered and OK
|
APP_SM_WIFI_MANAGER_STA_CONNECTING, /** After SSID and PW entered and OK
|
||||||
clicked, connection to WiFI network is
|
clicked, connection to WiFI network is
|
||||||
@ -60,24 +64,24 @@ enum {
|
|||||||
APP_SM_WIFI_MANAGER_STA_CONNECTED, /** Connecting to WiFi worked */
|
APP_SM_WIFI_MANAGER_STA_CONNECTED, /** Connecting to WiFi worked */
|
||||||
APP_SM_WIFI_OK_SERVER_CONNECTING, /** Once connected to WiFi an attempt to
|
APP_SM_WIFI_OK_SERVER_CONNECTING, /** Once connected to WiFi an attempt to
|
||||||
reach the server is performed */
|
reach the server is performed */
|
||||||
APP_SM_WIFI_OK_SERVER_CONNNECTED, /** Server is reachable, all fine */
|
APP_SM_WIFI_OK_SERVER_CONNECTED, /** Server is reachable, all fine */
|
||||||
/** Exceptions during WIFi Setup */
|
/** Exceptions during WIFi Setup */
|
||||||
APP_SM_WIFI_MANAGER_CONNECT_FAILED, /** Cannot connect to WiFi (e.g. wrong
|
APP_SM_WIFI_MANAGER_CONNECT_FAILED, /** Cannot connect to WiFi (e.g. wrong
|
||||||
password, WPA Enterprise etc.) */
|
password, WPA Enterprise etc.) */
|
||||||
APP_SM_WIFI_OK_SERVER_CONNECT_FAILED, /** Connected to WiFi but server not
|
APP_SM_WIFI_OK_SERVER_CONNECT_FAILED, /** Connected to WiFi but server not
|
||||||
reachable, e.g. firewall block/
|
reachable, e.g. firewall block/
|
||||||
whitelisting needed etc. */
|
whitelisting needed etc. */
|
||||||
APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED, /** Server reachable but sensor
|
APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED, /** Server reachable but sensor
|
||||||
not configured correctly*/
|
not configured correctly*/
|
||||||
|
|
||||||
/** During Normal Operation */
|
/** During Normal Operation */
|
||||||
APP_SM_WIFI_LOST, /** Connection to WiFi network failed credentials incorrect
|
APP_SM_WIFI_LOST, /** Connection to WiFi network failed credentials incorrect
|
||||||
encryption not supported etc. */
|
encryption not supported etc. */
|
||||||
APP_SM_SERVER_LOST, /** Connected to WiFi network but the server cannot be
|
APP_SM_SERVER_LOST, /** Connected to WiFi network but the server cannot be
|
||||||
reached through the internet, e.g. blocked by firewall
|
reached through the internet, e.g. blocked by firewall
|
||||||
*/
|
*/
|
||||||
APP_SM_SENSOR_CONFIG_FAILED, /** Server is reachable but there is some
|
APP_SM_SENSOR_CONFIG_FAILED, /** Server is reachable but there is some
|
||||||
configuration issue to be fixed on the server
|
configuration issue to be fixed on the server
|
||||||
side */
|
side */
|
||||||
APP_SM_NORMAL,
|
APP_SM_NORMAL,
|
||||||
};
|
};
|
||||||
@ -122,8 +126,8 @@ public:
|
|||||||
/** Call handler */
|
/** Call handler */
|
||||||
handler();
|
handler();
|
||||||
|
|
||||||
Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
// Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
||||||
(unsigned int)handler, period);
|
// (unsigned int)handler, period);
|
||||||
|
|
||||||
/** Update period time */
|
/** Update period time */
|
||||||
count = millis();
|
count = millis();
|
||||||
@ -143,12 +147,9 @@ private:
|
|||||||
class AgServer {
|
class AgServer {
|
||||||
public:
|
public:
|
||||||
void begin(void) {
|
void begin(void) {
|
||||||
inF = false;
|
|
||||||
inUSAQI = false;
|
|
||||||
configFailed = false;
|
configFailed = false;
|
||||||
serverFailed = false;
|
serverFailed = false;
|
||||||
memset(models, 0, sizeof(models));
|
loadConfig();
|
||||||
memset(mqttBroker, 0, sizeof(mqttBroker));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -193,6 +194,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Get "country" */
|
/** Get "country" */
|
||||||
|
bool inF = false;
|
||||||
if (JSON.typeof_(root["country"]) == "string") {
|
if (JSON.typeof_(root["country"]) == "string") {
|
||||||
String _country = root["country"];
|
String _country = root["country"];
|
||||||
country = _country;
|
country = _country;
|
||||||
@ -205,6 +207,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Get "pmsStandard" */
|
/** Get "pmsStandard" */
|
||||||
|
bool inUSAQI = false;
|
||||||
if (JSON.typeof_(root["pmStandard"]) == "string") {
|
if (JSON.typeof_(root["pmStandard"]) == "string") {
|
||||||
String standard = root["pmStandard"];
|
String standard = root["pmStandard"];
|
||||||
if (standard == "ugm3") {
|
if (standard == "ugm3") {
|
||||||
@ -222,6 +225,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Get "ledBarMode" */
|
/** Get "ledBarMode" */
|
||||||
|
uint8_t ledBarMode = UseLedBarOff;
|
||||||
if (JSON.typeof_(root["ledBarMode"]) == "string") {
|
if (JSON.typeof_(root["ledBarMode"]) == "string") {
|
||||||
String mode = root["ledBarMode"];
|
String mode = root["ledBarMode"];
|
||||||
if (mode == "co2") {
|
if (mode == "co2") {
|
||||||
@ -236,13 +240,18 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Get model */
|
/** Get model */
|
||||||
|
bool _saveConfig = false;
|
||||||
if (JSON.typeof_(root["model"]) == "string") {
|
if (JSON.typeof_(root["model"]) == "string") {
|
||||||
String model = root["model"];
|
String model = root["model"];
|
||||||
if (model.length()) {
|
if (model.length()) {
|
||||||
int len =
|
int len = model.length() < sizeof(config.models)
|
||||||
model.length() < sizeof(models) ? model.length() : sizeof(models);
|
? model.length()
|
||||||
memset(models, 0, sizeof(models));
|
: sizeof(config.models);
|
||||||
memcpy(models, model.c_str(), len);
|
if (model != String(config.models)) {
|
||||||
|
memset(config.models, 0, sizeof(config.models));
|
||||||
|
memcpy(config.models, model.c_str(), len);
|
||||||
|
_saveConfig = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,10 +259,14 @@ public:
|
|||||||
if (JSON.typeof_(root["mqttBrokerUrl"]) == "string") {
|
if (JSON.typeof_(root["mqttBrokerUrl"]) == "string") {
|
||||||
String mqtt = root["mqttBrokerUrl"];
|
String mqtt = root["mqttBrokerUrl"];
|
||||||
if (mqtt.length()) {
|
if (mqtt.length()) {
|
||||||
int len = mqtt.length() < sizeof(mqttBroker) ? mqtt.length()
|
int len = mqtt.length() < sizeof(config.mqttBrokers)
|
||||||
: sizeof(mqttBroker);
|
? mqtt.length()
|
||||||
memset(mqttBroker, 0, sizeof(mqttBroker));
|
: sizeof(config.mqttBrokers);
|
||||||
memcpy(mqttBroker, mqtt.c_str(), len);
|
if (mqtt != String(config.mqttBrokers)) {
|
||||||
|
memset(config.mqttBrokers, 0, sizeof(config.mqttBrokers));
|
||||||
|
memcpy(config.mqttBrokers, mqtt.c_str(), len);
|
||||||
|
_saveConfig = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,6 +286,14 @@ public:
|
|||||||
|
|
||||||
/** Show configuration */
|
/** Show configuration */
|
||||||
showServerConfig();
|
showServerConfig();
|
||||||
|
if (_saveConfig || (inF != config.inF) || (inUSAQI != config.inUSAQI) ||
|
||||||
|
(ledBarMode != config.useRGBLedBar)) {
|
||||||
|
config.inF = inF;
|
||||||
|
config.inUSAQI = inUSAQI;
|
||||||
|
config.useRGBLedBar = ledBarMode;
|
||||||
|
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -313,7 +334,7 @@ public:
|
|||||||
* @return true F unit
|
* @return true F unit
|
||||||
* @return false C Unit
|
* @return false C Unit
|
||||||
*/
|
*/
|
||||||
bool isTemperatureUnitF(void) { return inF; }
|
bool isTemperatureUnitF(void) { return config.inF; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get PMS standard unit
|
* @brief Get PMS standard unit
|
||||||
@ -321,10 +342,10 @@ public:
|
|||||||
* @return true USAQI
|
* @return true USAQI
|
||||||
* @return false ugm3
|
* @return false ugm3
|
||||||
*/
|
*/
|
||||||
bool isPMSinUSAQI(void) { return inUSAQI; }
|
bool isPMSinUSAQI(void) { return config.inUSAQI; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get status of get server coniguration is failed
|
* @brief Get status of get server configuration is failed
|
||||||
*
|
*
|
||||||
* @return true Failed
|
* @return true Failed
|
||||||
* @return false Success
|
* @return false Success
|
||||||
@ -372,32 +393,33 @@ public:
|
|||||||
*
|
*
|
||||||
* @return int days, -1 if invalid.
|
* @return int days, -1 if invalid.
|
||||||
*/
|
*/
|
||||||
int getCo2Abccalib(void) { return co2AbcCalib; }
|
int getCo2AbcDaysConfig(void) { return co2AbcCalib; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get device configuration model name
|
* @brief Get device configuration model name
|
||||||
*
|
*
|
||||||
* @return String Model name, empty string if server failed
|
* @return String Model name, empty string if server failed
|
||||||
*/
|
*/
|
||||||
String getModelName(void) { return String(models); }
|
String getModelName(void) { return String(config.models); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get mqttBroker url
|
* @brief Get mqttBroker url
|
||||||
*
|
*
|
||||||
* @return String Broker url, empty if server failed
|
* @return String Broker url, empty if server failed
|
||||||
*/
|
*/
|
||||||
String getMqttBroker(void) { return String(mqttBroker); }
|
String getMqttBroker(void) { return String(config.mqttBrokers); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Show server configuration parameter
|
* @brief Show server configuration parameter
|
||||||
*/
|
*/
|
||||||
void showServerConfig(void) {
|
void showServerConfig(void) {
|
||||||
Serial.println("Server configuration: ");
|
Serial.println("Server configuration: ");
|
||||||
Serial.printf(" inF: %s\r\n", inF ? "true" : "false");
|
Serial.printf(" inF: %s\r\n", config.inF ? "true" : "false");
|
||||||
Serial.printf(" inUSAQI: %s\r\n", inUSAQI ? "true" : "false");
|
Serial.printf(" inUSAQI: %s\r\n",
|
||||||
Serial.printf(" useRGBLedBar: %d\r\n", (int)ledBarMode);
|
config.inUSAQI ? "true" : "false");
|
||||||
Serial.printf(" Model: %s\r\n", models);
|
Serial.printf(" useRGBLedBar: %d\r\n", (int)config.useRGBLedBar);
|
||||||
Serial.printf(" Mqtt Broker: %s\r\n", mqttBroker);
|
Serial.printf(" Model: %s\r\n", config.models);
|
||||||
|
Serial.printf(" Mqtt Broker: %s\r\n", config.mqttBrokers);
|
||||||
Serial.printf(" S8 calib period: %d\r\n", co2AbcCalib);
|
Serial.printf(" S8 calib period: %d\r\n", co2AbcCalib);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,7 +428,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return UseLedBar
|
* @return UseLedBar
|
||||||
*/
|
*/
|
||||||
UseLedBar getLedBarMode(void) { return ledBarMode; }
|
UseLedBar getLedBarMode(void) { return (UseLedBar)config.useRGBLedBar; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the Country
|
* @brief Get the Country
|
||||||
@ -416,20 +438,200 @@ public:
|
|||||||
String getCountry(void) { return country; }
|
String getCountry(void) { return country; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
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 configFailed; /** Flag indicate get server configuration failed */
|
||||||
bool serverFailed; /** Flag indicate post data to server failed */
|
bool serverFailed; /** Flag indicate post data to server failed */
|
||||||
bool co2Calib; /** Is co2Ppmcalibration requset */
|
bool co2Calib; /** Is co2Ppmcalibration requset */
|
||||||
bool ledBarTestRequested; /** */
|
bool ledBarTestRequested; /** */
|
||||||
int co2AbcCalib = -1; /** update auto calibration number of day */
|
int co2AbcCalib = -1; /** update auto calibration number of day */
|
||||||
UseLedBar ledBarMode = UseLedBarCO2; /** */
|
|
||||||
char models[20]; /** */
|
|
||||||
char mqttBroker[256]; /** */
|
|
||||||
String country; /***/
|
String country; /***/
|
||||||
|
|
||||||
|
struct config_s {
|
||||||
|
bool inF;
|
||||||
|
bool inUSAQI;
|
||||||
|
uint8_t useRGBLedBar;
|
||||||
|
char models[20];
|
||||||
|
char mqttBrokers[256];
|
||||||
|
uint32_t checksum;
|
||||||
|
};
|
||||||
|
struct config_s config;
|
||||||
|
|
||||||
|
void defaultConfig(void) {
|
||||||
|
config.inF = false;
|
||||||
|
config.inUSAQI = false;
|
||||||
|
memset(config.models, 0, sizeof(config.models));
|
||||||
|
memset(config.mqttBrokers, 0, sizeof(config.mqttBrokers));
|
||||||
|
|
||||||
|
Serial.println("Load config default");
|
||||||
|
saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadConfig(void) {
|
||||||
|
if (EEPROM.readBytes(0, &config, sizeof(config)) != sizeof(config)) {
|
||||||
|
config.inF = false;
|
||||||
|
config.inUSAQI = false;
|
||||||
|
memset(config.models, 0, sizeof(config.models));
|
||||||
|
memset(config.mqttBrokers, 0, sizeof(config.mqttBrokers));
|
||||||
|
|
||||||
|
Serial.println("Load configure failed");
|
||||||
|
} else {
|
||||||
|
uint32_t sum = 0;
|
||||||
|
uint8_t *data = (uint8_t *)&config;
|
||||||
|
for (int i = 0; i < sizeof(config) - 4; i++) {
|
||||||
|
sum += data[i];
|
||||||
|
}
|
||||||
|
if (sum != config.checksum) {
|
||||||
|
Serial.println("config checksum failed");
|
||||||
|
defaultConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
showServerConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveConfig(void) {
|
||||||
|
config.checksum = 0;
|
||||||
|
uint8_t *data = (uint8_t *)&config;
|
||||||
|
for (int i = 0; i < sizeof(config) - 4; i++) {
|
||||||
|
config.checksum += data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
EEPROM.writeBytes(0, &config, sizeof(config));
|
||||||
|
EEPROM.commit();
|
||||||
|
Serial.println("Save config");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
AgServer agServer;
|
AgServer agServer;
|
||||||
|
|
||||||
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
||||||
|
int32_t event_id, void *event_data);
|
||||||
|
|
||||||
|
class AgMqtt {
|
||||||
|
private:
|
||||||
|
bool _isBegin = false;
|
||||||
|
String uri;
|
||||||
|
String hostname;
|
||||||
|
String user;
|
||||||
|
String pass;
|
||||||
|
int port;
|
||||||
|
esp_mqtt_client_handle_t client;
|
||||||
|
bool clientConnected = false;
|
||||||
|
int connectFailedCount = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AgMqtt() {}
|
||||||
|
~AgMqtt() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize mqtt
|
||||||
|
*
|
||||||
|
* @param uri Complete mqtt uri, ex:
|
||||||
|
* mqtts://username:password@my.broker.com:4711
|
||||||
|
* @return true Success
|
||||||
|
* @return false Failure
|
||||||
|
*/
|
||||||
|
bool begin(String uri) {
|
||||||
|
if (_isBegin) {
|
||||||
|
Serial.println("Mqtt already begin, call 'end' and try again");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uri.isEmpty()) {
|
||||||
|
Serial.println("Mqtt uri is empty");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->uri = uri;
|
||||||
|
Serial.printf("mqtt init '%s'\r\n", uri.c_str());
|
||||||
|
|
||||||
|
/** config esp_mqtt client */
|
||||||
|
esp_mqtt_client_config_t config = {
|
||||||
|
.uri = this->uri.c_str(),
|
||||||
|
};
|
||||||
|
|
||||||
|
/** init client */
|
||||||
|
client = esp_mqtt_client_init(&config);
|
||||||
|
if (client == NULL) {
|
||||||
|
Serial.println("mqtt client init failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Register event */
|
||||||
|
if (esp_mqtt_client_register_event(client, MQTT_EVENT_ANY,
|
||||||
|
mqtt_event_handler, NULL) != ESP_OK) {
|
||||||
|
Serial.println("mqtt client register event failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (esp_mqtt_client_start(client) != ESP_OK) {
|
||||||
|
Serial.println("mqtt client start failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isBegin = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deinitialize mqtt
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void end(void) {
|
||||||
|
if (_isBegin == false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_mqtt_client_disconnect(client);
|
||||||
|
esp_mqtt_client_stop(client);
|
||||||
|
esp_mqtt_client_destroy(client);
|
||||||
|
_isBegin = false;
|
||||||
|
|
||||||
|
Serial.println("mqtt de-init");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool publish(const char *topic, const char *payload, int len) {
|
||||||
|
if ((_isBegin == false) || (clientConnected == false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (esp_mqtt_client_publish(client, topic, payload, len, 0, 0) == ESP_OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get current complete mqtt uri
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
String getUri(void) { return uri; }
|
||||||
|
|
||||||
|
void _connectionHandler(bool connected) {
|
||||||
|
clientConnected = connected;
|
||||||
|
if (clientConnected == false) {
|
||||||
|
connectFailedCount++;
|
||||||
|
} else {
|
||||||
|
connectFailedCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mqtt client connect status
|
||||||
|
*
|
||||||
|
* @return true Connected
|
||||||
|
* @return false Disconnected or Not initialize
|
||||||
|
*/
|
||||||
|
bool isConnected(void) { return (_isBegin && clientConnected); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get number of times connection failed
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int connectionFailedCount(void) { return connectFailedCount; }
|
||||||
|
};
|
||||||
|
AgMqtt agMqtt;
|
||||||
|
|
||||||
/** Create airgradient instance for 'ONE_INDOOR' board */
|
/** Create airgradient instance for 'ONE_INDOOR' board */
|
||||||
AirGradient ag(ONE_INDOOR);
|
AirGradient ag(ONE_INDOOR);
|
||||||
|
|
||||||
@ -439,6 +641,9 @@ U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
|
|||||||
/** wifi manager instance */
|
/** wifi manager instance */
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
|
|
||||||
|
/** Web server instance */
|
||||||
|
WebServer webServer;
|
||||||
|
|
||||||
static bool wifiHasConfig = false; /** */
|
static bool wifiHasConfig = false; /** */
|
||||||
static int connectCountDown; /** wifi configuration countdown */
|
static int connectCountDown; /** wifi configuration countdown */
|
||||||
static int ledCount; /** For LED animation */
|
static int ledCount; /** For LED animation */
|
||||||
@ -446,13 +651,14 @@ static int ledSmState = APP_SM_NORMAL; /** Save display SM */
|
|||||||
static int dispSmState = APP_SM_NORMAL; /** Save LED SM */
|
static int dispSmState = APP_SM_NORMAL; /** Save LED SM */
|
||||||
|
|
||||||
static int tvocIndex = -1;
|
static int tvocIndex = -1;
|
||||||
|
static int tvocRawIndex = -1;
|
||||||
static int noxIndex = -1;
|
static int noxIndex = -1;
|
||||||
static int co2Ppm = -1;
|
static int co2Ppm = -1;
|
||||||
static int pm25 = -1;
|
static int pm25 = -1;
|
||||||
static int pm01 = -1;
|
static int pm01 = -1;
|
||||||
static int pm10 = -1;
|
static int pm10 = -1;
|
||||||
static int pm03PCount = -1;
|
static int pm03PCount = -1;
|
||||||
static float temp = -1;
|
static float temp = -1001;
|
||||||
static int hum = -1;
|
static int hum = -1;
|
||||||
static int bootCount;
|
static int bootCount;
|
||||||
static String wifiSSID = "";
|
static String wifiSSID = "";
|
||||||
@ -475,8 +681,16 @@ static void pmPoll(void);
|
|||||||
static void sendDataToServer(void);
|
static void sendDataToServer(void);
|
||||||
static void tempHumPoll(void);
|
static void tempHumPoll(void);
|
||||||
static void co2Poll(void);
|
static void co2Poll(void);
|
||||||
|
static void showNr(void);
|
||||||
|
static void webServerInit(void);
|
||||||
|
static String getServerSyncData(bool localServer);
|
||||||
|
|
||||||
/** Init schedule */
|
/** Init schedule */
|
||||||
|
bool hasSensorS8 = true;
|
||||||
|
bool hasSensorPMS = true;
|
||||||
|
bool hasSensorSGP = true;
|
||||||
|
bool hasSensorSHT = true;
|
||||||
|
int pmFailCount = 0;
|
||||||
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, updateDispLedBar);
|
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, updateDispLedBar);
|
||||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
||||||
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
||||||
@ -486,8 +700,12 @@ AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumPoll);
|
|||||||
AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocPoll);
|
AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocPoll);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
/** Serial fore print debug message */
|
EEPROM.begin(512);
|
||||||
|
|
||||||
|
/** Serial for print debug message */
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
delay(100); /** For bester show log */
|
||||||
|
showNr();
|
||||||
|
|
||||||
/** Init I2C */
|
/** Init I2C */
|
||||||
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
||||||
@ -523,15 +741,25 @@ void setup() {
|
|||||||
}
|
}
|
||||||
if (test) {
|
if (test) {
|
||||||
ledTest();
|
ledTest();
|
||||||
} else {
|
}
|
||||||
/** WIFI connect */
|
/** WIFI connect */
|
||||||
connectToWifi();
|
connectToWifi();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send first data to ping server and get server configuration
|
* Send first data to ping server and get server configuration
|
||||||
*/
|
*/
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
webServerInit();
|
||||||
|
|
||||||
|
/** MQTT init */
|
||||||
|
if (agServer.getMqttBroker().isEmpty() == false) {
|
||||||
|
if (agMqtt.begin(agServer.getMqttBroker())) {
|
||||||
|
Serial.println("MQTT client init success");
|
||||||
|
} else {
|
||||||
|
Serial.println("MQTT client init failure");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sendPing();
|
sendPing();
|
||||||
Serial.println(F("WiFi connected!"));
|
Serial.println(F("WiFi connected!"));
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
@ -559,10 +787,22 @@ void loop() {
|
|||||||
dispLedSchedule.run();
|
dispLedSchedule.run();
|
||||||
configSchedule.run();
|
configSchedule.run();
|
||||||
serverSchedule.run();
|
serverSchedule.run();
|
||||||
|
|
||||||
|
if (hasSensorS8) {
|
||||||
co2Schedule.run();
|
co2Schedule.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSensorPMS) {
|
||||||
pmsSchedule.run();
|
pmsSchedule.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSensorSHT) {
|
||||||
tempHumSchedule.run();
|
tempHumSchedule.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSensorSGP) {
|
||||||
tvocSchedule.run();
|
tvocSchedule.run();
|
||||||
|
}
|
||||||
|
|
||||||
/** Check for handle WiFi reconnect */
|
/** Check for handle WiFi reconnect */
|
||||||
updateWiFiConnect();
|
updateWiFiConnect();
|
||||||
@ -646,6 +886,85 @@ static void co2Poll(void) {
|
|||||||
Serial.printf("CO2 index: %d\r\n", co2Ppm);
|
Serial.printf("CO2 index: %d\r\n", co2Ppm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void showNr(void) { Serial.println("Serial nr: " + getDevId()); }
|
||||||
|
|
||||||
|
void webServerMeasureCurrentGet(void) {
|
||||||
|
webServer.send(200, "application/json", getServerSyncData(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
void webServerHandler(void *param) {
|
||||||
|
for (;;) {
|
||||||
|
webServer.handleClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void webServerInit(void) {
|
||||||
|
String host = "airgradient_" + getDevId();
|
||||||
|
if (!MDNS.begin(host)) {
|
||||||
|
Serial.println("Init MDNS failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
webServer.on("/measures/current", HTTP_GET, webServerMeasureCurrentGet);
|
||||||
|
webServer.begin();
|
||||||
|
MDNS.addService("http", "tcp", 80);
|
||||||
|
|
||||||
|
if (xTaskCreate(webServerHandler, "webserver", 1024 * 4, NULL, 5, NULL) !=
|
||||||
|
pdTRUE) {
|
||||||
|
Serial.println("Create task handle webserver failed");
|
||||||
|
}
|
||||||
|
Serial.printf("Webserver init: %s.local\r\n", host.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getServerSyncData(bool localServer) {
|
||||||
|
JSONVar root;
|
||||||
|
root["wifi"] = WiFi.RSSI();
|
||||||
|
if (localServer) {
|
||||||
|
root["serialno"] = getDevId();
|
||||||
|
}
|
||||||
|
if (hasSensorS8) {
|
||||||
|
if (co2Ppm >= 0) {
|
||||||
|
root["rco2"] = co2Ppm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSensorPMS) {
|
||||||
|
if (pm01 >= 0) {
|
||||||
|
root["pm01"] = pm01;
|
||||||
|
}
|
||||||
|
if (pm25 >= 0) {
|
||||||
|
root["pm02"] = pm25;
|
||||||
|
}
|
||||||
|
if (pm10 >= 0) {
|
||||||
|
root["pm10"] = pm10;
|
||||||
|
}
|
||||||
|
if (pm03PCount >= 0) {
|
||||||
|
root["pm003_count"] = pm03PCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSensorSGP) {
|
||||||
|
if (tvocIndex >= 0) {
|
||||||
|
root["tvoc_index"] = tvocIndex;
|
||||||
|
}
|
||||||
|
if (tvocRawIndex >= 0) {
|
||||||
|
root["tvoc_raw"] = tvocRawIndex;
|
||||||
|
}
|
||||||
|
if (noxIndex >= 0) {
|
||||||
|
root["noxIndex"] = noxIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSensorSHT) {
|
||||||
|
if (temp > -1001) {
|
||||||
|
root["atmp"] = ag.round2(temp);
|
||||||
|
}
|
||||||
|
if (hum >= 0) {
|
||||||
|
root["rhum"] = hum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
root["boot"] = bootCount;
|
||||||
|
|
||||||
|
return JSON.stringify(root);
|
||||||
|
}
|
||||||
|
|
||||||
static void sendPing() {
|
static void sendPing() {
|
||||||
JSONVar root;
|
JSONVar root;
|
||||||
root["wifi"] = WiFi.RSSI();
|
root["wifi"] = WiFi.RSSI();
|
||||||
@ -671,8 +990,8 @@ static void sendPing() {
|
|||||||
|
|
||||||
delay(1500);
|
delay(1500);
|
||||||
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
||||||
dispSmHandler(APP_SM_WIFI_OK_SERVER_CONNNECTED);
|
dispSmHandler(APP_SM_WIFI_OK_SERVER_CONNECTED);
|
||||||
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNNECTED);
|
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNECTED);
|
||||||
} else {
|
} else {
|
||||||
dispSmHandler(APP_SM_WIFI_OK_SERVER_CONNECT_FAILED);
|
dispSmHandler(APP_SM_WIFI_OK_SERVER_CONNECT_FAILED);
|
||||||
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNECT_FAILED);
|
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNECT_FAILED);
|
||||||
@ -717,7 +1036,7 @@ static void displayShowDashboard(String err) {
|
|||||||
|
|
||||||
/** Show temperature */
|
/** Show temperature */
|
||||||
if (agServer.isTemperatureUnitF()) {
|
if (agServer.isTemperatureUnitF()) {
|
||||||
if (temp > -10001) {
|
if (temp > -1001) {
|
||||||
float tempF = (temp * 9 / 5) + 32;
|
float tempF = (temp * 9 / 5) + 32;
|
||||||
sprintf(strBuf, "%.1f°F", tempF);
|
sprintf(strBuf, "%.1f°F", tempF);
|
||||||
} else {
|
} else {
|
||||||
@ -725,7 +1044,7 @@ static void displayShowDashboard(String err) {
|
|||||||
}
|
}
|
||||||
u8g2.drawUTF8(1, 10, strBuf);
|
u8g2.drawUTF8(1, 10, strBuf);
|
||||||
} else {
|
} else {
|
||||||
if (temp > -10001) {
|
if (temp > -1001) {
|
||||||
sprintf(strBuf, "%.1f°C", temp);
|
sprintf(strBuf, "%.1f°C", temp);
|
||||||
} else {
|
} else {
|
||||||
sprintf(strBuf, "-°C");
|
sprintf(strBuf, "-°C");
|
||||||
@ -752,7 +1071,7 @@ static void displayShowDashboard(String err) {
|
|||||||
if (err == "WiFi N/A") {
|
if (err == "WiFi N/A") {
|
||||||
u8g2.setFont(u8g2_font_t0_12_tf);
|
u8g2.setFont(u8g2_font_t0_12_tf);
|
||||||
if (agServer.isTemperatureUnitF()) {
|
if (agServer.isTemperatureUnitF()) {
|
||||||
if (temp > -10001) {
|
if (temp > -1001) {
|
||||||
float tempF = (temp * 9 / 5) + 32;
|
float tempF = (temp * 9 / 5) + 32;
|
||||||
sprintf(strBuf, "%.1f", tempF);
|
sprintf(strBuf, "%.1f", tempF);
|
||||||
} else {
|
} else {
|
||||||
@ -760,7 +1079,7 @@ static void displayShowDashboard(String err) {
|
|||||||
}
|
}
|
||||||
u8g2.drawUTF8(1, 10, strBuf);
|
u8g2.drawUTF8(1, 10, strBuf);
|
||||||
} else {
|
} else {
|
||||||
if (temp > -10001) {
|
if (temp > -1001) {
|
||||||
sprintf(strBuf, "%.1f", temp);
|
sprintf(strBuf, "%.1f", temp);
|
||||||
} else {
|
} else {
|
||||||
sprintf(strBuf, "-");
|
sprintf(strBuf, "-");
|
||||||
@ -935,7 +1254,7 @@ static void connectToWifi() {
|
|||||||
if (clientConnected != clientConnectChanged) {
|
if (clientConnected != clientConnectChanged) {
|
||||||
clientConnectChanged = clientConnected;
|
clientConnectChanged = clientConnected;
|
||||||
if (clientConnectChanged) {
|
if (clientConnectChanged) {
|
||||||
ledSmHandler(APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE);
|
ledSmHandler(APP_SM_WIFI_MANAGER_PORTAL_ACTIVE);
|
||||||
} else {
|
} else {
|
||||||
ledCount = LED_BAR_COUNT_INIT_VALUE;
|
ledCount = LED_BAR_COUNT_INIT_VALUE;
|
||||||
ledSmHandler(APP_SM_WIFI_MANAGER_MODE);
|
ledSmHandler(APP_SM_WIFI_MANAGER_MODE);
|
||||||
@ -1032,6 +1351,11 @@ static void setRGBledColor(char color) {
|
|||||||
ag.ledBar.setColor(r, g, b, ledNum);
|
ag.ledBar.setColor(r, g, b, ledNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dispSensorNotFound(String ss) {
|
||||||
|
displayShowText("Sensor init", "Error:", ss + " not found");
|
||||||
|
delay(2000);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize board
|
* @brief Initialize board
|
||||||
*/
|
*/
|
||||||
@ -1044,12 +1368,16 @@ static void boardInit(void) {
|
|||||||
|
|
||||||
/** Init sensor SGP41 */
|
/** Init sensor SGP41 */
|
||||||
if (ag.sgp41.begin(Wire) == false) {
|
if (ag.sgp41.begin(Wire) == false) {
|
||||||
failedHandler("Init SGP41 failed");
|
Serial.println("SGP41 sensor not found");
|
||||||
|
hasSensorSGP = false;
|
||||||
|
dispSensorNotFound("SGP41");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** INit SHT */
|
/** INit SHT */
|
||||||
if (ag.sht.begin(Wire) == false) {
|
if (ag.sht.begin(Wire) == false) {
|
||||||
failedHandler("Init SHT failed");
|
Serial.println("SHTx sensor not found");
|
||||||
|
hasSensorSHT = false;
|
||||||
|
dispSensorNotFound("SHT");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Init watchdog */
|
/** Init watchdog */
|
||||||
@ -1057,12 +1385,18 @@ static void boardInit(void) {
|
|||||||
|
|
||||||
/** Init S8 CO2 sensor */
|
/** Init S8 CO2 sensor */
|
||||||
if (ag.s8.begin(Serial1) == false) {
|
if (ag.s8.begin(Serial1) == false) {
|
||||||
failedHandler("Init SenseAirS8 failed");
|
// failedHandler("Init SenseAirS8 failed");
|
||||||
|
Serial.println("CO2 S8 sensor not found");
|
||||||
|
hasSensorS8 = false;
|
||||||
|
dispSensorNotFound("S8");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Init PMS5003 */
|
/** Init PMS5003 */
|
||||||
if (ag.pms5003.begin(Serial0) == false) {
|
if (ag.pms5003.begin(Serial0) == false) {
|
||||||
failedHandler("Init PMS5003 failed");
|
Serial.println("PMS sensor not found");
|
||||||
|
hasSensorPMS = false;
|
||||||
|
|
||||||
|
dispSensorNotFound("PMS");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1084,13 +1418,35 @@ static void failedHandler(String msg) {
|
|||||||
static void serverConfigPoll(void) {
|
static void serverConfigPoll(void) {
|
||||||
if (agServer.pollServerConfig(getDevId())) {
|
if (agServer.pollServerConfig(getDevId())) {
|
||||||
if (agServer.isCo2Calib()) {
|
if (agServer.isCo2Calib()) {
|
||||||
|
if (hasSensorS8) {
|
||||||
co2Calibration();
|
co2Calibration();
|
||||||
}
|
} else {
|
||||||
if (agServer.getCo2Abccalib() > 0) {
|
Serial.println("CO2 S8 not available, calib ignored");
|
||||||
if (ag.s8.setAutoCalib(agServer.getCo2Abccalib() * 24) == false) {
|
|
||||||
Serial.println("Set S8 auto calib failed");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (agServer.getCo2AbcDaysConfig() > 0) {
|
||||||
|
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");
|
||||||
|
} else {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("CO2 S8 not available, set 'abcDays' ignored");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (agServer.isLedBarTestRequested()) {
|
if (agServer.isLedBarTestRequested()) {
|
||||||
if (agServer.getCountry() == "TH") {
|
if (agServer.getCountry() == "TH") {
|
||||||
ledTest2Min();
|
ledTest2Min();
|
||||||
@ -1098,6 +1454,16 @@ static void serverConfigPoll(void) {
|
|||||||
ledTest();
|
ledTest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String mqttUri = agServer.getMqttBroker();
|
||||||
|
if (mqttUri != agMqtt.getUri()) {
|
||||||
|
agMqtt.end();
|
||||||
|
if (agMqtt.begin(mqttUri)) {
|
||||||
|
Serial.println("Connect to new mqtt broker success");
|
||||||
|
} else {
|
||||||
|
Serial.println("Connect to new mqtt broker failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1187,7 +1553,7 @@ static void ledSmHandler(int sm) {
|
|||||||
ag.ledBar.setColor(0, 0, 255, ag.ledBar.getNumberOfLeds() / 2);
|
ag.ledBar.setColor(0, 0, 255, ag.ledBar.getNumberOfLeds() / 2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE: {
|
case APP_SM_WIFI_MANAGER_PORTAL_ACTIVE: {
|
||||||
/** WiFi Manager has connected to mobile phone */
|
/** WiFi Manager has connected to mobile phone */
|
||||||
ag.ledBar.setColor(0, 0, 255);
|
ag.ledBar.setColor(0, 0, 255);
|
||||||
break;
|
break;
|
||||||
@ -1208,7 +1574,7 @@ static void ledSmHandler(int sm) {
|
|||||||
singleLedAnimation(0, 255, 0);
|
singleLedAnimation(0, 255, 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNNECTED: {
|
case APP_SM_WIFI_OK_SERVER_CONNECTED: {
|
||||||
/** Server is reachable, all fine */
|
/** Server is reachable, all fine */
|
||||||
ag.ledBar.setColor(0, 255, 0);
|
ag.ledBar.setColor(0, 255, 0);
|
||||||
break;
|
break;
|
||||||
@ -1219,13 +1585,13 @@ static void ledSmHandler(int sm) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNECT_FAILED: {
|
case APP_SM_WIFI_OK_SERVER_CONNECT_FAILED: {
|
||||||
/** Connected to WiFi but server not reachable, e.g. firewall block/
|
/** Connected to WiFi but server not reachable, e.g. firewall block/
|
||||||
* whitelisting needed etc. */
|
* whitelisting needed etc. */
|
||||||
ag.ledBar.setColor(233, 183, 54); /** orange */
|
ag.ledBar.setColor(233, 183, 54); /** orange */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED: {
|
case APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED: {
|
||||||
/** Server reachable but sensor not configured correctly */
|
/** Server reachable but sensor not configured correctly */
|
||||||
ag.ledBar.setColor(139, 24, 248); /** violet */
|
ag.ledBar.setColor(139, 24, 248); /** violet */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1241,7 +1607,7 @@ static void ledSmHandler(int sm) {
|
|||||||
}
|
}
|
||||||
case APP_SM_SERVER_LOST: {
|
case APP_SM_SERVER_LOST: {
|
||||||
/** Connected to WiFi network but the server cannot be reached through the
|
/** Connected to WiFi network but the server cannot be reached through the
|
||||||
* internet, e.g. blocked by firewall */
|
* internet, e.g. blocked by firewall */
|
||||||
|
|
||||||
ag.ledBar.setColor(233, 183, 54, 0);
|
ag.ledBar.setColor(233, 183, 54, 0);
|
||||||
|
|
||||||
@ -1250,7 +1616,7 @@ static void ledSmHandler(int sm) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_SENSOR_CONFIG_FAILED: {
|
case APP_SM_SENSOR_CONFIG_FAILED: {
|
||||||
/** Server is reachable but there is some configuration issue to be fixed on
|
/** Server is reachable but there is some configuration issue to be fixed on
|
||||||
* the server side */
|
* the server side */
|
||||||
|
|
||||||
ag.ledBar.setColor(139, 24, 248, 0);
|
ag.ledBar.setColor(139, 24, 248, 0);
|
||||||
@ -1289,7 +1655,7 @@ static void dispSmHandler(int sm) {
|
|||||||
|
|
||||||
switch (sm) {
|
switch (sm) {
|
||||||
case APP_SM_WIFI_MANAGER_MODE:
|
case APP_SM_WIFI_MANAGER_MODE:
|
||||||
case APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE: {
|
case APP_SM_WIFI_MANAGER_PORTAL_ACTIVE: {
|
||||||
if (connectCountDown >= 0) {
|
if (connectCountDown >= 0) {
|
||||||
displayShowWifiText(String(connectCountDown) + "s to connect",
|
displayShowWifiText(String(connectCountDown) + "s to connect",
|
||||||
"to WiFi hotspot:", "\"airgradient-",
|
"to WiFi hotspot:", "\"airgradient-",
|
||||||
@ -1310,7 +1676,7 @@ static void dispSmHandler(int sm) {
|
|||||||
displayShowText("Connecting to", "Server", "...");
|
displayShowText("Connecting to", "Server", "...");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNNECTED: {
|
case APP_SM_WIFI_OK_SERVER_CONNECTED: {
|
||||||
displayShowText("Server", "connection", "successful");
|
displayShowText("Server", "connection", "successful");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1341,7 +1707,7 @@ static void dispSmHandler(int sm) {
|
|||||||
case APP_SM_NORMAL: {
|
case APP_SM_NORMAL: {
|
||||||
displayShowDashboard("");
|
displayShowDashboard("");
|
||||||
}
|
}
|
||||||
detault:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1430,9 +1796,12 @@ static void updateDispLedBar(void) {
|
|||||||
*/
|
*/
|
||||||
static void tvocPoll(void) {
|
static void tvocPoll(void) {
|
||||||
tvocIndex = ag.sgp41.getTvocIndex();
|
tvocIndex = ag.sgp41.getTvocIndex();
|
||||||
|
tvocRawIndex = ag.sgp41.getTvocRaw();
|
||||||
noxIndex = ag.sgp41.getNoxIndex();
|
noxIndex = ag.sgp41.getNoxIndex();
|
||||||
|
|
||||||
Serial.printf("tvocIndexindex: %d\r\n", tvocIndex);
|
Serial.println();
|
||||||
|
Serial.printf(" TVOC index: %d\r\n", tvocIndex);
|
||||||
|
Serial.printf("TVOC raw index: %d\r\n", tvocRawIndex);
|
||||||
Serial.printf(" NOx index: %d\r\n", noxIndex);
|
Serial.printf(" NOx index: %d\r\n", noxIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1447,58 +1816,42 @@ static void pmPoll(void) {
|
|||||||
pm10 = ag.pms5003.getPm10Ae();
|
pm10 = ag.pms5003.getPm10Ae();
|
||||||
pm03PCount = ag.pms5003.getPm03ParticleCount();
|
pm03PCount = ag.pms5003.getPm03ParticleCount();
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
Serial.printf(" PMS0.1: %d\r\n", pm01);
|
Serial.printf(" PMS0.1: %d\r\n", pm01);
|
||||||
Serial.printf(" PMS2.5: %d\r\n", pm25);
|
Serial.printf(" PMS2.5: %d\r\n", pm25);
|
||||||
Serial.printf(" PMS10.0: %d\r\n", pm10);
|
Serial.printf(" PMS10.0: %d\r\n", pm10);
|
||||||
Serial.printf("PMS3.0 Count: %d\r\n", pm03PCount);
|
Serial.printf("PMS3.0 Count: %d\r\n", pm03PCount);
|
||||||
|
pmFailCount = 0;
|
||||||
} else {
|
} else {
|
||||||
|
pmFailCount++;
|
||||||
|
Serial.printf("PM read failed: %d\r\n", pmFailCount);
|
||||||
|
if (pmFailCount >= 3) {
|
||||||
pm01 = -1;
|
pm01 = -1;
|
||||||
pm25 = -1;
|
pm25 = -1;
|
||||||
pm10 = -1;
|
pm10 = -1;
|
||||||
pm03PCount = -1;
|
pm03PCount = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Send data to server
|
* @brief Send data to server
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static void sendDataToServer(void) {
|
static void sendDataToServer(void) {
|
||||||
JSONVar root;
|
String syncData = getServerSyncData(false);
|
||||||
root["wifi"] = WiFi.RSSI();
|
if (agServer.postToServer(getDevId(), syncData)) {
|
||||||
if (co2Ppm >= 0) {
|
|
||||||
root["rco2"] = co2Ppm;
|
|
||||||
}
|
|
||||||
if (pm01 >= 0) {
|
|
||||||
root["pm01"] = pm01;
|
|
||||||
}
|
|
||||||
if (pm25 >= 0) {
|
|
||||||
root["pm02"] = pm25;
|
|
||||||
}
|
|
||||||
if (pm10 >= 0) {
|
|
||||||
root["pm10"] = pm10;
|
|
||||||
}
|
|
||||||
if (pm03PCount >= 0) {
|
|
||||||
root["pm003_count"] = pm03PCount;
|
|
||||||
}
|
|
||||||
if (tvocIndex >= 0) {
|
|
||||||
root["tvoc_index"] = tvocIndex;
|
|
||||||
}
|
|
||||||
if (noxIndex >= 0) {
|
|
||||||
root["noxIndex"] = noxIndex;
|
|
||||||
}
|
|
||||||
if (temp >= 0) {
|
|
||||||
root["atmp"] = temp;
|
|
||||||
}
|
|
||||||
if (hum >= 0) {
|
|
||||||
root["rhum"] = hum;
|
|
||||||
}
|
|
||||||
root["boot"] = bootCount;
|
|
||||||
|
|
||||||
// NOTE Need determine offline mode to reset watchdog timer
|
|
||||||
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
|
||||||
resetWatchdog();
|
resetWatchdog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (agMqtt.isConnected()) {
|
||||||
|
String topic = "airgradient/readings/" + getDevId();
|
||||||
|
if (agMqtt.publish(topic.c_str(), syncData.c_str(), syncData.length())) {
|
||||||
|
Serial.println("Mqtt sync success");
|
||||||
|
} else {
|
||||||
|
Serial.println("Mqtt sync failure");
|
||||||
|
}
|
||||||
|
}
|
||||||
bootCount++;
|
bootCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1517,3 +1870,54 @@ static void tempHumPoll(void) {
|
|||||||
Serial.println("Measure SHT failed");
|
Serial.println("Measure SHT failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
||||||
|
int32_t event_id, void *event_data) {
|
||||||
|
|
||||||
|
ESP_LOGD(TAG,
|
||||||
|
"Event dispatched from event loop base=%s, event_id=%" PRIi32 "",
|
||||||
|
base, event_id);
|
||||||
|
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
|
||||||
|
esp_mqtt_client_handle_t client = event->client;
|
||||||
|
int msg_id;
|
||||||
|
switch ((esp_mqtt_event_id_t)event_id) {
|
||||||
|
case MQTT_EVENT_CONNECTED:
|
||||||
|
Serial.println("MQTT_EVENT_CONNECTED");
|
||||||
|
// msg_id = esp_mqtt_client_subscribe(client, "helloworld", 0);
|
||||||
|
// Serial.printf("sent subscribe successful, msg_id=%d\r\n", msg_id);
|
||||||
|
agMqtt._connectionHandler(true);
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_DISCONNECTED:
|
||||||
|
Serial.println("MQTT_EVENT_DISCONNECTED");
|
||||||
|
agMqtt._connectionHandler(false);
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_SUBSCRIBED:
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_UNSUBSCRIBED:
|
||||||
|
Serial.printf("MQTT_EVENT_UNSUBSCRIBED, msg_id=%d\r\n", event->msg_id);
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_PUBLISHED:
|
||||||
|
Serial.printf("MQTT_EVENT_PUBLISHED, msg_id=%d\r\n", event->msg_id);
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_DATA:
|
||||||
|
Serial.println("MQTT_EVENT_DATA");
|
||||||
|
// add null terminal to data
|
||||||
|
// event->data[event->data_len] = 0;
|
||||||
|
// rpc_attritbutes_handler(event->data, event->data_len);
|
||||||
|
break;
|
||||||
|
case MQTT_EVENT_ERROR:
|
||||||
|
Serial.println("MQTT_EVENT_ERROR");
|
||||||
|
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
|
||||||
|
Serial.printf("reported from esp-tls: %d",
|
||||||
|
event->error_handle->esp_tls_last_esp_err);
|
||||||
|
Serial.printf("reported from tls stack: %d",
|
||||||
|
event->error_handle->esp_tls_stack_err);
|
||||||
|
Serial.printf("captured as transport's socket errno: %d",
|
||||||
|
event->error_handle->esp_transport_sock_errno);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.printf("Other event id:%d\r\n", event->event_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1470
examples/Open_Air/Open_Air.ino
Normal file
1470
examples/Open_Air/Open_Air.ino
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,974 +0,0 @@
|
|||||||
/*
|
|
||||||
This is the code for the AirGradient Open Air open-source hardware outdoor Air
|
|
||||||
Quality Monitor with an ESP32-C3 Microcontroller.
|
|
||||||
|
|
||||||
It is an air quality monitor for PM2.5, CO2, TVOCs, NOx, Temperature and
|
|
||||||
Humidity and can send data over Wifi.
|
|
||||||
|
|
||||||
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/open-air-pst-kit-1-3/
|
|
||||||
|
|
||||||
The codes needs the following libraries installed:
|
|
||||||
“WifiManager by tzapu, tablatronix” tested with version 2.0.16-rc.2
|
|
||||||
"Arduino_JSON" by Arduino Version 0.2.0
|
|
||||||
|
|
||||||
Please make sure you have esp32 board manager installed. Tested with
|
|
||||||
version 2.0.11.
|
|
||||||
|
|
||||||
Important flashing settings:
|
|
||||||
- Set board to "ESP32C3 Dev Module"
|
|
||||||
- Enable "USB CDC On Boot"
|
|
||||||
- Flash frequency "80Mhz"
|
|
||||||
- Flash mode "QIO"
|
|
||||||
- Flash size "4MB"
|
|
||||||
- Partition scheme "Default 4MB with spiffs (1.2MB APP/1,5MB SPIFFS)"
|
|
||||||
- JTAG adapter "Disabled"
|
|
||||||
|
|
||||||
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 <HTTPClient.h>
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
#include <WiFiManager.h>
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @brief Application state machine state
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
enum {
|
|
||||||
APP_SM_WIFI_MANAGER_MODE, /** In WiFi Manger Mode */
|
|
||||||
APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE, /** WiFi Manager has connected to mobile
|
|
||||||
phone */
|
|
||||||
APP_SM_WIFI_MANAGER_STA_CONNECTING, /** After SSID and PW entered and OK
|
|
||||||
clicked, connection to WiFI network is
|
|
||||||
attempted*/
|
|
||||||
APP_SM_WIFI_MANAGER_STA_CONNECTED, /** Connecting to WiFi worked */
|
|
||||||
APP_SM_WIFI_OK_SERVER_CONNECTING, /** Once connected to WiFi an attempt to
|
|
||||||
reach the server is performed */
|
|
||||||
APP_SM_WIFI_OK_SERVER_CONNNECTED, /** Server is reachable, all fine */
|
|
||||||
/** Exceptions during WIFi Setup */
|
|
||||||
APP_SM_WIFI_MANAGER_CONNECT_FAILED, /** Cannot connect to WiFi (e.g. wrong
|
|
||||||
password, WPA Enterprise etc.) */
|
|
||||||
APP_SM_WIFI_OK_SERVER_CONNECT_FAILED, /** Connected to WiFi but server not
|
|
||||||
reachable, e.g. firewall block/
|
|
||||||
whitelisting needed etc. */
|
|
||||||
APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED, /** Server reachable but sensor
|
|
||||||
not configured correctly*/
|
|
||||||
|
|
||||||
/** During Normal Operation */
|
|
||||||
APP_SM_WIFI_LOST, /** Connection to WiFi network failed credentials incorrect
|
|
||||||
encryption not supported etc. */
|
|
||||||
APP_SM_SERVER_LOST, /** Connected to WiFi network but the server cannot be
|
|
||||||
reached through the internet, e.g. blocked by firewall
|
|
||||||
*/
|
|
||||||
APP_SM_SENSOR_CONFIG_FAILED, /** Server is reachable but there is some
|
|
||||||
configuration issue to be fixed on the server
|
|
||||||
side */
|
|
||||||
APP_SM_NORMAL,
|
|
||||||
};
|
|
||||||
|
|
||||||
#define LED_FAST_BLINK_DELAY 250 /** ms */
|
|
||||||
#define LED_SLOW_BLINK_DELAY 1000 /** ms */
|
|
||||||
#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 */
|
|
||||||
#define SENSOR_TEMP_HUM_UPDATE_INTERVAL 5000 /** ms */
|
|
||||||
#define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */
|
|
||||||
#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();
|
|
||||||
|
|
||||||
Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
|
||||||
(unsigned int)handler, period);
|
|
||||||
|
|
||||||
/** Update period time */
|
|
||||||
count = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void setPeriod(int period) { this->period = period; }
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
bool pollServerConfig(String id) {
|
|
||||||
String uri =
|
|
||||||
"http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config";
|
|
||||||
|
|
||||||
/** Init http client */
|
|
||||||
HTTPClient client;
|
|
||||||
if (client.begin(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get "pmStandard" */
|
|
||||||
if (JSON.typeof_(root["pmStandard"]) == "string") {
|
|
||||||
String standard = root["pmStandard"];
|
|
||||||
if (standard == "ugm3") {
|
|
||||||
inUSAQI = false;
|
|
||||||
} else {
|
|
||||||
inUSAQI = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get 'abcDays' */
|
|
||||||
if (JSON.typeof_(root["abcDays"]) == "number") {
|
|
||||||
co2AbcCalib = root["abcDays"];
|
|
||||||
} else {
|
|
||||||
co2AbcCalib = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Show configuration */
|
|
||||||
showServerConfig();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get temperature configuration unit
|
|
||||||
*
|
|
||||||
* @return true F unit
|
|
||||||
* @return false C Unit
|
|
||||||
*/
|
|
||||||
bool isTemperatureUnitF(void) { return inF; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get PMS standard unit
|
|
||||||
*
|
|
||||||
* @return true USAQI
|
|
||||||
* @return false ugm3
|
|
||||||
*/
|
|
||||||
bool isPMSinUSAQI(void) { return inUSAQI; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get status of get server coniguration is failed
|
|
||||||
*
|
|
||||||
* @return true Failed
|
|
||||||
* @return false Success
|
|
||||||
*/
|
|
||||||
bool isConfigFailed(void) { return configFailed; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get status of post server configuration is failed
|
|
||||||
*
|
|
||||||
* @return true Failed
|
|
||||||
* @return false Success
|
|
||||||
*/
|
|
||||||
bool isServerFailed(void) { return serverFailed; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get the Co2 auto calib period
|
|
||||||
*
|
|
||||||
* @return int days, -1 if invalid.
|
|
||||||
*/
|
|
||||||
int getCo2Abccalib(void) { return co2AbcCalib; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get device configuration model name
|
|
||||||
*
|
|
||||||
* @return String Model name, empty string if server failed
|
|
||||||
*/
|
|
||||||
String getModelName(void) { return String(models); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get mqttBroker url
|
|
||||||
*
|
|
||||||
* @return String Broker url, empty if server failed
|
|
||||||
*/
|
|
||||||
String getMqttBroker(void) { return String(mqttBroker); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Show server configuration parameter
|
|
||||||
*/
|
|
||||||
void showServerConfig(void) {
|
|
||||||
Serial.println("Server configuration: ");
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get server config led bar mode
|
|
||||||
*
|
|
||||||
* @return UseLedBar
|
|
||||||
*/
|
|
||||||
UseLedBar getLedBarMode(void) { return ledBarMode; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
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 */
|
|
||||||
UseLedBar ledBarMode = UseLedBarCO2; /** */
|
|
||||||
char models[20]; /** */
|
|
||||||
char mqttBroker[256]; /** */
|
|
||||||
};
|
|
||||||
AgServer agServer;
|
|
||||||
|
|
||||||
/** Create airgradient instance for 'OPEN_AIR_OUTDOOR' board */
|
|
||||||
AirGradient ag(OPEN_AIR_OUTDOOR);
|
|
||||||
|
|
||||||
static int ledSmState = APP_SM_NORMAL;
|
|
||||||
|
|
||||||
int loopCount = 0;
|
|
||||||
|
|
||||||
WiFiManager wifiManager; /** wifi manager instance */
|
|
||||||
static bool wifiHasConfig = false;
|
|
||||||
static String wifiSSID = "";
|
|
||||||
|
|
||||||
int tvocIndex = -1;
|
|
||||||
int noxIndex = -1;
|
|
||||||
int co2Ppm = 0;
|
|
||||||
|
|
||||||
int pm25_1 = -1;
|
|
||||||
int pm01_1 = -1;
|
|
||||||
int pm10_1 = -1;
|
|
||||||
int pm03PCount_1 = -1;
|
|
||||||
float temp_1;
|
|
||||||
int hum_1;
|
|
||||||
|
|
||||||
int pm25_2 = -1;
|
|
||||||
int pm01_2 = -1;
|
|
||||||
int pm10_2 = -1;
|
|
||||||
int pm03PCount_2 = -1;
|
|
||||||
float temp_2;
|
|
||||||
int hum_2;
|
|
||||||
|
|
||||||
int pm1Value01;
|
|
||||||
int pm1Value25;
|
|
||||||
int pm1Value10;
|
|
||||||
int pm1PCount;
|
|
||||||
int pm1temp;
|
|
||||||
int pm1hum;
|
|
||||||
int pm2Value01;
|
|
||||||
int pm2Value25;
|
|
||||||
int pm2Value10;
|
|
||||||
int pm2PCount;
|
|
||||||
int pm2temp;
|
|
||||||
int pm2hum;
|
|
||||||
int countPosition;
|
|
||||||
const int targetCount = 20;
|
|
||||||
|
|
||||||
enum {
|
|
||||||
FW_MODE_PST, /** PMS5003T, S8 and SGP41 */
|
|
||||||
FW_MODE_PPT, /** PMS5003T_1, PMS5003T_2, SGP41 */
|
|
||||||
FW_MODE_PP /** PMS5003T_1, PMS5003T_2 */
|
|
||||||
};
|
|
||||||
int fw_mode = FW_MODE_PST;
|
|
||||||
|
|
||||||
void boardInit(void);
|
|
||||||
void failedHandler(String msg);
|
|
||||||
void co2Calibration(void);
|
|
||||||
static String getDevId(void);
|
|
||||||
static void updateWiFiConnect(void);
|
|
||||||
static void tvocPoll(void);
|
|
||||||
static void pmPoll(void);
|
|
||||||
static void sendDataToServer(void);
|
|
||||||
static void co2Poll(void);
|
|
||||||
static void serverConfigPoll(void);
|
|
||||||
static const char *getFwMode(int mode);
|
|
||||||
|
|
||||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
|
||||||
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
|
||||||
AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Poll);
|
|
||||||
AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmPoll);
|
|
||||||
AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocPoll);
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
|
|
||||||
/** Board init */
|
|
||||||
boardInit();
|
|
||||||
|
|
||||||
/** Server init */
|
|
||||||
agServer.begin();
|
|
||||||
|
|
||||||
/** WiFi connect */
|
|
||||||
connectToWifi();
|
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
|
||||||
wifiHasConfig = true;
|
|
||||||
sendPing();
|
|
||||||
|
|
||||||
agServer.pollServerConfig(getDevId());
|
|
||||||
if (agServer.isConfigFailed()) {
|
|
||||||
ledSmHandler(APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED);
|
|
||||||
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ledSmHandler(APP_SM_NORMAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
configSchedule.run();
|
|
||||||
serverSchedule.run();
|
|
||||||
if (fw_mode == FW_MODE_PST) {
|
|
||||||
co2Schedule.run();
|
|
||||||
}
|
|
||||||
pmsSchedule.run();
|
|
||||||
if (fw_mode == FW_MODE_PST || fw_mode == FW_MODE_PPT) {
|
|
||||||
tvocSchedule.run();
|
|
||||||
}
|
|
||||||
updateWiFiConnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendPing() {
|
|
||||||
JSONVar root;
|
|
||||||
root["wifi"] = WiFi.RSSI();
|
|
||||||
root["boot"] = loopCount;
|
|
||||||
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
|
||||||
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNNECTED);
|
|
||||||
} else {
|
|
||||||
ledSmHandler(APP_SM_WIFI_OK_SERVER_CONNECT_FAILED);
|
|
||||||
}
|
|
||||||
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sendDataToServer(void) {
|
|
||||||
JSONVar root;
|
|
||||||
root["wifi"] = WiFi.RSSI();
|
|
||||||
root["boot"] = loopCount;
|
|
||||||
if (fw_mode == FW_MODE_PST) {
|
|
||||||
if (co2Ppm >= 0) {
|
|
||||||
root["rco2"] = co2Ppm;
|
|
||||||
}
|
|
||||||
if (pm01_1 >= 0) {
|
|
||||||
root["pm01"] = pm01_1;
|
|
||||||
}
|
|
||||||
if (pm25_1 >= 0) {
|
|
||||||
root["pm02"] = pm25_1;
|
|
||||||
}
|
|
||||||
if (pm10_1 >= 0) {
|
|
||||||
root["pm10"] = pm10_1;
|
|
||||||
}
|
|
||||||
if (pm03PCount_1 >= 0) {
|
|
||||||
root["pm003_count"] = pm03PCount_1;
|
|
||||||
}
|
|
||||||
if (tvocIndex >= 0) {
|
|
||||||
root["tvoc_index"] = tvocIndex;
|
|
||||||
}
|
|
||||||
if (noxIndex >= 0) {
|
|
||||||
root["noxIndex"] = noxIndex;
|
|
||||||
}
|
|
||||||
if (temp_1 >= 0) {
|
|
||||||
root["atmp"] = temp_1;
|
|
||||||
}
|
|
||||||
if (hum_1 >= 0) {
|
|
||||||
root["rhum"] = hum_1;
|
|
||||||
}
|
|
||||||
} else if (fw_mode == FW_MODE_PPT) {
|
|
||||||
if (tvocIndex > 0) {
|
|
||||||
root["tvoc_index"] = loopCount;
|
|
||||||
}
|
|
||||||
if (noxIndex > 0) {
|
|
||||||
root["nox_index"] = loopCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fw_mode == FW_MODE_PP || FW_MODE_PPT) {
|
|
||||||
root["pm01"] = (int)((pm01_1 + pm01_2) / 2);
|
|
||||||
root["pm02"] = (int)((pm25_1 + pm25_2) / 2);
|
|
||||||
root["pm003_count"] = (int)((pm03PCount_1 + pm03PCount_2) / 2);
|
|
||||||
root["atmp"] = (int)((temp_1 + temp_2) / 2);
|
|
||||||
root["rhum"] = (int)((hum_1 + hum_2) / 2);
|
|
||||||
root["channels"]["1"]["pm01"] = pm01_1;
|
|
||||||
root["channels"]["1"]["pm02"] = pm25_1;
|
|
||||||
root["channels"]["1"]["pm10"] = pm10_1;
|
|
||||||
root["channels"]["1"]["pm003_count"] = pm03PCount_1;
|
|
||||||
root["channels"]["1"]["atmp"] = temp_1;
|
|
||||||
root["channels"]["1"]["rhum"] = hum_1;
|
|
||||||
root["channels"]["2"]["pm01"] = pm01_2;
|
|
||||||
root["channels"]["2"]["pm02"] = pm25_2;
|
|
||||||
root["channels"]["2"]["pm10"] = pm10_2;
|
|
||||||
root["channels"]["2"]["pm003_count"] = pm03PCount_2;
|
|
||||||
root["channels"]["2"]["atmp"] = temp_2;
|
|
||||||
root["channels"]["2"]["rhum"] = hum_2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Send data to sensor */
|
|
||||||
if (agServer.postToServer(getDevId(), JSON.stringify(root))) {
|
|
||||||
resetWatchdog();
|
|
||||||
}
|
|
||||||
loopCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void resetWatchdog() {
|
|
||||||
Serial.println("Watchdog reset");
|
|
||||||
ag.watchdog.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wifiMangerClientConnected(void) {
|
|
||||||
return WiFi.softAPgetStationNum() ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wifi Manager
|
|
||||||
void connectToWifi() {
|
|
||||||
wifiSSID = "airgradient-" + String(getNormalizedMac());
|
|
||||||
|
|
||||||
wifiManager.setConfigPortalBlocking(false);
|
|
||||||
wifiManager.setTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
|
|
||||||
|
|
||||||
wifiManager.setAPCallback([](WiFiManager *obj) {
|
|
||||||
/** This callback if wifi connnected failed and try to start configuration
|
|
||||||
* portal */
|
|
||||||
ledSmState = APP_SM_WIFI_MANAGER_MODE;
|
|
||||||
});
|
|
||||||
wifiManager.setSaveConfigCallback([]() {
|
|
||||||
/** Wifi connected save the configuration */
|
|
||||||
ledSmHandler(APP_SM_WIFI_MANAGER_STA_CONNECTED);
|
|
||||||
});
|
|
||||||
wifiManager.setSaveParamsCallback([]() {
|
|
||||||
/** Wifi set connect: ssid, password */
|
|
||||||
ledSmHandler(APP_SM_WIFI_MANAGER_STA_CONNECTING);
|
|
||||||
});
|
|
||||||
wifiManager.autoConnect(wifiSSID.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT);
|
|
||||||
|
|
||||||
xTaskCreate(
|
|
||||||
[](void *obj) {
|
|
||||||
while (wifiManager.getConfigPortalActive()) {
|
|
||||||
wifiManager.process();
|
|
||||||
}
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
},
|
|
||||||
"wifi_cfg", 4096, NULL, 10, NULL);
|
|
||||||
|
|
||||||
uint32_t stimer = millis();
|
|
||||||
bool clientConnectChanged = false;
|
|
||||||
while (wifiManager.getConfigPortalActive()) {
|
|
||||||
if (WiFi.isConnected() == false) {
|
|
||||||
if (ledSmState == APP_SM_WIFI_MANAGER_MODE) {
|
|
||||||
uint32_t ms = (uint32_t)(millis() - stimer);
|
|
||||||
if (ms >= 100) {
|
|
||||||
stimer = millis();
|
|
||||||
ledSmHandler(ledSmState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Check for client connect to change led color */
|
|
||||||
bool clientConnected = wifiMangerClientConnected();
|
|
||||||
if (clientConnected != clientConnectChanged) {
|
|
||||||
clientConnectChanged = clientConnected;
|
|
||||||
if (clientConnectChanged) {
|
|
||||||
ledSmHandler(APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE);
|
|
||||||
} else {
|
|
||||||
ledSmHandler(APP_SM_WIFI_MANAGER_MODE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Show display wifi connect result failed */
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
delay(2000);
|
|
||||||
if (WiFi.isConnected() == false) {
|
|
||||||
ledSmHandler(APP_SM_WIFI_MANAGER_CONNECT_FAILED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String getNormalizedMac() {
|
|
||||||
String mac = WiFi.macAddress();
|
|
||||||
mac.replace(":", "");
|
|
||||||
mac.toLowerCase();
|
|
||||||
return mac;
|
|
||||||
}
|
|
||||||
|
|
||||||
void boardInit(void) {
|
|
||||||
if (Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin()) == false) {
|
|
||||||
failedHandler("Init I2C failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
ag.watchdog.begin();
|
|
||||||
|
|
||||||
ag.button.begin();
|
|
||||||
|
|
||||||
ag.statusLed.begin();
|
|
||||||
|
|
||||||
/** detect sensor: PMS5003, PMS5003T, SGP41 and S8 */
|
|
||||||
if (ag.s8.begin(Serial1) == false) {
|
|
||||||
Serial.println("S8 not detect run mode 'PPT'");
|
|
||||||
fw_mode = FW_MODE_PPT;
|
|
||||||
|
|
||||||
/** De-initialize Serial1 */
|
|
||||||
Serial1.end();
|
|
||||||
}
|
|
||||||
if (ag.sgp41.begin(Wire) == false) {
|
|
||||||
if (fw_mode == FW_MODE_PST) {
|
|
||||||
failedHandler("Init SGP41 failed");
|
|
||||||
} else {
|
|
||||||
Serial.println("SGP41 not detect run mode 'PP'");
|
|
||||||
fw_mode = FW_MODE_PP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ag.pms5003t_1.begin(Serial0) == false) {
|
|
||||||
failedHandler("Init PMS5003T_1 failed");
|
|
||||||
}
|
|
||||||
if (fw_mode != FW_MODE_PST) {
|
|
||||||
if (ag.pms5003t_2.begin(Serial1) == false) {
|
|
||||||
failedHandler("Init PMS5003T_2 failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fw_mode != FW_MODE_PST) {
|
|
||||||
pmsSchedule.setPeriod(2000);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("Firmware node: %s\r\n", getFwMode(fw_mode));
|
|
||||||
}
|
|
||||||
|
|
||||||
void failedHandler(String msg) {
|
|
||||||
while (true) {
|
|
||||||
Serial.println(msg);
|
|
||||||
vTaskDelay(1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void co2Calibration(void) {
|
|
||||||
/** Count down for co2CalibCountdown secs */
|
|
||||||
for (int i = 0; i < SENSOR_CO2_CALIB_COUNTDOWN_MAX; i++) {
|
|
||||||
Serial.printf("Start CO2 calib after %d sec\r\n",
|
|
||||||
SENSOR_CO2_CALIB_COUNTDOWN_MAX - i);
|
|
||||||
delay(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ag.s8.setBaselineCalibration()) {
|
|
||||||
Serial.println("Calibration success");
|
|
||||||
delay(1000);
|
|
||||||
Serial.println("Wait for calib finish...");
|
|
||||||
int count = 0;
|
|
||||||
while (ag.s8.isBaseLineCalibrationDone() == false) {
|
|
||||||
delay(1000);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
Serial.printf("Calib finish after %d sec\r\n", count);
|
|
||||||
delay(2000);
|
|
||||||
} else {
|
|
||||||
Serial.println("Calibration failure!!!");
|
|
||||||
delay(2000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief WiFi reconnect handler
|
|
||||||
*/
|
|
||||||
static void updateWiFiConnect(void) {
|
|
||||||
static uint32_t lastRetry;
|
|
||||||
if (wifiHasConfig == false) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (WiFi.isConnected()) {
|
|
||||||
lastRetry = millis();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uint32_t ms = (uint32_t)(millis() - lastRetry);
|
|
||||||
if (ms >= WIFI_CONNECT_RETRY_MS) {
|
|
||||||
lastRetry = millis();
|
|
||||||
WiFi.reconnect();
|
|
||||||
|
|
||||||
Serial.printf("Re-Connect WiFi\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Update tvocIndexindex
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static void tvocPoll(void) {
|
|
||||||
tvocIndex = ag.sgp41.getTvocIndex();
|
|
||||||
noxIndex = ag.sgp41.getNoxIndex();
|
|
||||||
|
|
||||||
Serial.printf("tvocIndexindex: %d\r\n", tvocIndex);
|
|
||||||
Serial.printf(" NOx index: %d\r\n", noxIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Update PMS data
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static void pmPoll(void) {
|
|
||||||
if (fw_mode == FW_MODE_PST) {
|
|
||||||
if (ag.pms5003t_1.readData()) {
|
|
||||||
pm01_1 = ag.pms5003t_1.getPm01Ae();
|
|
||||||
pm25_1 = ag.pms5003t_1.getPm25Ae();
|
|
||||||
pm25_1 = ag.pms5003t_1.getPm10Ae();
|
|
||||||
pm03PCount_1 = ag.pms5003t_1.getPm03ParticleCount();
|
|
||||||
temp_1 = ag.pms5003t_1.getTemperature();
|
|
||||||
hum_1 = ag.pms5003t_1.getRelativeHumidity();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (ag.pms5003t_1.readData() && ag.pms5003t_2.readData()) {
|
|
||||||
pm1Value01 = pm1Value01 + ag.pms5003t_1.getPm01Ae();
|
|
||||||
pm1Value25 = pm1Value25 + ag.pms5003t_1.getPm25Ae();
|
|
||||||
pm1Value10 = pm1Value10 + ag.pms5003t_1.getPm10Ae();
|
|
||||||
pm1PCount = pm1PCount + ag.pms5003t_1.getPm03ParticleCount();
|
|
||||||
pm1temp = pm1temp + ag.pms5003t_1.getTemperature();
|
|
||||||
pm1hum = pm1hum + ag.pms5003t_1.getRelativeHumidity();
|
|
||||||
pm2Value01 = pm2Value01 + ag.pms5003t_2.getPm01Ae();
|
|
||||||
pm2Value25 = pm2Value25 + ag.pms5003t_2.getPm25Ae();
|
|
||||||
pm2Value10 = pm2Value10 + ag.pms5003t_2.getPm10Ae();
|
|
||||||
pm2PCount = pm2PCount + ag.pms5003t_2.getPm03ParticleCount();
|
|
||||||
pm2temp = pm2temp + ag.pms5003t_2.getTemperature();
|
|
||||||
pm2hum = pm2hum + ag.pms5003t_2.getRelativeHumidity();
|
|
||||||
countPosition++;
|
|
||||||
if (countPosition == targetCount) {
|
|
||||||
pm01_1 = pm1Value01 / targetCount;
|
|
||||||
pm25_1 = pm1Value25 / targetCount;
|
|
||||||
pm10_1 = pm1Value10 / targetCount;
|
|
||||||
pm03PCount_1 = pm1PCount / targetCount;
|
|
||||||
temp_1 = pm1temp / targetCount;
|
|
||||||
hum_1 = pm1hum / targetCount;
|
|
||||||
pm01_2 = pm2Value01 / targetCount;
|
|
||||||
pm25_2 = pm2Value25 / targetCount;
|
|
||||||
pm10_2 = pm2Value10 / targetCount;
|
|
||||||
pm03PCount_2 = pm2PCount / targetCount;
|
|
||||||
temp_2 = pm2temp / targetCount;
|
|
||||||
hum_2 = pm2hum / targetCount;
|
|
||||||
|
|
||||||
countPosition = 0;
|
|
||||||
|
|
||||||
pm1Value01 = 0;
|
|
||||||
pm1Value25 = 0;
|
|
||||||
pm1Value10 = 0;
|
|
||||||
pm1PCount = 0;
|
|
||||||
pm1temp = 0;
|
|
||||||
pm1hum = 0;
|
|
||||||
pm2Value01 = 0;
|
|
||||||
pm2Value25 = 0;
|
|
||||||
pm2Value10 = 0;
|
|
||||||
pm2PCount = 0;
|
|
||||||
pm2temp = 0;
|
|
||||||
pm2hum = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void co2Poll(void) {
|
|
||||||
co2Ppm = ag.s8.getCo2();
|
|
||||||
Serial.printf("CO2 index: %d\r\n", co2Ppm);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void serverConfigPoll(void) {
|
|
||||||
if (agServer.pollServerConfig(getDevId())) {
|
|
||||||
/** Only support CO2 S8 sensor on FW_MODE_PST */
|
|
||||||
if (fw_mode == FW_MODE_PST) {
|
|
||||||
if (agServer.isCo2Calib()) {
|
|
||||||
co2Calibration();
|
|
||||||
}
|
|
||||||
if (agServer.getCo2Abccalib() > 0) {
|
|
||||||
if (ag.s8.setAutoCalib(agServer.getCo2Abccalib() * 24) == false) {
|
|
||||||
Serial.println("Set S8 auto calib failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static String getDevId(void) { return getNormalizedMac(); }
|
|
||||||
|
|
||||||
void ledBlinkDelay(uint32_t tdelay) {
|
|
||||||
ag.statusLed.setOn();
|
|
||||||
delay(tdelay);
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
delay(tdelay);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ledSmHandler(int sm) {
|
|
||||||
if (sm > APP_SM_NORMAL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ledSmState = sm;
|
|
||||||
switch (sm) {
|
|
||||||
case APP_SM_WIFI_MANAGER_MODE: {
|
|
||||||
ag.statusLed.setToggle();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_MAMAGER_PORTAL_ACTIVE: {
|
|
||||||
ag.statusLed.setOn();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_MANAGER_STA_CONNECTING: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_MANAGER_STA_CONNECTED: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNECTING: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNNECTED: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
|
|
||||||
/** two time slow blink, then off */
|
|
||||||
for (int i = 0; i < 2; i++) {
|
|
||||||
ledBlinkDelay(LED_SLOW_BLINK_DELAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_MANAGER_CONNECT_FAILED: {
|
|
||||||
/** Three time fast blink then 2 sec pause. Repeat 3 times */
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
|
|
||||||
for (int j = 0; j < 3; j++) {
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
ledBlinkDelay(LED_FAST_BLINK_DELAY);
|
|
||||||
}
|
|
||||||
delay(2000);
|
|
||||||
}
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_OK_SERVER_CONNECT_FAILED: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
for (int j = 0; j < 3; j++) {
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
ledBlinkDelay(LED_FAST_BLINK_DELAY);
|
|
||||||
}
|
|
||||||
delay(2000);
|
|
||||||
}
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
for (int j = 0; j < 3; j++) {
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
ledBlinkDelay(LED_FAST_BLINK_DELAY);
|
|
||||||
}
|
|
||||||
delay(2000);
|
|
||||||
}
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_WIFI_LOST: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_SERVER_LOST: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_SENSOR_CONFIG_FAILED: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case APP_SM_NORMAL: {
|
|
||||||
ag.statusLed.setOff();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *getFwMode(int mode) {
|
|
||||||
switch (mode) {
|
|
||||||
case FW_MODE_PST:
|
|
||||||
return "FW_MODE_PST";
|
|
||||||
case FW_MODE_PPT:
|
|
||||||
return "FW_MODE_PPT";
|
|
||||||
case FW_MODE_PP:
|
|
||||||
return "FW_MODE_PP";
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return "FW_MODE_UNKNOW";
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
name=AirGradient Air Quality Sensor
|
name=AirGradient Air Quality Sensor
|
||||||
version=3.0.3
|
version=3.0.4
|
||||||
author=AirGradient <support@airgradient.com>
|
author=AirGradient <support@airgradient.com>
|
||||||
maintainer=AirGradient <support@airgradient.com>
|
maintainer=AirGradient <support@airgradient.com>
|
||||||
sentence=ESP32-C3 / ESP8266 library for air quality monitor measuring PM, CO2, Temperature, TVOC and Humidity with OLED display.
|
sentence=ESP32-C3 / ESP8266 library for air quality monitor measuring PM, CO2, Temperature, TVOC and Humidity with OLED display.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "AirGradient.h"
|
#include "AirGradient.h"
|
||||||
|
|
||||||
#define AG_LIB_VER "3.0.3"
|
#define AG_LIB_VER "3.0.4"
|
||||||
|
|
||||||
AirGradient::AirGradient(BoardType type)
|
AirGradient::AirGradient(BoardType type)
|
||||||
: pms5003(type), pms5003t_1(type), pms5003t_2(type), s8(type), sgp41(type),
|
: pms5003(type), pms5003t_1(type), pms5003t_2(type), s8(type), sgp41(type),
|
||||||
@ -36,3 +36,7 @@ int AirGradient::getI2cSclPin(void) {
|
|||||||
String AirGradient::getVersion(void) { return AG_LIB_VER; }
|
String AirGradient::getVersion(void) { return AG_LIB_VER; }
|
||||||
|
|
||||||
BoardType AirGradient::getBoardType(void) { return boardType; }
|
BoardType AirGradient::getBoardType(void) { return boardType; }
|
||||||
|
|
||||||
|
double AirGradient::round2(double value) {
|
||||||
|
return (int)(value * 100 + 0.5) / 100.0;
|
||||||
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#ifndef _AIR_GRADIENT_H_
|
#ifndef _AIR_GRADIENT_H_
|
||||||
#define _AIR_GRADIENT_H_
|
#define _AIR_GRADIENT_H_
|
||||||
|
|
||||||
#include "display/oled.h"
|
#include "Display/Display.h"
|
||||||
#include "main/BoardDef.h"
|
#include "Main/BoardDef.h"
|
||||||
#include "main/HardwareWatchdog.h"
|
#include "Main/HardwareWatchdog.h"
|
||||||
#include "main/LedBar.h"
|
#include "Main/LedBar.h"
|
||||||
#include "main/PushButton.h"
|
#include "Main/PushButton.h"
|
||||||
#include "main/StatusLed.h"
|
#include "Main/StatusLed.h"
|
||||||
#include "pms/pms5003.h"
|
#include "PMS/PMS5003.h"
|
||||||
#include "pms/pms5003t.h"
|
#include "PMS/PMS5003T.h"
|
||||||
#include "s8/s8.h"
|
#include "S8/S8.h"
|
||||||
#include "sgp41/sgp41.h"
|
#include "Sgp41/Sgp41.h"
|
||||||
#include "sht/sht.h"
|
#include "Sht/Sht.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Class with define all the sensor has supported by Airgradient. Each
|
* @brief Class with define all the sensor has supported by Airgradient. Each
|
||||||
@ -107,6 +107,14 @@ public:
|
|||||||
*/
|
*/
|
||||||
String getVersion(void);
|
String getVersion(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Round double value with for 2 decimal
|
||||||
|
*
|
||||||
|
* @param valuem Round value
|
||||||
|
* @return double
|
||||||
|
*/
|
||||||
|
double round2(double value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BoardType boardType;
|
BoardType boardType;
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "oled.h"
|
#include "Display.h"
|
||||||
#include "../library/Adafruit_SH110x/Adafruit_SH110X.h"
|
#include "../Libraries/Adafruit_SH110x/Adafruit_SH110X.h"
|
||||||
#include "../library/Adafruit_SSD1306_Wemos_OLED/Adafruit_SSD1306.h"
|
#include "../Libraries/Adafruit_SSD1306_Wemos_OLED/Adafruit_SSD1306.h"
|
||||||
|
|
||||||
#define disp(func) \
|
#define disp(func) \
|
||||||
if (this->_boardType == DIY_BASIC) { \
|
if (this->_boardType == DIY_BASIC) { \
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user