mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-06-26 08:11:33 +02:00
Compare commits
71 Commits
Author | SHA1 | Date | |
---|---|---|---|
d78205aa20 | |||
c1228bbd06 | |||
1eb43f684b | |||
4798e44cb7 | |||
a867e9af38 | |||
a59d5a1bb8 | |||
b4d6006678 | |||
236c5bab84 | |||
852fdc4360 | |||
f7e85a92e8 | |||
e99fc2ecdc | |||
67785ed99b | |||
45ac4f116b | |||
173e3caf2f | |||
351af57591 | |||
0bda7a1c4b | |||
9a31c107fa | |||
5449fa15ea | |||
3e4e2affa8 | |||
d3a242a0b7 | |||
e5e2887c4d | |||
4eda2e4cb5 | |||
fcee721d58 | |||
7d12e63e34 | |||
8ff8b7929e | |||
66c53daed6 | |||
5de3a34dd0 | |||
b94112e22a | |||
99e925e7bd | |||
d8cba0d346 | |||
39de897621 | |||
9f1a793848 | |||
be9ba88d52 | |||
0084b6fb91 | |||
75b579bafa | |||
cf5ff99d8a | |||
ea204d90b1 | |||
13f6c2c747 | |||
760f827d0d | |||
8c8e0d4dea | |||
b749495bf4 | |||
7e3eabf09f | |||
e636876c9b | |||
68953d7390 | |||
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 |
51
.github/workflows/check.yml
vendored
Normal file
51
.github/workflows/check.yml
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
compile:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- "BASIC"
|
||||
- "ONE"
|
||||
- "Open_Air"
|
||||
- "TestCO2"
|
||||
- "TestPM"
|
||||
- "TestSht"
|
||||
fqbn:
|
||||
- "esp8266:esp8266:d1_mini"
|
||||
- "esp32:esp32:esp32c3"
|
||||
include:
|
||||
- fqbn: "esp8266:esp8266:d1_mini"
|
||||
core: "esp8266:esp8266@3.1.2"
|
||||
core_url: "https://arduino.esp8266.com/stable/package_esp8266com_index.json"
|
||||
- fqbn: "esp32:esp32:esp32c3"
|
||||
board_options: "JTAGAdapter=default,CDCOnBoot=cdc,PartitionScheme=default,CPUFreq=160,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=verbose,EraseFlash=none"
|
||||
core: "esp32:esp32@2.0.11"
|
||||
exclude:
|
||||
- example: "BASIC_v4"
|
||||
fqbn: "esp32:esp32:esp32c3"
|
||||
- example: "ONE_I-9PSL"
|
||||
fqbn: "esp8266:esp8266:d1_mini"
|
||||
- example: "Open_Air"
|
||||
fqbn: "esp8266:esp8266:d1_mini"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run:
|
||||
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh |
|
||||
sh -s 0.35.3
|
||||
- run: bin/arduino-cli --verbose core install '${{ matrix.core }}'
|
||||
--additional-urls '${{ matrix.core_url }}'
|
||||
- run: bin/arduino-cli --verbose lib install
|
||||
WiFiManager@2.0.16-rc.2
|
||||
Arduino_JSON@0.2.0
|
||||
U8g2@2.34.22
|
||||
- run: bin/arduino-cli --verbose lib install --git-url .
|
||||
env:
|
||||
ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL: "true"
|
||||
- run: bin/arduino-cli --verbose compile 'examples/${{ matrix.example }}'
|
||||
--fqbn '${{ matrix.fqbn }}' --board-options '${{ matrix.board_options }}'
|
||||
# TODO: at this point it would be a good idea to run some smoke tests on
|
||||
# the resulting image (e.g. that it boots successfully and sends metrics)
|
||||
# but that would either require a high fidelity device emulator, or a
|
||||
# "hardware lab" runner that is directly connected to a relevant device.
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.vscode
|
||||
*.DS_Store
|
||||
build
|
||||
.vscode
|
||||
|
@ -50,7 +50,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
||||
#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 SENSOR_TEMP_HUM_UPDATE_INTERVAL 2000 /** ms */
|
||||
#define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */
|
||||
#define WIFI_HOTSPOT_PASSWORD_DEFAULT \
|
||||
"cleanair" /** default WiFi AP password \
|
||||
@ -79,8 +79,8 @@ public:
|
||||
/** Call handler */
|
||||
handler();
|
||||
|
||||
Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
||||
(unsigned int)handler, period);
|
||||
// Serial.printf("[AgSchedule] handle 0x%08x, period: %d(ms)\r\n",
|
||||
// (unsigned int)handler, period);
|
||||
|
||||
/** Update period time */
|
||||
count = millis();
|
||||
@ -115,7 +115,7 @@ public:
|
||||
* @return true Success
|
||||
* @return false Failure
|
||||
*/
|
||||
bool pollServerConfig(String id) {
|
||||
bool fetchServerConfiguration(String id) {
|
||||
String uri =
|
||||
"http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config";
|
||||
|
||||
@ -271,7 +271,7 @@ public:
|
||||
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 false Success
|
||||
@ -305,7 +305,7 @@ public:
|
||||
*
|
||||
* @return int days, -1 if invalid.
|
||||
*/
|
||||
int getCo2Abccalib(void) { return co2AbcCalib; }
|
||||
int getCo2AbcDaysConfig(void) { return co2AbcCalib; }
|
||||
|
||||
/**
|
||||
* @brief Get device configuration model name
|
||||
@ -359,7 +359,7 @@ AirGradient ag = AirGradient(DIY_BASIC);
|
||||
|
||||
static int co2Ppm = -1;
|
||||
static int pm25 = -1;
|
||||
static float temp = -1;
|
||||
static float temp = -1001;
|
||||
static int hum = -1;
|
||||
static long val;
|
||||
static String wifiSSID = "";
|
||||
@ -368,24 +368,30 @@ static bool wifiHasConfig = false; /** */
|
||||
static void boardInit(void);
|
||||
static void failedHandler(String msg);
|
||||
static void co2Calibration(void);
|
||||
static void serverConfigPoll(void);
|
||||
static void co2Poll(void);
|
||||
static void pmPoll(void);
|
||||
static void tempHumPoll(void);
|
||||
static void updateServerConfiguration(void);
|
||||
static void co2Update(void);
|
||||
static void pmUpdate(void);
|
||||
static void tempHumUpdate(void);
|
||||
static void sendDataToServer(void);
|
||||
static void dispHandler(void);
|
||||
static String getDevId(void);
|
||||
static void updateWiFiConnect(void);
|
||||
static void showNr(void);
|
||||
|
||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll);
|
||||
bool hasSensorS8 = true;
|
||||
bool hasSensorPMS = true;
|
||||
bool hasSensorSHT = true;
|
||||
int pmFailCount = 0;
|
||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, updateServerConfiguration);
|
||||
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
|
||||
AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler);
|
||||
AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Poll);
|
||||
AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmPoll);
|
||||
AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumPoll);
|
||||
AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update);
|
||||
AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmUpdate);
|
||||
AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
showNr();
|
||||
|
||||
/** Init I2C */
|
||||
Wire.begin(ag.getI2cSdaPin(), ag.getI2cSclPin());
|
||||
@ -407,7 +413,7 @@ void setup() {
|
||||
wifiHasConfig = true;
|
||||
sendPing();
|
||||
|
||||
agServer.pollServerConfig(getDevId());
|
||||
agServer.fetchServerConfiguration(getDevId());
|
||||
if (agServer.isCo2Calib()) {
|
||||
co2Calibration();
|
||||
}
|
||||
@ -436,9 +442,15 @@ void loop() {
|
||||
configSchedule.run();
|
||||
serverSchedule.run();
|
||||
dispSchedule.run();
|
||||
co2Schedule.run();
|
||||
pmsSchedule.run();
|
||||
tempHumSchedule.run();
|
||||
if (hasSensorS8) {
|
||||
co2Schedule.run();
|
||||
}
|
||||
if (hasSensorPMS) {
|
||||
pmsSchedule.run();
|
||||
}
|
||||
if (hasSensorSHT) {
|
||||
tempHumSchedule.run();
|
||||
}
|
||||
|
||||
updateWiFiConnect();
|
||||
}
|
||||
@ -469,6 +481,7 @@ void displayShowText(String ln1, String ln2, String ln3) {
|
||||
ag.display.setText(ln3);
|
||||
|
||||
ag.display.show();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// Wifi Manager
|
||||
@ -507,22 +520,28 @@ void connectToWifi() {
|
||||
static void boardInit(void) {
|
||||
/** Init SHT sensor */
|
||||
if (ag.sht.begin(Wire) == false) {
|
||||
failedHandler("SHT init failed");
|
||||
hasSensorSHT = false;
|
||||
Serial.println("SHT sensor not found");
|
||||
}
|
||||
|
||||
/** CO2 init */
|
||||
if (ag.s8.begin(&Serial) == false) {
|
||||
failedHandler("SenseAirS8 init failed");
|
||||
Serial.println("CO2 S8 snsor not found");
|
||||
hasSensorS8 = false;
|
||||
}
|
||||
|
||||
/** PMS init */
|
||||
if (ag.pms5003.begin(&Serial) == false) {
|
||||
failedHandler("PMS5003 init failed");
|
||||
Serial.println("PMS sensor not found");
|
||||
hasSensorPMS = false;
|
||||
}
|
||||
|
||||
/** Display init */
|
||||
ag.display.begin(Wire);
|
||||
ag.display.setTextColor(1);
|
||||
ag.display.clear();
|
||||
ag.display.show();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
static void failedHandler(String msg) {
|
||||
@ -557,34 +576,59 @@ static void co2Calibration(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static void serverConfigPoll(void) {
|
||||
if (agServer.pollServerConfig(getDevId())) {
|
||||
static void updateServerConfiguration(void) {
|
||||
if (agServer.fetchServerConfiguration(getDevId())) {
|
||||
if (agServer.isCo2Calib()) {
|
||||
co2Calibration();
|
||||
if (hasSensorS8) {
|
||||
co2Calibration();
|
||||
} else {
|
||||
Serial.println("CO2 S8 not available, calib ignored");
|
||||
}
|
||||
}
|
||||
if (agServer.getCo2Abccalib() > 0) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void co2Poll() {
|
||||
static void co2Update() {
|
||||
co2Ppm = ag.s8.getCo2();
|
||||
Serial.printf("CO2 index: %d\r\n", co2Ppm);
|
||||
}
|
||||
|
||||
void pmPoll() {
|
||||
void pmUpdate() {
|
||||
if (ag.pms5003.readData()) {
|
||||
pm25 = ag.pms5003.getPm25Ae();
|
||||
Serial.printf("PMS2.5: %d\r\n", pm25);
|
||||
pmFailCount = 0;
|
||||
} else {
|
||||
pm25 = -1;
|
||||
Serial.printf("PM read failed, %d", pmFailCount);
|
||||
pmFailCount++;
|
||||
if (pmFailCount >= 3) {
|
||||
pm25 = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void tempHumPoll() {
|
||||
static void tempHumUpdate() {
|
||||
if (ag.sht.measure()) {
|
||||
temp = ag.sht.getTemperature();
|
||||
hum = ag.sht.getRelativeHumidity();
|
||||
@ -604,8 +648,8 @@ static void sendDataToServer() {
|
||||
if (pm25 >= 0) {
|
||||
root["pm02"] = pm25;
|
||||
}
|
||||
if (temp >= 0) {
|
||||
root["atmp"] = temp;
|
||||
if (temp > -1001) {
|
||||
root["atmp"] = ag.round2(temp);
|
||||
}
|
||||
if (hum >= 0) {
|
||||
root["rhum"] = hum;
|
||||
@ -622,16 +666,42 @@ static void dispHandler() {
|
||||
String ln3 = "";
|
||||
|
||||
if (agServer.isPMSinUSAQI()) {
|
||||
ln1 = "AQI:" + String(ag.pms5003.convertPm25ToUsAqi(pm25));
|
||||
if (pm25 < 0) {
|
||||
ln1 = "AQI: -";
|
||||
} else {
|
||||
ln1 = "AQI:" + String(ag.pms5003.convertPm25ToUsAqi(pm25));
|
||||
}
|
||||
} else {
|
||||
ln1 = "PM :" + String(pm25) + " ug";
|
||||
if (pm25 < 0) {
|
||||
ln1 = "PM :- ug";
|
||||
|
||||
} else {
|
||||
ln1 = "PM :" + String(pm25) + " ug";
|
||||
}
|
||||
}
|
||||
ln2 = "CO2:" + String(co2Ppm);
|
||||
if (co2Ppm > -1001) {
|
||||
ln2 = "CO2:" + String(co2Ppm);
|
||||
} else {
|
||||
ln2 = "CO2: -";
|
||||
}
|
||||
|
||||
String _hum = "-";
|
||||
if (hum > 0) {
|
||||
_hum = String(hum);
|
||||
}
|
||||
|
||||
String _temp = "-";
|
||||
|
||||
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 {
|
||||
ln3 = String(temp).substring(0, 4) + " " + String(hum) + "%";
|
||||
if (temp > -1001) {
|
||||
_temp = String(temp).substring(0, 4);
|
||||
}
|
||||
ln3 = _temp + " " + _hum + "%";
|
||||
}
|
||||
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 mac = WiFi.macAddress();
|
||||
mac.replace(":", "");
|
File diff suppressed because it is too large
Load Diff
1589
examples/Open_Air/Open_Air.ino
Normal file
1589
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
|
||||
version=3.0.3
|
||||
version=3.0.6
|
||||
author=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.
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "AirGradient.h"
|
||||
|
||||
#define AG_LIB_VER "3.0.3"
|
||||
#define AG_LIB_VER "3.0.6"
|
||||
|
||||
AirGradient::AirGradient(BoardType type)
|
||||
: pms5003(type), pms5003t_1(type), pms5003t_2(type), s8(type), sgp41(type),
|
||||
@ -36,3 +36,11 @@ int AirGradient::getI2cSclPin(void) {
|
||||
String AirGradient::getVersion(void) { return AG_LIB_VER; }
|
||||
|
||||
BoardType AirGradient::getBoardType(void) { return boardType; }
|
||||
|
||||
double AirGradient::round2(double value) {
|
||||
return (int)(value * 100 + 0.5) / 100.0;
|
||||
}
|
||||
|
||||
String AirGradient::getBoardName(void) {
|
||||
return String(getBoardDefName(boardType));
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
#ifndef _AIR_GRADIENT_H_
|
||||
#define _AIR_GRADIENT_H_
|
||||
|
||||
#include "display/oled.h"
|
||||
#include "main/BoardDef.h"
|
||||
#include "main/HardwareWatchdog.h"
|
||||
#include "main/LedBar.h"
|
||||
#include "main/PushButton.h"
|
||||
#include "main/StatusLed.h"
|
||||
#include "pms/pms5003.h"
|
||||
#include "pms/pms5003t.h"
|
||||
#include "s8/s8.h"
|
||||
#include "sgp41/sgp41.h"
|
||||
#include "sht/sht.h"
|
||||
#include "Display/Display.h"
|
||||
#include "Main/BoardDef.h"
|
||||
#include "Main/HardwareWatchdog.h"
|
||||
#include "Main/LedBar.h"
|
||||
#include "Main/PushButton.h"
|
||||
#include "Main/StatusLed.h"
|
||||
#include "PMS/PMS5003.h"
|
||||
#include "PMS/PMS5003T.h"
|
||||
#include "S8/S8.h"
|
||||
#include "Sgp41/Sgp41.h"
|
||||
#include "Sht/Sht.h"
|
||||
|
||||
/**
|
||||
* @brief Class with define all the sensor has supported by Airgradient. Each
|
||||
@ -107,6 +107,21 @@ public:
|
||||
*/
|
||||
String getVersion(void);
|
||||
|
||||
/**
|
||||
* @brief Get the Board Name object
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String getBoardName(void);
|
||||
|
||||
/**
|
||||
* @brief Round double value with for 2 decimal
|
||||
*
|
||||
* @param valuem Round value
|
||||
* @return double
|
||||
*/
|
||||
double round2(double value);
|
||||
|
||||
private:
|
||||
BoardType boardType;
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "oled.h"
|
||||
#include "../library/Adafruit_SH110x/Adafruit_SH110X.h"
|
||||
#include "../library/Adafruit_SSD1306_Wemos_OLED/Adafruit_SSD1306.h"
|
||||
#include "Display.h"
|
||||
#include "../Libraries/Adafruit_SH110x/Adafruit_SH110X.h"
|
||||
#include "../Libraries/Adafruit_SSD1306_Wemos_OLED/Adafruit_SSD1306.h"
|
||||
|
||||
#define disp(func) \
|
||||
if (this->_boardType == DIY_BASIC) { \
|
@ -1,7 +1,7 @@
|
||||
#ifndef _AIR_GRADIENT_OLED_H_
|
||||
#define _AIR_GRADIENT_OLED_H_
|
||||
|
||||
#include "../main/BoardDef.h"
|
||||
#include "../Main/BoardDef.h"
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user