-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add SHT21 sensor support #9582
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
Tomas-Kuchta-FPV
wants to merge
4
commits into
meshtastic:develop
Choose a base branch
from
Tomas-Kuchta-FPV:feat/SHT21
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.
+134
−3
Open
Add SHT21 sensor support #9582
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ class ScanI2C | |
| MAX17048, | ||
| MCP9808, | ||
| SHT31, | ||
| SHT21, | ||
| SHT4X, | ||
| SHTC3, | ||
| LPS22HB, | ||
|
|
||
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,85 @@ | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR | ||
|
|
||
| #include "SHT21Sensor.h" | ||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "TelemetrySensor.h" | ||
| #include <Wire.h> | ||
| #include <cmath> | ||
|
|
||
| // SHT21 I2C address and commands (use no-hold commands to avoid clock-stretch issues) | ||
| static const uint8_t CMD_TEMP_NOHOLD = 0xF3; | ||
| static const uint8_t CMD_HUM_NOHOLD = 0xF5; | ||
|
|
||
| static bool read_sht21_raw(TwoWire *bus, uint8_t addr, uint8_t cmd, uint16_t &raw, uint16_t waitMs) | ||
| { | ||
| bus->beginTransmission(addr); | ||
| bus->write(cmd); | ||
| if (bus->endTransmission() != 0) | ||
| return false; | ||
| // Wait for measurement to complete (no-hold mode) | ||
| delay(waitMs); | ||
| int toRead = 3; // two data bytes + CRC | ||
| bus->requestFrom((int)addr, toRead); | ||
| if (bus->available() < 2) | ||
| return false; | ||
| uint8_t msb = bus->read(); | ||
| uint8_t lsb = bus->read(); | ||
| if (bus->available()) | ||
| (void)bus->read(); | ||
|
|
||
| raw = ((uint16_t)msb << 8) | lsb; | ||
| // clear status bits per datasheet | ||
| raw &= ~0x0003; | ||
| return true; | ||
| } | ||
|
|
||
| SHT21Sensor::SHT21Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_SHT21, "SHT21") {} | ||
|
|
||
| bool SHT21Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) | ||
| { | ||
| LOG_INFO("Init sensor: %s", sensorName); | ||
| i2c = bus; | ||
| i2c_addr = dev->address.address ? dev->address.address : SHT21_ADDR; | ||
|
|
||
| uint16_t raw; | ||
| // use no-hold command and wait for conversion (max ~85ms for temp) | ||
| if (!read_sht21_raw(i2c, i2c_addr, CMD_TEMP_NOHOLD, raw, 85)) { | ||
| return false; | ||
| } | ||
| float temp = -46.85f + 175.72f * ((float)raw / 65536.0f); | ||
| // basic sanity check | ||
| if (temp < -50.0f || temp > 150.0f) | ||
| return false; | ||
|
|
||
| status = true; | ||
| initI2CSensor(); | ||
| return true; | ||
| } | ||
|
|
||
| bool SHT21Sensor::getMetrics(meshtastic_Telemetry *measurement) | ||
| { | ||
| measurement->variant.environment_metrics.has_temperature = true; | ||
| measurement->variant.environment_metrics.has_relative_humidity = true; | ||
|
|
||
| LOG_DEBUG("SHT21 getMetrics"); | ||
|
|
||
| uint16_t raw_t = 0, raw_h = 0; | ||
| if (!read_sht21_raw(i2c, i2c_addr, CMD_TEMP_NOHOLD, raw_t, 85)) | ||
| return false; | ||
| // humidity max conversion time ~29ms | ||
| if (!read_sht21_raw(i2c, i2c_addr, CMD_HUM_NOHOLD, raw_h, 30)) | ||
| return false; | ||
|
|
||
| float temp = -46.85f + 175.72f * ((float)raw_t / 65536.0f); | ||
| float rh = -6.0f + 125.0f * ((float)raw_h / 65536.0f); | ||
|
|
||
| // Round to 2 decimal places for telemetry | ||
| measurement->variant.environment_metrics.temperature = roundf(temp * 100.0f) / 100.0f; | ||
| measurement->variant.environment_metrics.relative_humidity = roundf(rh * 100.0f) / 100.0f; | ||
|
|
||
| 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,22 @@ | ||
| #include "configuration.h" | ||
|
|
||
| #if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR | ||
|
|
||
| #include "../mesh/generated/meshtastic/telemetry.pb.h" | ||
| #include "TelemetrySensor.h" | ||
| #include <Wire.h> | ||
|
|
||
| class SHT21Sensor : public TelemetrySensor | ||
| { | ||
| private: | ||
| TwoWire *i2c = nullptr; | ||
| uint8_t i2c_addr = 0; | ||
| bool status = false; | ||
|
|
||
| public: | ||
| SHT21Sensor(); | ||
| virtual bool getMetrics(meshtastic_Telemetry *measurement) override; | ||
| virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override; | ||
| }; | ||
|
|
||
| #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.
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.
is there a register you can look up on the SHT21 that is unique to that model, or at least able to distinguish it from the other chips that share the address? That way we could detect it and use it without a firmware rebuild.