-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add HM330X PM Sensor #11026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
oscgonfer
wants to merge
2
commits into
meshtastic:master
Choose a base branch
from
oscgonfer:feat/add-hm330x
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add HM330X PM Sensor #11026
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,7 @@ class ScanI2C | |
| SCD30, | ||
| ADS1115, | ||
| SPA06, | ||
| HM330X | ||
| } DeviceType; | ||
|
|
||
| // typedef uint8_t DeviceAddress; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR && __has_include(<Seeed_HM330X.h>) | ||
|
|
||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "HM330XSensor.h" | ||
| #include "../detect/reClockI2C.h" | ||
|
|
||
| HM330XSensor::HM330XSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_HM330X, "HM330X"){}; | ||
|
|
||
| bool HM330XSensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) | ||
| { | ||
| LOG_INFO("Init sensor: %s", sensorName); | ||
|
|
||
| _bus = bus; | ||
| _address = dev->address.address; | ||
|
|
||
| #ifdef HM330X_I2C_CLOCK_SPEED | ||
| #ifdef CAN_RECLOCK_I2C | ||
| uint32_t currentClock = reClockI2C(HM330X_I2C_CLOCK_SPEED, _bus, false); | ||
| #elif !HAS_SCREEN | ||
| reClockI2C(HM330X_I2C_CLOCK_SPEED, _bus, true); | ||
| #else | ||
| LOG_WARN("%s can't be used at this clock speed, with a screen", sensorName); | ||
| return false; | ||
| #endif /* CAN_RECLOCK_I2C */ | ||
| #endif /* HM330X_I2C_CLOCK_SPEED */ | ||
|
|
||
| if (hm330x.init(_bus) != HM330XErrorCode::NO_ERROR) { | ||
| #if defined(HM330X_I2C_CLOCK_SPEED) && defined(CAN_RECLOCK_I2C) | ||
| reClockI2C(currentClock, _bus, false); | ||
| #endif | ||
| LOG_WARN("%s error in sensor init", sensorName); | ||
| return false; | ||
| } | ||
|
|
||
| #if defined(HM330X_I2C_CLOCK_SPEED) && defined(CAN_RECLOCK_I2C) | ||
| reClockI2C(currentClock, _bus, false); | ||
| #endif | ||
|
|
||
| status = 1; | ||
| LOG_INFO("%s Enabled", sensorName); | ||
|
|
||
| initI2CSensor(); | ||
| return true; | ||
| }; | ||
|
|
||
| uint32_t HM330XSensor::wakeUp() | ||
| { | ||
| state = State::ACTIVE; | ||
| measureStarted = getTime(); | ||
| return HM330X_WARMUP_MS; | ||
| } | ||
|
|
||
| int32_t HM330XSensor::wakeUpTimeMs() | ||
| { | ||
| return HM330X_WARMUP_MS; | ||
| } | ||
|
|
||
| bool HM330XSensor::canSleep() | ||
| { | ||
| // Sleep not available in this sensor | ||
| return false; | ||
| } | ||
|
|
||
| bool HM330XSensor::isActive() | ||
| { | ||
| return state == State::ACTIVE; | ||
| } | ||
|
|
||
| int32_t HM330XSensor::pendingForReadyMs() | ||
| { | ||
| uint32_t now; | ||
| now = getTime(); | ||
| uint32_t sincePMMeasureStarted = (now - measureStarted) * 1000; | ||
| LOG_DEBUG("%s: Since measure started: %ums", sensorName, sincePMMeasureStarted); | ||
|
|
||
| if (sincePMMeasureStarted < HM330X_WARMUP_MS) { | ||
| LOG_INFO("%s: not enough time passed since starting measurement", sensorName); | ||
| return HM330X_WARMUP_MS - sincePMMeasureStarted; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| bool HM330XSensor::getMetrics(meshtastic_Telemetry *measurement) | ||
| { | ||
| #ifdef HM330X_I2C_CLOCK_SPEED | ||
| #ifdef CAN_RECLOCK_I2C | ||
| uint32_t currentClock = reClockI2C(HM330X_I2C_CLOCK_SPEED, _bus, false); | ||
| #elif !HAS_SCREEN | ||
| reClockI2C(HM330X_I2C_CLOCK_SPEED, _bus, true); | ||
| #else | ||
| LOG_WARN("%s can't be used at this clock speed, with a screen", sensorName); | ||
| return false; | ||
| #endif /* CAN_RECLOCK_I2C */ | ||
| #endif /* HM330X_I2C_CLOCK_SPEED */ | ||
|
|
||
| if (hm330x.read_sensor_value(buffer, 29)) { | ||
| LOG_WARN("%s: read result failed", sensorName); | ||
| #if defined(HM330X_I2C_CLOCK_SPEED) && defined(CAN_RECLOCK_I2C) | ||
| reClockI2C(currentClock, _bus, false); | ||
| #endif | ||
| return false; | ||
| } | ||
|
|
||
| #if defined(HM330X_I2C_CLOCK_SPEED) && defined(CAN_RECLOCK_I2C) | ||
| reClockI2C(currentClock, _bus, false); | ||
| #endif | ||
|
|
||
| if (hm330x.checksum_calc(buffer) != HM330XErrorCode::NO_ERROR) { | ||
| LOG_ERROR("%s: Checksum error", sensorName); | ||
| return false; | ||
| } | ||
|
|
||
| auto read16 = [](const uint8_t *data, uint8_t idx) -> uint16_t { return (data[idx] << 8) | data[idx + 1]; }; | ||
|
|
||
| // TODO - This is suspiciously not in terms of PM1, PM2.5 and PM10.0 relationship | ||
| // Normally, PM1.0 > PM2.5 > PM10, but in this case it's not. Are the buckets cumulative? | ||
| // It's not documented in the datasheet, so safely assuming that it's correct as in the datasheet | ||
| measurement->variant.air_quality_metrics.has_pm10_standard = true; | ||
| measurement->variant.air_quality_metrics.has_pm25_standard = true; | ||
| measurement->variant.air_quality_metrics.has_pm100_standard = true; | ||
|
|
||
| measurement->variant.air_quality_metrics.pm10_standard = read16(buffer, 4); | ||
| measurement->variant.air_quality_metrics.pm25_standard = read16(buffer, 6); | ||
| measurement->variant.air_quality_metrics.pm100_standard = read16(buffer, 8); | ||
|
|
||
| // TODO - Add admin command to remove environmental metrics to save protobuf space | ||
| // TODO - Decide if these should be enabled | ||
| // measurement->variant.air_quality_metrics.has_pm10_environmental = true; | ||
| // measurement->variant.air_quality_metrics.has_pm25_environmental = true; | ||
| // measurement->variant.air_quality_metrics.has_pm100_environmental = true; | ||
|
|
||
| // measurement->variant.air_quality_metrics.pm10_environmental = read16(buffer, 10); | ||
| // measurement->variant.air_quality_metrics.pm25_environmental = read16(buffer, 12); | ||
| // measurement->variant.air_quality_metrics.pm100_environmental = read16(buffer, 14); | ||
|
|
||
| LOG_DEBUG("%s: Got readings: pM1p0_standard=%u, pM2p5_standard=%u, pM10p0_standard=%u", sensorName, | ||
| measurement->variant.air_quality_metrics.pm10_standard, measurement->variant.air_quality_metrics.pm25_standard, | ||
| measurement->variant.air_quality_metrics.pm100_standard); | ||
|
|
||
| return true; | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR && __has_include(<Seeed_HM330X.h>) | ||
|
|
||
| #include "../detect/reClockI2C.h" | ||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "RTC.h" | ||
| #include "TelemetrySensor.h" | ||
| #include <Seeed_HM330X.h> | ||
|
|
||
| #define HM330X_I2C_CLOCK_SPEED 400000 | ||
| #define HM330X_WARMUP_MS 30000 | ||
| #define HM330X_NO_ERROR 0 | ||
| #define HM330X_FRAME_LENGTH 30 | ||
|
|
||
| class HM330XSensor : public TelemetrySensor | ||
| { | ||
| private: | ||
| enum class State { IDLE, ACTIVE }; | ||
| State state = State::IDLE; | ||
| uint32_t measureStarted = 0; | ||
| uint8_t buffer[HM330X_FRAME_LENGTH]{}; | ||
| TwoWire *_bus{}; | ||
| uint8_t _address{}; | ||
|
|
||
| HM330X hm330x; | ||
| HM330XErrorCode checksum(uint8_t* data); | ||
|
|
||
| public: | ||
| HM330XSensor(); | ||
| virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override; | ||
| virtual bool getMetrics(meshtastic_Telemetry *measurement) override; | ||
|
|
||
| virtual bool isActive() override; | ||
| // virtual void sleep() override; // no need for this on this sensor | ||
| virtual uint32_t wakeUp() override; | ||
| virtual bool canSleep() override; | ||
| virtual int32_t wakeUpTimeMs() override; | ||
| virtual int32_t pendingForReadyMs() override; | ||
| }; | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 20253
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 8147
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 50375
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 252
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 19542
🏁 Script executed:
Repository: meshtastic/firmware
Length of output: 22122
Keep
canSleep()true for HM330XAirQualityTelemetryModuleonly callswakeUp()andpendingForReadyMs()when this returns true, so returning false skips the warmup gate and the sensor is read immediately. Leavesleep()as a no-op if needed, but don’t block readiness tracking.🤖 Prompt for AI Agents