diff --git a/examples/BASIC_v4/BASIC_v4.ino b/examples/BASIC_v4/BASIC_v4.ino index cc0dc1c..43a61c4 100644 --- a/examples/BASIC_v4/BASIC_v4.ino +++ b/examples/BASIC_v4/BASIC_v4.ino @@ -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 \ @@ -115,7 +115,7 @@ public: * @return true Success * @return false Failure */ - bool pollServerConfig(String id) { + bool fetchServerConfigure(String id) { String uri = "http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config"; @@ -368,10 +368,10 @@ 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 serverConfigUpdate(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); @@ -382,12 +382,12 @@ 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, serverConfigUpdate); 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); @@ -413,7 +413,7 @@ void setup() { wifiHasConfig = true; sendPing(); - agServer.pollServerConfig(getDevId()); + agServer.fetchServerConfigure(getDevId()); if (agServer.isCo2Calib()) { co2Calibration(); } @@ -576,8 +576,8 @@ static void co2Calibration(void) { } } -static void serverConfigPoll(void) { - if (agServer.pollServerConfig(getDevId())) { +static void serverConfigUpdate(void) { + if (agServer.fetchServerConfigure(getDevId())) { if (agServer.isCo2Calib()) { if (hasSensorS8) { co2Calibration(); @@ -609,12 +609,12 @@ static void serverConfigPoll(void) { } } -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); @@ -628,7 +628,7 @@ void pmPoll() { } } -static void tempHumPoll() { +static void tempHumUpdate() { if (ag.sht.measure()) { temp = ag.sht.getTemperature(); hum = ag.sht.getRelativeHumidity(); diff --git a/examples/ONE_I-9PSL/ONE_I-9PSL.ino b/examples/ONE_I-9PSL/ONE_I-9PSL.ino index 55f435e..c7955d6 100644 --- a/examples/ONE_I-9PSL/ONE_I-9PSL.ino +++ b/examples/ONE_I-9PSL/ONE_I-9PSL.ino @@ -98,7 +98,7 @@ enum { #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 \ @@ -167,13 +167,14 @@ public: } /** - * @brief Get server configuration + * @brief Fetch server configuration, if get sucessed and configuratrion + * parameter has changed store into local storage * * @param id Device ID * @return true Success * @return false Failure */ - bool pollServerConfig(String id) { + bool fetchServerConfigure(String id) { String uri = "http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config"; @@ -680,7 +681,7 @@ static String wifiSSID = ""; static void boardInit(void); static void failedHandler(String msg); -static void serverConfigPoll(void); +static void serverConfigUpdate(void); static void co2Calibration(void); static void setRGBledPMcolor(int pm25Value); static void ledSmHandler(int sm); @@ -690,12 +691,12 @@ static void sensorLedColorHandler(void); static void appLedHandler(void); static void appDispHandler(void); static void updateWiFiConnect(void); -static void updateDispLedBar(void); -static void tvocPoll(void); -static void pmPoll(void); +static void displayAndLedBarUpdate(void); +static void tvocUpdate(void); +static void pmUpdate(void); static void sendDataToServer(void); -static void tempHumPoll(void); -static void co2Poll(void); +static void tempHumUpdate(void); +static void co2Update(void); static void showNr(void); static void webServerInit(void); static String getServerSyncData(bool localServer); @@ -709,13 +710,13 @@ bool hasSensorSGP = true; bool hasSensorSHT = true; int pmFailCount = 0; uint32_t factoryBtnPressTime = 0; -AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, updateDispLedBar); -AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll); +AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, displayAndLedBarUpdate); +AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigUpdate); AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer); -AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Poll); -AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmPoll); -AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumPoll); -AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocPoll); +AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update); +AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmUpdate); +AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate); +AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocUpdate); void setup() { EEPROM.begin(512); @@ -785,7 +786,7 @@ void setup() { Serial.println(WiFi.localIP()); /** Get first connected to wifi */ - agServer.pollServerConfig(getDevId()); + agServer.fetchServerConfigure(getDevId()); if (agServer.isConfigFailed()) { dispSmHandler(APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED); ledSmHandler(APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED); @@ -903,7 +904,7 @@ static void ledTest2Min(void) { } } -static void co2Poll(void) { +static void co2Update(void) { co2Ppm = ag.s8.getCo2(); Serial.printf("CO2 index: %d\r\n", co2Ppm); } @@ -917,14 +918,15 @@ void webServerMeasureCurrentGet(void) { /** * Sends metrics in Prometheus/OpenMetrics format to the currently connected * webServer client. - * - * For background, see: https://prometheus.io/docs/instrumenting/exposition_formats/ + * + * For background, see: + * https://prometheus.io/docs/instrumenting/exposition_formats/ */ void webServerMetricsGet(void) { String response; String current_metric_name; - const auto add_metric = [&](const String &name, const String &help, const String &type, const String &unit = "") - { + const auto add_metric = [&](const String &name, const String &help, + const String &type, const String &unit = "") { current_metric_name = "airgradient_" + name; if (!unit.isEmpty()) current_metric_name += "_" + unit; @@ -938,69 +940,114 @@ void webServerMetricsGet(void) { }; add_metric("info", "AirGradient device information", "info"); - add_metric_point("airgradient_serial_number=\"" + getDevId() + "\",airgradient_device_type=\"ONE_I-9PSL\",airgradient_library_version=\"" + ag.getVersion() + "\"", "1"); + add_metric_point("airgradient_serial_number=\"" + getDevId() + + "\",airgradient_device_type=\"" + ag.getBoardName() + + "\",airgradient_library_version=\"" + ag.getVersion() + + "\"", + "1"); - add_metric("config_ok", "1 if the AirGradient device was able to successfully fetch its configuration from the server", "gauge"); + add_metric("config_ok", + "1 if the AirGradient device was able to successfully fetch its " + "configuration from the server", + "gauge"); add_metric_point("", agServer.isConfigFailed() ? "0" : "1"); - add_metric("post_ok", "1 if the AirGradient device was able to successfully send to the server", "gauge"); + add_metric( + "post_ok", + "1 if the AirGradient device was able to successfully send to the server", + "gauge"); add_metric_point("", agServer.isServerFailed() ? "0" : "1"); - add_metric("wifi_rssi", "WiFi signal strength from the AirGradient device perspective, in dBm", "gauge", "dbm"); + add_metric( + "wifi_rssi", + "WiFi signal strength from the AirGradient device perspective, in dBm", + "gauge", "dbm"); add_metric_point("", String(WiFi.RSSI())); if (hasSensorS8 && co2Ppm >= 0) { - add_metric("co2", "Carbon dioxide concentration as measured by the AirGradient S8 sensor, in parts per million", "gauge", "ppm"); + add_metric("co2", + "Carbon dioxide concentration as measured by the AirGradient S8 " + "sensor, in parts per million", + "gauge", "ppm"); add_metric_point("", String(co2Ppm)); } if (hasSensorPMS) { if (pm01 >= 0) { - add_metric("pm1", "PM1.0 concentration as measured by the AirGradient PMS sensor, in micrograms per cubic meter", "gauge", "ugm3"); + add_metric("pm1", + "PM1.0 concentration as measured by the AirGradient PMS " + "sensor, in micrograms per cubic meter", + "gauge", "ugm3"); add_metric_point("", String(pm01)); } if (pm25 >= 0) { - add_metric("pm2d5", "PM2.5 concentration as measured by the AirGradient PMS sensor, in micrograms per cubic meter", "gauge", "ugm3"); + add_metric("pm2d5", + "PM2.5 concentration as measured by the AirGradient PMS " + "sensor, in micrograms per cubic meter", + "gauge", "ugm3"); add_metric_point("", String(pm25)); } if (pm10 >= 0) { - add_metric("pm10", "PM10 concentration as measured by the AirGradient PMS sensor, in micrograms per cubic meter", "gauge", "ugm3"); + add_metric("pm10", + "PM10 concentration as measured by the AirGradient PMS " + "sensor, in micrograms per cubic meter", + "gauge", "ugm3"); add_metric_point("", String(pm10)); } if (pm03PCount >= 0) { - add_metric("pm0d3", "PM0.3 concentration as measured by the AirGradient PMS sensor, in number of particules per 100 milliliters", "gauge", "p100ml"); + add_metric("pm0d3", + "PM0.3 concentration as measured by the AirGradient PMS " + "sensor, in number of particules per 100 milliliters", + "gauge", "p100ml"); add_metric_point("", String(pm03PCount)); } } if (hasSensorSGP) { if (tvocIndex >= 0) { - add_metric("tvoc_index", "The processed Total Volatile Organic Compounds (TVOC) index as measured by the AirGradient SGP sensor", "gauge"); + add_metric("tvoc_index", + "The processed Total Volatile Organic Compounds (TVOC) index " + "as measured by the AirGradient SGP sensor", + "gauge"); add_metric_point("", String(tvocIndex)); } if (tvocRawIndex >= 0) { - add_metric("tvoc_raw_index", "The raw input value to the Total Volatile Organic Compounds (TVOC) index as measured by the AirGradient SGP sensor", "gauge"); + add_metric("tvoc_raw_index", + "The raw input value to the Total Volatile Organic Compounds " + "(TVOC) index as measured by the AirGradient SGP sensor", + "gauge"); add_metric_point("", String(tvocRawIndex)); } if (noxIndex >= 0) { - add_metric("nox_index", "The processed Nitrous Oxide (NOx) index as measured by the AirGradient SGP sensor", "gauge"); + add_metric("nox_index", + "The processed Nitrous Oxide (NOx) index as measured by the " + "AirGradient SGP sensor", + "gauge"); add_metric_point("", String(noxIndex)); } } if (hasSensorSHT) { if (temp > -1001) { - add_metric("temperature", "The ambient temperature as measured by the AirGradient SHT sensor, in degrees Celsius", "gauge", "degc"); + add_metric("temperature", + "The ambient temperature as measured by the AirGradient SHT " + "sensor, in degrees Celsius", + "gauge", "degc"); add_metric_point("", String(temp)); } if (hum >= 0) { - add_metric("humidity", "The relative humidity as measured by the AirGradient SHT sensor", "gauge", "percent"); + add_metric( + "humidity", + "The relative humidity as measured by the AirGradient SHT sensor", + "gauge", "percent"); add_metric_point("", String(hum)); } } response += "# EOF\n"; - webServer.send(200, "application/openmetrics-text; version=1.0.0; charset=utf-8", response); + webServer.send(200, + "application/openmetrics-text; version=1.0.0; charset=utf-8", + response); } void webServerHandler(void *param) { @@ -1674,8 +1721,8 @@ static void failedHandler(String msg) { /** * @brief Send data to server */ -static void serverConfigPoll(void) { - if (agServer.pollServerConfig(getDevId())) { +static void serverConfigUpdate(void) { + if (agServer.fetchServerConfigure(getDevId())) { if (agServer.isCo2Calib()) { if (hasSensorS8) { co2Calibration(); @@ -2113,7 +2160,7 @@ static void updateWiFiConnect(void) { * @brief APP display and LED handler * */ -static void updateDispLedBar(void) { +static void displayAndLedBarUpdate(void) { if (factoryBtnPressTime == 0) { appDispHandler(); } @@ -2124,7 +2171,7 @@ static void updateDispLedBar(void) { * @brief Update tvocIndexindex * */ -static void tvocPoll(void) { +static void tvocUpdate(void) { tvocIndex = ag.sgp41.getTvocIndex(); tvocRawIndex = ag.sgp41.getTvocRaw(); noxIndex = ag.sgp41.getNoxIndex(); @@ -2139,7 +2186,7 @@ static void tvocPoll(void) { * @brief Update PMS data * */ -static void pmPoll(void) { +static void pmUpdate(void) { if (ag.pms5003.readData()) { pm01 = ag.pms5003.getPm01Ae(); pm25 = ag.pms5003.getPm25Ae(); @@ -2180,7 +2227,7 @@ static void sendDataToServer(void) { /** * @brief Update temperature and humidity value */ -static void tempHumPoll(void) { +static void tempHumUpdate(void) { if (ag.sht.measure()) { temp = ag.sht.getTemperature(); diff --git a/examples/Open_Air/Open_Air.ino b/examples/Open_Air/Open_Air.ino index 5bda9ba..c1cc73d 100644 --- a/examples/Open_Air/Open_Air.ino +++ b/examples/Open_Air/Open_Air.ino @@ -99,7 +99,7 @@ enum { #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 \ @@ -175,7 +175,7 @@ public: * @return true Success * @return false Failure */ - bool pollServerConfig(String id) { + bool fetchServerConfigure(String id) { String uri = "http://hw.airgradient.com/sensors/airgradient:" + id + "/one/config"; @@ -709,11 +709,11 @@ 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 tvocUpdate(void); +static void pmUpdate(void); static void sendDataToServer(void); -static void co2Poll(void); -static void serverConfigPoll(void); +static void co2Update(void); +static void serverConfigUpdate(void); static const char *getFwMode(int mode); static void showNr(void); static void webServerInit(void); @@ -726,11 +726,11 @@ bool hasSensorPMS1 = true; bool hasSensorPMS2 = true; bool hasSensorSGP = true; uint32_t factoryBtnPressTime = 0; -AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigPoll); +AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, serverConfigUpdate); 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); +AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update); +AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, pmUpdate); +AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, tvocUpdate); void setup() { EEPROM.begin(512); @@ -764,7 +764,7 @@ void setup() { wifiHasConfig = true; sendPing(); - agServer.pollServerConfig(getDevId()); + agServer.fetchServerConfigure(getDevId()); if (agServer.isConfigFailed()) { ledSmHandler(APP_SM_WIFI_OK_SERVER_OK_SENSOR_CONFIG_FAILED); delay(DISPLAY_DELAY_SHOW_CONTENT_MS); @@ -1011,7 +1011,7 @@ static void updateWiFiConnect(void) { * @brief Update tvocIndexindex * */ -static void tvocPoll(void) { +static void tvocUpdate(void) { tvocIndex = ag.sgp41.getTvocIndex(); tvocRawIndex = ag.sgp41.getTvocRaw(); noxIndex = ag.sgp41.getNoxIndex(); @@ -1026,7 +1026,7 @@ static void tvocPoll(void) { * @brief Update PMS data * */ -static void pmPoll(void) { +static void pmUpdate(void) { bool pmsResult_1 = false; bool pmsResult_2 = false; if (hasSensorPMS1 && ag.pms5003t_1.readData()) { @@ -1134,13 +1134,13 @@ static void pmPoll(void) { } } -static void co2Poll(void) { +static void co2Update(void) { co2Ppm = ag.s8.getCo2(); Serial.printf("CO2 index: %d\r\n", co2Ppm); } -static void serverConfigPoll(void) { - if (agServer.pollServerConfig(getDevId())) { +static void serverConfigUpdate(void) { + if (agServer.fetchServerConfigure(getDevId())) { /** Only support CO2 S8 sensor on FW_MODE_PST */ if (fw_mode == FW_MODE_PST) { if (agServer.isCo2Calib()) { diff --git a/src/PMS/PMS5003.cpp b/src/PMS/PMS5003.cpp index 128d1b1..268f9bb 100644 --- a/src/PMS/PMS5003.cpp +++ b/src/PMS/PMS5003.cpp @@ -1,5 +1,6 @@ #include "PMS5003.h" #include "Arduino.h" +#include "PMSUtils.h" #if defined(ESP8266) #include @@ -63,7 +64,8 @@ bool PMS5003::begin(void) { #if defined(ESP8266) bsp->Pms5003.uart_tx_pin; - SoftwareSerial *uart = new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin); + SoftwareSerial *uart = + new SoftwareSerial(bsp->Pms5003.uart_tx_pin, bsp->Pms5003.uart_rx_pin); uart->begin(9600); if (pms.begin(uart) == false) { AgLog("PMS failed"); @@ -81,31 +83,6 @@ bool PMS5003::begin(void) { return true; } -/** - * @brief Convert PM2.5 to US AQI - * - * @param pm02 - * @return int - */ -int PMS5003::pm25ToAQI(int pm02) { - if (pm02 <= 12.0) - return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0); - else if (pm02 <= 35.4) - return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50); - else if (pm02 <= 55.4) - return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100); - else if (pm02 <= 150.4) - return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150); - else if (pm02 <= 250.4) - return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200); - else if (pm02 <= 350.4) - return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300); - else if (pm02 <= 500.4) - return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400); - else - return 500; -} - /** * @brief Read all package data then call to @ref getPMxxx to get the target * data @@ -155,7 +132,7 @@ int PMS5003::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; } * @param pm25 PM2.5 index * @return int PM2.5 US AQI */ -int PMS5003::convertPm25ToUsAqi(int pm25) { return this->pm25ToAQI(pm25); } +int PMS5003::convertPm25ToUsAqi(int pm25) { return pm25ToAQI(pm25); } /** * @brief Check device initialized or not diff --git a/src/PMS/PMS5003.h b/src/PMS/PMS5003.h index 69e6de4..15b907c 100644 --- a/src/PMS/PMS5003.h +++ b/src/PMS/PMS5003.h @@ -2,8 +2,8 @@ #define _AIR_GRADIENT_PMS5003_H_ #include "../Main/BoardDef.h" -#include "Stream.h" #include "PMS.h" +#include "Stream.h" /** * @brief The class define how to handle PMS5003 sensor bas on @ref PMS class @@ -41,6 +41,5 @@ private: bool begin(void); bool isBegin(void); - int pm25ToAQI(int pm02); }; #endif /** _AIR_GRADIENT_PMS5003_H_ */ diff --git a/src/PMS/PMS5003T.cpp b/src/PMS/PMS5003T.cpp index 57040a5..03b0fd2 100644 --- a/src/PMS/PMS5003T.cpp +++ b/src/PMS/PMS5003T.cpp @@ -1,5 +1,6 @@ #include "PMS5003T.h" #include "Arduino.h" +#include "PMSUtils.h" #if defined(ESP8266) #include @@ -105,31 +106,6 @@ bool PMS5003T::begin(void) { return true; } -/** - * @brief Convert PM2.5 to US AQI - * - * @param pm02 - * @return int - */ -int PMS5003T::pm25ToAQI(int pm02) { - if (pm02 <= 12.0) - return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0); - else if (pm02 <= 35.4) - return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50); - else if (pm02 <= 55.4) - return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100); - else if (pm02 <= 150.4) - return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150); - else if (pm02 <= 250.4) - return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200); - else if (pm02 <= 350.4) - return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300); - else if (pm02 <= 500.4) - return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400); - else - return 500; -} - /** * @brief Read all package data then call to @ref getPMxxx to get the target * data @@ -179,7 +155,7 @@ int PMS5003T::getPm03ParticleCount(void) { return pmsData.PM_RAW_0_3; } * @param pm25 PM2.5 index * @return int PM2.5 US AQI */ -int PMS5003T::convertPm25ToUsAqi(int pm25) { return this->pm25ToAQI(pm25); } +int PMS5003T::convertPm25ToUsAqi(int pm25) { return pm25ToAQI(pm25); } /** * @brief Get temperature, Must call this method after @ref readData() success diff --git a/src/PMS/PMS5003T.h b/src/PMS/PMS5003T.h index a93cd40..ec4d78e 100644 --- a/src/PMS/PMS5003T.h +++ b/src/PMS/PMS5003T.h @@ -1,10 +1,10 @@ #ifndef _PMS5003T_H_ #define _PMS5003T_H_ -#include #include "../Main/BoardDef.h" #include "PMS.h" #include "Stream.h" +#include /** * @brief The class define how to handle PMS5003T sensor bas on @ref PMS class @@ -42,7 +42,6 @@ private: #endif bool begin(void); - int pm25ToAQI(int pm02); PMS pms; PMS::DATA pmsData; bool isBegin(void); diff --git a/src/PMS/PMSUtils.cpp b/src/PMS/PMSUtils.cpp new file mode 100644 index 0000000..f47813f --- /dev/null +++ b/src/PMS/PMSUtils.cpp @@ -0,0 +1,26 @@ +#include "PMSUtils.h" + +/** + * @brief Convert PM2.5 to US AQI + * + * @param pm02 + * @return int + */ +int pm25ToAQI(int pm02) { + if (pm02 <= 12.0) + return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0); + else if (pm02 <= 35.4) + return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50); + else if (pm02 <= 55.4) + return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100); + else if (pm02 <= 150.4) + return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150); + else if (pm02 <= 250.4) + return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200); + else if (pm02 <= 350.4) + return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300); + else if (pm02 <= 500.4) + return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400); + else + return 500; +} diff --git a/src/PMS/PMSUtils.h b/src/PMS/PMSUtils.h new file mode 100644 index 0000000..ffb5a84 --- /dev/null +++ b/src/PMS/PMSUtils.h @@ -0,0 +1,6 @@ +#ifndef _PMS_UTILS_H_ +#define _PMS_UTILS_H_ + +int pm25ToAQI(int pm02); + +#endif /** _PMS_UTILS_H_ */