Skip to content

Commit

Permalink
add ikea pm sensor overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Rubell committed Nov 22, 2023
1 parent d5a134b commit 83c60c5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 45 deletions.
8 changes: 4 additions & 4 deletions src/components/uart/drivers/ws_uart_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ class ws_uart_drv {
@returns The UART device's unique identifier.
*/
/*******************************************************************************/
const char *getDeviceID() { return _deviceID; }
const char *getDriverID() { return _driverID; }

/*******************************************************************************/
/*!
@brief Sets the UART device's unique identifier.
@brief Sets the UART driver's identifer.
@param id
The UART device's unique identifier.
*/
/*******************************************************************************/
void setDriverID(const char *id) { _deviceID = id; }
void setDriverID(const char *id) { _driverID = strdup(id); }

/*******************************************************************************/
/*!
Expand Down Expand Up @@ -169,7 +169,7 @@ class ws_uart_drv {
private:
unsigned long
_prvPoll; ///< Last time the UART device was polled, in milliseconds
const char *_deviceID = nullptr; ///< UART device's ID
const char *_driverID = nullptr; ///< UART device's ID
};

#endif // WS_UART_DRV_H
66 changes: 35 additions & 31 deletions src/components/uart/drivers/ws_uart_drv_pm25aqi.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
: ws_uart_drv(swSerial, interval) {
_swSerial = swSerial;
pollingInterval = (unsigned long)interval;
// Set driver ID
setDriverID("pms5003");
};
#else
/*******************************************************************************/
Expand All @@ -57,8 +55,6 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
: ws_uart_drv(hwSerial, interval) {
_hwSerial = hwSerial;
pollingInterval = (unsigned long)interval;
// Set driver ID
setDriverID("pms5003");
};
#endif // USE_SW_UART

Expand Down Expand Up @@ -151,34 +147,42 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
// We'll be sending back six sensor_events: pm10_standard, pm25_standard,
// pm100_standard, pm10_env, pm25_env, and pm100_env
msgUARTResponse.payload.resp_uart_device_event.sensor_event_count = 6;
// getDeviceID();
Serial.print("Device ID: ");
Serial.println(getDriverID());
strcpy(msgUARTResponse.payload.resp_uart_device_event.device_id,
getDeviceID());

// Pack sensor data into UART response message
packUARTResponse(&msgUARTResponse, 0,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD,
(float)_data.pm10_standard);

packUARTResponse(&msgUARTResponse, 1,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD,
(float)_data.pm25_standard);

packUARTResponse(&msgUARTResponse, 2,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD,
(float)_data.pm100_standard);

packUARTResponse(&msgUARTResponse, 3,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV,
(float)_data.pm10_env);

packUARTResponse(&msgUARTResponse, 4,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
(float)_data.pm25_env);

packUARTResponse(&msgUARTResponse, 5,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV,
(float)_data.pm100_env);
"pm1006"); // TODO: Change to non-fixed value

// check if driverID is pm1006
if (strcmp(getDriverID(), "pm1006") == 0) {
// PM1006 returns only PM2.5_ENV readings
packUARTResponse(&msgUARTResponse, 4,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
(float)_data.pm25_env);
} else {
packUARTResponse(&msgUARTResponse, 0,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD,
(float)_data.pm10_standard);

packUARTResponse(&msgUARTResponse, 1,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD,
(float)_data.pm25_standard);

packUARTResponse(&msgUARTResponse, 2,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD,
(float)_data.pm100_standard);

packUARTResponse(&msgUARTResponse, 3,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV,
(float)_data.pm10_env);

packUARTResponse(&msgUARTResponse, 4,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV,
(float)_data.pm25_env);

packUARTResponse(&msgUARTResponse, 5,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV,
(float)_data.pm100_env);
}

// Encode message data
uint8_t mqttBuffer[512] = {0};
Expand Down
27 changes: 19 additions & 8 deletions src/components/uart/ws_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ void ws_uart::initUARTBus(
*/
/*******************************************************************************/
bool ws_uart::initUARTDevicePM25AQI(SoftwareSerial *swSerial,
int32_t pollingInterval) {
int32_t pollingInterval,
const char *device_id) {
if (_pm25aqi != nullptr) {
WS_DEBUG_PRINTLN(
"[ERROR, UART]: pms5003 driver already initialized on bus!");
return false;
}
WS_DEBUG_PRINTLN("[INFO, UART]: Initializing PM25AQI driver...");
_pm25aqi = new ws_uart_drv_pm25aqi(swSerial, pollingInterval);
_pm25aqi->setDriverID(
device_id); // Since the AdafruitPM25 driver works with both Adafruit PM
// sensor and PM1006, we need to set the driver ID
if (!_pm25aqi->begin()) {
WS_DEBUG_PRINTLN("[ERROR, UART]: PM25 driver initialization failed!");
return false;
Expand All @@ -106,13 +110,17 @@ bool ws_uart::initUARTDevicePM25AQI(SoftwareSerial *swSerial,
*/
/*******************************************************************************/
bool ws_uart::initUARTDevicePM25AQI(HardwareSerial *hwSerial,
int32_t pollingInterval) {
int32_t pollingInterval,
const char *device_id) {
if (_pm25aqi != nullptr) {
WS_DEBUG_PRINTLN(
"[ERROR, UART]: pms5003 driver already initialized on bus!");
return false;
}
_pm25aqi = new ws_uart_drv_pm25aqi(hwSerial, pollingInterval);
_pm25aqi->setDriverID(
device_id); // Since the AdafruitPM25 driver works with both Adafruit PM
// sensor and PM1006, we need to set the driver ID
if (!_pm25aqi->begin()) {
WS_DEBUG_PRINTLN("[ERROR, UART]: PM25 driver initialization failed!");
return false;
Expand Down Expand Up @@ -148,20 +156,23 @@ bool ws_uart::initUARTDevice(

// Do we already have a device with this ID?
for (ws_uart_drv *ptrUARTDriver : uartDrivers) {
if (strcmp(ptrUARTDriver->getDeviceID(), msgUARTRequest->device_id) == 0) {
if (strcmp(ptrUARTDriver->getDriverID(), msgUARTRequest->device_id) == 0) {
deinitUARTDevice(
msgUARTRequest->device_id); // Deinit the device and free resources
}
}

// Check which device type we are initializing
if (strcmp(msgUARTRequest->device_id, "pms5003") == 0) {
// Attempt to initialize PMS5003 driver with either SW or HW UART
if (strcmp(msgUARTRequest->device_id, "pms5003") == 0 ||
strcmp(msgUARTRequest->device_id, "pm1006") == 0) {
// Attempt to initialize Adafruit_PM25 driver with either SW or HW UART
#ifdef USE_SW_UART
if (!initUARTDevicePM25AQI(_swSerial, msgUARTRequest->polling_interval))
if (!initUARTDevicePM25AQI(_swSerial, msgUARTRequest->polling_interval,
msgUARTRequest->device_id))
return false;
#else
if (!initUARTDevicePM25AQI(_hwSerial, msgUARTRequest->polling_interval))
if (!initUARTDevicePM25AQI(_hwSerial, msgUARTRequest->polling_interval,
msgUARTRequest->device_id))
return false;
#endif
WS_DEBUG_PRINTLN("[INFO, UART]: PM25 UART driver initialized!");
Expand All @@ -185,7 +196,7 @@ void ws_uart::deinitUARTDevice(const char *device_id) {
// Iterate through the vector
while (iter != uartDrivers.end()) {
ws_uart_drv *ptrUARTDriver = *iter; // Get a pointer to the driver
if (strcmp(ptrUARTDriver->getDeviceID(), device_id) == 0) {
if (strcmp(ptrUARTDriver->getDriverID(), device_id) == 0) {
if (ptrUARTDriver == _pm25aqi) {
_pm25aqi = nullptr;
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/uart/ws_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class ws_uart {
void update(); ///< Updates the UART device at every polling interval, must be
///< called by main app.
#ifdef USE_SW_UART
bool initUARTDevicePM25AQI(SoftwareSerial *swSerial, int32_t pollingInterval);
bool initUARTDevicePM25AQI(SoftwareSerial *swSerial, int32_t pollingInterval,
const char *device_id);
#else
bool initUARTDevicePM25AQI(HardwareSerial *hwSerial, int32_t pollingInterval);
bool initUARTDevicePM25AQI(HardwareSerial *hwSerial, int32_t pollingInterval,
const char *device_id);
#endif
private:
#ifdef USE_SW_UART
Expand Down

0 comments on commit 83c60c5

Please sign in to comment.