Skip to content

Commit 83c60c5

Browse files
author
Brent Rubell
committed
add ikea pm sensor overhead
1 parent d5a134b commit 83c60c5

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

src/components/uart/drivers/ws_uart_drv.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ class ws_uart_drv {
8989
@returns The UART device's unique identifier.
9090
*/
9191
/*******************************************************************************/
92-
const char *getDeviceID() { return _deviceID; }
92+
const char *getDriverID() { return _driverID; }
9393

9494
/*******************************************************************************/
9595
/*!
96-
@brief Sets the UART device's unique identifier.
96+
@brief Sets the UART driver's identifer.
9797
@param id
9898
The UART device's unique identifier.
9999
*/
100100
/*******************************************************************************/
101-
void setDriverID(const char *id) { _deviceID = id; }
101+
void setDriverID(const char *id) { _driverID = strdup(id); }
102102

103103
/*******************************************************************************/
104104
/*!
@@ -169,7 +169,7 @@ class ws_uart_drv {
169169
private:
170170
unsigned long
171171
_prvPoll; ///< Last time the UART device was polled, in milliseconds
172-
const char *_deviceID = nullptr; ///< UART device's ID
172+
const char *_driverID = nullptr; ///< UART device's ID
173173
};
174174

175175
#endif // WS_UART_DRV_H

src/components/uart/drivers/ws_uart_drv_pm25aqi.h

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
4040
: ws_uart_drv(swSerial, interval) {
4141
_swSerial = swSerial;
4242
pollingInterval = (unsigned long)interval;
43-
// Set driver ID
44-
setDriverID("pms5003");
4543
};
4644
#else
4745
/*******************************************************************************/
@@ -57,8 +55,6 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
5755
: ws_uart_drv(hwSerial, interval) {
5856
_hwSerial = hwSerial;
5957
pollingInterval = (unsigned long)interval;
60-
// Set driver ID
61-
setDriverID("pms5003");
6258
};
6359
#endif // USE_SW_UART
6460

@@ -151,34 +147,42 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
151147
// We'll be sending back six sensor_events: pm10_standard, pm25_standard,
152148
// pm100_standard, pm10_env, pm25_env, and pm100_env
153149
msgUARTResponse.payload.resp_uart_device_event.sensor_event_count = 6;
154-
// getDeviceID();
150+
Serial.print("Device ID: ");
151+
Serial.println(getDriverID());
155152
strcpy(msgUARTResponse.payload.resp_uart_device_event.device_id,
156-
getDeviceID());
157-
158-
// Pack sensor data into UART response message
159-
packUARTResponse(&msgUARTResponse, 0,
160-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD,
161-
(float)_data.pm10_standard);
162-
163-
packUARTResponse(&msgUARTResponse, 1,
164-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD,
165-
(float)_data.pm25_standard);
166-
167-
packUARTResponse(&msgUARTResponse, 2,
168-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD,
169-
(float)_data.pm100_standard);
170-
171-
packUARTResponse(&msgUARTResponse, 3,
172-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV,
173-
(float)_data.pm10_env);
174-
175-
packUARTResponse(&msgUARTResponse, 4,
176-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
177-
(float)_data.pm25_env);
178-
179-
packUARTResponse(&msgUARTResponse, 5,
180-
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV,
181-
(float)_data.pm100_env);
153+
"pm1006"); // TODO: Change to non-fixed value
154+
155+
// check if driverID is pm1006
156+
if (strcmp(getDriverID(), "pm1006") == 0) {
157+
// PM1006 returns only PM2.5_ENV readings
158+
packUARTResponse(&msgUARTResponse, 4,
159+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
160+
(float)_data.pm25_env);
161+
} else {
162+
packUARTResponse(&msgUARTResponse, 0,
163+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD,
164+
(float)_data.pm10_standard);
165+
166+
packUARTResponse(&msgUARTResponse, 1,
167+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD,
168+
(float)_data.pm25_standard);
169+
170+
packUARTResponse(&msgUARTResponse, 2,
171+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD,
172+
(float)_data.pm100_standard);
173+
174+
packUARTResponse(&msgUARTResponse, 3,
175+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV,
176+
(float)_data.pm10_env);
177+
178+
packUARTResponse(&msgUARTResponse, 4,
179+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
180+
(float)_data.pm25_env);
181+
182+
packUARTResponse(&msgUARTResponse, 5,
183+
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV,
184+
(float)_data.pm100_env);
185+
}
182186

183187
// Encode message data
184188
uint8_t mqttBuffer[512] = {0};

src/components/uart/ws_uart.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ void ws_uart::initUARTBus(
7777
*/
7878
/*******************************************************************************/
7979
bool ws_uart::initUARTDevicePM25AQI(SoftwareSerial *swSerial,
80-
int32_t pollingInterval) {
80+
int32_t pollingInterval,
81+
const char *device_id) {
8182
if (_pm25aqi != nullptr) {
8283
WS_DEBUG_PRINTLN(
8384
"[ERROR, UART]: pms5003 driver already initialized on bus!");
8485
return false;
8586
}
8687
WS_DEBUG_PRINTLN("[INFO, UART]: Initializing PM25AQI driver...");
8788
_pm25aqi = new ws_uart_drv_pm25aqi(swSerial, pollingInterval);
89+
_pm25aqi->setDriverID(
90+
device_id); // Since the AdafruitPM25 driver works with both Adafruit PM
91+
// sensor and PM1006, we need to set the driver ID
8892
if (!_pm25aqi->begin()) {
8993
WS_DEBUG_PRINTLN("[ERROR, UART]: PM25 driver initialization failed!");
9094
return false;
@@ -106,13 +110,17 @@ bool ws_uart::initUARTDevicePM25AQI(SoftwareSerial *swSerial,
106110
*/
107111
/*******************************************************************************/
108112
bool ws_uart::initUARTDevicePM25AQI(HardwareSerial *hwSerial,
109-
int32_t pollingInterval) {
113+
int32_t pollingInterval,
114+
const char *device_id) {
110115
if (_pm25aqi != nullptr) {
111116
WS_DEBUG_PRINTLN(
112117
"[ERROR, UART]: pms5003 driver already initialized on bus!");
113118
return false;
114119
}
115120
_pm25aqi = new ws_uart_drv_pm25aqi(hwSerial, pollingInterval);
121+
_pm25aqi->setDriverID(
122+
device_id); // Since the AdafruitPM25 driver works with both Adafruit PM
123+
// sensor and PM1006, we need to set the driver ID
116124
if (!_pm25aqi->begin()) {
117125
WS_DEBUG_PRINTLN("[ERROR, UART]: PM25 driver initialization failed!");
118126
return false;
@@ -148,20 +156,23 @@ bool ws_uart::initUARTDevice(
148156

149157
// Do we already have a device with this ID?
150158
for (ws_uart_drv *ptrUARTDriver : uartDrivers) {
151-
if (strcmp(ptrUARTDriver->getDeviceID(), msgUARTRequest->device_id) == 0) {
159+
if (strcmp(ptrUARTDriver->getDriverID(), msgUARTRequest->device_id) == 0) {
152160
deinitUARTDevice(
153161
msgUARTRequest->device_id); // Deinit the device and free resources
154162
}
155163
}
156164

157165
// Check which device type we are initializing
158-
if (strcmp(msgUARTRequest->device_id, "pms5003") == 0) {
159-
// Attempt to initialize PMS5003 driver with either SW or HW UART
166+
if (strcmp(msgUARTRequest->device_id, "pms5003") == 0 ||
167+
strcmp(msgUARTRequest->device_id, "pm1006") == 0) {
168+
// Attempt to initialize Adafruit_PM25 driver with either SW or HW UART
160169
#ifdef USE_SW_UART
161-
if (!initUARTDevicePM25AQI(_swSerial, msgUARTRequest->polling_interval))
170+
if (!initUARTDevicePM25AQI(_swSerial, msgUARTRequest->polling_interval,
171+
msgUARTRequest->device_id))
162172
return false;
163173
#else
164-
if (!initUARTDevicePM25AQI(_hwSerial, msgUARTRequest->polling_interval))
174+
if (!initUARTDevicePM25AQI(_hwSerial, msgUARTRequest->polling_interval,
175+
msgUARTRequest->device_id))
165176
return false;
166177
#endif
167178
WS_DEBUG_PRINTLN("[INFO, UART]: PM25 UART driver initialized!");
@@ -185,7 +196,7 @@ void ws_uart::deinitUARTDevice(const char *device_id) {
185196
// Iterate through the vector
186197
while (iter != uartDrivers.end()) {
187198
ws_uart_drv *ptrUARTDriver = *iter; // Get a pointer to the driver
188-
if (strcmp(ptrUARTDriver->getDeviceID(), device_id) == 0) {
199+
if (strcmp(ptrUARTDriver->getDriverID(), device_id) == 0) {
189200
if (ptrUARTDriver == _pm25aqi) {
190201
_pm25aqi = nullptr;
191202
}

src/components/uart/ws_uart.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ class ws_uart {
4444
void update(); ///< Updates the UART device at every polling interval, must be
4545
///< called by main app.
4646
#ifdef USE_SW_UART
47-
bool initUARTDevicePM25AQI(SoftwareSerial *swSerial, int32_t pollingInterval);
47+
bool initUARTDevicePM25AQI(SoftwareSerial *swSerial, int32_t pollingInterval,
48+
const char *device_id);
4849
#else
49-
bool initUARTDevicePM25AQI(HardwareSerial *hwSerial, int32_t pollingInterval);
50+
bool initUARTDevicePM25AQI(HardwareSerial *hwSerial, int32_t pollingInterval,
51+
const char *device_id);
5052
#endif
5153
private:
5254
#ifdef USE_SW_UART

0 commit comments

Comments
 (0)