From eb4ee3769b60d121c9bdd41c44e6af55002ea0ce Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 30 Mar 2019 19:10:29 +0100 Subject: [PATCH 01/76] test --- platformio.ini | 2 +- src/esphome/application.cpp | 12 ++++- src/esphome/application.h | 20 ++++++++ src/esphome/sensor/ppd42x_sensor.cpp | 73 ++++++++++++++++++++++++++++ src/esphome/sensor/ppd42x_sensor.h | 61 +++++++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/esphome/sensor/ppd42x_sensor.cpp create mode 100644 src/esphome/sensor/ppd42x_sensor.h diff --git a/platformio.ini b/platformio.ini index 6f8d4f7d..9680cb1f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ lib_deps = ESP Async WebServer@1.1.1 FastLED@3.2.0 NeoPixelBus@2.4.1 - ESPAsyncTCP@1.2.0 + ESPAsyncTCP@1.1.3 build_flags = -Wno-reorder -DUSE_WEB_SERVER diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 7d422be0..6486cd74 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1105,7 +1105,17 @@ sensor::TotalDailyEnergy *Application::make_total_daily_energy_sensor(const std: return total; } #endif - +#ifdef USE_PPD42X_SENSOR +sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm2_5_pin, + uint32_t update_interval) { + auto *ppd42x = this->register_component( + new Ppd42xSensorComponent(friendly_name, pm10_0_pin.copy(), pm2_5_pin.copy(), update_interval)); + this->register_sensor(ppd42x); + return ppd42x; +} +#endif void Application::set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; } void Application::register_component_(Component *comp) { diff --git a/src/esphome/application.h b/src/esphome/application.h index fad6758f..90c23256 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -125,6 +125,7 @@ #include "esphome/sensor/total_daily_energy.h" #include "esphome/sensor/tsl2561_sensor.h" #include "esphome/sensor/ultrasonic_sensor.h" +#include "esphome/sensor/ppd42x_sensor.h" #include "esphome/sensor/uptime_sensor.h" #include "esphome/sensor/wifi_signal_sensor.h" #include "esphome/sensor/sds011_component.h" @@ -575,6 +576,25 @@ class Application { const GPIOInputPin &echo_pin, uint32_t update_interval = 60000); #endif +#ifdef USE_PPD42X_SENSOR + /** Create an PPD42x particle sensor. + * + * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one + * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement + * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin + * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP + * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory to be there). + * + * @param friendly_name The friendly name for this sensor advertised to Home Assistant. + * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. + * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. + * @param update_interval The time in ms between updates, defaults to 60 seconds. + */ + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_2_5_pin, + uint32_t update_interval = 60000); +#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *make_wifi_signal_sensor(const std::string &name, uint32_t update_interval = 60000); diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp new file mode 100644 index 00000000..31db38ad --- /dev/null +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -0,0 +1,73 @@ +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/ppd42x_sensor.h" + +#include "esphome/log.h" +#include "esphome/helpers.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +static const char *TAG = "sensor.ppd42x"; + +Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, GPIOPin *pm_10_0_pin, + uint32_t update_interval) + : PollingSensorComponent(name, update_interval), pm_02_5_pin_(pm_02_5_pin), pm_10_0_pin_(pm_10_0_pin) {} +void Ppd42xSensorComponent::setup() { + ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); + this->pm_02_5_pin_->setup(); + this->pm_10_0_pin_->setup(); +} +void Ppd42xSensorComponent::update() { + + uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); + + ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_10_0, time_pm_02_5); + + if (time_pm_10_0 == 0) { + ESP_LOGD(TAG, "'%s' - PM10.0 measurement timed out!", this->name_.c_str()); + this->publish_state(NAN); + } else { + float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_10_0); + this->publish_state(result); + ESP_LOGD(TAG, "'%s' - Got PM10.0 Concentration: %.1f µg/m³", this->name_.c_str(), result); + this->publish_state(result); + } + if (time_pm_02_5 == 0) { + ESP_LOGD(TAG, "'%s' - PM 2.5 measurement timed out!", this->name_.c_str()); + this->publish_state(NAN); + } else { + float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_02_5); + this->publish_state(result); + ESP_LOGD(TAG, "'%s' - Got PM 2.5 Concentration: %.1f µg/m³", this->name_.c_str(), result); + this->publish_state(result); + } +} +void Ppd42xSensorComponent::dump_config() { + LOG_SENSOR("", "PPD42X Sensor", this); + LOG_PIN(" PM10.0 Pin: ", this->pm_10_0_pin_); + LOG_PIN(" PM 2.5 Pin: ", this->pm_02_5_pin_); + ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); + LOG_UPDATE_INTERVAL(this); +} +float Ppd42xSensorComponent::us_to_pm(uint32_t sampleLength, uint32_t time_pm) { + float ratio = time_pm/(sampleLength*10.0); + return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; +} +float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } +std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } +int8_t Ppd42xSensorComponent::accuracy_decimals() { + return 2; // cm precision +} +void Ppd42xSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } + +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h new file mode 100644 index 00000000..2d3fc7c1 --- /dev/null +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -0,0 +1,61 @@ +#ifndef ESPHOME_SENSOR_PPD42X_SENSOR_H +#define ESPHOME_SENSOR_PPD42X_SENSOR_H + +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/sensor.h" +#include "esphome/esphal.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +class Ppd42xSensorComponent : public PollingSensorComponent { + public: + /** Construct the PPD42X sensor with the specified 2.5ppm pin and 10.0ppm pin. + * + * @param pm_2_5_pin The pm_2_5 pin where pulses are sent to. + * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. + * @param update_interval The interval in ms the sensor should check for new values. + */ + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_2_5_pin, GPIOPin *pm_10_0_pin, + uint32_t update_interval = 60000); + + /// Set the timeout for waiting for the pm_10_0 in µs. + void set_timeout_us(uint32_t timeout_us); + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + /// Set up pins and register interval. + void setup() override; + void dump_config() override; + + void update() override; + + std::string unit_of_measurement() override; + std::string icon() override; + int8_t accuracy_decimals() override; + + float get_setup_priority() const override; + + + protected: + /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. + static float us_to_pm(uint32_t us, uint32_t us); + /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. + + GPIOPin *pm_2_5_pin_; + GPIOPin *pm_10_0_pin_; + uint32_t timeout_us_{11662}; + +}; + +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR + +#endif // ESPHOME_SENSOR_PPD42X_SENSOR_H From 49ed1e30645330c5227e3a6cd08c703bf92ad295 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:25:36 +0200 Subject: [PATCH 02/76] tests.yaml --- src/esphome/defines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 271d1334..667758a1 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -38,6 +38,7 @@ #define USE_HTU21D_SENSOR #define USE_HDC1080_SENSOR #define USE_ULTRASONIC_SENSOR +#define USE_PPD42X_SENSOR #define USE_WIFI_SIGNAL_SENSOR #define USE_OUTPUT #ifdef ARDUINO_ARCH_ESP32 From 9b8a2da0223ed804b44855e66f8257619db4c837 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:34:57 +0200 Subject: [PATCH 03/76] us_to_pm fix --- src/esphome/sensor/ppd42x_sensor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 2d3fc7c1..dde43ea3 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t us, uint32_t us); + static float us_to_pm(uint32_t l_us, uint32_t t_us); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_2_5_pin_; From f00130623d1c02618b9b8574373b5b53d5c081e9 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:55:58 +0200 Subject: [PATCH 04/76] us_to_pm fix 2 --- src/esphome/sensor/ppd42x_sensor.cpp | 2 +- src/esphome/sensor/ppd42x_sensor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 31db38ad..a63b461d 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -26,7 +26,7 @@ void Ppd42xSensorComponent::update() { uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); - ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_10_0, time_pm_02_5); + ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); if (time_pm_10_0 == 0) { ESP_LOGD(TAG, "'%s' - PM10.0 measurement timed out!", this->name_.c_str()); diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index dde43ea3..637cf510 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -48,7 +48,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { GPIOPin *pm_2_5_pin_; GPIOPin *pm_10_0_pin_; - uint32_t timeout_us_{11662}; + uint32_t timeout_us_{30000}; }; From c5b057928f64edfee9d06752e12687ede436fde0 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 12:14:33 +0200 Subject: [PATCH 05/76] pm_02_5_pin_ fix --- src/esphome/application.cpp | 24 ++++++++++++------------ src/esphome/application.h | 2 +- src/esphome/sensor/ppd42x_sensor.cpp | 4 ++-- src/esphome/sensor/ppd42x_sensor.h | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 6486cd74..38133307 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -432,7 +432,17 @@ sensor::UltrasonicSensorComponent *Application::make_ultrasonic_sensor(const std return ultrasonic; } #endif - +#ifdef USE_PPD42X_SENSOR +sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval) { + auto *ppd42x = this->register_component( + new Ppd42xSensorComponent(friendly_name, pm_10_0_pin.copy(), pm_02_5_pin.copy(), update_interval)); + this->register_sensor(ppd42x); + return ppd42x; +} +#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *Application::make_wifi_signal_sensor(const std::string &name, uint32_t update_interval) { auto *wifi = this->register_component(new WiFiSignalSensor(name, update_interval)); @@ -1105,17 +1115,7 @@ sensor::TotalDailyEnergy *Application::make_total_daily_energy_sensor(const std: return total; } #endif -#ifdef USE_PPD42X_SENSOR -sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm2_5_pin, - uint32_t update_interval) { - auto *ppd42x = this->register_component( - new Ppd42xSensorComponent(friendly_name, pm10_0_pin.copy(), pm2_5_pin.copy(), update_interval)); - this->register_sensor(ppd42x); - return ppd42x; -} -#endif + void Application::set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; } void Application::register_component_(Component *comp) { diff --git a/src/esphome/application.h b/src/esphome/application.h index 90c23256..411479e3 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -592,7 +592,7 @@ class Application { */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_2_5_pin, + const GPIOInputPin &pm_02_5_pin, uint32_t update_interval = 60000); #endif diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index a63b461d..9f1bf2dd 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -13,9 +13,9 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; -Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, GPIOPin *pm_10_0_pin, +Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_02_5_pin_(pm_02_5_pin), pm_10_0_pin_(pm_10_0_pin) {} + : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->pm_02_5_pin_->setup(); diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 637cf510..64fcaeb9 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -46,7 +46,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { static float us_to_pm(uint32_t l_us, uint32_t t_us); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. - GPIOPin *pm_2_5_pin_; + GPIOPin *pm_02_5_pin_; GPIOPin *pm_10_0_pin_; uint32_t timeout_us_{30000}; From 92055b2a966a2d6352e61a7b6a1e4264438d42b3 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 13:32:22 +0200 Subject: [PATCH 06/76] pm_02_5_pin_ fix 2 --- src/esphome/sensor/ppd42x_sensor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 64fcaeb9..76acd452 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -20,7 +20,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. * @param update_interval The interval in ms the sensor should check for new values. */ - Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_2_5_pin, GPIOPin *pm_10_0_pin, + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t l_us, uint32_t t_us); + static float us_to_pm(uint32_t sampleLength, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_02_5_pin_; From da62e60c7443283574202b7afbdce7901220d515 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 13:47:30 +0200 Subject: [PATCH 07/76] uper case fix --- src/esphome/sensor/ppd42x_sensor.cpp | 4 ++-- src/esphome/sensor/ppd42x_sensor.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 9f1bf2dd..82ca2868 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -54,8 +54,8 @@ void Ppd42xSensorComponent::dump_config() { ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); LOG_UPDATE_INTERVAL(this); } -float Ppd42xSensorComponent::us_to_pm(uint32_t sampleLength, uint32_t time_pm) { - float ratio = time_pm/(sampleLength*10.0); +float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { + float ratio = time_pm/(sample_length*10.0); return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; } float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 76acd452..dbc323b8 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t sampleLength, uint32_t time_pm); + static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_02_5_pin_; From d330b2517d178a5ae7678d9b50b8c01ca5435eff Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:24:05 +0200 Subject: [PATCH 08/76] ident fix --- src/esphome/application.cpp | 6 +++--- src/esphome/application.h | 8 ++++---- src/esphome/sensor/ppd42x_sensor.cpp | 14 ++++++++------ src/esphome/sensor/ppd42x_sensor.h | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 38133307..96b4b914 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -434,9 +434,9 @@ sensor::UltrasonicSensorComponent *Application::make_ultrasonic_sensor(const std #endif #ifdef USE_PPD42X_SENSOR sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval) { + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval) { auto *ppd42x = this->register_component( new Ppd42xSensorComponent(friendly_name, pm_10_0_pin.copy(), pm_02_5_pin.copy(), update_interval)); this->register_sensor(ppd42x); diff --git a/src/esphome/application.h b/src/esphome/application.h index 411479e3..a8e08ef1 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -583,7 +583,7 @@ class Application { * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP - * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory to be there). + * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). * * @param friendly_name The friendly name for this sensor advertised to Home Assistant. * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. @@ -591,9 +591,9 @@ class Application { * @param update_interval The time in ms between updates, defaults to 60 seconds. */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval = 60000); + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval = 60000); #endif #ifdef USE_WIFI_SIGNAL_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 82ca2868..085ee2e8 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -14,7 +14,7 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval) + uint32_t update_interval) : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); @@ -23,8 +23,10 @@ void Ppd42xSensorComponent::setup() { } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_10_0 = + pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_02_5 = + pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); @@ -55,9 +57,9 @@ void Ppd42xSensorComponent::dump_config() { LOG_UPDATE_INTERVAL(this); } float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { - float ratio = time_pm/(sample_length*10.0); - return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; -} + float ratio = time_pm / (sample_length * 10.0); + return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;} + float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index dbc323b8..fe35103f 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -21,7 +21,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param update_interval The interval in ms the sensor should check for new values. */ Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval = 60000); + uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. void set_timeout_us(uint32_t timeout_us); From 93a474275e0dbf97150ab21321c9ff4e57fccd14 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:44:07 +0200 Subject: [PATCH 09/76] spaces fix --- src/esphome/application.h | 6 ++---- src/esphome/sensor/ppd42x_sensor.cpp | 10 ++++++---- src/esphome/sensor/ppd42x_sensor.h | 5 ++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index a8e08ef1..aaead71a 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -590,10 +590,8 @@ class Application { * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. * @param update_interval The time in ms between updates, defaults to 60 seconds. */ - sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval = 60000); + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, uint32_t update_interval = 60000); #endif #ifdef USE_WIFI_SIGNAL_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 085ee2e8..d54975ba 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -15,7 +15,7 @@ static const char *TAG = "sensor.ppd42x"; Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} + : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->pm_02_5_pin_->setup(); @@ -23,9 +23,9 @@ void Ppd42xSensorComponent::setup() { } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = + uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - uint32_t time_pm_02_5 = + uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); @@ -62,7 +62,9 @@ float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } -std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; +} + int8_t Ppd42xSensorComponent::accuracy_decimals() { return 2; // cm precision } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index fe35103f..7d86781e 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -20,7 +20,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. * @param update_interval The interval in ms the sensor should check for new values. */ - Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. @@ -48,8 +48,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { GPIOPin *pm_02_5_pin_; GPIOPin *pm_10_0_pin_; - uint32_t timeout_us_{30000}; - + uint32_t timeout_us_{30000}; }; } // namespace sensor From eea52c8509e62f93d4a3e318e2e43ab9317a5f86 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:56:43 +0200 Subject: [PATCH 10/76] spaces fix 2 --- src/esphome/sensor/ppd42x_sensor.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index d54975ba..ac383e47 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -58,12 +58,12 @@ void Ppd42xSensorComponent::dump_config() { } float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { float ratio = time_pm / (sample_length * 10.0); - return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;} + return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; +} float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } -std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; -} +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } int8_t Ppd42xSensorComponent::accuracy_decimals() { return 2; // cm precision From ff5ccdd63688983555073f65b71f1733fb26ae8e Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 15:11:19 +0200 Subject: [PATCH 11/76] spaces fix 3 --- src/esphome/sensor/ppd42x_sensor.cpp | 1 - src/esphome/sensor/ppd42x_sensor.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index ac383e47..ccfe8756 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -22,7 +22,6 @@ void Ppd42xSensorComponent::setup() { this->pm_10_0_pin_->setup(); } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); uint32_t time_pm_02_5 = diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 7d86781e..930bf52b 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -39,7 +39,6 @@ class Ppd42xSensorComponent : public PollingSensorComponent { int8_t accuracy_decimals() override; float get_setup_priority() const override; - protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. From 6c4d904529c4298d934461b392213e7ee38cfd76 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 15:25:39 +0200 Subject: [PATCH 12/76] spaces fix 4 --- src/esphome/application.h | 6 +++--- src/esphome/sensor/ppd42x_sensor.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index aaead71a..937e8ad4 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -581,13 +581,13 @@ class Application { * * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement - * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin - * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP + * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 µg/m³ will be sent to) and the PM2_5 pin + * (where particule > 2.5 µg/m³ will be sent to). Note that in order to not block indefinitely if we don't receive UP * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). * * @param friendly_name The friendly name for this sensor advertised to Home Assistant. * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. - * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. + * @param pm_02_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. * @param update_interval The time in ms between updates, defaults to 60 seconds. */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 930bf52b..2be3fb89 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -39,9 +39,9 @@ class Ppd42xSensorComponent : public PollingSensorComponent { int8_t accuracy_decimals() override; float get_setup_priority() const override; - + protected: - /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. + /// Helper function to convert the specified pm_xx_x duration in µs to µg/m³. static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. From 9d89b61030ea3cd68b5841cf3c95a1f4032f6547 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 30 Mar 2019 19:10:29 +0100 Subject: [PATCH 13/76] test --- platformio.ini | 2 +- src/esphome/application.cpp | 12 ++++- src/esphome/application.h | 20 ++++++++ src/esphome/sensor/ppd42x_sensor.cpp | 73 ++++++++++++++++++++++++++++ src/esphome/sensor/ppd42x_sensor.h | 61 +++++++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/esphome/sensor/ppd42x_sensor.cpp create mode 100644 src/esphome/sensor/ppd42x_sensor.h diff --git a/platformio.ini b/platformio.ini index 6f8d4f7d..9680cb1f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ lib_deps = ESP Async WebServer@1.1.1 FastLED@3.2.0 NeoPixelBus@2.4.1 - ESPAsyncTCP@1.2.0 + ESPAsyncTCP@1.1.3 build_flags = -Wno-reorder -DUSE_WEB_SERVER diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 7d422be0..6486cd74 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1105,7 +1105,17 @@ sensor::TotalDailyEnergy *Application::make_total_daily_energy_sensor(const std: return total; } #endif - +#ifdef USE_PPD42X_SENSOR +sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm2_5_pin, + uint32_t update_interval) { + auto *ppd42x = this->register_component( + new Ppd42xSensorComponent(friendly_name, pm10_0_pin.copy(), pm2_5_pin.copy(), update_interval)); + this->register_sensor(ppd42x); + return ppd42x; +} +#endif void Application::set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; } void Application::register_component_(Component *comp) { diff --git a/src/esphome/application.h b/src/esphome/application.h index fad6758f..90c23256 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -125,6 +125,7 @@ #include "esphome/sensor/total_daily_energy.h" #include "esphome/sensor/tsl2561_sensor.h" #include "esphome/sensor/ultrasonic_sensor.h" +#include "esphome/sensor/ppd42x_sensor.h" #include "esphome/sensor/uptime_sensor.h" #include "esphome/sensor/wifi_signal_sensor.h" #include "esphome/sensor/sds011_component.h" @@ -575,6 +576,25 @@ class Application { const GPIOInputPin &echo_pin, uint32_t update_interval = 60000); #endif +#ifdef USE_PPD42X_SENSOR + /** Create an PPD42x particle sensor. + * + * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one + * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement + * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin + * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP + * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory to be there). + * + * @param friendly_name The friendly name for this sensor advertised to Home Assistant. + * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. + * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. + * @param update_interval The time in ms between updates, defaults to 60 seconds. + */ + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_2_5_pin, + uint32_t update_interval = 60000); +#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *make_wifi_signal_sensor(const std::string &name, uint32_t update_interval = 60000); diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp new file mode 100644 index 00000000..31db38ad --- /dev/null +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -0,0 +1,73 @@ +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/ppd42x_sensor.h" + +#include "esphome/log.h" +#include "esphome/helpers.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +static const char *TAG = "sensor.ppd42x"; + +Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, GPIOPin *pm_10_0_pin, + uint32_t update_interval) + : PollingSensorComponent(name, update_interval), pm_02_5_pin_(pm_02_5_pin), pm_10_0_pin_(pm_10_0_pin) {} +void Ppd42xSensorComponent::setup() { + ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); + this->pm_02_5_pin_->setup(); + this->pm_10_0_pin_->setup(); +} +void Ppd42xSensorComponent::update() { + + uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); + + ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_10_0, time_pm_02_5); + + if (time_pm_10_0 == 0) { + ESP_LOGD(TAG, "'%s' - PM10.0 measurement timed out!", this->name_.c_str()); + this->publish_state(NAN); + } else { + float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_10_0); + this->publish_state(result); + ESP_LOGD(TAG, "'%s' - Got PM10.0 Concentration: %.1f µg/m³", this->name_.c_str(), result); + this->publish_state(result); + } + if (time_pm_02_5 == 0) { + ESP_LOGD(TAG, "'%s' - PM 2.5 measurement timed out!", this->name_.c_str()); + this->publish_state(NAN); + } else { + float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_02_5); + this->publish_state(result); + ESP_LOGD(TAG, "'%s' - Got PM 2.5 Concentration: %.1f µg/m³", this->name_.c_str(), result); + this->publish_state(result); + } +} +void Ppd42xSensorComponent::dump_config() { + LOG_SENSOR("", "PPD42X Sensor", this); + LOG_PIN(" PM10.0 Pin: ", this->pm_10_0_pin_); + LOG_PIN(" PM 2.5 Pin: ", this->pm_02_5_pin_); + ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); + LOG_UPDATE_INTERVAL(this); +} +float Ppd42xSensorComponent::us_to_pm(uint32_t sampleLength, uint32_t time_pm) { + float ratio = time_pm/(sampleLength*10.0); + return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; +} +float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } +std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } +int8_t Ppd42xSensorComponent::accuracy_decimals() { + return 2; // cm precision +} +void Ppd42xSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } + +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h new file mode 100644 index 00000000..2d3fc7c1 --- /dev/null +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -0,0 +1,61 @@ +#ifndef ESPHOME_SENSOR_PPD42X_SENSOR_H +#define ESPHOME_SENSOR_PPD42X_SENSOR_H + +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/sensor.h" +#include "esphome/esphal.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +class Ppd42xSensorComponent : public PollingSensorComponent { + public: + /** Construct the PPD42X sensor with the specified 2.5ppm pin and 10.0ppm pin. + * + * @param pm_2_5_pin The pm_2_5 pin where pulses are sent to. + * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. + * @param update_interval The interval in ms the sensor should check for new values. + */ + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_2_5_pin, GPIOPin *pm_10_0_pin, + uint32_t update_interval = 60000); + + /// Set the timeout for waiting for the pm_10_0 in µs. + void set_timeout_us(uint32_t timeout_us); + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + /// Set up pins and register interval. + void setup() override; + void dump_config() override; + + void update() override; + + std::string unit_of_measurement() override; + std::string icon() override; + int8_t accuracy_decimals() override; + + float get_setup_priority() const override; + + + protected: + /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. + static float us_to_pm(uint32_t us, uint32_t us); + /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. + + GPIOPin *pm_2_5_pin_; + GPIOPin *pm_10_0_pin_; + uint32_t timeout_us_{11662}; + +}; + +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR + +#endif // ESPHOME_SENSOR_PPD42X_SENSOR_H From a52c2a6054668eb64dadc54deb75beb84e3b0b04 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:25:36 +0200 Subject: [PATCH 14/76] tests.yaml --- src/esphome/defines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 271d1334..667758a1 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -38,6 +38,7 @@ #define USE_HTU21D_SENSOR #define USE_HDC1080_SENSOR #define USE_ULTRASONIC_SENSOR +#define USE_PPD42X_SENSOR #define USE_WIFI_SIGNAL_SENSOR #define USE_OUTPUT #ifdef ARDUINO_ARCH_ESP32 From 96f38d5baa13c21aebf28d2f453b5e88db4ccc87 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:34:57 +0200 Subject: [PATCH 15/76] us_to_pm fix --- src/esphome/sensor/ppd42x_sensor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 2d3fc7c1..dde43ea3 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t us, uint32_t us); + static float us_to_pm(uint32_t l_us, uint32_t t_us); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_2_5_pin_; From f6d1f446b1e833de64875b33ab4022fb0ea6c7d1 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 11:55:58 +0200 Subject: [PATCH 16/76] us_to_pm fix 2 --- src/esphome/sensor/ppd42x_sensor.cpp | 2 +- src/esphome/sensor/ppd42x_sensor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 31db38ad..a63b461d 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -26,7 +26,7 @@ void Ppd42xSensorComponent::update() { uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); - ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_10_0, time_pm_02_5); + ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); if (time_pm_10_0 == 0) { ESP_LOGD(TAG, "'%s' - PM10.0 measurement timed out!", this->name_.c_str()); diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index dde43ea3..637cf510 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -48,7 +48,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { GPIOPin *pm_2_5_pin_; GPIOPin *pm_10_0_pin_; - uint32_t timeout_us_{11662}; + uint32_t timeout_us_{30000}; }; From abf3fc81f1f80b1b71bdef3ad8b36f83ea85e72d Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 12:14:33 +0200 Subject: [PATCH 17/76] pm_02_5_pin_ fix --- src/esphome/application.cpp | 24 ++++++++++++------------ src/esphome/application.h | 2 +- src/esphome/sensor/ppd42x_sensor.cpp | 4 ++-- src/esphome/sensor/ppd42x_sensor.h | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 6486cd74..38133307 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -432,7 +432,17 @@ sensor::UltrasonicSensorComponent *Application::make_ultrasonic_sensor(const std return ultrasonic; } #endif - +#ifdef USE_PPD42X_SENSOR +sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval) { + auto *ppd42x = this->register_component( + new Ppd42xSensorComponent(friendly_name, pm_10_0_pin.copy(), pm_02_5_pin.copy(), update_interval)); + this->register_sensor(ppd42x); + return ppd42x; +} +#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *Application::make_wifi_signal_sensor(const std::string &name, uint32_t update_interval) { auto *wifi = this->register_component(new WiFiSignalSensor(name, update_interval)); @@ -1105,17 +1115,7 @@ sensor::TotalDailyEnergy *Application::make_total_daily_energy_sensor(const std: return total; } #endif -#ifdef USE_PPD42X_SENSOR -sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm2_5_pin, - uint32_t update_interval) { - auto *ppd42x = this->register_component( - new Ppd42xSensorComponent(friendly_name, pm10_0_pin.copy(), pm2_5_pin.copy(), update_interval)); - this->register_sensor(ppd42x); - return ppd42x; -} -#endif + void Application::set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; } void Application::register_component_(Component *comp) { diff --git a/src/esphome/application.h b/src/esphome/application.h index 90c23256..411479e3 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -592,7 +592,7 @@ class Application { */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_2_5_pin, + const GPIOInputPin &pm_02_5_pin, uint32_t update_interval = 60000); #endif diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index a63b461d..9f1bf2dd 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -13,9 +13,9 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; -Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, GPIOPin *pm_10_0_pin, +Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_02_5_pin_(pm_02_5_pin), pm_10_0_pin_(pm_10_0_pin) {} + : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->pm_02_5_pin_->setup(); diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 637cf510..64fcaeb9 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -46,7 +46,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { static float us_to_pm(uint32_t l_us, uint32_t t_us); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. - GPIOPin *pm_2_5_pin_; + GPIOPin *pm_02_5_pin_; GPIOPin *pm_10_0_pin_; uint32_t timeout_us_{30000}; From 5c81c0ec0e8128f7eea7fccfb8b943229a6a24fe Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 13:32:22 +0200 Subject: [PATCH 18/76] pm_02_5_pin_ fix 2 --- src/esphome/sensor/ppd42x_sensor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 64fcaeb9..76acd452 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -20,7 +20,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. * @param update_interval The interval in ms the sensor should check for new values. */ - Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_2_5_pin, GPIOPin *pm_10_0_pin, + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t l_us, uint32_t t_us); + static float us_to_pm(uint32_t sampleLength, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_02_5_pin_; From c4de10157495d6991c28ed67b555d4059167c74d Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 13:47:30 +0200 Subject: [PATCH 19/76] uper case fix --- src/esphome/sensor/ppd42x_sensor.cpp | 4 ++-- src/esphome/sensor/ppd42x_sensor.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 9f1bf2dd..82ca2868 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -54,8 +54,8 @@ void Ppd42xSensorComponent::dump_config() { ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); LOG_UPDATE_INTERVAL(this); } -float Ppd42xSensorComponent::us_to_pm(uint32_t sampleLength, uint32_t time_pm) { - float ratio = time_pm/(sampleLength*10.0); +float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { + float ratio = time_pm/(sample_length*10.0); return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; } float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 76acd452..dbc323b8 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -43,7 +43,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. - static float us_to_pm(uint32_t sampleLength, uint32_t time_pm); + static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. GPIOPin *pm_02_5_pin_; From f9fbfc33ea28fd19ba09529f2b87601df9fbe7c7 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:24:05 +0200 Subject: [PATCH 20/76] ident fix --- src/esphome/application.cpp | 6 +++--- src/esphome/application.h | 8 ++++---- src/esphome/sensor/ppd42x_sensor.cpp | 14 ++++++++------ src/esphome/sensor/ppd42x_sensor.h | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 38133307..96b4b914 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -434,9 +434,9 @@ sensor::UltrasonicSensorComponent *Application::make_ultrasonic_sensor(const std #endif #ifdef USE_PPD42X_SENSOR sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval) { + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval) { auto *ppd42x = this->register_component( new Ppd42xSensorComponent(friendly_name, pm_10_0_pin.copy(), pm_02_5_pin.copy(), update_interval)); this->register_sensor(ppd42x); diff --git a/src/esphome/application.h b/src/esphome/application.h index 411479e3..a8e08ef1 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -583,7 +583,7 @@ class Application { * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP - * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory to be there). + * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). * * @param friendly_name The friendly name for this sensor advertised to Home Assistant. * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. @@ -591,9 +591,9 @@ class Application { * @param update_interval The time in ms between updates, defaults to 60 seconds. */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval = 60000); + const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, + uint32_t update_interval = 60000); #endif #ifdef USE_WIFI_SIGNAL_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 82ca2868..085ee2e8 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -14,7 +14,7 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval) + uint32_t update_interval) : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); @@ -23,8 +23,10 @@ void Ppd42xSensorComponent::setup() { } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_10_0 = + pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); + uint32_t time_pm_02_5 = + pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); @@ -55,9 +57,9 @@ void Ppd42xSensorComponent::dump_config() { LOG_UPDATE_INTERVAL(this); } float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { - float ratio = time_pm/(sample_length*10.0); - return 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; -} + float ratio = time_pm / (sample_length * 10.0); + return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;} + float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index dbc323b8..fe35103f 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -21,7 +21,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param update_interval The interval in ms the sensor should check for new values. */ Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval = 60000); + uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. void set_timeout_us(uint32_t timeout_us); From 823f72b163d7599be9692f60796fc0246fdcca35 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:44:07 +0200 Subject: [PATCH 21/76] spaces fix --- src/esphome/application.h | 6 ++---- src/esphome/sensor/ppd42x_sensor.cpp | 10 ++++++---- src/esphome/sensor/ppd42x_sensor.h | 5 ++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index a8e08ef1..aaead71a 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -590,10 +590,8 @@ class Application { * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. * @param update_interval The time in ms between updates, defaults to 60 seconds. */ - sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval = 60000); + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, + const GPIOInputPin &pm_02_5_pin, uint32_t update_interval = 60000); #endif #ifdef USE_WIFI_SIGNAL_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index 085ee2e8..d54975ba 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -15,7 +15,7 @@ static const char *TAG = "sensor.ppd42x"; Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} + : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->pm_02_5_pin_->setup(); @@ -23,9 +23,9 @@ void Ppd42xSensorComponent::setup() { } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = + uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - uint32_t time_pm_02_5 = + uint32_t time_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); @@ -62,7 +62,9 @@ float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } -std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; +} + int8_t Ppd42xSensorComponent::accuracy_decimals() { return 2; // cm precision } diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index fe35103f..7d86781e 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -20,7 +20,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. * @param update_interval The interval in ms the sensor should check for new values. */ - Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, + Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, uint32_t update_interval = 60000); /// Set the timeout for waiting for the pm_10_0 in µs. @@ -48,8 +48,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { GPIOPin *pm_02_5_pin_; GPIOPin *pm_10_0_pin_; - uint32_t timeout_us_{30000}; - + uint32_t timeout_us_{30000}; }; } // namespace sensor From 1cb9e495b726a02e7350505501ef83135f98ba11 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 14:56:43 +0200 Subject: [PATCH 22/76] spaces fix 2 --- src/esphome/sensor/ppd42x_sensor.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index d54975ba..ac383e47 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -58,12 +58,12 @@ void Ppd42xSensorComponent::dump_config() { } float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { float ratio = time_pm / (sample_length * 10.0); - return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;} + return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; +} float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } -std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; -} +std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } int8_t Ppd42xSensorComponent::accuracy_decimals() { return 2; // cm precision From 40f1fa89218a8f55292bb725acced399b2af5955 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 15:11:19 +0200 Subject: [PATCH 23/76] spaces fix 3 --- src/esphome/sensor/ppd42x_sensor.cpp | 1 - src/esphome/sensor/ppd42x_sensor.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index ac383e47..ccfe8756 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -22,7 +22,6 @@ void Ppd42xSensorComponent::setup() { this->pm_10_0_pin_->setup(); } void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); uint32_t time_pm_02_5 = diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 7d86781e..930bf52b 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -39,7 +39,6 @@ class Ppd42xSensorComponent : public PollingSensorComponent { int8_t accuracy_decimals() override; float get_setup_priority() const override; - protected: /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. From cfefff8b0dee5a064a922a103529d9a9768230e0 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 15:25:39 +0200 Subject: [PATCH 24/76] spaces fix 4 --- src/esphome/application.h | 6 +++--- src/esphome/sensor/ppd42x_sensor.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index aaead71a..937e8ad4 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -581,13 +581,13 @@ class Application { * * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement - * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 /M3 will be sent to) and the PM2_5 pin - * (where particule > 2.5 /M3 will be sent to). Note that in order to not block indefinitely if we don't receive UP + * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 µg/m³ will be sent to) and the PM2_5 pin + * (where particule > 2.5 µg/m³ will be sent to). Note that in order to not block indefinitely if we don't receive UP * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). * * @param friendly_name The friendly name for this sensor advertised to Home Assistant. * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. - * @param pm_2_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. + * @param pm_02_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. * @param update_interval The time in ms between updates, defaults to 60 seconds. */ sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 930bf52b..2be3fb89 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -39,9 +39,9 @@ class Ppd42xSensorComponent : public PollingSensorComponent { int8_t accuracy_decimals() override; float get_setup_priority() const override; - + protected: - /// Helper function to convert the specified pm_10_0 duration in µg/m³ to meters. + /// Helper function to convert the specified pm_xx_x duration in µs to µg/m³. static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. From 09196ff842507c76716917f1d3fc5576a85664ea Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 31 Mar 2019 19:04:38 +0200 Subject: [PATCH 25/76] ppd42x txt 5 --- src/esphome/application.h | 8 ++++---- src/esphome/sensor/ppd42x_sensor.cpp | 6 +++--- src/esphome/sensor/ppd42x_sensor.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index 937e8ad4..d82a576a 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -579,10 +579,10 @@ class Application { #ifdef USE_PPD42X_SENSOR /** Create an PPD42x particle sensor. * - * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one - * pin. The time between the UP and DOWN is then (with some maths) converted to a measurement - * in microg/m3. You need to specify the PM10_0 pin (where particule > 10.0 µg/m³ will be sent to) and the PM2_5 pin - * (where particule > 2.5 µg/m³ will be sent to). Note that in order to not block indefinitely if we don't receive UP + * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one + * pin or two pins (P1 and P2). The time between the UP and DOWN is then (with some maths) converted to a measurement + * in pcs/L. You need to specify the PM10_0 pin (where particule > 10.0 pcs/L will be sent to) and the PM2_5 pin + * (where particule > 2.5 pcs/L will be sent to). Note that in order to not block indefinitely if we don't receive UP * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). * * @param friendly_name The friendly name for this sensor advertised to Home Assistant. diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index ccfe8756..e65e9f55 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -35,7 +35,7 @@ void Ppd42xSensorComponent::update() { } else { float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_10_0); this->publish_state(result); - ESP_LOGD(TAG, "'%s' - Got PM10.0 Concentration: %.1f µg/m³", this->name_.c_str(), result); + ESP_LOGD(TAG, "'%s' - Got PM10.0 Concentration: %.1f pcs/L", this->name_.c_str(), result); this->publish_state(result); } if (time_pm_02_5 == 0) { @@ -44,7 +44,7 @@ void Ppd42xSensorComponent::update() { } else { float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_02_5); this->publish_state(result); - ESP_LOGD(TAG, "'%s' - Got PM 2.5 Concentration: %.1f µg/m³", this->name_.c_str(), result); + ESP_LOGD(TAG, "'%s' - Got PM 2.5 Concentration: %.1f pcs/L", this->name_.c_str(), result); this->publish_state(result); } } @@ -61,7 +61,7 @@ float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) } float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } -std::string Ppd42xSensorComponent::unit_of_measurement() { return "µg/m³"; } +std::string Ppd42xSensorComponent::unit_of_measurement() { return "pcs/L"; } std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } int8_t Ppd42xSensorComponent::accuracy_decimals() { diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 2be3fb89..d4c1fa9a 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -21,7 +21,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { * @param update_interval The interval in ms the sensor should check for new values. */ Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval = 60000); + uint32_t update_interval = 5000); /// Set the timeout for waiting for the pm_10_0 in µs. void set_timeout_us(uint32_t timeout_us); @@ -41,7 +41,7 @@ class Ppd42xSensorComponent : public PollingSensorComponent { float get_setup_priority() const override; protected: - /// Helper function to convert the specified pm_xx_x duration in µs to µg/m³. + /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. From a88d08af0e3229200f51e6eec9622534c006b171 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Fri, 5 Apr 2019 23:10:01 +0200 Subject: [PATCH 26/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x_sensor.cpp | 135 +++++++++++++++++--------- src/esphome/sensor/ppd42x_sensor.h | 69 +++++++------ src/esphome/sensor/ppd42xy_sensor.cpp | 28 ++++++ src/esphome/sensor/ppd42xy_sensor.h | 61 ++++++++++++ 4 files changed, 220 insertions(+), 73 deletions(-) create mode 100644 src/esphome/sensor/ppd42xy_sensor.cpp create mode 100644 src/esphome/sensor/ppd42xy_sensor.h diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x_sensor.cpp index e65e9f55..8fab6767 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x_sensor.cpp @@ -1,76 +1,123 @@ #include "esphome/defines.h" -#ifdef USE_PPD42X_SENSOR - -#include "esphome/sensor/ppd42x_sensor.h" +#ifdef USE_PPD42X +#include "esphome/sensor/ppd42x.h" #include "esphome/log.h" -#include "esphome/helpers.h" ESPHOME_NAMESPACE_BEGIN namespace sensor { static const char *TAG = "sensor.ppd42x"; - -Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} void Ppd42xSensorComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); + this->starttime_ = millis(); this->pm_02_5_pin_->setup(); this->pm_10_0_pin_->setup(); } -void Ppd42xSensorComponent::update() { - uint32_t time_pm_10_0 = - pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - uint32_t time_pm_02_5 = + +void PPD42XComponent::loop() { + const uint32_t now = millis(); + uint32_t duration_pm_02_5 = pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); + uint32_t duration_pm_10_0 = + pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); + this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pm_02_5; + this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pm_10_0; + + if (now - this->starttime_ > this->timeout_us_) { + // last transmission too long ago + this->starttime_ = now; + parse_data_(); - ESP_LOGV(TAG, "PM10.0 took %uµs and PM 2.5 %uµs ", time_pm_10_0, time_pm_02_5); + } +} +float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } - if (time_pm_10_0 == 0) { - ESP_LOGD(TAG, "'%s' - PM10.0 measurement timed out!", this->name_.c_str()); - this->publish_state(NAN); - } else { - float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_10_0); - this->publish_state(result); - ESP_LOGD(TAG, "'%s' - Got PM10.0 Concentration: %.1f pcs/L", this->name_.c_str(), result); - this->publish_state(result); - } - if (time_pm_02_5 == 0) { - ESP_LOGD(TAG, "'%s' - PM 2.5 measurement timed out!", this->name_.c_str()); - this->publish_state(NAN); - } else { - float result = Ppd42xSensorComponent::us_to_pm(this->timeout_us_, time_pm_02_5); - this->publish_state(result); - ESP_LOGD(TAG, "'%s' - Got PM 2.5 Concentration: %.1f pcs/L", this->name_.c_str(), result); - this->publish_state(result); + +void PPD42XComponent::parse_data_() { + switch (this->type_) { + case PPD42X_TYPE___: { + uint16_t pm_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; + uint16_t pm_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; + ESP_LOGD(TAG, + "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", + pm_02_5_concentration, pm_10_0_concentration); + if (this->pm_02_5_sensor_ != nullptr) + this->pm_02_5_sensor_->publish_state(pm_02_5_concentration); + if (this->pm_10_0_sensor_ != nullptr) + this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + break; + } + case PPD42X_TYPE_NS: { + uint16_t pm_02_5_concentration = 0; + uint16_t pm_10_0_concentration = 0; + ESP_LOGD(TAG, + "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", + pm_02_5_concentration, pm_10_0_concentration); + if (this->pm_02_5_sensor_ != nullptr) + this->pm_02_5_sensor_->publish_state(pm_02_5_concentration); + if (this->pm_10_0_sensor_ != nullptr) + this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + break; + } } + + this->status_clear_warning(); } -void Ppd42xSensorComponent::dump_config() { - LOG_SENSOR("", "PPD42X Sensor", this); - LOG_PIN(" PM10.0 Pin: ", this->pm_10_0_pin_); - LOG_PIN(" PM 2.5 Pin: ", this->pm_02_5_pin_); + + +PPD42XSensor *PPD42XComponent::make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin) { + return this->pm_02_5_sensor_ = new PPD42XSensor(name, pm_pin, PPD42X_SENSOR_TYPE_PM_02_5); +} +PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin) { + return this->pm_10_0_sensor_ = new PPD42XSensor(name, pm_pin, PPD42X_SENSOR_TYPE_PM_10_0); +} +PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} +void PPD42XComponent::dump_config() { + ESP_LOGCONFIG(TAG, "PPD42X:"); + LOG_SENSOR(" ", "PM02.5", this->pm_02_5_sensor_); + LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); LOG_UPDATE_INTERVAL(this); } -float Ppd42xSensorComponent::us_to_pm(uint32_t sample_length, uint32_t time_pm) { - float ratio = time_pm / (sample_length * 10.0); - return 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; +float Ppd42xSensorComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { + float ratio = time_pm / (sample_length * 10.0f); + return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; } +std::string PPD42XSensor::unit_of_measurement() { + switch (this->type_) { + case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_10_0: + return "pcs/L"; + } + return ""; +} +std::string PPD42XSensor::icon() { + switch (this->type_) { + case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_10_0: + // Not the ideal icon, but Otto can't find a better one ;) + return ICON_CHEMICAL_WEAPON; + } + return ""; +} +int8_t PPD42XSensor::accuracy_decimals() { + switch (this->type_) { + case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_10_0: + return 0; + } -float Ppd42xSensorComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } -std::string Ppd42xSensorComponent::unit_of_measurement() { return "pcs/L"; } -std::string Ppd42xSensorComponent::icon() { return "mdi:arrow-expand-vertical"; } - -int8_t Ppd42xSensorComponent::accuracy_decimals() { - return 2; // cm precision + return 0; } void Ppd42xSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin *pm_pin, PPD42XSensorType type) : Sensor(name), GPIOPin(*pm_pin), type_(type) {} + } // namespace sensor ESPHOME_NAMESPACE_END -#endif // USE_PPD42X_SENSOR +#endif // USE_PPD42X diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index d4c1fa9a..61db186e 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -1,52 +1,63 @@ -#ifndef ESPHOME_SENSOR_PPD42X_SENSOR_H -#define ESPHOME_SENSOR_PPD42X_SENSOR_H +#ifndef ESPHOME_SENSOR_PPD42X_H +#define ESPHOME_SENSOR_PPD42X_H #include "esphome/defines.h" -#ifdef USE_PPD42X_SENSOR +#ifdef USE_PPD42X +#include "esphome/component.h" #include "esphome/sensor/sensor.h" -#include "esphome/esphal.h" +#include "esphome/uart_component.h" +#include "esphome/helpers.h" ESPHOME_NAMESPACE_BEGIN namespace sensor { -class Ppd42xSensorComponent : public PollingSensorComponent { - public: - /** Construct the PPD42X sensor with the specified 2.5ppm pin and 10.0ppm pin. - * - * @param pm_2_5_pin The pm_2_5 pin where pulses are sent to. - * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. - * @param update_interval The interval in ms the sensor should check for new values. - */ - Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval = 5000); - - /// Set the timeout for waiting for the pm_10_0 in µs. - void set_timeout_us(uint32_t timeout_us); - - // ========== INTERNAL METHODS ========== - // (In most use cases you won't need these) - /// Set up pins and register interval. - void setup() override; - void dump_config() override; +enum PPD42XType { + PPD42X_TYPE___ = 0, + PPD42X_TYPE_NS, +}; + +enum PPD42XSensorType { + /// PM2.5 concentration in pcs/L, PPD42, PPD42NS + PPD42X_SENSOR_TYPE_PM_02_5, + /// PM10.0 concentration in pcs/L, PPD42, PPD42NS + PPD42X_SENSOR_TYPE_PM_10_0, - void update() override; +}; + +class PPD42XSensor : public sensor::Sensor { + public: + PPD42XSensor(const std::string &name, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; int8_t accuracy_decimals() override; + protected: + const PPD42XSensorType type_; +}; + +class PPD42XComponent : public Component { + public: + PPD42XComponent(PPD42XType type); + + void loop() override; float get_setup_priority() const override; + void dump_config() override; + + PPD42XSensor *make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin); + PPD42XSensor *make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin); protected: + void parse_data_(); + const PPD42XType type_; + PPD42XSensor *pm_02_5_sensor_{nullptr}; + PPD42XSensor *pm_10_0_sensor_{nullptr}; /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. - - GPIOPin *pm_02_5_pin_; - GPIOPin *pm_10_0_pin_; uint32_t timeout_us_{30000}; }; @@ -54,6 +65,6 @@ class Ppd42xSensorComponent : public PollingSensorComponent { ESPHOME_NAMESPACE_END -#endif // USE_PPD42X_SENSOR +#endif // USE_PPD42X -#endif // ESPHOME_SENSOR_PPD42X_SENSOR_H +#endif // ESPHOME_SENSOR_PPD42X_H diff --git a/src/esphome/sensor/ppd42xy_sensor.cpp b/src/esphome/sensor/ppd42xy_sensor.cpp new file mode 100644 index 00000000..089cca68 --- /dev/null +++ b/src/esphome/sensor/ppd42xy_sensor.cpp @@ -0,0 +1,28 @@ +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/ppd42x_sensor.h" + +#include "esphome/log.h" +#include "esphome/helpers.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +static const char *TAG = "sensor.ppd42x"; + +Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, + uint32_t update_interval) + : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} + + + + +i +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR diff --git a/src/esphome/sensor/ppd42xy_sensor.h b/src/esphome/sensor/ppd42xy_sensor.h new file mode 100644 index 00000000..257f619f --- /dev/null +++ b/src/esphome/sensor/ppd42xy_sensor.h @@ -0,0 +1,61 @@ +#ifndef ESPHOME_SENSOR_PPD42X_SENSOR_H +#define ESPHOME_SENSOR_PPD42X_SENSOR_H + +#include "esphome/defines.h" + +#ifdef USE_PPD42X_SENSOR + +#include "esphome/sensor/sensor.h" +#include "esphome/esphal.h" + +ESPHOME_NAMESPACE_BEGIN + +namespace sensor { + +class Ppd42xSensorComponent : public PollingSensorComponent { + public: + /** Construct the PPD42X sensor with the specified 2.5ppm pin and 10.0ppm pin. + * + * @param pm_2_5_pin The pm_2_5 pin where the pm_2_5 is listened for. + * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. + * @param update_interval The interval in ms the sensor should check for new values. + */ + Ppd42x10SensorComponent(const std::string &name, GPIOPin *pm_10_0_pin + uint32_t update_interval = 5000); + Ppd42x02SensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, + uint32_t update_interval = 5000); + + /// Set the timeout for waiting for the PIN in µs. + void set_timeout_us(uint32_t timeout_us); + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + /// Set up pins and register interval. + void setup() override; + void dump_config() override; + + void update() override; + + std::string unit_of_measurement() override; + std::string icon() override; + int8_t accuracy_decimals() override; + + float get_setup_priority() const override; + + protected: + /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. + static float us_to_pm(uint32_t sample_length, uint32_t time_pm); + /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. + + GPIOPin *pm_02_5_pin_; + GPIOPin *pm_10_0_pin_; + uint32_t timeout_us_{30000}; +}; + +} // namespace sensor + +ESPHOME_NAMESPACE_END + +#endif // USE_PPD42X_SENSOR + +#endif // ESPHOME_SENSOR_PPD42X_SENSOR_H From a35e6548c734e2521eeff97cbe8d1b4e45402c81 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 00:08:54 +0200 Subject: [PATCH 27/76] Refactore ppd42x.py --- src/esphome/application.cpp | 21 +++++---- src/esphome/application.h | 4 +- src/esphome/sensor/ppd42x_sensor.h | 3 ++ src/esphome/sensor/ppd42xy_sensor.cpp | 28 ------------ src/esphome/sensor/ppd42xy_sensor.h | 61 --------------------------- 5 files changed, 15 insertions(+), 102 deletions(-) delete mode 100644 src/esphome/sensor/ppd42xy_sensor.cpp delete mode 100644 src/esphome/sensor/ppd42xy_sensor.h diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 96b4b914..15de9539 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -432,17 +432,6 @@ sensor::UltrasonicSensorComponent *Application::make_ultrasonic_sensor(const std return ultrasonic; } #endif -#ifdef USE_PPD42X_SENSOR -sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, - uint32_t update_interval) { - auto *ppd42x = this->register_component( - new Ppd42xSensorComponent(friendly_name, pm_10_0_pin.copy(), pm_02_5_pin.copy(), update_interval)); - this->register_sensor(ppd42x); - return ppd42x; -} -#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *Application::make_wifi_signal_sensor(const std::string &name, uint32_t update_interval) { auto *wifi = this->register_component(new WiFiSignalSensor(name, update_interval)); @@ -1099,6 +1088,16 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso return this->register_component(new PMSX003Component(parent, type)); } #endif +#ifdef USE_PPD42X_SENSOR +sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, + sensor::PPD42XType type, + uint32_t update_interval) { + auto *ppd42x = this->register_component( + new Ppd42xSensorComponent(friendly_name, type, update_interval)); + this->register_sensor(ppd42x); + return ppd42x; +} +#endif #ifdef USE_A4988 stepper::A4988 *Application::make_a4988(const GPIOOutputPin &step_pin, const GPIOOutputPin &dir_pin) { diff --git a/src/esphome/application.h b/src/esphome/application.h index d82a576a..6e86cd92 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -590,8 +590,8 @@ class Application { * @param pm_02_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. * @param update_interval The time in ms between updates, defaults to 60 seconds. */ - sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, const GPIOInputPin &pm_10_0_pin, - const GPIOInputPin &pm_02_5_pin, uint32_t update_interval = 60000); + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, sensor::PPD42XType type + uint32_t update_interval = 60000); #endif #ifdef USE_WIFI_SIGNAL_SENSOR diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x_sensor.h index 61db186e..7e53b3e1 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x_sensor.h @@ -59,6 +59,9 @@ class PPD42XComponent : public Component { static float us_to_pm(uint32_t sample_length, uint32_t time_pm); /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. uint32_t timeout_us_{30000}; + uint32_t starttime_ {0}; + uint32_t lowpulseoccupancy_02_5_ {0}; + uint32_t lowpulseoccupancy_10_0_ {0}; }; } // namespace sensor diff --git a/src/esphome/sensor/ppd42xy_sensor.cpp b/src/esphome/sensor/ppd42xy_sensor.cpp deleted file mode 100644 index 089cca68..00000000 --- a/src/esphome/sensor/ppd42xy_sensor.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "esphome/defines.h" - -#ifdef USE_PPD42X_SENSOR - -#include "esphome/sensor/ppd42x_sensor.h" - -#include "esphome/log.h" -#include "esphome/helpers.h" - -ESPHOME_NAMESPACE_BEGIN - -namespace sensor { - -static const char *TAG = "sensor.ppd42x"; - -Ppd42xSensorComponent::Ppd42xSensorComponent(const std::string &name, GPIOPin *pm_10_0_pin, GPIOPin *pm_02_5_pin, - uint32_t update_interval) - : PollingSensorComponent(name, update_interval), pm_10_0_pin_(pm_10_0_pin), pm_02_5_pin_(pm_02_5_pin) {} - - - - -i -} // namespace sensor - -ESPHOME_NAMESPACE_END - -#endif // USE_PPD42X_SENSOR diff --git a/src/esphome/sensor/ppd42xy_sensor.h b/src/esphome/sensor/ppd42xy_sensor.h deleted file mode 100644 index 257f619f..00000000 --- a/src/esphome/sensor/ppd42xy_sensor.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef ESPHOME_SENSOR_PPD42X_SENSOR_H -#define ESPHOME_SENSOR_PPD42X_SENSOR_H - -#include "esphome/defines.h" - -#ifdef USE_PPD42X_SENSOR - -#include "esphome/sensor/sensor.h" -#include "esphome/esphal.h" - -ESPHOME_NAMESPACE_BEGIN - -namespace sensor { - -class Ppd42xSensorComponent : public PollingSensorComponent { - public: - /** Construct the PPD42X sensor with the specified 2.5ppm pin and 10.0ppm pin. - * - * @param pm_2_5_pin The pm_2_5 pin where the pm_2_5 is listened for. - * @param pm_10_0_pin The pm_10_0 pin where the pm_10_0 is listened for. - * @param update_interval The interval in ms the sensor should check for new values. - */ - Ppd42x10SensorComponent(const std::string &name, GPIOPin *pm_10_0_pin - uint32_t update_interval = 5000); - Ppd42x02SensorComponent(const std::string &name, GPIOPin *pm_02_5_pin, - uint32_t update_interval = 5000); - - /// Set the timeout for waiting for the PIN in µs. - void set_timeout_us(uint32_t timeout_us); - - // ========== INTERNAL METHODS ========== - // (In most use cases you won't need these) - /// Set up pins and register interval. - void setup() override; - void dump_config() override; - - void update() override; - - std::string unit_of_measurement() override; - std::string icon() override; - int8_t accuracy_decimals() override; - - float get_setup_priority() const override; - - protected: - /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. - static float us_to_pm(uint32_t sample_length, uint32_t time_pm); - /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. - - GPIOPin *pm_02_5_pin_; - GPIOPin *pm_10_0_pin_; - uint32_t timeout_us_{30000}; -}; - -} // namespace sensor - -ESPHOME_NAMESPACE_END - -#endif // USE_PPD42X_SENSOR - -#endif // ESPHOME_SENSOR_PPD42X_SENSOR_H From 879c8a0c9470885540738a6451288a20574a2fb9 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 00:28:14 +0200 Subject: [PATCH 28/76] Refactore ppd42x.py --- src/esphome/application.cpp | 8 ++------ src/esphome/application.h | 31 ++++++++++++++----------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 15de9539..7fdba622 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1090,12 +1090,8 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #endif #ifdef USE_PPD42X_SENSOR sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - sensor::PPD42XType type, - uint32_t update_interval) { - auto *ppd42x = this->register_component( - new Ppd42xSensorComponent(friendly_name, type, update_interval)); - this->register_sensor(ppd42x); - return ppd42x; + sensor::PPD42XType type) { + return this->register_sensor(new Ppd42xSensorComponent(friendly_name, type)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index 6e86cd92..d0b91e25 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -576,23 +576,6 @@ class Application { const GPIOInputPin &echo_pin, uint32_t update_interval = 60000); #endif -#ifdef USE_PPD42X_SENSOR - /** Create an PPD42x particle sensor. - * - * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one - * pin or two pins (P1 and P2). The time between the UP and DOWN is then (with some maths) converted to a measurement - * in pcs/L. You need to specify the PM10_0 pin (where particule > 10.0 pcs/L will be sent to) and the PM2_5 pin - * (where particule > 2.5 pcs/L will be sent to). Note that in order to not block indefinitely if we don't receive UP - * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). - * - * @param friendly_name The friendly name for this sensor advertised to Home Assistant. - * @param pm_10_0_pin The pin the short pulse will be sent to, can be integer or GPIOOutputPin. - * @param pm_02_5_pin The pin we wait that we wait on for the echo, can be integer or GPIOInputPin. - * @param update_interval The time in ms between updates, defaults to 60 seconds. - */ - sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, sensor::PPD42XType type - uint32_t update_interval = 60000); -#endif #ifdef USE_WIFI_SIGNAL_SENSOR sensor::WiFiSignalSensor *make_wifi_signal_sensor(const std::string &name, uint32_t update_interval = 60000); @@ -828,6 +811,20 @@ class Application { #ifdef USE_PMSX003 sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif +#ifdef USE_PPD42X_SENSOR + /** Create an PPD42x particle sensor. + * + * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one + * pin or two pins (P1 and P2). The time between the UP and DOWN is then (with some maths) converted to a measurement + * in pcs/L. You need to specify the PM10_0 pin (where particule > 10.0 pcs/L will be sent to) and the PM2_5 pin + * (where particule > 2.5 pcs/L will be sent to). Note that in order to not block indefinitely if we don't receive UP + * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). + * + * @param friendly_name The friendly name for this sensor advertised to Home Assistant. + * @param update_interval The time in ms between updates, defaults to 60 seconds. + */ + sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, sensor::PPD42XType type); +#endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR sensor::TotalDailyEnergy *make_total_daily_energy_sensor(const std::string &name, time::RealTimeClockComponent *time, From 8aa1b78cfeac71417a6068fe33b692f14af758ee Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 11:05:47 +0200 Subject: [PATCH 29/76] Refactore ppd42x.py --- src/esphome/application.cpp | 7 +++---- src/esphome/application.h | 17 +++-------------- src/esphome/defines.h | 2 +- .../sensor/{ppd42x_sensor.cpp => ppd42x.cpp} | 15 +++++++-------- .../sensor/{ppd42x_sensor.h => ppd42x.h} | 19 ++++++++++++++----- 5 files changed, 28 insertions(+), 32 deletions(-) rename src/esphome/sensor/{ppd42x_sensor.cpp => ppd42x.cpp} (84%) rename src/esphome/sensor/{ppd42x_sensor.h => ppd42x.h} (81%) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 7fdba622..d4ab5c13 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1088,10 +1088,9 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso return this->register_component(new PMSX003Component(parent, type)); } #endif -#ifdef USE_PPD42X_SENSOR -sensor::Ppd42xSensorComponent *Application::make_ppd42x_sensor(const std::string &friendly_name, - sensor::PPD42XType type) { - return this->register_sensor(new Ppd42xSensorComponent(friendly_name, type)); +#ifdef USE_PPD42X +sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type) { + return this->register_component(new PPD42XComponent(type)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index d0b91e25..62348f11 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -116,6 +116,7 @@ #include "esphome/sensor/mqtt_subscribe_sensor.h" #include "esphome/sensor/ms5611.h" #include "esphome/sensor/pmsx003.h" +#include "esphome/sensor/ppd42x.h" #include "esphome/sensor/pulse_counter.h" #include "esphome/sensor/rotary_encoder.h" #include "esphome/sensor/sensor.h" @@ -125,7 +126,6 @@ #include "esphome/sensor/total_daily_energy.h" #include "esphome/sensor/tsl2561_sensor.h" #include "esphome/sensor/ultrasonic_sensor.h" -#include "esphome/sensor/ppd42x_sensor.h" #include "esphome/sensor/uptime_sensor.h" #include "esphome/sensor/wifi_signal_sensor.h" #include "esphome/sensor/sds011_component.h" @@ -811,19 +811,8 @@ class Application { #ifdef USE_PMSX003 sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif -#ifdef USE_PPD42X_SENSOR - /** Create an PPD42x particle sensor. - * - * This can for example be an PPD42 particle sensor. It listens during a short periode for impulses from one - * pin or two pins (P1 and P2). The time between the UP and DOWN is then (with some maths) converted to a measurement - * in pcs/L. You need to specify the PM10_0 pin (where particule > 10.0 pcs/L will be sent to) and the PM2_5 pin - * (where particule > 2.5 pcs/L will be sent to). Note that in order to not block indefinitely if we don't receive UP - * , this class has a default timeout of around 30s. You can change that using the configuration file ( mandatory). - * - * @param friendly_name The friendly name for this sensor advertised to Home Assistant. - * @param update_interval The time in ms between updates, defaults to 60 seconds. - */ - sensor::Ppd42xSensorComponent *make_ppd42x_sensor(const std::string &friendly_name, sensor::PPD42XType type); +#ifdef USE_PPD42X + sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 667758a1..994b64d3 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -38,7 +38,7 @@ #define USE_HTU21D_SENSOR #define USE_HDC1080_SENSOR #define USE_ULTRASONIC_SENSOR -#define USE_PPD42X_SENSOR +#define USE_PPD42X #define USE_WIFI_SIGNAL_SENSOR #define USE_OUTPUT #ifdef ARDUINO_ARCH_ESP32 diff --git a/src/esphome/sensor/ppd42x_sensor.cpp b/src/esphome/sensor/ppd42x.cpp similarity index 84% rename from src/esphome/sensor/ppd42x_sensor.cpp rename to src/esphome/sensor/ppd42x.cpp index 8fab6767..cfa77bf3 100644 --- a/src/esphome/sensor/ppd42x_sensor.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -10,7 +10,7 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { static const char *TAG = "sensor.ppd42x"; -void Ppd42xSensorComponent::setup() { +void PPD42XComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->starttime_ = millis(); this->pm_02_5_pin_->setup(); @@ -69,20 +69,19 @@ void PPD42XComponent::parse_data_() { PPD42XSensor *PPD42XComponent::make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin) { - return this->pm_02_5_sensor_ = new PPD42XSensor(name, pm_pin, PPD42X_SENSOR_TYPE_PM_02_5); + return this->pm_02_5_sensor_ = new PPD42XSensor(name, *pm_pin, PPD42X_SENSOR_TYPE_PM_02_5); } PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin) { - return this->pm_10_0_sensor_ = new PPD42XSensor(name, pm_pin, PPD42X_SENSOR_TYPE_PM_10_0); + return this->pm_10_0_sensor_ = new PPD42XSensor(name, *pm_pin, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} +PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM02.5", this->pm_02_5_sensor_); LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); - LOG_UPDATE_INTERVAL(this); } -float Ppd42xSensorComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { +float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { float ratio = time_pm / (sample_length * 10.0f); return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; } @@ -112,9 +111,9 @@ int8_t PPD42XSensor::accuracy_decimals() { return 0; } -void Ppd42xSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } +void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin *pm_pin, PPD42XSensorType type) : Sensor(name), GPIOPin(*pm_pin), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pm_pin, PPD42XSensorType type) : Sensor(name), pm_pin_(pm_pin), type_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x_sensor.h b/src/esphome/sensor/ppd42x.h similarity index 81% rename from src/esphome/sensor/ppd42x_sensor.h rename to src/esphome/sensor/ppd42x.h index 7e53b3e1..b266073f 100644 --- a/src/esphome/sensor/ppd42x_sensor.h +++ b/src/esphome/sensor/ppd42x.h @@ -29,7 +29,7 @@ enum PPD42XSensorType { class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, PPD42XSensorType type); + PPD42XSensor(const std::string &name, GPIOPin pm_pin, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; @@ -37,6 +37,8 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType type_; + const GPIOPin pm_pin_; + const std::string name_; }; class PPD42XComponent : public Component { @@ -46,22 +48,29 @@ class PPD42XComponent : public Component { void loop() override; float get_setup_priority() const override; void dump_config() override; + void setup() override; + void get_update_interval(); PPD42XSensor *make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin); PPD42XSensor *make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin); protected: void parse_data_(); - const PPD42XType type_; - PPD42XSensor *pm_02_5_sensor_{nullptr}; - PPD42XSensor *pm_10_0_sensor_{nullptr}; /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. - static float us_to_pm(uint32_t sample_length, uint32_t time_pm); + static float us_to_pl(uint32_t sample_length, uint32_t time_pm); + void set_timeout_us(uint32_t timeout_us); + /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. uint32_t timeout_us_{30000}; uint32_t starttime_ {0}; uint32_t lowpulseoccupancy_02_5_ {0}; uint32_t lowpulseoccupancy_10_0_ {0}; + GPIOPin *pm_02_5_pin_; + GPIOPin *pm_10_0_pin_; + + const PPD42XType type_; + PPD42XSensor *pm_02_5_sensor_{nullptr}; + PPD42XSensor *pm_10_0_sensor_{nullptr}; }; } // namespace sensor From 5dde0689700a3b625bdbe5609b3e93bd924a0d6a Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 18:12:47 +0200 Subject: [PATCH 30/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 58 +++++++++++++++++------------------ src/esphome/sensor/ppd42x.h | 21 ++++++------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index cfa77bf3..1941ad15 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,18 +13,18 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); this->starttime_ = millis(); - this->pm_02_5_pin_->setup(); - this->pm_10_0_pin_->setup(); + this->pl_02_5_pin_->setup(); + this->pl_10_0_pin_->setup(); } void PPD42XComponent::loop() { const uint32_t now = millis(); - uint32_t duration_pm_02_5 = - pulseIn(this->pm_02_5_pin_->get_pin(), uint8_t(!this->pm_02_5_pin_->is_inverted()), this->timeout_us_); - uint32_t duration_pm_10_0 = - pulseIn(this->pm_10_0_pin_->get_pin(), uint8_t(!this->pm_10_0_pin_->is_inverted()), this->timeout_us_); - this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pm_02_5; - this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pm_10_0; + uint32_t duration_pl_02_5 = + pulseIn(this->pl_02_5_pin_->get_pin(), uint8_t(!this->pl_02_5_pin_->is_inverted()), this->timeout_us_); + uint32_t duration_pl_10_0 = + pulseIn(this->pl_10_0_pin_->get_pin(), uint8_t(!this->pl_10_0_pin_->is_inverted()), this->timeout_us_); + this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; + this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; if (now - this->starttime_ > this->timeout_us_) { // last transmission too long ago @@ -39,27 +39,27 @@ float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDW void PPD42XComponent::parse_data_() { switch (this->type_) { case PPD42X_TYPE___: { - uint16_t pm_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; - uint16_t pm_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; + uint16_t pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; + uint16_t pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", - pm_02_5_concentration, pm_10_0_concentration); - if (this->pm_02_5_sensor_ != nullptr) - this->pm_02_5_sensor_->publish_state(pm_02_5_concentration); - if (this->pm_10_0_sensor_ != nullptr) - this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + pl_02_5_concentration, pl_10_0_concentration); + if (this->pl_02_5_sensor_ != nullptr) + this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); + if (this->pl_10_0_sensor_ != nullptr) + this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } case PPD42X_TYPE_NS: { - uint16_t pm_02_5_concentration = 0; - uint16_t pm_10_0_concentration = 0; + uint16_t pl_02_5_concentration = 0; + uint16_t pl_10_0_concentration = 0; ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", - pm_02_5_concentration, pm_10_0_concentration); - if (this->pm_02_5_sensor_ != nullptr) - this->pm_02_5_sensor_->publish_state(pm_02_5_concentration); - if (this->pm_10_0_sensor_ != nullptr) - this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + pl_02_5_concentration, pl_10_0_concentration); + if (this->pl_02_5_sensor_ != nullptr) + this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); + if (this->pl_10_0_sensor_ != nullptr) + this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } } @@ -68,17 +68,17 @@ void PPD42XComponent::parse_data_() { } -PPD42XSensor *PPD42XComponent::make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin) { - return this->pm_02_5_sensor_ = new PPD42XSensor(name, *pm_pin, PPD42X_SENSOR_TYPE_PM_02_5); +PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin *pl_pin) { + return this->pl_02_5_sensor_ = new PPD42XSensor(name, *pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); } -PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin) { - return this->pm_10_0_sensor_ = new PPD42XSensor(name, *pm_pin, PPD42X_SENSOR_TYPE_PM_10_0); +PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin *pl_pin) { + return this->pl_10_0_sensor_ = new PPD42XSensor(name, *pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); } PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); - LOG_SENSOR(" ", "PM02.5", this->pm_02_5_sensor_); - LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); + LOG_SENSOR(" ", "PM02.5", this->pl_02_5_sensor_); + LOG_SENSOR(" ", "PM10.0", this->pl_10_0_sensor_); ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); } float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { @@ -113,7 +113,7 @@ int8_t PPD42XSensor::accuracy_decimals() { } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pm_pin, PPD42XSensorType type) : Sensor(name), pm_pin_(pm_pin), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin *pl_pin, PPD42XSensorType type) : Sensor(name), pl_pin_(pl_pin), type_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index b266073f..0a7356db 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -29,7 +29,7 @@ enum PPD42XSensorType { class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, GPIOPin pm_pin, PPD42XSensorType type); + PPD42XSensor(const std::string &name, GPIOPin *pl_pin, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; @@ -37,8 +37,7 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType type_; - const GPIOPin pm_pin_; - const std::string name_; + const GPIOPin pl_pin_; }; class PPD42XComponent : public Component { @@ -51,26 +50,26 @@ class PPD42XComponent : public Component { void setup() override; void get_update_interval(); - PPD42XSensor *make_pm_02_5_sensor(const std::string &name, GPIOPin *pm_pin); - PPD42XSensor *make_pm_10_0_sensor(const std::string &name, GPIOPin *pm_pin); + PPD42XSensor *make_pl_02_5_sensor(const std::string &name, GPIOPin *pl_pin); + PPD42XSensor *make_pl_10_0_sensor(const std::string &name, GPIOPin *pl_pin); protected: void parse_data_(); - /// Helper function to convert the specified pm_xx_x duration in µs to pcs/L. + /// Helper function to convert the specified pl_xx_x duration in µs to pcs/L. static float us_to_pl(uint32_t sample_length, uint32_t time_pm); void set_timeout_us(uint32_t timeout_us); - /// Helper function to convert the specified distance in meters to the pm_10_0 duration in µs. + /// Helper function to convert the specified distance in meters to the pl_10_0 duration in µs. uint32_t timeout_us_{30000}; uint32_t starttime_ {0}; uint32_t lowpulseoccupancy_02_5_ {0}; uint32_t lowpulseoccupancy_10_0_ {0}; - GPIOPin *pm_02_5_pin_; - GPIOPin *pm_10_0_pin_; + GPIOPin *pl_02_5_pin_; + GPIOPin *pl_10_0_pin_; const PPD42XType type_; - PPD42XSensor *pm_02_5_sensor_{nullptr}; - PPD42XSensor *pm_10_0_sensor_{nullptr}; + PPD42XSensor *pl_02_5_sensor_{nullptr}; + PPD42XSensor *pl_10_0_sensor_{nullptr}; }; } // namespace sensor From 994face6f8bd7d6abdd81af01a03907374ca7c76 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 18:16:23 +0200 Subject: [PATCH 31/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 0a7356db..15936928 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -15,7 +15,7 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { enum PPD42XType { - PPD42X_TYPE___ = 0, + PPD42X_TYPE = 0, PPD42X_TYPE_NS, }; From 0d2d43fa136e0a9dfc086ec04254ae8b352a4595 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 18:20:20 +0200 Subject: [PATCH 32/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 1941ad15..2006f1fb 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -38,7 +38,7 @@ float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDW void PPD42XComponent::parse_data_() { switch (this->type_) { - case PPD42X_TYPE___: { + case PPD42X_TYPE: { uint16_t pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; uint16_t pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; ESP_LOGD(TAG, From 263b835576ae672ef57b826f80652cdf02d1fdd8 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 18:39:00 +0200 Subject: [PATCH 33/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 15936928..6d91ae2e 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -43,7 +43,7 @@ class PPD42XSensor : public sensor::Sensor { class PPD42XComponent : public Component { public: PPD42XComponent(PPD42XType type); - + void set_timeout_us(uint32_t timeout_us); void loop() override; float get_setup_priority() const override; void dump_config() override; @@ -57,7 +57,7 @@ class PPD42XComponent : public Component { void parse_data_(); /// Helper function to convert the specified pl_xx_x duration in µs to pcs/L. static float us_to_pl(uint32_t sample_length, uint32_t time_pm); - void set_timeout_us(uint32_t timeout_us); + /// Helper function to convert the specified distance in meters to the pl_10_0 duration in µs. uint32_t timeout_us_{30000}; From f3363d479a4c8cdcd0f491b49ceb6bc07928e2f1 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 19:06:43 +0200 Subject: [PATCH 34/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 2006f1fb..0a8983e1 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -39,8 +39,8 @@ float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDW void PPD42XComponent::parse_data_() { switch (this->type_) { case PPD42X_TYPE: { - uint16_t pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; - uint16_t pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", pl_02_5_concentration, pl_10_0_concentration); @@ -51,8 +51,8 @@ void PPD42XComponent::parse_data_() { break; } case PPD42X_TYPE_NS: { - uint16_t pl_02_5_concentration = 0; - uint16_t pl_10_0_concentration = 0; + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", pl_02_5_concentration, pl_10_0_concentration); From 86b9d9242b18b6ed6873d8cc417a74b2df673c9c Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 19:26:41 +0200 Subject: [PATCH 35/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 8 ++++---- src/esphome/sensor/ppd42x.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 0a8983e1..9600f5c6 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -42,7 +42,7 @@ void PPD42XComponent::parse_data_() { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; ESP_LOGD(TAG, - "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", + "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -54,7 +54,7 @@ void PPD42XComponent::parse_data_() { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); ESP_LOGD(TAG, - "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", + "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -69,10 +69,10 @@ void PPD42XComponent::parse_data_() { PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin *pl_pin) { - return this->pl_02_5_sensor_ = new PPD42XSensor(name, *pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); + return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); } PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin *pl_pin) { - return this->pl_10_0_sensor_ = new PPD42XSensor(name, *pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); + return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); } PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} void PPD42XComponent::dump_config() { diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 6d91ae2e..dc35f7ad 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -29,7 +29,7 @@ enum PPD42XSensorType { class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, GPIOPin *pl_pin, PPD42XSensorType type); + PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; @@ -50,8 +50,8 @@ class PPD42XComponent : public Component { void setup() override; void get_update_interval(); - PPD42XSensor *make_pl_02_5_sensor(const std::string &name, GPIOPin *pl_pin); - PPD42XSensor *make_pl_10_0_sensor(const std::string &name, GPIOPin *pl_pin); + PPD42XSensor *make_pl_02_5_sensor(const std::string &name, GPIOPin pl_pin); + PPD42XSensor *make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin); protected: void parse_data_(); From cb4e98c83550bec6c9a57b85168f92484c8c13b9 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 19:44:24 +0200 Subject: [PATCH 36/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 9600f5c6..de50eb4d 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -68,10 +68,10 @@ void PPD42XComponent::parse_data_() { } -PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin *pl_pin) { +PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin pl_pin) { return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); } -PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin *pl_pin) { +PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin) { return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); } PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} @@ -113,7 +113,7 @@ int8_t PPD42XSensor::accuracy_decimals() { } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin *pl_pin, PPD42XSensorType type) : Sensor(name), pl_pin_(pl_pin), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type) : Sensor(name), pl_pin_(pl_pin), type_(type) {} } // namespace sensor From 21330b41b5ba9c644d6f54eed5e8f9c56f7fe560 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 21:48:15 +0200 Subject: [PATCH 37/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 20 ++++++++++---------- src/esphome/sensor/ppd42x.h | 11 +++++------ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index de50eb4d..886a265b 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -25,25 +25,25 @@ void PPD42XComponent::loop() { pulseIn(this->pl_10_0_pin_->get_pin(), uint8_t(!this->pl_10_0_pin_->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; - + if (now - this->starttime_ > this->timeout_us_) { // last transmission too long ago this->starttime_ = now; parse_data_(); - } } + float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } void PPD42XComponent::parse_data_() { switch (this->type_) { case PPD42X_TYPE: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_) ; - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_) ; + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); ESP_LOGD(TAG, - "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", - pl_02_5_concentration, pl_10_0_concentration); + "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); if (this->pl_10_0_sensor_ != nullptr) @@ -67,14 +67,13 @@ void PPD42XComponent::parse_data_() { this->status_clear_warning(); } - PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin pl_pin) { return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); } PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin) { return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} +PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM02.5", this->pl_02_5_sensor_); @@ -90,7 +89,7 @@ std::string PPD42XSensor::unit_of_measurement() { case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: return "pcs/L"; - } + } return ""; } std::string PPD42XSensor::icon() { @@ -113,7 +112,8 @@ int8_t PPD42XSensor::accuracy_decimals() { } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type) : Sensor(name), pl_pin_(pl_pin), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type) + : Sensor(name), pl_pin_(pl_pin), type_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index dc35f7ad..3b4dae78 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -7,7 +7,6 @@ #include "esphome/component.h" #include "esphome/sensor/sensor.h" -#include "esphome/uart_component.h" #include "esphome/helpers.h" ESPHOME_NAMESPACE_BEGIN @@ -58,16 +57,16 @@ class PPD42XComponent : public Component { /// Helper function to convert the specified pl_xx_x duration in µs to pcs/L. static float us_to_pl(uint32_t sample_length, uint32_t time_pm); - + /// Helper function to convert the specified distance in meters to the pl_10_0 duration in µs. uint32_t timeout_us_{30000}; - uint32_t starttime_ {0}; - uint32_t lowpulseoccupancy_02_5_ {0}; - uint32_t lowpulseoccupancy_10_0_ {0}; + uint32_t starttime_{0}; + uint32_t lowpulseoccupancy_02_5_{0}; + uint32_t lowpulseoccupancy_10_0_{0}; GPIOPin *pl_02_5_pin_; GPIOPin *pl_10_0_pin_; - const PPD42XType type_; + const PPD42XType type_; PPD42XSensor *pl_02_5_sensor_{nullptr}; PPD42XSensor *pl_10_0_sensor_{nullptr}; }; From 5b62b379640d441ca946890b63b2219ddddbab3a Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 22:05:54 +0200 Subject: [PATCH 38/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 886a265b..cf71d045 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -41,8 +41,7 @@ void PPD42XComponent::parse_data_() { case PPD42X_TYPE: { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, - "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -53,9 +52,8 @@ void PPD42XComponent::parse_data_() { case PPD42X_TYPE_NS: { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, - "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", - pl_02_5_concentration, pl_10_0_concentration); + ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); if (this->pl_10_0_sensor_ != nullptr) From 84f147cbe04a4ada8fd1a1c401b1f29ed04f244e Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 22:32:18 +0200 Subject: [PATCH 39/76] Refactore ppd42x.py --- src/esphome/sensor/ppd42x.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index cf71d045..4fd30db4 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -30,12 +30,11 @@ void PPD42XComponent::loop() { // last transmission too long ago this->starttime_ = now; parse_data_(); - } + } } float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } - void PPD42XComponent::parse_data_() { switch (this->type_) { case PPD42X_TYPE: { From 2161981f4d8a939e7d469d43fc85273698e36613 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sat, 6 Apr 2019 23:07:34 +0200 Subject: [PATCH 40/76] Refactore ppd42x.py --- src/esphome/application.cpp | 8 ++++++-- src/esphome/application.h | 4 +++- src/esphome/sensor/ppd42x.cpp | 4 +++- src/esphome/sensor/ppd42x.h | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index d4ab5c13..504c903e 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1089,8 +1089,12 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso } #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type) { - return this->register_component(new PPD42XComponent(type)); +sensor::PPD42XComponent *Application::make_ppd42x(const GPIOInputPin &pl_02_5, + const GPIOInputPin &pl_10_0, + sensor::PPD42XType type) { + return this->register_component(new PPD42XComponent(const GPIOInputPin &pl_02_5, + const GPIOInputPin &pl_10_0, + type)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index 62348f11..128f1226 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,9 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type); + sensor::PPD42XComponent *make_ppd42x(const GPIOOutputPin &pl_02_5, + const GPIOOutputPin &pl_10_0, + sensor::PPD42XType type); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 4fd30db4..c80995be 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -70,7 +70,9 @@ PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIO PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin) { return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(PPD42XType type) : type_(type) {} +PPD42XComponent::PPD42XComponent(GPIOOutputPin &pl_02_5, GPIOOutputPin &pl_10_0, + PPD42XType type) : pl_02_5_pin_(pl_02_5), pl_10_0_pin_(pl_10_0), + type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM02.5", this->pl_02_5_sensor_); diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 3b4dae78..d9d110d4 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -41,7 +41,8 @@ class PPD42XSensor : public sensor::Sensor { class PPD42XComponent : public Component { public: - PPD42XComponent(PPD42XType type); + PPD42XComponent(const GPIOOutputPin &pl_02_5, const GPIOOutputPin &pl_10_0, + PPD42XType type); void set_timeout_us(uint32_t timeout_us); void loop() override; float get_setup_priority() const override; From c554d902490be3c8a41bd7ecd19cf0d55b3bdac7 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 06:53:27 +0200 Subject: [PATCH 41/76] from scrach --- src/esphome/application.cpp | 10 +- src/esphome/application.h | 4 +- src/esphome/sensor/ppd42x.cpp | 229 +++++++++++++++++++++++++--------- src/esphome/sensor/ppd42x.h | 67 +++++----- 4 files changed, 211 insertions(+), 99 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 504c903e..10b03507 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1089,12 +1089,10 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso } #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_ppd42x(const GPIOInputPin &pl_02_5, - const GPIOInputPin &pl_10_0, - sensor::PPD42XType type) { - return this->register_component(new PPD42XComponent(const GPIOInputPin &pl_02_5, - const GPIOInputPin &pl_10_0, - type)); +sensor::PPD42XComponent *Application::make_pmsx003(UARTComponent *parent, sensor::PPD42XType type) { + return this->register_component(new PPD42XComponent(parent, type)); +} +#endif } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index 128f1226..06a36082 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,9 +812,7 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(const GPIOOutputPin &pl_02_5, - const GPIOOutputPin &pl_10_0, - sensor::PPD42XType type); + sensor::PPD42XComponent *make_pmsx003(UARTComponent *parent, sensor::PPD42XType type); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index c80995be..9d7736e3 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -10,109 +10,218 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { static const char *TAG = "sensor.ppd42x"; -void PPD42XComponent::setup() { - ESP_LOGCONFIG(TAG, "Setting up PPD42X Sensor..."); - this->starttime_ = millis(); - this->pl_02_5_pin_->setup(); - this->pl_10_0_pin_->setup(); -} void PPD42XComponent::loop() { const uint32_t now = millis(); - uint32_t duration_pl_02_5 = - pulseIn(this->pl_02_5_pin_->get_pin(), uint8_t(!this->pl_02_5_pin_->is_inverted()), this->timeout_us_); - uint32_t duration_pl_10_0 = - pulseIn(this->pl_10_0_pin_->get_pin(), uint8_t(!this->pl_10_0_pin_->is_inverted()), this->timeout_us_); - this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; - this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; - - if (now - this->starttime_ > this->timeout_us_) { - // last transmission too long ago - this->starttime_ = now; - parse_data_(); + if (now - this->last_transmission_ >= 500) { + // last transmission too long ago. Reset RX index. + this->data_index_ = 0; } -} + if (this->available() == 0) + return; + + this->last_transmission_ = now; + while (this->available() != 0) { + this->read_byte(&this->data_[this->data_index_]); + auto check = this->check_byte_(); + if (!check.has_value()) { + // finished + this->parse_data_(); + this->data_index_ = 0; + } else if (!*check) { + // wrong data + this->data_index_ = 0; + } else { + // next byte + this->data_index_++; + } + } +} float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } +optional PPD42XComponent::check_byte_() { + uint8_t index = this->data_index_; + uint8_t byte = this->data_[index]; + + if (index == 0) + return byte == 0x42; + + if (index == 1) + return byte == 0x4D; + + if (index == 2) + return true; + + uint16_t payload_length = this->get_16_bit_uint_(2); + if (index == 3) { + bool length_matches = false; + switch (this->type_) { + case PPD42X_TYPE_X003: + length_matches = payload_length == 28 || payload_length == 20; + break; + case PPD42X_TYPE_5003T: + length_matches = payload_length == 28; + break; + case PPD42X_TYPE_5003ST: + length_matches = payload_length == 36; + break; + } + + if (!length_matches) { + ESP_LOGW(TAG, "PPD42X length %u doesn't match. Are you using the correct PPD42X type?", payload_length); + return false; + } + return true; + } + + // start (16bit) + length (16bit) + DATA (payload_length-2 bytes) + checksum (16bit) + uint8_t total_size = 4 + payload_length; + + if (index < total_size - 1) + return true; + + // checksum is without checksum bytes + uint16_t checksum = 0; + for (uint8_t i = 0; i < total_size - 2; i++) + checksum += this->data_[i]; + + uint16_t check = this->get_16_bit_uint_(total_size - 2); + if (checksum != check) { + ESP_LOGW(TAG, "PPD42X checksum mismatch! 0x%02X!=0x%02X", checksum, check); + return false; + } + + return {}; +} void PPD42XComponent::parse_data_() { switch (this->type_) { - case PPD42X_TYPE: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, - pl_10_0_concentration); - if (this->pl_02_5_sensor_ != nullptr) - this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); - if (this->pl_10_0_sensor_ != nullptr) - this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); + case PPD42X_TYPE_X003: { + uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10); + uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); + uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); + ESP_LOGD(TAG, + "Got PM1.0 Concentration: %u µg/m^3, PM2.5 Concentration %u µg/m^3, PM10.0 Concentration: %u µg/m^3", + pm_1_0_concentration, pm_2_5_concentration, pm_10_0_concentration); + if (this->pm_1_0_sensor_ != nullptr) + this->pm_1_0_sensor_->publish_state(pm_1_0_concentration); + if (this->pm_2_5_sensor_ != nullptr) + this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); + if (this->pm_10_0_sensor_ != nullptr) + this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + break; + } + case PPD42X_TYPE_5003T: { + uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); + float temperature = this->get_16_bit_uint_(24) / 10.0f; + float humidity = this->get_16_bit_uint_(26) / 10.0f; + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3, Temperature: %.1f°C, Humidity: %.1f%%", pm_2_5_concentration, + temperature, humidity); + if (this->pm_2_5_sensor_ != nullptr) + this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); + if (this->temperature_sensor_ != nullptr) + this->temperature_sensor_->publish_state(temperature); + if (this->humidity_sensor_ != nullptr) + this->humidity_sensor_->publish_state(humidity); break; } - case PPD42X_TYPE_NS: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, - pl_10_0_concentration); - if (this->pl_02_5_sensor_ != nullptr) - this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); - if (this->pl_10_0_sensor_ != nullptr) - this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); + case PPD42X_TYPE_5003ST: { + uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); + uint16_t formaldehyde = this->get_16_bit_uint_(28); + float temperature = this->get_16_bit_uint_(30) / 10.0f; + float humidity = this->get_16_bit_uint_(32) / 10.0f; + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3, Temperature: %.1f°C, Humidity: %.1f%% Formaldehyde: %u µg/m^3", + pm_2_5_concentration, temperature, humidity, formaldehyde); + if (this->pm_2_5_sensor_ != nullptr) + this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); + if (this->temperature_sensor_ != nullptr) + this->temperature_sensor_->publish_state(temperature); + if (this->humidity_sensor_ != nullptr) + this->humidity_sensor_->publish_state(humidity); + if (this->formaldehyde_sensor_ != nullptr) + this->formaldehyde_sensor_->publish_state(formaldehyde); break; } } this->status_clear_warning(); } - -PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOPin pl_pin) { - return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_02_5); +uint16_t PPD42XComponent::get_16_bit_uint_(uint8_t start_index) { + return (uint16_t(this->data_[start_index]) << 8) | uint16_t(this->data_[start_index + 1]); +} +PPD42XSensor *PPD42XComponent::make_pm_1_0_sensor(const std::string &name) { + return this->pm_1_0_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_1_0); +} +PPD42XSensor *PPD42XComponent::make_pm_2_5_sensor(const std::string &name) { + return this->pm_2_5_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_2_5); } -PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin) { - return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl_pin, PPD42X_SENSOR_TYPE_PM_10_0); +PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name) { + return this->pm_10_0_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(GPIOOutputPin &pl_02_5, GPIOOutputPin &pl_10_0, - PPD42XType type) : pl_02_5_pin_(pl_02_5), pl_10_0_pin_(pl_10_0), - type_(type) {} +PPD42XSensor *PPD42XComponent::make_temperature_sensor(const std::string &name) { + return this->temperature_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_TEMPERATURE); +} +PPD42XSensor *PPD42XComponent::make_humidity_sensor(const std::string &name) { + return this->humidity_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_HUMIDITY); +} +PPD42XSensor *PPD42XComponent::make_formaldehyde_sensor(const std::string &name) { + return this->formaldehyde_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_FORMALDEHYDE); +} +PPD42XComponent::PPD42XComponent(UARTComponent *parent, PPD42XType type) : UARTDevice(parent), type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); - LOG_SENSOR(" ", "PM02.5", this->pl_02_5_sensor_); - LOG_SENSOR(" ", "PM10.0", this->pl_10_0_sensor_); - ESP_LOGCONFIG(TAG, " Timeout: %u µs", this->timeout_us_); -} -float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { - float ratio = time_pm / (sample_length * 10.0f); - return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; + LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_); + LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); + LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); + LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_); } + std::string PPD42XSensor::unit_of_measurement() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_1_0: + case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - return "pcs/L"; + case PPD42X_SENSOR_TYPE_FORMALDEHYDE: + return UNIT_MICROGRAMS_PER_CUBIC_METER; + case PPD42X_SENSOR_TYPE_TEMPERATURE: + return UNIT_C; + case PPD42X_SENSOR_TYPE_HUMIDITY: + return UNIT_PERCENT; } return ""; } std::string PPD42XSensor::icon() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_1_0: + case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - // Not the ideal icon, but Otto can't find a better one ;) + case PPD42X_SENSOR_TYPE_FORMALDEHYDE: + // Not the ideal icon, but I can't find a better one. return ICON_CHEMICAL_WEAPON; + case PPD42X_SENSOR_TYPE_TEMPERATURE: + return ICON_EMPTY; + case PPD42X_SENSOR_TYPE_HUMIDITY: + return ICON_WATER_PERCENT; } return ""; } int8_t PPD42XSensor::accuracy_decimals() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_02_5: + case PPD42X_SENSOR_TYPE_PM_1_0: + case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: + case PPD42X_SENSOR_TYPE_FORMALDEHYDE: return 0; + case PPD42X_SENSOR_TYPE_TEMPERATURE: + case PPD42X_SENSOR_TYPE_HUMIDITY: + return 1; } return 0; } -void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } - -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type) - : Sensor(name), pl_pin_(pl_pin), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, PPD42XSensorType type) : Sensor(name), type_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index d9d110d4..ef372329 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -7,6 +7,7 @@ #include "esphome/component.h" #include "esphome/sensor/sensor.h" +#include "esphome/uart_component.h" #include "esphome/helpers.h" ESPHOME_NAMESPACE_BEGIN @@ -14,21 +15,29 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { enum PPD42XType { - PPD42X_TYPE = 0, - PPD42X_TYPE_NS, + PPD42X_TYPE_X003 = 0, + PPD42X_TYPE_5003T, + PPD42X_TYPE_5003ST, }; enum PPD42XSensorType { - /// PM2.5 concentration in pcs/L, PPD42, PPD42NS - PPD42X_SENSOR_TYPE_PM_02_5, - /// PM10.0 concentration in pcs/L, PPD42, PPD42NS + /// PM1.0 concentration in µg/m^3, PPD42X + PPD42X_SENSOR_TYPE_PM_1_0 = 0, + /// PM2.5 concentration in µg/m^3, PPD42X, PMS5003T, PMS5003ST + PPD42X_SENSOR_TYPE_PM_2_5, + /// PM10.0 concentration in µg/m^3, PPD42X PPD42X_SENSOR_TYPE_PM_10_0, - + /// Temperature in °C, PMS5003T, PMS5003ST + PPD42X_SENSOR_TYPE_TEMPERATURE, + /// Relative Humidity in %, PMS5003T, PMS5003T + PPD42X_SENSOR_TYPE_HUMIDITY, + /// Formaldehyde in µg/m^3, PMS5003ST + PPD42X_SENSOR_TYPE_FORMALDEHYDE, }; class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, GPIOPin pl_pin, PPD42XSensorType type); + PPD42XSensor(const std::string &name, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; @@ -36,40 +45,38 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType type_; - const GPIOPin pl_pin_; }; -class PPD42XComponent : public Component { +class PPD42XComponent : public UARTDevice, public Component { public: - PPD42XComponent(const GPIOOutputPin &pl_02_5, const GPIOOutputPin &pl_10_0, - PPD42XType type); - void set_timeout_us(uint32_t timeout_us); + PPD42XComponent(UARTComponent *parent, PPD42XType type); + void loop() override; float get_setup_priority() const override; void dump_config() override; - void setup() override; - void get_update_interval(); - PPD42XSensor *make_pl_02_5_sensor(const std::string &name, GPIOPin pl_pin); - PPD42XSensor *make_pl_10_0_sensor(const std::string &name, GPIOPin pl_pin); + PPD42XSensor *make_pm_1_0_sensor(const std::string &name); + PPD42XSensor *make_pm_2_5_sensor(const std::string &name); + PPD42XSensor *make_pm_10_0_sensor(const std::string &name); + PPD42XSensor *make_temperature_sensor(const std::string &name); + PPD42XSensor *make_humidity_sensor(const std::string &name); + PPD42XSensor *make_formaldehyde_sensor(const std::string &name); protected: + optional check_byte_(); void parse_data_(); - /// Helper function to convert the specified pl_xx_x duration in µs to pcs/L. - static float us_to_pl(uint32_t sample_length, uint32_t time_pm); - - - /// Helper function to convert the specified distance in meters to the pl_10_0 duration in µs. - uint32_t timeout_us_{30000}; - uint32_t starttime_{0}; - uint32_t lowpulseoccupancy_02_5_{0}; - uint32_t lowpulseoccupancy_10_0_{0}; - GPIOPin *pl_02_5_pin_; - GPIOPin *pl_10_0_pin_; - + uint16_t get_16_bit_uint_(uint8_t start_index); + + uint8_t data_[64]; + uint8_t data_index_{0}; + uint32_t last_transmission_{0}; const PPD42XType type_; - PPD42XSensor *pl_02_5_sensor_{nullptr}; - PPD42XSensor *pl_10_0_sensor_{nullptr}; + PPD42XSensor *pm_1_0_sensor_{nullptr}; + PPD42XSensor *pm_2_5_sensor_{nullptr}; + PPD42XSensor *pm_10_0_sensor_{nullptr}; + PPD42XSensor *temperature_sensor_{nullptr}; + PPD42XSensor *humidity_sensor_{nullptr}; + PPD42XSensor *formaldehyde_sensor_{nullptr}; }; } // namespace sensor From c640774950e92f9dee278d1edf76f317b7a178a2 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 07:03:14 +0200 Subject: [PATCH 42/76] from scrach --- src/esphome/application.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 10b03507..af3f2ae7 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1088,13 +1088,12 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso return this->register_component(new PMSX003Component(parent, type)); } #endif + #ifdef USE_PPD42X sensor::PPD42XComponent *Application::make_pmsx003(UARTComponent *parent, sensor::PPD42XType type) { return this->register_component(new PPD42XComponent(parent, type)); } #endif -} -#endif #ifdef USE_A4988 stepper::A4988 *Application::make_a4988(const GPIOOutputPin &step_pin, const GPIOOutputPin &dir_pin) { From 48865e50af2788485e6b5c7f40fdc9deadbec067 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 07:50:42 +0200 Subject: [PATCH 43/76] from scrach --- src/esphome/application.cpp | 2 +- src/esphome/application.h | 2 +- src/esphome/defines.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index af3f2ae7..87b6ce9c 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1090,7 +1090,7 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_pmsx003(UARTComponent *parent, sensor::PPD42XType type) { +sensor::PPD42XComponent *Application::make_ppd42x(UARTComponent *parent, sensor::PPD42XType type) { return this->register_component(new PPD42XComponent(parent, type)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index 06a36082..0723d551 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,7 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_pmsx003(UARTComponent *parent, sensor::PPD42XType type); + sensor::PPD42XComponent *make_ppd42x(UARTComponent *parent, sensor::PPD42XType type); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 994b64d3..1b5bf7a1 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -38,7 +38,6 @@ #define USE_HTU21D_SENSOR #define USE_HDC1080_SENSOR #define USE_ULTRASONIC_SENSOR -#define USE_PPD42X #define USE_WIFI_SIGNAL_SENSOR #define USE_OUTPUT #ifdef ARDUINO_ARCH_ESP32 @@ -125,6 +124,7 @@ #define USE_MQTT_SUBSCRIBE_SENSOR #define USE_CSE7766 #define USE_PMSX003 +#define USE_PPD42X #define USE_STEPPER #define USE_A4988 #define USE_ULN2003 From 4d3e52489d2e5a9acfbc263eec354ac20ceb4617 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 09:11:19 +0200 Subject: [PATCH 44/76] from scrach --- src/esphome/sensor/ppd42x.cpp | 72 +++++------------------------------ src/esphome/sensor/ppd42x.h | 24 ++---------- 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 9d7736e3..783e34ca 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -56,13 +56,13 @@ optional PPD42XComponent::check_byte_() { if (index == 3) { bool length_matches = false; switch (this->type_) { - case PPD42X_TYPE_X003: + case PPD42X_TYPE_: length_matches = payload_length == 28 || payload_length == 20; break; - case PPD42X_TYPE_5003T: + case PPD42X_TYPE_NJ: length_matches = payload_length == 28; break; - case PPD42X_TYPE_5003ST: + case PPD42X_TYPE_NS: length_matches = payload_length == 36; break; } @@ -96,50 +96,31 @@ optional PPD42XComponent::check_byte_() { void PPD42XComponent::parse_data_() { switch (this->type_) { - case PPD42X_TYPE_X003: { - uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10); + case PPD42X_TYPE_: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); ESP_LOGD(TAG, "Got PM1.0 Concentration: %u µg/m^3, PM2.5 Concentration %u µg/m^3, PM10.0 Concentration: %u µg/m^3", pm_1_0_concentration, pm_2_5_concentration, pm_10_0_concentration); - if (this->pm_1_0_sensor_ != nullptr) - this->pm_1_0_sensor_->publish_state(pm_1_0_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); if (this->pm_10_0_sensor_ != nullptr) this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); break; } - case PPD42X_TYPE_5003T: { + case PPD42X_TYPE_NJ: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - float temperature = this->get_16_bit_uint_(24) / 10.0f; - float humidity = this->get_16_bit_uint_(26) / 10.0f; - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3, Temperature: %.1f°C, Humidity: %.1f%%", pm_2_5_concentration, - temperature, humidity); + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3 ", pm_2_5_concentration + ); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); - if (this->temperature_sensor_ != nullptr) - this->temperature_sensor_->publish_state(temperature); - if (this->humidity_sensor_ != nullptr) - this->humidity_sensor_->publish_state(humidity); break; } - case PPD42X_TYPE_5003ST: { + case PPD42X_TYPE_NS: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - uint16_t formaldehyde = this->get_16_bit_uint_(28); - float temperature = this->get_16_bit_uint_(30) / 10.0f; - float humidity = this->get_16_bit_uint_(32) / 10.0f; - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3, Temperature: %.1f°C, Humidity: %.1f%% Formaldehyde: %u µg/m^3", - pm_2_5_concentration, temperature, humidity, formaldehyde); + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3 ", pm_2_5_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); - if (this->temperature_sensor_ != nullptr) - this->temperature_sensor_->publish_state(temperature); - if (this->humidity_sensor_ != nullptr) - this->humidity_sensor_->publish_state(humidity); - if (this->formaldehyde_sensor_ != nullptr) - this->formaldehyde_sensor_->publish_state(formaldehyde); break; } } @@ -149,74 +130,41 @@ void PPD42XComponent::parse_data_() { uint16_t PPD42XComponent::get_16_bit_uint_(uint8_t start_index) { return (uint16_t(this->data_[start_index]) << 8) | uint16_t(this->data_[start_index + 1]); } -PPD42XSensor *PPD42XComponent::make_pm_1_0_sensor(const std::string &name) { - return this->pm_1_0_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_1_0); -} PPD42XSensor *PPD42XComponent::make_pm_2_5_sensor(const std::string &name) { return this->pm_2_5_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_2_5); } PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name) { return this->pm_10_0_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XSensor *PPD42XComponent::make_temperature_sensor(const std::string &name) { - return this->temperature_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_TEMPERATURE); -} -PPD42XSensor *PPD42XComponent::make_humidity_sensor(const std::string &name) { - return this->humidity_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_HUMIDITY); -} -PPD42XSensor *PPD42XComponent::make_formaldehyde_sensor(const std::string &name) { - return this->formaldehyde_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_FORMALDEHYDE); -} PPD42XComponent::PPD42XComponent(UARTComponent *parent, PPD42XType type) : UARTDevice(parent), type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); - LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_); LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); - LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); - LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); - LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_); + } std::string PPD42XSensor::unit_of_measurement() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_1_0: case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - case PPD42X_SENSOR_TYPE_FORMALDEHYDE: return UNIT_MICROGRAMS_PER_CUBIC_METER; - case PPD42X_SENSOR_TYPE_TEMPERATURE: - return UNIT_C; - case PPD42X_SENSOR_TYPE_HUMIDITY: - return UNIT_PERCENT; } return ""; } std::string PPD42XSensor::icon() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_1_0: case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - case PPD42X_SENSOR_TYPE_FORMALDEHYDE: // Not the ideal icon, but I can't find a better one. - return ICON_CHEMICAL_WEAPON; - case PPD42X_SENSOR_TYPE_TEMPERATURE: - return ICON_EMPTY; - case PPD42X_SENSOR_TYPE_HUMIDITY: - return ICON_WATER_PERCENT; } return ""; } int8_t PPD42XSensor::accuracy_decimals() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_1_0: case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - case PPD42X_SENSOR_TYPE_FORMALDEHYDE: return 0; - case PPD42X_SENSOR_TYPE_TEMPERATURE: - case PPD42X_SENSOR_TYPE_HUMIDITY: - return 1; } return 0; diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index ef372329..150b473b 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -15,24 +15,16 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { enum PPD42XType { - PPD42X_TYPE_X003 = 0, - PPD42X_TYPE_5003T, - PPD42X_TYPE_5003ST, + PPD42X_TYPE_ = 0, + PPD42X_TYPE_NJ, + PPD42X_TYPE_NS, }; enum PPD42XSensorType { - /// PM1.0 concentration in µg/m^3, PPD42X - PPD42X_SENSOR_TYPE_PM_1_0 = 0, - /// PM2.5 concentration in µg/m^3, PPD42X, PMS5003T, PMS5003ST + /// PM2.5 concentration in µg/m^3, PPD42, PPD42NJ, PPD42NS PPD42X_SENSOR_TYPE_PM_2_5, /// PM10.0 concentration in µg/m^3, PPD42X PPD42X_SENSOR_TYPE_PM_10_0, - /// Temperature in °C, PMS5003T, PMS5003ST - PPD42X_SENSOR_TYPE_TEMPERATURE, - /// Relative Humidity in %, PMS5003T, PMS5003T - PPD42X_SENSOR_TYPE_HUMIDITY, - /// Formaldehyde in µg/m^3, PMS5003ST - PPD42X_SENSOR_TYPE_FORMALDEHYDE, }; class PPD42XSensor : public sensor::Sensor { @@ -55,12 +47,8 @@ class PPD42XComponent : public UARTDevice, public Component { float get_setup_priority() const override; void dump_config() override; - PPD42XSensor *make_pm_1_0_sensor(const std::string &name); PPD42XSensor *make_pm_2_5_sensor(const std::string &name); PPD42XSensor *make_pm_10_0_sensor(const std::string &name); - PPD42XSensor *make_temperature_sensor(const std::string &name); - PPD42XSensor *make_humidity_sensor(const std::string &name); - PPD42XSensor *make_formaldehyde_sensor(const std::string &name); protected: optional check_byte_(); @@ -71,12 +59,8 @@ class PPD42XComponent : public UARTDevice, public Component { uint8_t data_index_{0}; uint32_t last_transmission_{0}; const PPD42XType type_; - PPD42XSensor *pm_1_0_sensor_{nullptr}; PPD42XSensor *pm_2_5_sensor_{nullptr}; PPD42XSensor *pm_10_0_sensor_{nullptr}; - PPD42XSensor *temperature_sensor_{nullptr}; - PPD42XSensor *humidity_sensor_{nullptr}; - PPD42XSensor *formaldehyde_sensor_{nullptr}; }; } // namespace sensor From eceeec2b58b855d1d90a9e2920b1ee232e57abac Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 09:20:01 +0200 Subject: [PATCH 45/76] from scrach --- src/esphome/sensor/ppd42x.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 783e34ca..2da8b8c4 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -100,8 +100,8 @@ void PPD42XComponent::parse_data_() { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); ESP_LOGD(TAG, - "Got PM1.0 Concentration: %u µg/m^3, PM2.5 Concentration %u µg/m^3, PM10.0 Concentration: %u µg/m^3", - pm_1_0_concentration, pm_2_5_concentration, pm_10_0_concentration); + "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", + pm_2_5_concentration, pm_10_0_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); if (this->pm_10_0_sensor_ != nullptr) @@ -110,7 +110,7 @@ void PPD42XComponent::parse_data_() { } case PPD42X_TYPE_NJ: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3 ", pm_2_5_concentration + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration ); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); @@ -118,7 +118,7 @@ void PPD42XComponent::parse_data_() { } case PPD42X_TYPE_NS: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3 ", pm_2_5_concentration); + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); break; From f851b06fe14a0c3746e2ed7c5e2f054607d76c3c Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 09:28:40 +0200 Subject: [PATCH 46/76] from scrach --- src/esphome/sensor/ppd42x.cpp | 6 +++--- src/esphome/sensor/ppd42x.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 2da8b8c4..2f3b1cf0 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -56,7 +56,7 @@ optional PPD42XComponent::check_byte_() { if (index == 3) { bool length_matches = false; switch (this->type_) { - case PPD42X_TYPE_: + case PPD42X_TYPE: length_matches = payload_length == 28 || payload_length == 20; break; case PPD42X_TYPE_NJ: @@ -96,7 +96,7 @@ optional PPD42XComponent::check_byte_() { void PPD42XComponent::parse_data_() { switch (this->type_) { - case PPD42X_TYPE_: { + case PPD42X_TYPE: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); ESP_LOGD(TAG, @@ -157,7 +157,7 @@ std::string PPD42XSensor::icon() { case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: // Not the ideal icon, but I can't find a better one. - } + return ICON_CHEMICAL_WEAPON; } return ""; } int8_t PPD42XSensor::accuracy_decimals() { diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 150b473b..cb100de0 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -15,7 +15,7 @@ ESPHOME_NAMESPACE_BEGIN namespace sensor { enum PPD42XType { - PPD42X_TYPE_ = 0, + PPD42X_TYPE = 0, PPD42X_TYPE_NJ, PPD42X_TYPE_NS, }; From 9ccc6aac0780ea4a162caa20525ad3f1fe7dc379 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 09:44:40 +0200 Subject: [PATCH 47/76] from scrach --- src/esphome/sensor/ppd42x.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 2f3b1cf0..21cea165 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -99,9 +99,8 @@ void PPD42XComponent::parse_data_() { case PPD42X_TYPE: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); - ESP_LOGD(TAG, - "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", - pm_2_5_concentration, pm_10_0_concentration); + ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", pm_2_5_concentration, + pm_10_0_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); if (this->pm_10_0_sensor_ != nullptr) @@ -110,8 +109,7 @@ void PPD42XComponent::parse_data_() { } case PPD42X_TYPE_NJ: { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration - ); + ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration); if (this->pm_2_5_sensor_ != nullptr) this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); break; @@ -157,7 +155,8 @@ std::string PPD42XSensor::icon() { case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: // Not the ideal icon, but I can't find a better one. - return ICON_CHEMICAL_WEAPON; } + return ICON_CHEMICAL_WEAPON; + } return ""; } int8_t PPD42XSensor::accuracy_decimals() { From dc09017de3888b008f0673097d8b576dc24d3dd0 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 10:24:37 +0200 Subject: [PATCH 48/76] syntax error- --- src/esphome/sensor/ppd42x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 21cea165..9e498d67 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -154,7 +154,7 @@ std::string PPD42XSensor::icon() { switch (this->type_) { case PPD42X_SENSOR_TYPE_PM_2_5: case PPD42X_SENSOR_TYPE_PM_10_0: - // Not the ideal icon, but I can't find a better one. + // Not the ideal icon, but Otto can't find a better one ;) return ICON_CHEMICAL_WEAPON; } return ""; From 9131ad9e13df463f0d3e47464ab63e136d63d918 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 10:36:18 +0200 Subject: [PATCH 49/76] syntax error-- --- src/esphome/sensor/ppd42x.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 9e498d67..f297a5e4 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -139,7 +139,6 @@ void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); - } std::string PPD42XSensor::unit_of_measurement() { From 8fb6901d917116f145fffe0f54b2ce5bc1f63efe Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 12:37:28 +0200 Subject: [PATCH 50/76] adapt + --- src/esphome/application.cpp | 4 ++-- src/esphome/application.h | 2 +- src/esphome/sensor/ppd42x.cpp | 21 +++++++++++---------- src/esphome/sensor/ppd42x.h | 17 +++++++++-------- src/esphome/sensor/sensor.cpp | 1 + src/esphome/sensor/sensor.h | 1 + 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 87b6ce9c..5584e2d9 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1090,8 +1090,8 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_ppd42x(UARTComponent *parent, sensor::PPD42XType type) { - return this->register_component(new PPD42XComponent(parent, type)); +sensor::PPD42XComponent *Application::make_ppd42x(uint32_t update_interval, sensor::PPD42XType type) { + return this->register_component(new PPD42XComponent(update_interval, type)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index 0723d551..c78cf8b7 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,7 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(UARTComponent *parent, sensor::PPD42XType type); + sensor::PPD42XComponent *make_ppd42x(uint32_t update_interval = 60000, sensor::PPD42XType type); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index f297a5e4..c12a0a91 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -128,13 +128,13 @@ void PPD42XComponent::parse_data_() { uint16_t PPD42XComponent::get_16_bit_uint_(uint8_t start_index) { return (uint16_t(this->data_[start_index]) << 8) | uint16_t(this->data_[start_index + 1]); } -PPD42XSensor *PPD42XComponent::make_pm_2_5_sensor(const std::string &name) { - return this->pm_2_5_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_2_5); +PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, const GPIOInputPin &pl) { + return this->pm_2_5_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_02_5); } -PPD42XSensor *PPD42XComponent::make_pm_10_0_sensor(const std::string &name) { - return this->pm_10_0_sensor_ = new PPD42XSensor(name, PPD42X_SENSOR_TYPE_PM_10_0); +PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name) { + return this->pm_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(UARTComponent *parent, PPD42XType type) : UARTDevice(parent), type_(type) {} +PPD42XComponent::PPD42XComponent( PPD42XType type) : type_(type) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); @@ -143,15 +143,15 @@ void PPD42XComponent::dump_config() { std::string PPD42XSensor::unit_of_measurement() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_2_5: + case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: - return UNIT_MICROGRAMS_PER_CUBIC_METER; + return UNIT_PARTICLES_PER_LITER; } return ""; } std::string PPD42XSensor::icon() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_2_5: + case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: // Not the ideal icon, but Otto can't find a better one ;) return ICON_CHEMICAL_WEAPON; @@ -160,14 +160,15 @@ std::string PPD42XSensor::icon() { } int8_t PPD42XSensor::accuracy_decimals() { switch (this->type_) { - case PPD42X_SENSOR_TYPE_PM_2_5: + case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: return 0; } return 0; } -PPD42XSensor::PPD42XSensor(const std::string &name, PPD42XSensorType type) : Sensor(name), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin pl, PPD42XSensorType type) + : Sensor(name), pl_(pl), type_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index cb100de0..2b95b4a9 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -21,15 +21,15 @@ enum PPD42XType { }; enum PPD42XSensorType { - /// PM2.5 concentration in µg/m^3, PPD42, PPD42NJ, PPD42NS - PPD42X_SENSOR_TYPE_PM_2_5, - /// PM10.0 concentration in µg/m^3, PPD42X + /// PM2.5 concentration in pcs/L, PPD42, PPD42NJ, PPD42NS + PPD42X_SENSOR_TYPE_PM_02_5, + /// PM10.0 concentration in pcs/L, PPD42X PPD42X_SENSOR_TYPE_PM_10_0, }; class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, PPD42XSensorType type); + PPD42XSensor(const std::string &name, GPIOInputPin &pl, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; @@ -37,18 +37,19 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType type_; + const GPIOInputPin pl_; }; -class PPD42XComponent : public UARTDevice, public Component { +class PPD42XComponent : public Component { public: - PPD42XComponent(UARTComponent *parent, PPD42XType type); + PPD42XComponent(uint32_t update_interval, PPD42XType type); void loop() override; float get_setup_priority() const override; void dump_config() override; - PPD42XSensor *make_pm_2_5_sensor(const std::string &name); - PPD42XSensor *make_pm_10_0_sensor(const std::string &name); + PPD42XSensor *make_pl_02_5_sensor(const std::string &name, const GPIOInputPin &pl); + PPD42XSensor *make_pl_10_0_sensor(const std::string &name, const GPIOInputPin &pl); protected: optional check_byte_(); diff --git a/src/esphome/sensor/sensor.cpp b/src/esphome/sensor/sensor.cpp index 146aa008..e0c81b13 100644 --- a/src/esphome/sensor/sensor.cpp +++ b/src/esphome/sensor/sensor.cpp @@ -160,6 +160,7 @@ const char UNIT_K[] = "K"; const char UNIT_MICROSIEMENS_PER_CENTIMETER[] = "µS/cm"; const char UNIT_MICROGRAMS_PER_CUBIC_METER[] = "µg/m³"; const char ICON_CHEMICAL_WEAPON[] = "mdi:chemical-weapon"; +const char UNIT_PARTICLES_PER_LITER[] = "pcl/L"; SensorStateTrigger::SensorStateTrigger(Sensor *parent) { parent->add_on_state_callback([this](float value) { this->trigger(value); }); diff --git a/src/esphome/sensor/sensor.h b/src/esphome/sensor/sensor.h index 1c61355a..91437fc0 100644 --- a/src/esphome/sensor/sensor.h +++ b/src/esphome/sensor/sensor.h @@ -347,6 +347,7 @@ extern const char UNIT_DEGREES[]; extern const char UNIT_K[]; extern const char UNIT_MICROSIEMENS_PER_CENTIMETER[]; extern const char UNIT_MICROGRAMS_PER_CUBIC_METER[]; +extern const char UNIT_PARTICLES_PER_LITER[]; template SensorInRangeCondition *Sensor::make_sensor_in_range_condition() { return new SensorInRangeCondition(this); From bc5c738301af0049e05b3aa874e7e807049c7215 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 13:48:48 +0200 Subject: [PATCH 51/76] adapt ++ --- src/esphome/application.cpp | 4 +- src/esphome/application.h | 2 +- src/esphome/sensor/ppd42x.cpp | 151 +++++++++++----------------------- src/esphome/sensor/ppd42x.h | 23 +++--- 4 files changed, 65 insertions(+), 115 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 5584e2d9..f6a6bb65 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1090,8 +1090,8 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_ppd42x(uint32_t update_interval, sensor::PPD42XType type) { - return this->register_component(new PPD42XComponent(update_interval, type)); +sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type, uint32_t update_interval) { + return this->register_component(new PPD42XComponent(type, update_interval)); } #endif diff --git a/src/esphome/application.h b/src/esphome/application.h index c78cf8b7..861395f9 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,7 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(uint32_t update_interval = 60000, sensor::PPD42XType type); + sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type, uint32_t update_interval = 60000); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index c12a0a91..02447b8b 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,132 +13,77 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); - if (now - this->last_transmission_ >= 500) { - // last transmission too long ago. Reset RX index. - this->data_index_ = 0; - } - - if (this->available() == 0) - return; - - this->last_transmission_ = now; - while (this->available() != 0) { - this->read_byte(&this->data_[this->data_index_]); - auto check = this->check_byte_(); - if (!check.has_value()) { - // finished - this->parse_data_(); - this->data_index_ = 0; - } else if (!*check) { - // wrong data - this->data_index_ = 0; - } else { - // next byte - this->data_index_++; - } + uint32_t duration_pl_02_5 = + pulseIn(this->pl_02_5_pin_->get_pin(), uint8_t(!this->pl_02_5_pin_->is_inverted()), this->timeout_us_); + uint32_t duration_pl_10_0 = + pulseIn(this->pl_10_0_pin_->get_pin(), uint8_t(!this->pl_10_0_pin_->is_inverted()), this->timeout_us_); + this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; + this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; + + if (now - this->starttime_ > this->timeout_us_) { + // last transmission too long ago + this->starttime_ = now; + parse_data_(); } } float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } -optional PPD42XComponent::check_byte_() { - uint8_t index = this->data_index_; - uint8_t byte = this->data_[index]; - - if (index == 0) - return byte == 0x42; - - if (index == 1) - return byte == 0x4D; - - if (index == 2) - return true; - - uint16_t payload_length = this->get_16_bit_uint_(2); - if (index == 3) { - bool length_matches = false; - switch (this->type_) { - case PPD42X_TYPE: - length_matches = payload_length == 28 || payload_length == 20; - break; - case PPD42X_TYPE_NJ: - length_matches = payload_length == 28; - break; - case PPD42X_TYPE_NS: - length_matches = payload_length == 36; - break; - } - - if (!length_matches) { - ESP_LOGW(TAG, "PPD42X length %u doesn't match. Are you using the correct PPD42X type?", payload_length); - return false; - } - return true; - } - - // start (16bit) + length (16bit) + DATA (payload_length-2 bytes) + checksum (16bit) - uint8_t total_size = 4 + payload_length; - - if (index < total_size - 1) - return true; - - // checksum is without checksum bytes - uint16_t checksum = 0; - for (uint8_t i = 0; i < total_size - 2; i++) - checksum += this->data_[i]; - - uint16_t check = this->get_16_bit_uint_(total_size - 2); - if (checksum != check) { - ESP_LOGW(TAG, "PPD42X checksum mismatch! 0x%02X!=0x%02X", checksum, check); - return false; - } - - return {}; -} void PPD42XComponent::parse_data_() { switch (this->type_) { case PPD42X_TYPE: { - uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); - ESP_LOGD(TAG, "Got PM2.5 Concentration %u pcs/L, PM10.0 Concentration: %u pcs/L", pm_2_5_concentration, - pm_10_0_concentration); - if (this->pm_2_5_sensor_ != nullptr) - this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); - if (this->pm_10_0_sensor_ != nullptr) - this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + pl_10_0_concentration); + if (this->pl_02_5_sensor_ != nullptr) + this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); + if (this->pl_10_0_sensor_ != nullptr) + this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } - case PPD42X_TYPE_NJ: { - uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration); - if (this->pm_2_5_sensor_ != nullptr) - this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); + case PPD42X_TYPE_NS: { + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + pl_10_0_concentration); + if (this->pl_02_5_sensor_ != nullptr) + this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); + if (this->pl_10_0_sensor_ != nullptr) + this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } - case PPD42X_TYPE_NS: { - uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); - ESP_LOGD(TAG, "Got PM2.5 Concentration: %u pcs/L ", pm_2_5_concentration); - if (this->pm_2_5_sensor_ != nullptr) - this->pm_2_5_sensor_->publish_state(pm_2_5_concentration); + } + case PPD42X_TYPE_NJ: { + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + pl_10_0_concentration); + if (this->pl_02_5_sensor_ != nullptr) + this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); + if (this->pl_10_0_sensor_ != nullptr) + this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } } - - this->status_clear_warning(); +} +float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { + float ratio = time_pm / (sample_length * 10.0f); + return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; } uint16_t PPD42XComponent::get_16_bit_uint_(uint8_t start_index) { return (uint16_t(this->data_[start_index]) << 8) | uint16_t(this->data_[start_index + 1]); } PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, const GPIOInputPin &pl) { - return this->pm_2_5_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_02_5); + return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_02_5); } PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name) { - return this->pm_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); + return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent( PPD42XType type) : type_(type) {} +PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval) : type_(type), ui_(update_interval) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); - LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); - LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); + LOG_SENSOR(" ", "PM2.5", this->pl_02_5_sensor_); + LOG_SENSOR(" ", "PM10.0", this->pl_10_0_sensor_); } std::string PPD42XSensor::unit_of_measurement() { @@ -167,6 +112,8 @@ int8_t PPD42XSensor::accuracy_decimals() { return 0; } +void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } + PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin pl, PPD42XSensorType type) : Sensor(name), pl_(pl), type_(type) {} diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 2b95b4a9..2085afa8 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -38,12 +38,12 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType type_; const GPIOInputPin pl_; -}; +}; // class PPD42XSensor class PPD42XComponent : public Component { public: - PPD42XComponent(uint32_t update_interval, PPD42XType type); - + PPD42XComponent(PPD42XType type, uint32_t update_interval); + void set_timeout_us(uint32_t timeout_us); void loop() override; float get_setup_priority() const override; void dump_config() override; @@ -52,17 +52,20 @@ class PPD42XComponent : public Component { PPD42XSensor *make_pl_10_0_sensor(const std::string &name, const GPIOInputPin &pl); protected: - optional check_byte_(); void parse_data_(); - uint16_t get_16_bit_uint_(uint8_t start_index); + static float us_to_pl(uint32_t sample_length, uint32_t time_pm); + + uint32_t timeout_us_{30000}; + uint32_t starttime_{0}; + uint32_t lowpulseoccupancy_02_5_{0}; + uint32_t lowpulseoccupancy_10_0_{0}; - uint8_t data_[64]; - uint8_t data_index_{0}; uint32_t last_transmission_{0}; + uint32_t ui_{60000}; const PPD42XType type_; - PPD42XSensor *pm_2_5_sensor_{nullptr}; - PPD42XSensor *pm_10_0_sensor_{nullptr}; -}; + PPD42XSensor *pl_02_5_sensor_{nullptr}; + PPD42XSensor *pl_10_0_sensor_{nullptr}; +}; // class PPD42XComponent } // namespace sensor From 4e56ff3f3830ef21cdffbb5449936c7328c36997 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 14:05:46 +0200 Subject: [PATCH 52/76] adapt +++ --- src/esphome/sensor/ppd42x.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 02447b8b..4f35d09b 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,10 +13,12 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); + const GPIOInputPin pina = this->pl_02_5_sensor_->pl uint32_t duration_pl_02_5 = - pulseIn(this->pl_02_5_pin_->get_pin(), uint8_t(!this->pl_02_5_pin_->is_inverted()), this->timeout_us_); + pulseIn(pina->get_pin(), uint8_t(!pina->is_inverted()), this->timeout_us_); + const GPIOInputPin pinb = this->pl_10_0_sensor_->pl uint32_t duration_pl_10_0 = - pulseIn(this->pl_10_0_pin_->get_pin(), uint8_t(!this->pl_10_0_pin_->is_inverted()), this->timeout_us_); + pulseIn(pinb->get_pin(), uint8_t(!pinb->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; From 0a4b26c9f0a4e225a9ca574e19bd21d208788046 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 14:12:17 +0200 Subject: [PATCH 53/76] adapt ++++ --- src/esphome/sensor/ppd42x.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 4f35d09b..f8c5b528 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,10 +13,10 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); - const GPIOInputPin pina = this->pl_02_5_sensor_->pl + const GPIOInputPin pina = this->pl_02_5_sensor_->pl_ uint32_t duration_pl_02_5 = pulseIn(pina->get_pin(), uint8_t(!pina->is_inverted()), this->timeout_us_); - const GPIOInputPin pinb = this->pl_10_0_sensor_->pl + const GPIOInputPin pinb = this->pl_10_0_sensor_->pl_ uint32_t duration_pl_10_0 = pulseIn(pinb->get_pin(), uint8_t(!pinb->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; From 920c14df52e401c43e9b3fb1b75829d7a56f74c3 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 14:18:46 +0200 Subject: [PATCH 54/76] adapt +++++ --- src/esphome/sensor/ppd42x.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index f8c5b528..bc34b2e9 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,12 +13,13 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); - const GPIOInputPin pina = this->pl_02_5_sensor_->pl_ uint32_t duration_pl_02_5 = - pulseIn(pina->get_pin(), uint8_t(!pina->is_inverted()), this->timeout_us_); + pulseIn(this->pl_02_5_sensor_->pl_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_->is_inverted()), + this->timeout_us_); const GPIOInputPin pinb = this->pl_10_0_sensor_->pl_ uint32_t duration_pl_10_0 = - pulseIn(pinb->get_pin(), uint8_t(!pinb->is_inverted()), this->timeout_us_); + pulseIn(this->pl_10_0_sensor_->pl_->get_pin(), uint8_t(!this->pl_10_0_sensor_->pl_->is_inverted()), + this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; From 9796be0e49e6896bff208e4ed0b3e2fa14a9c064 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 15:34:47 +0200 Subject: [PATCH 55/76] adapt +++++! --- src/esphome/sensor/ppd42x.cpp | 27 +++++++++++---------------- src/esphome/sensor/ppd42x.h | 13 +++++++------ src/esphome/sensor/sensor.cpp | 2 +- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index bc34b2e9..c7b42b6e 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -14,11 +14,10 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); uint32_t duration_pl_02_5 = - pulseIn(this->pl_02_5_sensor_->pl_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_->is_inverted()), + pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_us_); - const GPIOInputPin pinb = this->pl_10_0_sensor_->pl_ uint32_t duration_pl_10_0 = - pulseIn(this->pl_10_0_sensor_->pl_->get_pin(), uint8_t(!this->pl_10_0_sensor_->pl_->is_inverted()), + pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; @@ -32,7 +31,7 @@ void PPD42XComponent::loop() { float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } void PPD42XComponent::parse_data_() { - switch (this->type_) { + switch (this->ctype_) { case PPD42X_TYPE: { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); @@ -55,7 +54,6 @@ void PPD42XComponent::parse_data_() { this->pl_10_0_sensor_->publish_state(pl_10_0_concentration); break; } - } case PPD42X_TYPE_NJ: { float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); @@ -73,16 +71,13 @@ float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { float ratio = time_pm / (sample_length * 10.0f); return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; } -uint16_t PPD42XComponent::get_16_bit_uint_(uint8_t start_index) { - return (uint16_t(this->data_[start_index]) << 8) | uint16_t(this->data_[start_index + 1]); -} -PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, const GPIOInputPin &pl) { +PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOInputPin *pl) { return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_02_5); } -PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name) { +PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOInputPin *pl) { return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval) : type_(type), ui_(update_interval) {} +PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval) : ctype_(type), ui_(update_interval) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM2.5", this->pl_02_5_sensor_); @@ -90,7 +85,7 @@ void PPD42XComponent::dump_config() { } std::string PPD42XSensor::unit_of_measurement() { - switch (this->type_) { + switch (this->stype_) { case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: return UNIT_PARTICLES_PER_LITER; @@ -98,7 +93,7 @@ std::string PPD42XSensor::unit_of_measurement() { return ""; } std::string PPD42XSensor::icon() { - switch (this->type_) { + switch (this->stype_) { case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: // Not the ideal icon, but Otto can't find a better one ;) @@ -107,7 +102,7 @@ std::string PPD42XSensor::icon() { return ""; } int8_t PPD42XSensor::accuracy_decimals() { - switch (this->type_) { + switch (this->stype_) { case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: return 0; @@ -117,8 +112,8 @@ int8_t PPD42XSensor::accuracy_decimals() { } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin pl, PPD42XSensorType type) - : Sensor(name), pl_(pl), type_(type) {} +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) + : Sensor(name), pl_pin_(pl), stype_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 2085afa8..23ae67a5 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -29,15 +29,16 @@ enum PPD42XSensorType { class PPD42XSensor : public sensor::Sensor { public: - PPD42XSensor(const std::string &name, GPIOInputPin &pl, PPD42XSensorType type); + PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type); std::string unit_of_measurement() override; std::string icon() override; int8_t accuracy_decimals() override; + GPIOInputPin *pl_pin_; protected: - const PPD42XSensorType type_; - const GPIOInputPin pl_; + const PPD42XSensorType stype_; + }; // class PPD42XSensor class PPD42XComponent : public Component { @@ -48,8 +49,8 @@ class PPD42XComponent : public Component { float get_setup_priority() const override; void dump_config() override; - PPD42XSensor *make_pl_02_5_sensor(const std::string &name, const GPIOInputPin &pl); - PPD42XSensor *make_pl_10_0_sensor(const std::string &name, const GPIOInputPin &pl); + PPD42XSensor *make_pl_02_5_sensor(const std::string &name, GPIOInputPin *pl); + PPD42XSensor *make_pl_10_0_sensor(const std::string &name, GPIOInputPin *pl); protected: void parse_data_(); @@ -62,7 +63,7 @@ class PPD42XComponent : public Component { uint32_t last_transmission_{0}; uint32_t ui_{60000}; - const PPD42XType type_; + const PPD42XType ctype_; PPD42XSensor *pl_02_5_sensor_{nullptr}; PPD42XSensor *pl_10_0_sensor_{nullptr}; }; // class PPD42XComponent diff --git a/src/esphome/sensor/sensor.cpp b/src/esphome/sensor/sensor.cpp index e0c81b13..131328be 100644 --- a/src/esphome/sensor/sensor.cpp +++ b/src/esphome/sensor/sensor.cpp @@ -160,7 +160,7 @@ const char UNIT_K[] = "K"; const char UNIT_MICROSIEMENS_PER_CENTIMETER[] = "µS/cm"; const char UNIT_MICROGRAMS_PER_CUBIC_METER[] = "µg/m³"; const char ICON_CHEMICAL_WEAPON[] = "mdi:chemical-weapon"; -const char UNIT_PARTICLES_PER_LITER[] = "pcl/L"; +const char UNIT_PARTICLES_PER_LITER[] = "pcs/l"; SensorStateTrigger::SensorStateTrigger(Sensor *parent) { parent->add_on_state_callback([this](float value) { this->trigger(value); }); From c588de926639883c6e1ad4bcdc5bff0a17c80b69 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 15:54:34 +0200 Subject: [PATCH 56/76] adapt +++++git add . --- src/esphome/sensor/ppd42x.cpp | 10 +++++----- src/esphome/sensor/ppd42x.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index c7b42b6e..7e20a11f 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -13,11 +13,11 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); - uint32_t duration_pl_02_5 = - pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), + uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), + uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_us_); - uint32_t duration_pl_10_0 = - pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), + uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), + uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; @@ -113,7 +113,7 @@ int8_t PPD42XSensor::accuracy_decimals() { void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) - : Sensor(name), pl_pin_(pl), stype_(type) {} + : Sensor(name), pl_pin_(pl), stype_(type) {} } // namespace sensor diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 23ae67a5..e2abb087 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -39,7 +39,7 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType stype_; -}; // class PPD42XSensor +}; // class PPD42XSensor class PPD42XComponent : public Component { public: @@ -66,7 +66,7 @@ class PPD42XComponent : public Component { const PPD42XType ctype_; PPD42XSensor *pl_02_5_sensor_{nullptr}; PPD42XSensor *pl_10_0_sensor_{nullptr}; -}; // class PPD42XComponent +}; // class PPD42XComponent } // namespace sensor From ea5b175417a6de98f7764d80cb3f80b1f053b97c Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 16:14:58 +0200 Subject: [PATCH 57/76] adapt ++++git add .git add .git add . --- src/esphome/sensor/ppd42x.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 7e20a11f..7e9b5d3d 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -14,11 +14,9 @@ static const char *TAG = "sensor.ppd42x"; void PPD42XComponent::loop() { const uint32_t now = millis(); uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), - this->timeout_us_); + uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_us_); uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), - this->timeout_us_); + uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_us_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; @@ -28,6 +26,7 @@ void PPD42XComponent::loop() { parse_data_(); } } + float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } void PPD42XComponent::parse_data_() { @@ -67,16 +66,20 @@ void PPD42XComponent::parse_data_() { } } } + float PPD42XComponent::us_to_pl(uint32_t sample_length, uint32_t time_pm) { float ratio = time_pm / (sample_length * 10.0f); return 1.1f * powf(ratio, 3) - 3.8f * powf(ratio, 2) + 520.0f * ratio + 0.62f; } + PPD42XSensor *PPD42XComponent::make_pl_02_5_sensor(const std::string &name, GPIOInputPin *pl) { return this->pl_02_5_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_02_5); } + PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIOInputPin *pl) { return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } + PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval) : ctype_(type), ui_(update_interval) {} void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); @@ -92,6 +95,7 @@ std::string PPD42XSensor::unit_of_measurement() { } return ""; } + std::string PPD42XSensor::icon() { switch (this->stype_) { case PPD42X_SENSOR_TYPE_PM_02_5: @@ -101,20 +105,19 @@ std::string PPD42XSensor::icon() { } return ""; } + int8_t PPD42XSensor::accuracy_decimals() { switch (this->stype_) { case PPD42X_SENSOR_TYPE_PM_02_5: case PPD42X_SENSOR_TYPE_PM_10_0: return 0; } - return 0; } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) : Sensor(name), pl_pin_(pl), stype_(type) {} - } // namespace sensor ESPHOME_NAMESPACE_END From 491fb947960cbf1fcff8913c742dd2f7e31f277a Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 16:54:13 +0200 Subject: [PATCH 58/76] adapt ++++++ --- src/esphome/sensor/ppd42x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 7e9b5d3d..e88ed61e 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -116,7 +116,7 @@ int8_t PPD42XSensor::accuracy_decimals() { } void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } -PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) +PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) : Sensor(name), pl_pin_(pl), stype_(type) {} } // namespace sensor From d65557c56599bec585f6013222694ad871092218 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 17:39:30 +0200 Subject: [PATCH 59/76] adapt +++++++ --- src/esphome/sensor/ppd42x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index e2abb087..d8b812e5 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -38,7 +38,7 @@ class PPD42XSensor : public sensor::Sensor { protected: const PPD42XSensorType stype_; - + }; // class PPD42XSensor class PPD42XComponent : public Component { From 52058f36a3e27cb5880b122d016435dee23b0bdf Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 19:57:02 +0200 Subject: [PATCH 60/76] includ otto remarks --- src/esphome/sensor/ppd42x.cpp | 34 +++++++++++++++++++--------------- src/esphome/sensor/ppd42x.h | 6 +++--- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index e88ed61e..1f07a1a9 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -11,16 +11,20 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; -void PPD42XComponent::loop() { +PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out) + : ctype_(type), ui_(update_interval), time_out_ms_(time_out) {} + + +void PPD42XComponent::update() { const uint32_t now = millis(); uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_us_); + uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_us_); + uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; - if (now - this->starttime_ > this->timeout_us_) { + if ((now - this->starttime_) > this->timeout_ms_) { // last transmission too long ago this->starttime_ = now; parse_data_(); @@ -32,9 +36,9 @@ float PPD42XComponent::get_setup_priority() const { return setup_priority::HARDW void PPD42XComponent::parse_data_() { switch (this->ctype_) { case PPD42X_TYPE: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_ms_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_ms_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %.0f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -43,9 +47,9 @@ void PPD42XComponent::parse_data_() { break; } case PPD42X_TYPE_NS: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_ms_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_ms_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %.0f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -54,9 +58,9 @@ void PPD42XComponent::parse_data_() { break; } case PPD42X_TYPE_NJ: { - float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_us_); - float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_us_); - ESP_LOGD(TAG, "Got PM2.5 Concentration %f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, + float pl_02_5_concentration = us_to_pl(this->lowpulseoccupancy_02_5_, this->timeout_ms_); + float pl_10_0_concentration = us_to_pl(this->lowpulseoccupancy_10_0_, this->timeout_ms_); + ESP_LOGD(TAG, "Got PM2.5 Concentration %.0f pcs/L, PM10.0 Concentration: %f pcs/L", pl_02_5_concentration, pl_10_0_concentration); if (this->pl_02_5_sensor_ != nullptr) this->pl_02_5_sensor_->publish_state(pl_02_5_concentration); @@ -80,7 +84,7 @@ PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIO return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } -PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval) : ctype_(type), ui_(update_interval) {} + void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM2.5", this->pl_02_5_sensor_); @@ -114,7 +118,7 @@ int8_t PPD42XSensor::accuracy_decimals() { } return 0; } -void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } +// void PPD42XComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_ms_ = timeout_us; } PPD42XSensor::PPD42XSensor(const std::string &name, GPIOInputPin *pl, PPD42XSensorType type) : Sensor(name), pl_pin_(pl), stype_(type) {} diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index d8b812e5..1a0ac988 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -43,7 +43,7 @@ class PPD42XSensor : public sensor::Sensor { class PPD42XComponent : public Component { public: - PPD42XComponent(PPD42XType type, uint32_t update_interval); + PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); void loop() override; float get_setup_priority() const override; @@ -56,13 +56,13 @@ class PPD42XComponent : public Component { void parse_data_(); static float us_to_pl(uint32_t sample_length, uint32_t time_pm); - uint32_t timeout_us_{30000}; + uint32_t timeout_ms_{30}; uint32_t starttime_{0}; uint32_t lowpulseoccupancy_02_5_{0}; uint32_t lowpulseoccupancy_10_0_{0}; uint32_t last_transmission_{0}; - uint32_t ui_{60000}; + uint32_t ui_{0}; const PPD42XType ctype_; PPD42XSensor *pl_02_5_sensor_{nullptr}; PPD42XSensor *pl_10_0_sensor_{nullptr}; From a7b16b12d7fc16510245f38a2fc2111415b424e7 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 20:26:06 +0200 Subject: [PATCH 61/76] includ otto remarks --- src/esphome/application.cpp | 5 +++-- src/esphome/sensor/ppd42x.cpp | 4 +--- src/esphome/sensor/ppd42x.h | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index f6a6bb65..4fe84a6b 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1090,8 +1090,9 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #endif #ifdef USE_PPD42X -sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type, uint32_t update_interval) { - return this->register_component(new PPD42XComponent(type, update_interval)); +sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type, uint32_t update_interval, + uint32_t timed_out) { + return this->register_component(new PPD42XComponent(type, update_interval, time_out)); } #endif diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 1f07a1a9..c4400684 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -12,8 +12,7 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out) - : ctype_(type), ui_(update_interval), time_out_ms_(time_out) {} - + : ctype_(type), ui_(update_interval), timeout_ms_(time_out) {} void PPD42XComponent::update() { const uint32_t now = millis(); @@ -84,7 +83,6 @@ PPD42XSensor *PPD42XComponent::make_pl_10_0_sensor(const std::string &name, GPIO return this->pl_10_0_sensor_ = new PPD42XSensor(name, pl, PPD42X_SENSOR_TYPE_PM_10_0); } - void PPD42XComponent::dump_config() { ESP_LOGCONFIG(TAG, "PPD42X:"); LOG_SENSOR(" ", "PM2.5", this->pl_02_5_sensor_); diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 1a0ac988..457f8cc6 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -45,7 +45,7 @@ class PPD42XComponent : public Component { public: PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); - void loop() override; + void update() override; float get_setup_priority() const override; void dump_config() override; From 38dff4d6859fc4971638aff0d234b81a7e6dd546 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 20:44:03 +0200 Subject: [PATCH 62/76] includ otto remarks --- src/esphome/sensor/ppd42x.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 457f8cc6..857c0789 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -7,7 +7,6 @@ #include "esphome/component.h" #include "esphome/sensor/sensor.h" -#include "esphome/uart_component.h" #include "esphome/helpers.h" ESPHOME_NAMESPACE_BEGIN @@ -41,7 +40,7 @@ class PPD42XSensor : public sensor::Sensor { }; // class PPD42XSensor -class PPD42XComponent : public Component { +class PPD42XComponent : public , PollingComponent { public: PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); From 6420022d7c681569f45663bc6d57afe25e82b79c Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 20:50:36 +0200 Subject: [PATCH 63/76] includ otto remarks --- src/esphome/sensor/ppd42x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 857c0789..dae36ab2 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -40,7 +40,7 @@ class PPD42XSensor : public sensor::Sensor { }; // class PPD42XSensor -class PPD42XComponent : public , PollingComponent { +class PPD42XComponent : public PollingComponent { public: PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); From 5b80aa8dadf6c2dcb6cfcc8952cc74122d3948e5 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:04:30 +0200 Subject: [PATCH 64/76] includ otto remarks --- src/esphome/application.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index 861395f9..2016a138 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,7 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type, uint32_t update_interval = 60000); + sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type, uint32_t update_interval = 60000, time_out = 30000); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR From 9aab8964e7b364accc6a4f627e09395cced00286 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:15:51 +0200 Subject: [PATCH 65/76] includ otto remarks --- src/esphome/application.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/esphome/application.h b/src/esphome/application.h index 2016a138..6a3e98fe 100644 --- a/src/esphome/application.h +++ b/src/esphome/application.h @@ -812,7 +812,8 @@ class Application { sensor::PMSX003Component *make_pmsx003(UARTComponent *parent, sensor::PMSX003Type type); #endif #ifdef USE_PPD42X - sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type, uint32_t update_interval = 60000, time_out = 30000); + sensor::PPD42XComponent *make_ppd42x(sensor::PPD42XType type, uint32_t update_interval = 60000, + uint32_t time_out = 30000); #endif #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR From b9fdb4efeb9f94218a51f52fdf36ea7df972f889 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:21:53 +0200 Subject: [PATCH 66/76] includ otto remarks --- src/esphome/application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 4fe84a6b..16266e89 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1091,7 +1091,7 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #ifdef USE_PPD42X sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type, uint32_t update_interval, - uint32_t timed_out) { + uint32_t time_out) { return this->register_component(new PPD42XComponent(type, update_interval, time_out)); } #endif From c6075cdf4bd9c65482115d99dcaae30246404989 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:30:26 +0200 Subject: [PATCH 67/76] includ otto remarks --- src/esphome/sensor/ppd42x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index dae36ab2..2586d134 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -40,7 +40,7 @@ class PPD42XSensor : public sensor::Sensor { }; // class PPD42XSensor -class PPD42XComponent : public PollingComponent { +class PPD42XComponent : public Component { public: PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); From f582d7a227501b03af1c7ab0686e8b6d8b2a10b6 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:54:55 +0200 Subject: [PATCH 68/76] includ otto remarks --- src/esphome/sensor/ppd42x.cpp | 27 +++++++++++++++------------ src/esphome/sensor/ppd42x.h | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index c4400684..dd955931 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -14,19 +14,22 @@ static const char *TAG = "sensor.ppd42x"; PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out) : ctype_(type), ui_(update_interval), timeout_ms_(time_out) {} -void PPD42XComponent::update() { +void PPD42XComponent::loop() { const uint32_t now = millis(); - uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); - uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), - uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); - this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; - this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; - - if ((now - this->starttime_) > this->timeout_ms_) { - // last transmission too long ago - this->starttime_ = now; - parse_data_(); + if(){ ((now - this->starttime_) > this->ui_) + + uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), + uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); + uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), + uint8_t(!this->pl_10_0_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); + this->lowpulseoccupancy_02_5_ = this->lowpulseoccupancy_02_5_ + duration_pl_02_5; + this->lowpulseoccupancy_10_0_ = this->lowpulseoccupancy_10_0_ + duration_pl_10_0; + + if ((now - this->starttime_) > this->timeout_ms_) { + // last transmission too long ago + this->starttime_ = now; + parse_data_(); + } } } diff --git a/src/esphome/sensor/ppd42x.h b/src/esphome/sensor/ppd42x.h index 2586d134..f17f38ad 100644 --- a/src/esphome/sensor/ppd42x.h +++ b/src/esphome/sensor/ppd42x.h @@ -44,7 +44,7 @@ class PPD42XComponent : public Component { public: PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out); void set_timeout_us(uint32_t timeout_us); - void update() override; + void loop() override; float get_setup_priority() const override; void dump_config() override; From 5dea20f5f4e71da0f5161b80ea77ea59b54fa246 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 21:58:21 +0200 Subject: [PATCH 69/76] includ otto remarks --- src/esphome/sensor/ppd42x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index dd955931..e7fb4251 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -16,7 +16,7 @@ PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint void PPD42XComponent::loop() { const uint32_t now = millis(); - if(){ ((now - this->starttime_) > this->ui_) + if ((now - this->starttime_) > this->ui_) { uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); From 5b92680a9398940149082c4fe1260e58e02a926f Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 22:09:56 +0200 Subject: [PATCH 70/76] includ otto remarks --- src/esphome/sensor/ppd42x.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index e7fb4251..8b740ec8 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -17,7 +17,6 @@ PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint void PPD42XComponent::loop() { const uint32_t now = millis(); if ((now - this->starttime_) > this->ui_) { - uint32_t duration_pl_02_5 = pulseIn(this->pl_02_5_sensor_->pl_pin_->get_pin(), uint8_t(!this->pl_02_5_sensor_->pl_pin_->is_inverted()), this->timeout_ms_); uint32_t duration_pl_10_0 = pulseIn(this->pl_10_0_sensor_->pl_pin_->get_pin(), From 20c873971c50dca8d2ac34861dc575dc64327083 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 7 Apr 2019 22:25:30 +0200 Subject: [PATCH 71/76] include otto remarks --- src/esphome/sensor/ppd42x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/sensor/ppd42x.cpp b/src/esphome/sensor/ppd42x.cpp index 8b740ec8..0bcf8a39 100644 --- a/src/esphome/sensor/ppd42x.cpp +++ b/src/esphome/sensor/ppd42x.cpp @@ -11,7 +11,7 @@ namespace sensor { static const char *TAG = "sensor.ppd42x"; -PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out) +PPD42XComponent::PPD42XComponent(PPD42XType type, uint32_t update_interval, uint32_t time_out) : ctype_(type), ui_(update_interval), timeout_ms_(time_out) {} void PPD42XComponent::loop() { From 6eebc5b2fa5c9c63077ac8f5e8de602da5d07289 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Wed, 10 Apr 2019 20:43:32 +0200 Subject: [PATCH 72/76] include otto remarks --- src/esphome/application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esphome/application.cpp b/src/esphome/application.cpp index 16266e89..889b7b41 100644 --- a/src/esphome/application.cpp +++ b/src/esphome/application.cpp @@ -1092,7 +1092,7 @@ sensor::PMSX003Component *Application::make_pmsx003(UARTComponent *parent, senso #ifdef USE_PPD42X sensor::PPD42XComponent *Application::make_ppd42x(sensor::PPD42XType type, uint32_t update_interval, uint32_t time_out) { - return this->register_component(new PPD42XComponent(type, update_interval, time_out)); + return this->register_component(new PPD42XComponent(type, time_out, update_interval)); } #endif From d1fdc6745da61fd52ffd7e7533dff633bad3102a Mon Sep 17 00:00:00 2001 From: pi4homez <48885059+pi4homez@users.noreply.github.com> Date: Sun, 14 Apr 2019 11:36:46 +0200 Subject: [PATCH 73/76] Update defines.h --- src/esphome/defines.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 1b5bf7a1..7de3cf83 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -125,6 +125,8 @@ #define USE_CSE7766 #define USE_PMSX003 #define USE_PPD42X +#define USE_ENDSTOP_COVER +#define USE_TIME_BASED_COVER #define USE_STEPPER #define USE_A4988 #define USE_ULN2003 From 0146dd543564a532bc0386608f786e5b56b07a3f Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 14 Apr 2019 14:06:49 +0200 Subject: [PATCH 74/76] res conflict --- src/esphome/defines.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 7de3cf83..7a32b295 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -124,7 +124,6 @@ #define USE_MQTT_SUBSCRIBE_SENSOR #define USE_CSE7766 #define USE_PMSX003 -#define USE_PPD42X #define USE_ENDSTOP_COVER #define USE_TIME_BASED_COVER #define USE_STEPPER From 3f7d1d6dbcfff5cbdebb01372df618bd34d6c955 Mon Sep 17 00:00:00 2001 From: pi4homez Date: Sun, 14 Apr 2019 14:51:59 +0200 Subject: [PATCH 75/76] res conflict --- src/esphome/defines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 7a32b295..7de3cf83 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -124,6 +124,7 @@ #define USE_MQTT_SUBSCRIBE_SENSOR #define USE_CSE7766 #define USE_PMSX003 +#define USE_PPD42X #define USE_ENDSTOP_COVER #define USE_TIME_BASED_COVER #define USE_STEPPER From feef5e2d7f3a707520b467b98694bb26ae276660 Mon Sep 17 00:00:00 2001 From: pi4homez <48885059+pi4homez@users.noreply.github.com> Date: Sun, 14 Apr 2019 15:09:20 +0200 Subject: [PATCH 76/76] conflict conflict --- src/esphome/defines.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esphome/defines.h b/src/esphome/defines.h index 7de3cf83..7a32b295 100644 --- a/src/esphome/defines.h +++ b/src/esphome/defines.h @@ -124,7 +124,6 @@ #define USE_MQTT_SUBSCRIBE_SENSOR #define USE_CSE7766 #define USE_PMSX003 -#define USE_PPD42X #define USE_ENDSTOP_COVER #define USE_TIME_BASED_COVER #define USE_STEPPER