mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 16:55:49 +00:00
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
109 lines
2.8 KiB
C++
109 lines
2.8 KiB
C++
#ifdef USE_ARDUINO
|
|
|
|
#include "esphome/core/log.h"
|
|
#include "hm3301.h"
|
|
|
|
namespace esphome {
|
|
namespace hm3301 {
|
|
|
|
static const char *const TAG = "hm3301.sensor";
|
|
|
|
static const uint8_t PM_1_0_VALUE_INDEX = 5;
|
|
static const uint8_t PM_2_5_VALUE_INDEX = 6;
|
|
static const uint8_t PM_10_0_VALUE_INDEX = 7;
|
|
|
|
void HM3301Component::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up HM3301...");
|
|
hm3301_ = make_unique<HM330X>();
|
|
error_code_ = hm3301_->init();
|
|
if (error_code_ != NO_ERROR) {
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
}
|
|
|
|
void HM3301Component::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "HM3301:");
|
|
LOG_I2C_DEVICE(this);
|
|
if (error_code_ == ERROR_COMM) {
|
|
ESP_LOGE(TAG, "Communication with HM3301 failed!");
|
|
}
|
|
|
|
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(" ", "AQI", this->aqi_sensor_);
|
|
}
|
|
|
|
float HM3301Component::get_setup_priority() const { return setup_priority::DATA; }
|
|
|
|
void HM3301Component::update() {
|
|
if (!this->read_sensor_value_(data_buffer_)) {
|
|
ESP_LOGW(TAG, "Read result failed");
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
if (!this->validate_checksum_(data_buffer_)) {
|
|
ESP_LOGW(TAG, "Checksum validation failed");
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
int16_t pm_1_0_value = -1;
|
|
if (this->pm_1_0_sensor_ != nullptr) {
|
|
pm_1_0_value = get_sensor_value_(data_buffer_, PM_1_0_VALUE_INDEX);
|
|
}
|
|
|
|
int16_t pm_2_5_value = -1;
|
|
if (this->pm_2_5_sensor_ != nullptr) {
|
|
pm_2_5_value = get_sensor_value_(data_buffer_, PM_2_5_VALUE_INDEX);
|
|
}
|
|
|
|
int16_t pm_10_0_value = -1;
|
|
if (this->pm_10_0_sensor_ != nullptr) {
|
|
pm_10_0_value = get_sensor_value_(data_buffer_, PM_10_0_VALUE_INDEX);
|
|
}
|
|
|
|
int8_t aqi_value = -1;
|
|
if (this->aqi_sensor_ != nullptr && pm_2_5_value != -1 && pm_10_0_value != -1) {
|
|
AbstractAQICalculator *calculator = this->aqi_calculator_factory_.get_calculator(this->aqi_calc_type_);
|
|
aqi_value = calculator->get_aqi(pm_2_5_value, pm_10_0_value);
|
|
}
|
|
|
|
if (pm_1_0_value != -1) {
|
|
this->pm_1_0_sensor_->publish_state(pm_1_0_value);
|
|
}
|
|
if (pm_2_5_value != -1) {
|
|
this->pm_2_5_sensor_->publish_state(pm_2_5_value);
|
|
}
|
|
if (pm_10_0_value != -1) {
|
|
this->pm_10_0_sensor_->publish_state(pm_10_0_value);
|
|
}
|
|
if (aqi_value != -1) {
|
|
this->aqi_sensor_->publish_state(aqi_value);
|
|
}
|
|
|
|
this->status_clear_warning();
|
|
}
|
|
|
|
bool HM3301Component::read_sensor_value_(uint8_t *data) { return !hm3301_->read_sensor_value(data, 29); }
|
|
|
|
bool HM3301Component::validate_checksum_(const uint8_t *data) {
|
|
uint8_t sum = 0;
|
|
for (int i = 0; i < 28; i++) {
|
|
sum += data[i];
|
|
}
|
|
|
|
return sum == data[28];
|
|
}
|
|
|
|
uint16_t HM3301Component::get_sensor_value_(const uint8_t *data, uint8_t i) {
|
|
return (uint16_t) data[i * 2] << 8 | data[i * 2 + 1];
|
|
}
|
|
|
|
} // namespace hm3301
|
|
} // namespace esphome
|
|
|
|
#endif // USE_ARDUINO
|