Browse Source

Add long warm up option for sensors (non-blocking), re-organize code, and non-blocking read of PMS

fastled
Oscar Rovira 7 years ago
parent
commit
bf486a3b84
7 changed files with 590 additions and 571 deletions
  1. +4
    -0
      code/espurna/config/prototypes.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +7
    -7
      code/espurna/sensor.ino
  4. +4
    -3
      code/espurna/sensors/BaseSensor.h
  5. +27
    -17
      code/espurna/sensors/PMSX003Sensor.h
  6. +545
    -544
      code/espurna/static/index.html.gz.h
  7. +3
    -0
      code/html/custom.js

+ 4
- 0
code/espurna/config/prototypes.h View File

@ -81,6 +81,10 @@ template<typename T> bool idbSend(const char * topic, unsigned char id, T payloa
#if EMON_ADS1115_SUPPORT
#include <ADS1115.h>
#endif
#if PMSX003_SUPPORT
#include <SoftwareSerial.h>
#include <PMS.h>
#endif
// -----------------------------------------------------------------------------
// Utils


BIN
code/espurna/data/index.html.gz View File


+ 7
- 7
code/espurna/sensor.ino View File

@ -48,11 +48,11 @@ String _sensorTopic(magnitude_t type) {
if (type == MAGNITUDE_POWER_FACTOR) return String(SENSOR_POWER_FACTOR_TOPIC);
if (type == MAGNITUDE_ENERGY) return String(SENSOR_ENERGY_TOPIC);
if (type == MAGNITUDE_ENERGY_DELTA) return String(SENSOR_ENERGY_DELTA_TOPIC);
if (type == MAGNITUDE_ANALOG) return String(SENSOR_ANALOG_TOPIC);
if (type == MAGNITUDE_EVENTS) return String(SENSOR_EVENTS_TOPIC);
if (type == MAGNITUDE_PM1dot0) return String(SENSOR_PM1dot0_TOPIC);
if (type == MAGNITUDE_PM2dot5) return String(SENSOR_PM2dot5_TOPIC);
if (type == MAGNITUDE_PM10) return String(SENSOR_PM10_TOPIC);
if (type == MAGNITUDE_ANALOG) return String(SENSOR_ANALOG_TOPIC);
if (type == MAGNITUDE_EVENTS) return String(SENSOR_EVENTS_TOPIC);
return String(SENSOR_UNKNOWN_TOPIC);
}
@ -68,11 +68,11 @@ unsigned char _sensorDecimals(magnitude_t type) {
if (type == MAGNITUDE_POWER_FACTOR) return SENSOR_POWER_FACTOR_DECIMALS;
if (type == MAGNITUDE_ENERGY) return SENSOR_ENERGY_DECIMALS;
if (type == MAGNITUDE_ENERGY_DELTA) return SENSOR_ENERGY_DECIMALS;
if (type == MAGNITUDE_ANALOG) return SENSOR_ANALOG_DECIMALS;
if (type == MAGNITUDE_EVENTS) return SENSOR_EVENTS_DECIMALS;
if (type == MAGNITUDE_PM1dot0) return SENSOR_PM1dot0_DECIMALS;
if (type == MAGNITUDE_PM2dot5) return SENSOR_PM2dot5_DECIMALS;
if (type == MAGNITUDE_PM10) return SENSOR_PM10_DECIMALS;
if (type == MAGNITUDE_ANALOG) return SENSOR_ANALOG_DECIMALS;
if (type == MAGNITUDE_EVENTS) return SENSOR_EVENTS_DECIMALS;
return 0;
}
@ -86,12 +86,12 @@ String _sensorUnits(magnitude_t type) {
if (type == MAGNITUDE_POWER_APPARENT) return String("W");
if (type == MAGNITUDE_POWER_REACTIVE) return String("W");
if (type == MAGNITUDE_POWER_FACTOR) return String("%");
if (type == MAGNITUDE_PM1dot0) return String("µg/m3");
if (type == MAGNITUDE_PM2dot5) return String("µg/m3");
if (type == MAGNITUDE_PM10) return String("µg/m3");
if (type == MAGNITUDE_ENERGY) return String("J");
if (type == MAGNITUDE_ENERGY_DELTA) return String("J");
if (type == MAGNITUDE_EVENTS) return String("/m");
if (type == MAGNITUDE_PM1dot0) return String("µg/m3");
if (type == MAGNITUDE_PM2dot5) return String("µg/m3");
if (type == MAGNITUDE_PM10) return String("µg/m3");
return String();
}


+ 4
- 3
code/espurna/sensors/BaseSensor.h View File

@ -23,19 +23,20 @@ typedef enum magnitude_t {
MAGNITUDE_ENERGY_DELTA,
MAGNITUDE_POWER_FACTOR,
MAGNITUDE_ANALOG,
MAGNITUDE_EVENTS,
MAGNITUDE_PM1dot0,
MAGNITUDE_PM2dot5,
MAGNITUDE_PM10,
MAGNITUDE_ANALOG,
MAGNITUDE_EVENTS,
MAGNITUDE_MAX,
} magnitude_t;
#define SENSOR_ERROR_OK 0
#define SENSOR_ERROR_OUT_OF_RANGE 1
#define SENSOR_ERROR_WARM_UP 2
class BaseSensor {


+ 27
- 17
code/espurna/sensors/PMSX003Sensor.h View File

@ -18,16 +18,17 @@ class PMSX003Sensor : public BaseSensor {
_pmsSerial = new SoftwareSerial(pin_rx, pin_tx, false, 256);
_pmsSerial->begin(9600);
_pms = new PMS(* _pmsSerial);
// _pmsSerial.begin(9600);
_pms->passiveMode();
_pin_rx = pin_rx;
_pin_tx = pin_tx;
_count = 3;
_startTime = millis();
}
// Descriptive name of the sensor
String name() {
char buffer[36];
snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial RX: %i TX: %i", _pin_rx, _pin_tx);
char buffer[28];
snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
return String(buffer);
}
@ -35,9 +36,11 @@ class PMSX003Sensor : public BaseSensor {
String slot(unsigned char index) {
if (index < _count) {
_error = SENSOR_ERROR_OK;
if (index == 0) return String("PM 1.0");
if (index == 1) return String("PM 2.5");
if (index == 2) return String("PM 10");
char buffer[36];
if (index == 0) snprintf(buffer, sizeof(buffer), "PM1.0 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
return String(buffer);
}
_error = SENSOR_ERROR_OUT_OF_RANGE;
return String();
@ -55,6 +58,23 @@ class PMSX003Sensor : public BaseSensor {
return MAGNITUDE_NONE;
}
void pre() {
if(millis() - _startTime > 30000) {
_error = SENSOR_ERROR_OK;
} else {
_error = SENSOR_ERROR_WARM_UP;
}
_pms->requestRead();
}
void tick() {
if(_pms->read(_data)) {
_pm1dot0 = _data.PM_AE_UG_1_0;
_pm2dot5 = _data.PM_AE_UG_2_5;
_pm10 = _data.PM_AE_UG_10_0;
}
}
// Current value for slot # index
double value(unsigned char index) {
if (index < _count) {
@ -66,15 +86,6 @@ class PMSX003Sensor : public BaseSensor {
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
void tick() {
if(_pms->read(_data, 1000)) {
// if (_pms.read(_data)) {
_pm1dot0 = _data.PM_AE_UG_1_0;
_pm2dot5 = _data.PM_AE_UG_2_5;
_pm10 = _data.PM_AE_UG_10_0;
}
}
protected:
unsigned int _pm1dot0;
@ -82,10 +93,9 @@ class PMSX003Sensor : public BaseSensor {
unsigned int _pm10;
unsigned int _pin_rx;
unsigned int _pin_tx;
unsigned long _startTime;
SoftwareSerial * _pmsSerial;
PMS * _pms;
// SoftwareSerial _pmsSerial;
// PMS _pms;
PMS::DATA _data;
};

+ 545
- 544
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 3
- 0
code/html/custom.js View File

@ -41,6 +41,9 @@ function sensorType(type) {
if (type == 11) return "Power Factor";
if (type == 12) return "Analog";
if (type == 13) return "Events";
if (type == 14) return "PM1.0"
if (type == 15) return "PM2.5"
if (type == 16) return "PM10"
return null;
}


Loading…
Cancel
Save