-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add AS3935 lightning sensor support #10931
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
ndoo
wants to merge
4
commits into
meshtastic:develop
Choose a base branch
from
meshmy:feat/as3935-lightning-sensor
base: develop
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a0fac16
Add AS3935 lightning sensor support
ndoo 7415f1b
Merge branch 'develop' into feat/as3935-lightning-sensor
caveman99 3d92d4b
Merge branch 'develop' into feat/as3935-lightning-sensor
caveman99 647572e
Merge branch 'develop' into feat/as3935-lightning-sensor
caveman99 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
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
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,121 @@ | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<SparkFun_AS3935.h>) | ||
|
|
||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "AS3935Sensor.h" | ||
| #include "TelemetrySensor.h" | ||
| #include "modules/Telemetry/EnvironmentTelemetry.h" | ||
| #include <SparkFun_AS3935.h> | ||
| #include <Throttle.h> | ||
|
|
||
| namespace | ||
| { | ||
| // No attachInterrupt(): the IRQ line stays asserted until read, so polling can't miss it, | ||
| // and the I2C read itself isn't ISR-safe anyway. | ||
| constexpr int32_t AS3935_CHECK_INTERVAL_MS = DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS; | ||
| constexpr uint8_t AS3935_DISTANCE_OUT_OF_RANGE = 0x3F; | ||
| // Strikes accumulate over a rolling window, reset by elapsed time rather than on | ||
| // getMetrics() (which also fires when replying to a peer's telemetry request). | ||
| constexpr uint32_t AS3935_STRIKE_WINDOW_MS = 60UL * 60UL * 1000; // 1 hour | ||
| } // namespace | ||
|
|
||
| AS3935Sensor::AS3935Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_AS3935, "AS3935") {} | ||
|
|
||
| AS3935Sensor::~AS3935Sensor() | ||
| { | ||
| if (lightning) { | ||
| #pragma GCC diagnostic push | ||
| #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" | ||
| delete lightning; | ||
| #pragma GCC diagnostic pop | ||
| lightning = nullptr; | ||
| } | ||
| } | ||
|
|
||
| bool AS3935Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) | ||
| { | ||
| LOG_INFO("Init sensor: %s", sensorName); | ||
|
|
||
| lightning = new SparkFun_AS3935(dev->address.address); | ||
| status = lightning->begin(*bus); | ||
| if (!status) { | ||
| initI2CSensor(); | ||
| return status; | ||
| } | ||
|
|
||
| // Defaults match the library's own example, except outdoor mode and unmasked | ||
| // disturbers (kept visible in the log). | ||
| lightning->setIndoorOutdoor(OUTDOOR); | ||
| lightning->setNoiseLevel(2); | ||
| lightning->watchdogThreshold(2); | ||
| lightning->spikeRejection(2); | ||
| lightning->maskDisturber(false); | ||
| lightning->lightningThreshold(1); | ||
|
|
||
| #ifdef AS3935_IRQ | ||
| pinMode(AS3935_IRQ, INPUT); | ||
| #endif | ||
|
|
||
| windowStartMs = millis(); | ||
| initI2CSensor(); | ||
| return status; | ||
| } | ||
|
|
||
| int32_t AS3935Sensor::runOnce() | ||
| { | ||
| #ifdef AS3935_IRQ | ||
| if (digitalRead(AS3935_IRQ) == HIGH) { | ||
| classifyPendingIrq(); | ||
| } | ||
| #endif | ||
| if (!Throttle::isWithinTimespanMs(windowStartMs, AS3935_STRIKE_WINDOW_MS)) { | ||
| strikeCountWindow = 0; | ||
| lastDistanceKm = -1; | ||
| windowStartMs = millis(); | ||
| } | ||
| return AS3935_CHECK_INTERVAL_MS; | ||
| } | ||
|
|
||
| void AS3935Sensor::classifyPendingIrq() | ||
| { | ||
| uint8_t interruptReason = lightning->readInterruptReg(); | ||
| switch (interruptReason) { | ||
| case LIGHTNING: { | ||
| strikeCountWindow++; | ||
| uint8_t distance = lightning->distanceToStorm(); | ||
| if (distance != AS3935_DISTANCE_OUT_OF_RANGE) { | ||
| lastDistanceKm = distance; | ||
| LOG_INFO("%s: lightning strike detected, distance=%dkm", sensorName, distance); | ||
| } else { | ||
| LOG_INFO("%s: lightning strike detected, distance unknown (out of range)", sensorName); | ||
| } | ||
| // No debounce here - EnvironmentTelemetryModule's airtime gate already paces every send. | ||
| if (environmentTelemetryModule) { | ||
| environmentTelemetryModule->requestImmediateSend(); | ||
| } | ||
| break; | ||
| } | ||
| case DISTURBER_DETECT: | ||
| LOG_DEBUG("%s: disturber detected (ignored)", sensorName); | ||
| break; | ||
| case NOISE_TO_HIGH: | ||
| LOG_DEBUG("%s: noise floor too high", sensorName); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| bool AS3935Sensor::getMetrics(meshtastic_Telemetry *measurement) | ||
| { | ||
| measurement->variant.environment_metrics.has_lightning_strike_count_1h = true; | ||
| measurement->variant.environment_metrics.lightning_strike_count_1h = strikeCountWindow; | ||
| if (lastDistanceKm >= 0) { | ||
| measurement->variant.environment_metrics.has_lightning_distance_km = true; | ||
| measurement->variant.environment_metrics.lightning_distance_km = lastDistanceKm; | ||
| } | ||
| 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,32 @@ | ||
| #pragma once | ||
|
|
||
| #ifndef _MT_AS3935SENSOR_H | ||
| #define _MT_AS3935SENSOR_H | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<SparkFun_AS3935.h>) | ||
|
|
||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "TelemetrySensor.h" | ||
| #include <SparkFun_AS3935.h> | ||
|
|
||
| class AS3935Sensor : public TelemetrySensor | ||
| { | ||
| private: | ||
| SparkFun_AS3935 *lightning = nullptr; | ||
| uint32_t strikeCountWindow = 0; | ||
| float lastDistanceKm = -1; // sentinel: no valid distance captured this window | ||
| uint32_t windowStartMs = 0; | ||
|
|
||
| void classifyPendingIrq(); | ||
|
|
||
| public: | ||
| AS3935Sensor(); | ||
| ~AS3935Sensor(); | ||
| virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override; | ||
| virtual bool getMetrics(meshtastic_Telemetry *measurement) override; | ||
| virtual int32_t runOnce() override; | ||
| }; | ||
|
|
||
| #endif | ||
| #endif |
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.
Uh oh!
There was an error while loading. Please reload this page.