|
@ -20,38 +20,24 @@ |
|
|
#define PMS_TYPE_5003T 2 |
|
|
#define PMS_TYPE_5003T 2 |
|
|
#define PMS_TYPE_5003ST 3 |
|
|
#define PMS_TYPE_5003ST 3 |
|
|
|
|
|
|
|
|
// These should not be static, instead a setType method should be used to |
|
|
|
|
|
// dinamically choose the type of sensor... |
|
|
|
|
|
|
|
|
// Sensor type specified data |
|
|
|
|
|
#define PMS_SLOT_MAX 4 |
|
|
|
|
|
#define PMS_DATA_MAX 17 |
|
|
|
|
|
const static struct { |
|
|
|
|
|
const char *name; |
|
|
|
|
|
unsigned char data_count; |
|
|
|
|
|
unsigned char slot_count; |
|
|
|
|
|
unsigned char slot_types[PMS_SLOT_MAX]; |
|
|
|
|
|
} pms_specs[] = { |
|
|
|
|
|
{"PMSX003", 13, 3, {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10}}, |
|
|
|
|
|
{"PMSX003_9", 9, 3, {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10}}, |
|
|
|
|
|
{"PMS5003T", 13, 3, {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY}}, |
|
|
|
|
|
{"PMS5003ST", 17, 4, {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY, MAGNITUDE_HCHO}} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// [MAGIC][LEN][DATA9|13|17][SUM] |
|
|
// [MAGIC][LEN][DATA9|13|17][SUM] |
|
|
#if PMS_TYPE == PMS_TYPE_5003ST |
|
|
|
|
|
#define PMS_TYPE_NAME "PMS5003ST" |
|
|
|
|
|
#define PMS_DATA_COUNT 17 |
|
|
|
|
|
#define PMS_SLOT_COUNT 4 |
|
|
|
|
|
#define PMS_SLOT_NAMES {"PM2.5", "TEMP", "HUMI", "HCHO"} |
|
|
|
|
|
#define PMS_SLOT_TYPES {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY, MAGNITUDE_HCHO} |
|
|
|
|
|
#elif PMS_TYPE == PMS_TYPE_5003T |
|
|
|
|
|
#define PMS_TYPE_NAME "PMS5003T" |
|
|
|
|
|
#define PMS_DATA_COUNT 13 |
|
|
|
|
|
#define PMS_SLOT_COUNT 3 |
|
|
|
|
|
#define PMS_SLOT_NAMES {"PM2.5", "TEMP", "HUMI"} |
|
|
|
|
|
#define PMS_SLOT_TYPES {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY} |
|
|
|
|
|
#elif PMS_TYPE == PMS_TYPE_X003_9 |
|
|
|
|
|
#define PMS_TYPE_NAME "PMSX003_9" |
|
|
|
|
|
#define PMS_DATA_COUNT 9 |
|
|
|
|
|
#define PMS_SLOT_COUNT 3 |
|
|
|
|
|
#define PMS_SLOT_NAMES {"PM1.0", "PM2.5", "PM10"} |
|
|
|
|
|
#define PMS_SLOT_TYPES {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10} |
|
|
|
|
|
#else |
|
|
|
|
|
#define PMS_TYPE_NAME "PMSX003" |
|
|
|
|
|
#define PMS_DATA_COUNT 13 |
|
|
|
|
|
#define PMS_SLOT_COUNT 3 |
|
|
|
|
|
#define PMS_SLOT_NAMES {"PM1.0", "PM2.5", "PM10"} |
|
|
|
|
|
#define PMS_SLOT_TYPES {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10} |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#define PMS_PACKET_SIZE ((PMS_DATA_COUNT + 3) * 2) |
|
|
|
|
|
#define PMS_PAYLOAD_SIZE (PMS_DATA_COUNT * 2 + 2) |
|
|
|
|
|
|
|
|
#define PMS_PACKET_SIZE(data_count) ((data_count + 3) * 2) |
|
|
|
|
|
#define PMS_PAYLOAD_SIZE(data_count) ((data_count + 1) * 2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PMS sensor utils |
|
|
// PMS sensor utils |
|
@ -95,15 +81,15 @@ class PMSX003 { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Read sensor's data |
|
|
// Read sensor's data |
|
|
bool readData(uint16_t data[PMS_DATA_COUNT]) { |
|
|
|
|
|
|
|
|
bool readData(uint16_t data[], unsigned char data_count) { |
|
|
|
|
|
|
|
|
do { |
|
|
do { |
|
|
|
|
|
|
|
|
int avail = _serial->available(); |
|
|
int avail = _serial->available(); |
|
|
#if SENSOR_DEBUG |
|
|
#if SENSOR_DEBUG |
|
|
//debugSend("[SENSOR] %s: Packet available = %d\n", PMS_TYPE_NAME, avail); |
|
|
|
|
|
|
|
|
//debugSend("[SENSOR] PMS: Packet available = %d\n", avail); |
|
|
#endif |
|
|
#endif |
|
|
if (avail < PMS_PACKET_SIZE) { |
|
|
|
|
|
|
|
|
if (avail < PMS_PACKET_SIZE(data_count)) { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -111,29 +97,27 @@ class PMSX003 { |
|
|
|
|
|
|
|
|
uint16_t sum = 0x42 + 0x4D; |
|
|
uint16_t sum = 0x42 + 0x4D; |
|
|
uint16_t size = read16(sum); |
|
|
uint16_t size = read16(sum); |
|
|
#if SENSOR_DEBUG |
|
|
|
|
|
debugSend("[SENSOR] %s: Payload size = %d\n", PMS_TYPE_NAME, size); |
|
|
|
|
|
#endif |
|
|
|
|
|
if (size != PMS_PAYLOAD_SIZE) { |
|
|
|
|
|
|
|
|
if (size != PMS_PAYLOAD_SIZE(data_count)) { |
|
|
#if SENSOR_DEBUG |
|
|
#if SENSOR_DEBUG |
|
|
debugSend(("[SENSOR] %s: Payload size != %d \n"), PMS_TYPE_NAME, PMS_PAYLOAD_SIZE); |
|
|
|
|
|
|
|
|
debugSend(("[SENSOR] PMS: Payload size: %d != %d.\n"), size, PMS_PAYLOAD_SIZE(data_count)); |
|
|
#endif |
|
|
#endif |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int i = 0; i < PMS_DATA_COUNT; i++) { |
|
|
|
|
|
|
|
|
for (int i = 0; i < data_count; i++) { |
|
|
data[i] = read16(sum); |
|
|
data[i] = read16(sum); |
|
|
#if SENSOR_DEBUG |
|
|
#if SENSOR_DEBUG |
|
|
//debugSend(("[SENSOR] %s: data[%d] = %d\n"), PMS_TYPE_NAME, i, data[i]); |
|
|
|
|
|
|
|
|
//debugSend(("[SENSOR] PMS: data[%d] = %d\n"), i, data[i]); |
|
|
#endif |
|
|
#endif |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint16_t checksum = read16(); |
|
|
uint16_t checksum = read16(); |
|
|
#if SENSOR_DEBUG |
|
|
|
|
|
debugSend(("[SENSOR] %s: Sum=%04X, Checksum=%04X\n"), PMS_TYPE_NAME, sum, checksum); |
|
|
|
|
|
#endif |
|
|
|
|
|
if (sum == checksum) { |
|
|
if (sum == checksum) { |
|
|
return true; |
|
|
return true; |
|
|
|
|
|
} else { |
|
|
|
|
|
#if SENSOR_DEBUG |
|
|
|
|
|
debugSend(("[SENSOR] PMS checksum: %04X != %04X\n"), sum, checksum); |
|
|
|
|
|
#endif |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
@ -171,7 +155,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
// --------------------------------------------------------------------- |
|
|
// --------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
PMSX003Sensor(): BaseSensor() { |
|
|
PMSX003Sensor(): BaseSensor() { |
|
|
_count = PMS_SLOT_COUNT; |
|
|
|
|
|
|
|
|
_count = pms_specs[_type].slot_count; |
|
|
_sensor_id = SENSOR_PMSX003_ID; |
|
|
_sensor_id = SENSOR_PMSX003_ID; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -191,6 +175,12 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
_dirty = true; |
|
|
_dirty = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Should call setType after constrcutor immediately to enable corresponding slot count |
|
|
|
|
|
void setType(unsigned char type) { |
|
|
|
|
|
_type = type; |
|
|
|
|
|
_count = pms_specs[_type].slot_count; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------- |
|
|
// --------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
unsigned char getRX() { |
|
|
unsigned char getRX() { |
|
@ -201,6 +191,10 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
return _pin_tx; |
|
|
return _pin_tx; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned char getType() { |
|
|
|
|
|
return _type; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------- |
|
|
// --------------------------------------------------------------------- |
|
|
// Sensor API |
|
|
// Sensor API |
|
|
// --------------------------------------------------------------------- |
|
|
// --------------------------------------------------------------------- |
|
@ -226,15 +220,14 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
// Descriptive name of the sensor |
|
|
// Descriptive name of the sensor |
|
|
String description() { |
|
|
String description() { |
|
|
char buffer[28]; |
|
|
char buffer[28]; |
|
|
snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", PMS_TYPE_NAME, _pin_rx, _pin_tx); |
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", pms_specs[_type].name, _pin_rx, _pin_tx); |
|
|
return String(buffer); |
|
|
return String(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Descriptive name of the slot # index |
|
|
// Descriptive name of the slot # index |
|
|
String slot(unsigned char index) { |
|
|
String slot(unsigned char index) { |
|
|
char buffer[36] = {0}; |
|
|
char buffer[36] = {0}; |
|
|
const static char *_slot_names[] = PMS_SLOT_NAMES; |
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s @ %s @ SwSerial(%u,%u)", _slot_names[index], PMS_TYPE_NAME, _pin_rx, _pin_tx); |
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%d @ %s @ SwSerial(%u,%u)", int(index + 1), pms_specs[_type].name, _pin_rx, _pin_tx); |
|
|
return String(buffer); |
|
|
return String(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -247,8 +240,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
|
|
|
|
|
|
// Type for slot # index |
|
|
// Type for slot # index |
|
|
unsigned char type(unsigned char index) { |
|
|
unsigned char type(unsigned char index) { |
|
|
const static unsigned char _slot_types[] = PMS_SLOT_TYPES; |
|
|
|
|
|
return _slot_types[index]; |
|
|
|
|
|
|
|
|
return pms_specs[_type].slot_types[index]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void pre() { |
|
|
void pre() { |
|
@ -266,7 +258,7 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
readCycle = _readCount % 30; |
|
|
readCycle = _readCount % 30; |
|
|
if (readCycle == 0) { |
|
|
if (readCycle == 0) { |
|
|
#if SENSOR_DEBUG |
|
|
#if SENSOR_DEBUG |
|
|
debugSend("[SENSOR] %s: Wake up: %d\n", PMS_TYPE_NAME, _readCount); |
|
|
|
|
|
|
|
|
debugSend("[SENSOR] %s: Wake up: %d\n", pms_specs[_type].name, _readCount); |
|
|
#endif |
|
|
#endif |
|
|
wakeUp(); |
|
|
wakeUp(); |
|
|
return; |
|
|
return; |
|
@ -280,29 +272,29 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
} |
|
|
} |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
uint16_t data[PMS_DATA_COUNT]; |
|
|
|
|
|
if (readData(data)) { |
|
|
|
|
|
#if PMS_TYPE == PMS_TYPE_5003ST |
|
|
|
|
|
|
|
|
uint16_t data[PMS_DATA_MAX]; |
|
|
|
|
|
if (readData(data, pms_specs[_type].data_count)) { |
|
|
|
|
|
if (_type == PMS_TYPE_5003ST) { |
|
|
_slot_values[0] = data[4]; |
|
|
_slot_values[0] = data[4]; |
|
|
_slot_values[1] = (double)data[13] / 10; |
|
|
_slot_values[1] = (double)data[13] / 10; |
|
|
_slot_values[2] = (double)data[14] / 10; |
|
|
_slot_values[2] = (double)data[14] / 10; |
|
|
_slot_values[3] = (double)data[12] / 1000; |
|
|
_slot_values[3] = (double)data[12] / 1000; |
|
|
#elif PMS_TYPE == PMS_TYPE_5003T |
|
|
|
|
|
|
|
|
} else if (_type == PMS_TYPE_5003T) { |
|
|
_slot_values[0] = data[4]; |
|
|
_slot_values[0] = data[4]; |
|
|
_slot_values[1] = (double)data[10] / 10; |
|
|
_slot_values[1] = (double)data[10] / 10; |
|
|
_slot_values[2] = (double)data[11] / 10; |
|
|
_slot_values[2] = (double)data[11] / 10; |
|
|
#else |
|
|
|
|
|
|
|
|
} else { |
|
|
_slot_values[0] = data[3]; |
|
|
_slot_values[0] = data[3]; |
|
|
_slot_values[1] = data[4]; |
|
|
_slot_values[1] = data[4]; |
|
|
_slot_values[2] = data[5]; |
|
|
_slot_values[2] = data[5]; |
|
|
#endif |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#if PMS_SMART_SLEEP |
|
|
#if PMS_SMART_SLEEP |
|
|
if (readCycle == 6) { |
|
|
if (readCycle == 6) { |
|
|
sleep(); |
|
|
sleep(); |
|
|
#if SENSOR_DEBUG |
|
|
#if SENSOR_DEBUG |
|
|
debugSend("[SENSOR] %s: Enter sleep mode: %d\n", PMS_TYPE_NAME, _readCount); |
|
|
|
|
|
|
|
|
debugSend("[SENSOR] %s: Enter sleep mode: %d\n", pms_specs[_type].name, _readCount); |
|
|
#endif |
|
|
#endif |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
@ -321,7 +313,8 @@ class PMSX003Sensor : public BaseSensor, PMSX003 { |
|
|
unsigned int _pin_rx; |
|
|
unsigned int _pin_rx; |
|
|
unsigned int _pin_tx; |
|
|
unsigned int _pin_tx; |
|
|
unsigned long _startTime; |
|
|
unsigned long _startTime; |
|
|
double _slot_values[PMS_SLOT_COUNT] = {0}; |
|
|
|
|
|
|
|
|
unsigned char _type = PMS_TYPE_X003; |
|
|
|
|
|
double _slot_values[PMS_SLOT_MAX] = {0}; |
|
|
|
|
|
|
|
|
#if PMS_SMART_SLEEP |
|
|
#if PMS_SMART_SLEEP |
|
|
unsigned int _readCount = 0; |
|
|
unsigned int _readCount = 0; |
|
|