mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-21 12:42:10 +02:00
Optimize PMS sensor read data on active mode send each second
This commit is contained in:
@ -288,6 +288,11 @@ void loop() {
|
|||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
if (configuration.hasSensorPMS1) {
|
if (configuration.hasSensorPMS1) {
|
||||||
ag->pms5003.handle();
|
ag->pms5003.handle();
|
||||||
|
static bool pmsConnected = false;
|
||||||
|
if (pmsConnected != ag->pms5003.connected()) {
|
||||||
|
pmsConnected = ag->pms5003.connected();
|
||||||
|
Serial.printf("PMS sensor %s ", pmsConnected?"connected":"removed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (configuration.hasSensorPMS1) {
|
if (configuration.hasSensorPMS1) {
|
||||||
@ -1009,7 +1014,7 @@ static void updateTvoc(void) {
|
|||||||
static void updatePm(void) {
|
static void updatePm(void) {
|
||||||
bool restart = false;
|
bool restart = false;
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
if (ag->pms5003.isFailed() == false) {
|
if (ag->pms5003.connected()) {
|
||||||
measurements.pm01_1 = ag->pms5003.getPm01Ae();
|
measurements.pm01_1 = ag->pms5003.getPm01Ae();
|
||||||
measurements.pm25_1 = ag->pms5003.getPm25Ae();
|
measurements.pm25_1 = ag->pms5003.getPm25Ae();
|
||||||
measurements.pm10_1 = ag->pms5003.getPm10Ae();
|
measurements.pm10_1 = ag->pms5003.getPm10Ae();
|
||||||
@ -1038,7 +1043,7 @@ static void updatePm(void) {
|
|||||||
} else {
|
} else {
|
||||||
bool pmsResult_1 = false;
|
bool pmsResult_1 = false;
|
||||||
bool pmsResult_2 = false;
|
bool pmsResult_2 = false;
|
||||||
if (configuration.hasSensorPMS1 && (ag->pms5003t_1.isFailed() == false)) {
|
if (configuration.hasSensorPMS1 && ag->pms5003t_1.connected()) {
|
||||||
measurements.pm01_1 = ag->pms5003t_1.getPm01Ae();
|
measurements.pm01_1 = ag->pms5003t_1.getPm01Ae();
|
||||||
measurements.pm25_1 = ag->pms5003t_1.getPm25Ae();
|
measurements.pm25_1 = ag->pms5003t_1.getPm25Ae();
|
||||||
measurements.pm10_1 = ag->pms5003t_1.getPm10Ae();
|
measurements.pm10_1 = ag->pms5003t_1.getPm10Ae();
|
||||||
@ -1081,7 +1086,7 @@ static void updatePm(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configuration.hasSensorPMS2 && (ag->pms5003t_2.isFailed() == false)) {
|
if (configuration.hasSensorPMS2 && ag->pms5003t_2.connected()) {
|
||||||
measurements.pm01_2 = ag->pms5003t_2.getPm01Ae();
|
measurements.pm01_2 = ag->pms5003t_2.getPm01Ae();
|
||||||
measurements.pm25_2 = ag->pms5003t_2.getPm25Ae();
|
measurements.pm25_2 = ag->pms5003t_2.getPm25Ae();
|
||||||
measurements.pm10_2 = ag->pms5003t_2.getPm10Ae();
|
measurements.pm10_2 = ag->pms5003t_2.getPm10Ae();
|
||||||
|
225
src/PMS/PMS.cpp
225
src/PMS/PMS.cpp
@ -8,146 +8,111 @@
|
|||||||
* @return true Sucecss
|
* @return true Sucecss
|
||||||
* @return false Failure
|
* @return false Failure
|
||||||
*/
|
*/
|
||||||
bool PMSBase::begin(Stream *stream) {
|
bool PMSBase::begin(HardwareSerial& serial) {
|
||||||
this->stream = stream;
|
|
||||||
|
|
||||||
failed = true;
|
|
||||||
failCount = 0;
|
failCount = 0;
|
||||||
lastRead = 0; // To read buffer on handle without wait after 1.5sec
|
_connected = false;
|
||||||
|
|
||||||
this->stream->flush();
|
|
||||||
|
|
||||||
// Run and check sensor data for 4sec
|
// Run and check sensor data for 4sec
|
||||||
while (1) {
|
serial.flush();
|
||||||
handle();
|
|
||||||
if (failed == false) {
|
unsigned long lastInit = millis();
|
||||||
return true;
|
while (true) {
|
||||||
|
readPackage(serial);
|
||||||
|
if (_connected) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(1);
|
delay(1);
|
||||||
uint32_t ms = (uint32_t)(millis() - lastRead);
|
unsigned long ms = (unsigned long)(millis() - lastInit);
|
||||||
if (ms >= 4000) {
|
if (ms >= 4000) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return _connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check and read sensor data then update variable.
|
* @brief Read PMS package send to device each 1sec
|
||||||
* Check result from method @isFailed before get value
|
*
|
||||||
|
* @param serial
|
||||||
*/
|
*/
|
||||||
void PMSBase::handle() {
|
void PMSBase::readPackage(HardwareSerial& serial) {
|
||||||
uint32_t ms;
|
uint8_t delayCount = 0;
|
||||||
if (lastRead == 0) {
|
while (serial.available()) {
|
||||||
lastRead = millis();
|
/** Get value */
|
||||||
if (lastRead == 0) {
|
uint8_t value = (uint8_t)serial.read();
|
||||||
lastRead = 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ms = (uint32_t)(millis() - lastRead);
|
|
||||||
/**
|
|
||||||
* The PMS in Active mode sends an update data every 1 second. If we read
|
|
||||||
* exactly every 1 sec then we may or may not get an update (depending on
|
|
||||||
* timing tolerances). Hence we read every 2.5 seconds and expect 2 ..3
|
|
||||||
* updates,
|
|
||||||
*/
|
|
||||||
if (ms < 2500) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bool result = false;
|
|
||||||
char buf[32];
|
|
||||||
int bufIndex;
|
|
||||||
int step = 0;
|
|
||||||
int len = 0;
|
|
||||||
int bcount = 0;
|
|
||||||
|
|
||||||
while (stream->available()) {
|
/** Process receiving package... */
|
||||||
char value = stream->read();
|
switch (readBufferIndex) {
|
||||||
switch (step) {
|
case 0: /** Start byte 1 */
|
||||||
case 0: {
|
|
||||||
if (value == 0x42) {
|
if (value == 0x42) {
|
||||||
step = 1;
|
readBuffer[readBufferIndex++] = value;
|
||||||
bufIndex = 0;
|
|
||||||
buf[bufIndex++] = value;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
case 1: /** Start byte 2 */
|
||||||
case 1: {
|
|
||||||
if (value == 0x4d) {
|
if (value == 0x4d) {
|
||||||
step = 2;
|
readBuffer[readBufferIndex++] = value;
|
||||||
buf[bufIndex++] = value;
|
|
||||||
// Serial.println("Got 0x4d");
|
|
||||||
} else {
|
} else {
|
||||||
step = 0;
|
readBufferIndex = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
case 2: /** Frame length */
|
||||||
case 2: {
|
if (value == 0x00) {
|
||||||
buf[bufIndex++] = value;
|
readBuffer[readBufferIndex++] = value;
|
||||||
if (bufIndex >= 4) {
|
|
||||||
len = toI16(&buf[2]);
|
|
||||||
if (len != 28) {
|
|
||||||
// Serial.printf("Got good bad len %d\r\n", len);
|
|
||||||
len += 4;
|
|
||||||
step = 3;
|
|
||||||
} else {
|
} else {
|
||||||
// Serial.println("Got good len");
|
readBufferIndex = 0;
|
||||||
step = 4;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
case 3: /** Frame length */
|
||||||
case 3: {
|
if (value == 0x1C) {
|
||||||
bufIndex++;
|
readBuffer[readBufferIndex++] = value;
|
||||||
if (bufIndex >= len) {
|
} else {
|
||||||
step = 0;
|
readBufferIndex = 0;
|
||||||
// Serial.println("Bad lengh read all buffer");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
default: /** Data */
|
||||||
|
readBuffer[readBufferIndex++] = value;
|
||||||
|
|
||||||
|
/** Check that received full bufer */
|
||||||
|
if (readBufferIndex >= sizeof(readBuffer)) {
|
||||||
|
/** validata package */
|
||||||
|
if (validate(readBuffer)) {
|
||||||
|
_connected = true; /** Set connected status */
|
||||||
|
|
||||||
|
/** Parse data */
|
||||||
|
parse(readBuffer);
|
||||||
|
|
||||||
|
/** Set last received package */
|
||||||
|
lastPackage = millis();
|
||||||
|
if (lastPackage == 0) {
|
||||||
|
lastPackage = 1;
|
||||||
}
|
}
|
||||||
case 4: {
|
|
||||||
buf[bufIndex++] = value;
|
|
||||||
if (bufIndex >= 32) {
|
|
||||||
result |= validate(buf);
|
|
||||||
step = 0;
|
|
||||||
// Serial.println("Got data");
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
/** Clear buffer index */
|
||||||
|
readBufferIndex = 0;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce core panic: delay 1 ms each 32bytes data
|
/** Avoid task watchdog timer reset... */
|
||||||
bcount++;
|
delayCount++;
|
||||||
if ((bcount % 32) == 0) {
|
if(delayCount >= 32)
|
||||||
|
{
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
/** Check that sensor removed */
|
||||||
lastRead = millis();
|
if (lastPackage) {
|
||||||
if (lastRead == 0) {
|
unsigned long ms = (unsigned long)(millis() - lastPackage);
|
||||||
lastRead = 1;
|
if (ms >= 1500) {
|
||||||
}
|
lastPackage = 0;
|
||||||
failed = false;
|
_connected = false;
|
||||||
} else {
|
|
||||||
if (ms > 5000) {
|
|
||||||
failed = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Check that PMS send is failed or disconnected
|
|
||||||
*
|
|
||||||
* @return true Failed
|
|
||||||
* @return false No problem
|
|
||||||
*/
|
|
||||||
bool PMSBase::isFailed(void) { return failed; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Increate number of fail
|
* @brief Increate number of fail
|
||||||
*
|
*
|
||||||
@ -174,112 +139,114 @@ int PMSBase::getFailCountMax(void) { return failCountMax; }
|
|||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getRaw0_1(void) { return toU16(&package[4]); }
|
uint16_t PMSBase::getRaw0_1(void) { return pms_raw0_1; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read PMS 2.5 ug/m3 with CF = 1 PM estimates
|
* @brief Read PMS 2.5 ug/m3 with CF = 1 PM estimates
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getRaw2_5(void) { return toU16(&package[6]); }
|
uint16_t PMSBase::getRaw2_5(void) { return pms_raw2_5; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read PMS 10 ug/m3 with CF = 1 PM estimates
|
* @brief Read PMS 10 ug/m3 with CF = 1 PM estimates
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getRaw10(void) { return toU16(&package[8]); }
|
uint16_t PMSBase::getRaw10(void) { return pms_raw10; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read PMS 0.1 ug/m3
|
* @brief Read PMS 0.1 ug/m3
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getPM0_1(void) { return toU16(&package[10]); }
|
uint16_t PMSBase::getPM0_1(void) { return pms_pm0_1; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read PMS 2.5 ug/m3
|
* @brief Read PMS 2.5 ug/m3
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getPM2_5(void) { return toU16(&package[12]); }
|
uint16_t PMSBase::getPM2_5(void) { return pms_pm2_5; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read PMS 10 ug/m3
|
* @brief Read PMS 10 ug/m3
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getPM10(void) { return toU16(&package[14]); }
|
uint16_t PMSBase::getPM10(void) { return pms_pm10; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 0.3 um/0.1L
|
* @brief Get numnber concentrations over 0.3 um/0.1L
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount0_3(void) { return toU16(&package[16]); }
|
uint16_t PMSBase::getCount0_3(void) { return pms_count0_3; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 0.5 um/0.1L
|
* @brief Get numnber concentrations over 0.5 um/0.1L
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount0_5(void) { return toU16(&package[18]); }
|
uint16_t PMSBase::getCount0_5(void) { return pms_count0_5; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 1.0 um/0.1L
|
* @brief Get numnber concentrations over 1.0 um/0.1L
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount1_0(void) { return toU16(&package[20]); }
|
uint16_t PMSBase::getCount1_0(void) { return pms_count1_0; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 2.5 um/0.1L
|
* @brief Get numnber concentrations over 2.5 um/0.1L
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount2_5(void) { return toU16(&package[22]); }
|
uint16_t PMSBase::getCount2_5(void) { return pms_count2_5; }
|
||||||
|
|
||||||
|
bool PMSBase::connected(void) { return _connected; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 5.0 um/0.1L (only PMS5003)
|
* @brief Get numnber concentrations over 5.0 um/0.1L (only PMS5003)
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount5_0(void) { return toU16(&package[24]); }
|
uint16_t PMSBase::getCount5_0(void) { return pms_count5_0; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get numnber concentrations over 10.0 um/0.1L (only PMS5003)
|
* @brief Get numnber concentrations over 10.0 um/0.1L (only PMS5003)
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getCount10(void) { return toU16(&package[26]); }
|
uint16_t PMSBase::getCount10(void) { return pms_count10; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get temperature (only PMS5003T)
|
* @brief Get temperature (only PMS5003T)
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
int16_t PMSBase::getTemp(void) { return toI16(&package[24]); }
|
int16_t PMSBase::getTemp(void) { return pms_temp; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get humidity (only PMS5003T)
|
* @brief Get humidity (only PMS5003T)
|
||||||
*
|
*
|
||||||
* @return uint16_t
|
* @return uint16_t
|
||||||
*/
|
*/
|
||||||
uint16_t PMSBase::getHum(void) { return toU16(&package[26]); }
|
uint16_t PMSBase::getHum(void) { return pms_hum; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get firmware version code
|
* @brief Get firmware version code
|
||||||
*
|
*
|
||||||
* @return uint8_t
|
* @return uint8_t
|
||||||
*/
|
*/
|
||||||
uint8_t PMSBase::getFirmwareVersion(void) { return package[28]; }
|
uint8_t PMSBase::getFirmwareVersion(void) { return pms_firmwareVersion; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Ge PMS5003 error code
|
* @brief Ge PMS5003 error code
|
||||||
*
|
*
|
||||||
* @return uint8_t
|
* @return uint8_t
|
||||||
*/
|
*/
|
||||||
uint8_t PMSBase::getErrorCode(void) { return package[29]; }
|
uint8_t PMSBase::getErrorCode(void) { return pms_errorCode; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert PMS2.5 to US AQI unit
|
* @brief Convert PMS2.5 to US AQI unit
|
||||||
@ -350,13 +317,13 @@ int PMSBase::compensate(int pm25, float humidity) {
|
|||||||
* @param buf bytes array (must be >= 2)
|
* @param buf bytes array (must be >= 2)
|
||||||
* @return int16_t
|
* @return int16_t
|
||||||
*/
|
*/
|
||||||
int16_t PMSBase::toI16(char *buf) {
|
int16_t PMSBase::toI16(const uint8_t *buf) {
|
||||||
int16_t value = buf[0];
|
int16_t value = buf[0];
|
||||||
value = (value << 8) | buf[1];
|
value = (value << 8) | buf[1];
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t PMSBase::toU16(char *buf) {
|
uint16_t PMSBase::toU16(const uint8_t *buf) {
|
||||||
uint16_t value = buf[0];
|
uint16_t value = buf[0];
|
||||||
value = (value << 8) | buf[1];
|
value = (value << 8) | buf[1];
|
||||||
return value;
|
return value;
|
||||||
@ -369,16 +336,32 @@ uint16_t PMSBase::toU16(char *buf) {
|
|||||||
* @return true Success
|
* @return true Success
|
||||||
* @return false Failed
|
* @return false Failed
|
||||||
*/
|
*/
|
||||||
bool PMSBase::validate(char *buf) {
|
bool PMSBase::validate(const uint8_t *buf) {
|
||||||
uint16_t sum = 0;
|
uint16_t sum = 0;
|
||||||
for (int i = 0; i < 30; i++) {
|
for (int i = 0; i < 30; i++) {
|
||||||
sum += buf[i];
|
sum += buf[i];
|
||||||
}
|
}
|
||||||
if (sum == toU16(&buf[30])) {
|
if (sum == toU16(&buf[30])) {
|
||||||
for (int i = 0; i < 32; i++) {
|
|
||||||
package[i] = buf[i];
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PMSBase::parse(const uint8_t *buf) {
|
||||||
|
pms_raw0_1 = toU16(&buf[4]);
|
||||||
|
pms_raw2_5 = toU16(&buf[6]);
|
||||||
|
pms_raw10 = toU16(&buf[8]);
|
||||||
|
pms_pm0_1 = toU16(&buf[10]);
|
||||||
|
pms_pm2_5 = toU16(&buf[12]);
|
||||||
|
pms_pm10 = toU16(&buf[14]);
|
||||||
|
pms_count0_3 = toU16(&buf[16]);
|
||||||
|
pms_count0_5 = toU16(&buf[18]);
|
||||||
|
pms_count1_0 = toU16(&buf[20]);
|
||||||
|
pms_count2_5 = toU16(&buf[22]);
|
||||||
|
pms_count5_0 = toU16(&buf[24]);
|
||||||
|
pms_count10 = toU16(&buf[26]);
|
||||||
|
pms_temp = toU16(&buf[24]);
|
||||||
|
pms_hum = toU16(&buf[26]);
|
||||||
|
pms_firmwareVersion = buf[28];
|
||||||
|
pms_errorCode = buf[29];
|
||||||
|
}
|
||||||
|
@ -7,9 +7,8 @@
|
|||||||
|
|
||||||
class PMSBase {
|
class PMSBase {
|
||||||
public:
|
public:
|
||||||
bool begin(Stream *stream);
|
bool begin(HardwareSerial& serial);
|
||||||
void handle();
|
void readPackage(HardwareSerial& serial);
|
||||||
bool isFailed(void);
|
|
||||||
void updateFailCount(void);
|
void updateFailCount(void);
|
||||||
void resetFailCount(void);
|
void resetFailCount(void);
|
||||||
int getFailCount(void);
|
int getFailCount(void);
|
||||||
@ -24,6 +23,7 @@ public:
|
|||||||
uint16_t getCount0_5(void);
|
uint16_t getCount0_5(void);
|
||||||
uint16_t getCount1_0(void);
|
uint16_t getCount1_0(void);
|
||||||
uint16_t getCount2_5(void);
|
uint16_t getCount2_5(void);
|
||||||
|
bool connected(void);
|
||||||
|
|
||||||
/** For PMS5003 */
|
/** For PMS5003 */
|
||||||
uint16_t getCount5_0(void);
|
uint16_t getCount5_0(void);
|
||||||
@ -39,17 +39,36 @@ public:
|
|||||||
int compensate(int pm25, float humidity);
|
int compensate(int pm25, float humidity);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Stream *stream;
|
static const uint8_t package_size = 32;
|
||||||
char package[32];
|
|
||||||
int packageIndex;
|
|
||||||
bool failed = false;
|
|
||||||
uint32_t lastRead;
|
|
||||||
const int failCountMax = 10;
|
const int failCountMax = 10;
|
||||||
int failCount = 0;
|
int failCount = 0;
|
||||||
|
|
||||||
int16_t toI16(char *buf);
|
uint8_t readBuffer[package_size];
|
||||||
uint16_t toU16(char* buf);
|
uint8_t readBufferIndex = 0;
|
||||||
bool validate(char *buf);
|
unsigned long lastPackage = 0;
|
||||||
|
bool _connected;
|
||||||
|
|
||||||
|
uint16_t pms_raw0_1;
|
||||||
|
uint16_t pms_raw2_5;
|
||||||
|
uint16_t pms_raw10;
|
||||||
|
uint16_t pms_pm0_1;
|
||||||
|
uint16_t pms_pm2_5;
|
||||||
|
uint16_t pms_pm10;
|
||||||
|
uint16_t pms_count0_3;
|
||||||
|
uint16_t pms_count0_5;
|
||||||
|
uint16_t pms_count1_0;
|
||||||
|
uint16_t pms_count2_5;
|
||||||
|
uint16_t pms_count5_0;
|
||||||
|
uint16_t pms_count10;
|
||||||
|
int16_t pms_temp;
|
||||||
|
uint16_t pms_hum;
|
||||||
|
uint8_t pms_errorCode;
|
||||||
|
uint8_t pms_firmwareVersion;
|
||||||
|
|
||||||
|
int16_t toI16(const uint8_t *buf);
|
||||||
|
uint16_t toU16(const uint8_t *buf);
|
||||||
|
bool validate(const uint8_t *buf);
|
||||||
|
void parse(const uint8_t* buf);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /** _PMS5003_BASE_H_ */
|
#endif /** _PMS5003_BASE_H_ */
|
||||||
|
@ -73,7 +73,7 @@ bool PMS5003::begin(void) {
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
this->_serial->begin(9600);
|
this->_serial->begin(9600);
|
||||||
if (pms.begin(this->_serial) == false) {
|
if (pms.begin(*this->_serial) == false) {
|
||||||
AgLog("PMS failed");
|
AgLog("PMS failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -148,6 +148,14 @@ int PMS5003::getFirmwareVersion(void) { return _ver; }
|
|||||||
*/
|
*/
|
||||||
uint8_t PMS5003::getErrorCode(void) { return pms.getErrorCode(); }
|
uint8_t PMS5003::getErrorCode(void) { return pms.getErrorCode(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Is sensor connect with device
|
||||||
|
*
|
||||||
|
* @return true Connected
|
||||||
|
* @return false Removed
|
||||||
|
*/
|
||||||
|
bool PMS5003::connected(void) { return pms.connected(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check device initialized or not
|
* @brief Check device initialized or not
|
||||||
*
|
*
|
||||||
@ -182,15 +190,7 @@ void PMS5003::end(void) {
|
|||||||
* @brief Check and read PMS sensor data. This method should be callack from
|
* @brief Check and read PMS sensor data. This method should be callack from
|
||||||
* loop process to continoue check sensor data if it's available
|
* loop process to continoue check sensor data if it's available
|
||||||
*/
|
*/
|
||||||
void PMS5003::handle(void) { pms.handle(); }
|
void PMS5003::handle(void) { pms.readPackage(*this->_serial); }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get sensor status
|
|
||||||
*
|
|
||||||
* @return true No problem
|
|
||||||
* @return false Communication timeout or sensor has removed
|
|
||||||
*/
|
|
||||||
bool PMS5003::isFailed(void) { return pms.isFailed(); }
|
|
||||||
|
|
||||||
void PMS5003::updateFailCount(void) {
|
void PMS5003::updateFailCount(void) {
|
||||||
pms.updateFailCount();
|
pms.updateFailCount();
|
||||||
|
@ -18,7 +18,6 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
void end(void);
|
void end(void);
|
||||||
void handle(void);
|
void handle(void);
|
||||||
bool isFailed(void);
|
|
||||||
void updateFailCount(void);
|
void updateFailCount(void);
|
||||||
void resetFailCount(void);
|
void resetFailCount(void);
|
||||||
int getFailCount(void);
|
int getFailCount(void);
|
||||||
@ -31,6 +30,7 @@ public:
|
|||||||
int compensate(int pm25, float humidity);
|
int compensate(int pm25, float humidity);
|
||||||
int getFirmwareVersion(void);
|
int getFirmwareVersion(void);
|
||||||
uint8_t getErrorCode(void);
|
uint8_t getErrorCode(void);
|
||||||
|
bool connected(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _isBegin = false;
|
bool _isBegin = false;
|
||||||
|
@ -98,7 +98,7 @@ bool PMS5003T::begin(void) {
|
|||||||
this->_serial->begin(9600, SERIAL_8N1, bsp->SenseAirS8.uart_rx_pin,
|
this->_serial->begin(9600, SERIAL_8N1, bsp->SenseAirS8.uart_rx_pin,
|
||||||
bsp->SenseAirS8.uart_tx_pin);
|
bsp->SenseAirS8.uart_tx_pin);
|
||||||
}
|
}
|
||||||
if (pms.begin(this->_serial) == false) {
|
if (pms.begin(*this->_serial) == false) {
|
||||||
AgLog("PMS failed");
|
AgLog("PMS failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -191,6 +191,14 @@ int PMS5003T::getFirmwareVersion(void) { return _ver; }
|
|||||||
*/
|
*/
|
||||||
uint8_t PMS5003T::getErrorCode(void) { return pms.getErrorCode(); }
|
uint8_t PMS5003T::getErrorCode(void) { return pms.getErrorCode(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Is sensor connect to device
|
||||||
|
*
|
||||||
|
* @return true Connected
|
||||||
|
* @return false Removed
|
||||||
|
*/
|
||||||
|
bool PMS5003T::connected(void) { return pms.connected(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check device initialized or not
|
* @brief Check device initialized or not
|
||||||
*
|
*
|
||||||
@ -222,15 +230,7 @@ void PMS5003T::end(void) {
|
|||||||
* @brief Check and read PMS sensor data. This method should be callack from
|
* @brief Check and read PMS sensor data. This method should be callack from
|
||||||
* loop process to continoue check sensor data if it's available
|
* loop process to continoue check sensor data if it's available
|
||||||
*/
|
*/
|
||||||
void PMS5003T::handle(void) { pms.handle(); }
|
void PMS5003T::handle(void) { pms.readPackage(*this->_serial); }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get sensor status
|
|
||||||
*
|
|
||||||
* @return true No problem
|
|
||||||
* @return false Communication timeout or sensor has removed
|
|
||||||
*/
|
|
||||||
bool PMS5003T::isFailed(void) { return pms.isFailed(); }
|
|
||||||
|
|
||||||
void PMS5003T::updateFailCount(void) {
|
void PMS5003T::updateFailCount(void) {
|
||||||
pms.updateFailCount();
|
pms.updateFailCount();
|
||||||
|
@ -21,7 +21,6 @@ public:
|
|||||||
void end(void);
|
void end(void);
|
||||||
|
|
||||||
void handle(void);
|
void handle(void);
|
||||||
bool isFailed(void);
|
|
||||||
void updateFailCount(void);
|
void updateFailCount(void);
|
||||||
void resetFailCount(void);
|
void resetFailCount(void);
|
||||||
int getFailCount(void);
|
int getFailCount(void);
|
||||||
@ -36,6 +35,7 @@ public:
|
|||||||
int compensate(int pm25, float humidity);
|
int compensate(int pm25, float humidity);
|
||||||
int getFirmwareVersion(void);
|
int getFirmwareVersion(void);
|
||||||
uint8_t getErrorCode(void);
|
uint8_t getErrorCode(void);
|
||||||
|
bool connected(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _isBegin = false;
|
bool _isBegin = false;
|
||||||
|
Reference in New Issue
Block a user