Skip to content

Commit

Permalink
Discard PM measurements which took too much time to produce
Browse files Browse the repository at this point in the history
  • Loading branch information
hg committed Nov 17, 2020
1 parent a475fd4 commit ec48f21
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion firmware/main/dallas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ DS18B20_Info *TempSensor::searchTempSensor(const OneWireBus *const owb) {
void TempSensor::runTempMeasurements(const DS18B20_Info *device,
const TempSensor *const config) {
int errCount = 0;
Measurement ms{.type = MeasurementType::MS_TEMPERATURE,
Measurement ms{.type = MeasurementType::Temperature,
.sensor = config->name};
TickType_t lastWakeTime = xTaskGetTickCount();

Expand Down
8 changes: 4 additions & 4 deletions firmware/main/measurement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void Measurement::set(const pms::ResponseSum &sum) {

const char *Measurement::getType() const {
switch (type) {
case MeasurementType::MS_TEMPERATURE:
case MeasurementType::Temperature:
return "meas/temp";

case MeasurementType::MS_PARTICULATES:
case MeasurementType::Particulates:
return "meas/part";

default:
Expand All @@ -61,13 +61,13 @@ const char *Measurement::getType() const {

bool Measurement::formatMsg(char *const msg, const size_t len) const {
switch (type) {
case MeasurementType::MS_TEMPERATURE: {
case MeasurementType::Temperature: {
constexpr auto tpl = R"({"dev":"%s","time":%ld,"sens":"%s","temp":%f})";
snprintf(msg, len, tpl, appSettings.devName, time, sensor, temp);
return true;
}

case MeasurementType::MS_PARTICULATES: {
case MeasurementType::Particulates: {
constexpr auto tpl =
R"({"dev":"%s","time":%ld,"sens":"%s","std":{"pm1":%u,"pm2.5":%u,"pm10":%u},"atm":{"pm1":%u,"pm2.5":%u,"pm10":%u},"cnt":{"pm0.3":%u,"pm0.5":%u,"pm1":%u,"pm2.5":%u,"pm5":%u,"pm10":%u}})";
snprintf(msg, len, tpl, appSettings.devName, time, sensor, pm.std.pm1Mcg,
Expand Down
2 changes: 1 addition & 1 deletion firmware/main/measurement.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "utils.hh"
#include <ctime>

enum class MeasurementType { MS_TEMPERATURE, MS_PARTICULATES };
enum class MeasurementType { Temperature, Particulates };

struct Measurement {
MeasurementType type;
Expand Down
33 changes: 22 additions & 11 deletions firmware/main/pms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void Response::swapBytes() {

Response res;
ResponseSum sum;
Measurement ms{.type = MeasurementType::MS_PARTICULATES,
.sensor = station.name};
Measurement ms{.type = MeasurementType::Particulates, .sensor = station.name};

TickType_t lastWake = xTaskGetTickCount();

Expand All @@ -79,8 +78,15 @@ void Response::swapBytes() {
}

Timer execTime;
bool periodOverflow = false;

for (int successful = 0; successful < CONFIG_PARTICULATE_MEASUREMENTS;) {
if (execTime.seconds() >= CONFIG_PARTICULATE_PERIOD_SECONDS) {
ESP_LOGE(logTag, "PM measurement took too much time");
periodOverflow = true;
break;
}

const int received = station.readResponse(res, secToTicks(5));

if (received != sizeof(res)) {
Expand Down Expand Up @@ -133,20 +139,25 @@ void Response::swapBytes() {

if (!sendEach) {
ms.time = getTimestamp();
ms.set(sum);
}

sent = station.writeCommand(cmd::cmdSleep);
if (sent != sizeof(cmd::cmdSleep)) {
ESP_LOGE(logTag, "could not send sleep command");
}

if (!sendEach) {
ESP_LOGI(logTag, "avg PM: 1=%u, 2.5=%u, 10=%u", ms.pm.atm.pm1Mcg,
ms.pm.atm.pm2Mcg, ms.pm.atm.pm10Mcg);
if (periodOverflow) {
// measurement period overflow, skip next iteration
lastWake = xTaskGetTickCount();
} else {
if (!sendEach) {
ms.set(sum);
ESP_LOGI(logTag, "avg PM: 1=%u, 2.5=%u, 10=%u", ms.pm.atm.pm1Mcg,
ms.pm.atm.pm2Mcg, ms.pm.atm.pm10Mcg);

if (!station.queue->putRetrying(ms)) {
ESP_LOGE(logTag, "could not queue averaged particulate measurement");
if (!station.queue->putRetrying(ms)) {
ESP_LOGE(logTag, "could not queue averaged PM measurement");
}
}
}

Expand Down Expand Up @@ -182,10 +193,10 @@ void Station::start(Queue<Measurement> &msQueue) {
ESP_ERROR_CHECK(
uart_set_pin(port, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));

std::string taskName{"pm_"};
taskName.append(name);
char buf[32];
snprintf(buf, sizeof(buf), "pm_%s", name);

xTaskCreate(collectionTask, taskName.c_str(), KiB(2), this, 4, nullptr);
xTaskCreate(collectionTask, buf, KiB(2), this, 4, nullptr);
}

void ResponseSum::addMeasurement(const Response &resp) {
Expand Down

0 comments on commit ec48f21

Please sign in to comment.