-
Notifications
You must be signed in to change notification settings - Fork 30
Add DSM501A driver #243
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
Open
fedirky
wants to merge
1
commit into
iotempire:master
Choose a base branch
from
fedirky:feature/dsm501a-sensor-driver
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.
Open
Add DSM501A driver #243
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| dsm501a | ||
| ======== | ||
|
|
||
| .. code-block:: cpp | ||
|
|
||
| dsm501a(name, pm1_pin, pm25_pin); | ||
|
|
||
| Create a new DSM501A dust sensor device. | ||
|
|
||
| The sensor exposes two pulse outputs. The driver measures low-pulse occupancy | ||
| for a 30 second sample window and publishes estimated concentrations for both | ||
| channels as separate subtopics. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| - ``name``: the name it can be addressed via MQTT in the network. | ||
| Inside the code it can be addressed via IN(name). | ||
|
|
||
| - ``pm1_pin``: where the DSM501A PM1 output pin is connected to | ||
|
|
||
| - ``pm25_pin``: where the DSM501A PM2.5 output pin is connected to | ||
|
|
||
| Outputs | ||
| ------- | ||
|
|
||
| - ``pm1``: estimated PM1 concentration from the PM1 output. | ||
| - ``pm25``: estimated PM2.5 concentration from the PM2.5 output. | ||
|
|
||
| The driver uses a fixed 30 second measurement window, matching the common | ||
| DSM501A low-pulse occupancy calculation pattern. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| .. code-block:: cpp | ||
|
|
||
| dsm501a(dust, D6, D5); | ||
|
|
||
| In this example: | ||
|
|
||
| - ``D6`` reads the DSM501A ``PM1`` output | ||
| - ``D5`` reads the DSM501A ``PM2.5`` output |
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,95 @@ | ||
| // dsm501a.cpp | ||
|
|
||
| // Important to include Functional Interrupt as else it does not allow to | ||
| // define a class-based (object specific) interrupt. | ||
| #include <FunctionalInterrupt.h> | ||
| #include "dev_dsm501a.h" | ||
|
|
||
| DSM501A::DSM501A(const char* name, uint8_t pm1_pin, uint8_t pm25_pin) : | ||
| Device(name, 500000) { | ||
| _pm1_pin = pm1_pin; | ||
| _pm25_pin = pm25_pin; | ||
| add_subdevice(new Subdevice(F("pm1"))); | ||
| add_subdevice(new Subdevice(F("pm25"))); | ||
| } | ||
|
|
||
| void DSM501A::pm1_changed() { | ||
| uint32_t now_us = micros(); | ||
| if(digitalRead(_pm1_pin) == LOW) { | ||
| if(!_pm1_low_active) { | ||
| _pm1_low_active = true; | ||
| _pm1_low_started_us = now_us; | ||
| } | ||
| } else if(_pm1_low_active) { | ||
| _pm1_lowpulse_us += now_us - _pm1_low_started_us; | ||
| _pm1_low_active = false; | ||
| } | ||
| } | ||
|
|
||
| void DSM501A::pm25_changed() { | ||
| uint32_t now_us = micros(); | ||
| if(digitalRead(_pm25_pin) == LOW) { | ||
| if(!_pm25_low_active) { | ||
| _pm25_low_active = true; | ||
| _pm25_low_started_us = now_us; | ||
| } | ||
| } else if(_pm25_low_active) { | ||
| _pm25_lowpulse_us += now_us - _pm25_low_started_us; | ||
| _pm25_low_active = false; | ||
| } | ||
| } | ||
|
|
||
| float DSM501A::calculate_concentration(uint32_t lowpulse_us) const { | ||
| float ratio = (float)lowpulse_us / ((float)_sampletime_ms * 10.0f); | ||
| float concentration = 0.001915f * ratio * ratio + 0.09522f * ratio - 0.04884f; | ||
| if(concentration < 0.0f) concentration = 0.0f; | ||
| return concentration; | ||
| } | ||
|
|
||
| void DSM501A::start() { | ||
| pinMode(_pm1_pin, INPUT); | ||
| pinMode(_pm25_pin, INPUT); | ||
| _sample_started_ms = millis(); | ||
|
|
||
| attachInterrupt(digitalPinToInterrupt(_pm1_pin), [&] () { pm1_changed(); }, CHANGE); | ||
| attachInterrupt(digitalPinToInterrupt(_pm25_pin), [&] () { pm25_changed(); }, CHANGE); | ||
|
|
||
| _started = true; | ||
| } | ||
|
|
||
| bool DSM501A::measure() { | ||
| uint32_t now_ms = millis(); | ||
| if((uint32_t)(now_ms - _sample_started_ms) < _sampletime_ms) { | ||
| return false; | ||
| } | ||
|
|
||
| // Snapshot and reset both low-pulse accumulators at the sample boundary. | ||
| // If a pulse is still active, split it at "now" so the next window keeps | ||
| // measuring seamlessly without losing time. | ||
| uint32_t pm1_lowpulse_us = 0; | ||
| uint32_t pm25_lowpulse_us = 0; | ||
| uint32_t now_us = micros(); | ||
|
|
||
| noInterrupts(); | ||
| pm1_lowpulse_us = _pm1_lowpulse_us; | ||
| pm25_lowpulse_us = _pm25_lowpulse_us; | ||
|
|
||
| if(_pm1_low_active) { | ||
| pm1_lowpulse_us += now_us - _pm1_low_started_us; | ||
| _pm1_low_started_us = now_us; | ||
| } | ||
| if(_pm25_low_active) { | ||
| pm25_lowpulse_us += now_us - _pm25_low_started_us; | ||
| _pm25_low_started_us = now_us; | ||
| } | ||
|
|
||
| _pm1_lowpulse_us = 0; | ||
| _pm25_lowpulse_us = 0; | ||
| interrupts(); | ||
|
|
||
| _sample_started_ms = now_ms; | ||
|
|
||
| value(0).printf("%.3f", calculate_concentration(pm1_lowpulse_us)); | ||
| value(1).printf("%.3f", calculate_concentration(pm25_lowpulse_us)); | ||
| return true; | ||
| } | ||
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,36 @@ | ||
| // dsm501a.h | ||
| // DSM501A dust sensor using low-pulse occupancy on two output pins | ||
|
|
||
| #ifndef _IOTEMPOWER_DSM501A_H_ | ||
| #define _IOTEMPOWER_DSM501A_H_ | ||
|
|
||
| #include <Arduino.h> | ||
| #include <device.h> | ||
|
|
||
| #define DSM501A_DEFAULT_SAMPLETIME_MS 30000 | ||
|
|
||
| class DSM501A : public Device { | ||
| private: | ||
| uint8_t _pm1_pin; | ||
| uint8_t _pm25_pin; | ||
| uint32_t _sampletime_ms = DSM501A_DEFAULT_SAMPLETIME_MS; | ||
| uint32_t _sample_started_ms = 0; | ||
|
|
||
| volatile uint32_t _pm1_lowpulse_us = 0; | ||
| volatile uint32_t _pm25_lowpulse_us = 0; | ||
| volatile uint32_t _pm1_low_started_us = 0; | ||
| volatile uint32_t _pm25_low_started_us = 0; | ||
| volatile bool _pm1_low_active = false; | ||
| volatile bool _pm25_low_active = false; | ||
|
|
||
| void pm1_changed(); | ||
| void pm25_changed(); | ||
| float calculate_concentration(uint32_t lowpulse_us) const; | ||
|
|
||
| public: | ||
| DSM501A(const char* name, uint8_t pm1_pin, uint8_t pm25_pin); | ||
| void start() override; | ||
| bool measure() override; | ||
| }; | ||
|
|
||
| #endif // _IOTEMPOWER_DSM501A_H_ |
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
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.
Ah, that looks dangerous - need to do some serious testing if that doesn't create regression with the led strips (also interrupt critical) - should maybe be made a more global concept? Need to know more before allowing the merge.