Skip to content

fix: fix data acquisition from SHT21 temperature and humidity sensor #2774

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

Merged
merged 1 commit into from
Jul 7, 2025

Conversation

marcnause
Copy link
Contributor

@marcnause marcnause commented Jul 6, 2025

Part of #2773

The fixed code can be used as a template for the Flutter version of the app.

Changes

  • Fixed problems in sensor data acquisition
  • Some minor refactoring and code cleanup

Screenshots / Recordings

Screen_recording_20250706_184339.mp4

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Fix SHT21 sensor data acquisition by correcting register addresses, conversion formulas, and checksum handling; refactor measurement API to use an enum; and improve logging, buffer management, and error handling across sensor and communication modules.

Bug Fixes:

  • Correct I2C register addresses, timing delays, raw-to-physical conversion formulas, and checksum logic for accurate SHT21 readings.
  • Handle empty or invalid sensor data by returning null on failures and logging appropriate error messages.

Enhancements:

  • Introduce a Mode enum and setMode method for selecting temperature or humidity measurements.
  • Simplify getRaw() to return a single Double value and streamline data handling in SensorSHT21 and UI fragments.
  • Clean up CommunicationHandler by removing unused fields, consolidating buffers, and updating logging with class-based TAGs.
  • Replace printStackTrace calls with structured Log statements across fragment and activity classes.

Documentation:

  • Add Javadoc for the I2C.readBulk method to document its parameters and behavior.

@marcnause marcnause requested review from CloudyPadmal and AsCress July 6, 2025 16:49
@marcnause marcnause added Bug Unexpected problem or unintended behavior in app java Pull requests that update Java code labels Jul 6, 2025
Copy link

sourcery-ai bot commented Jul 6, 2025

Reviewer's Guide

This PR refactors the SHT21 sensor acquisition pipeline by introducing an enum-based mode API, static and concise data‐conversion methods with robust checksum/error handling, updates dependent data‐fetch and UI code to the new API, and cleans up logging, unused fields, and adds documentation for I2C.readBulk.

Sequence diagram for SHT21 sensor data acquisition with new enum-based API

sequenceDiagram
    participant SensorSHT21 as SensorSHT21.SensorDataFetch
    participant SHT21 as SHT21
    participant I2C as I2C
    SensorSHT21->>SHT21: setMode(Mode.TEMPERATURE)
    SensorSHT21->>SHT21: getRaw()
    SHT21->>I2C: writeBulk(ADDRESS, [registerAddress])
    SHT21->>I2C: readBulk(ADDRESS, registerAddress, 3)
    I2C-->>SHT21: List<Integer> vals
    SHT21->>SHT21: calculateChecksum(vals, 2)
    SHT21->>SHT21: rawToTemp(vals)
    SHT21-->>SensorSHT21: Double temperature
    SensorSHT21->>SHT21: setMode(Mode.HUMIDITY)
    SensorSHT21->>SHT21: getRaw()
    SHT21->>I2C: writeBulk(ADDRESS, [registerAddress])
    SHT21->>I2C: readBulk(ADDRESS, registerAddress, 3)
    I2C-->>SHT21: List<Integer> vals
    SHT21->>SHT21: calculateChecksum(vals, 2)
    SHT21->>SHT21: rawToRH(vals)
    SHT21-->>SensorSHT21: Double humidity
Loading

Class diagram for SensorSHT21.SensorDataFetch data handling changes

classDiagram
    class SensorSHT21.SensorDataFetch {
        -Double dataSHT21Temp
        -Double dataSHT21Humidity
        -float timeElapsed
        +boolean getSensorData()
        +void updateUi()
    }
    SensorSHT21.SensorDataFetch --> SHT21
Loading

Class diagram for I2C readBulk method addition

classDiagram
    class I2C {
        +ArrayList<Integer> readBulk(int deviceAddress, int registerAddress, int bytesToRead)
    }
Loading

File-Level Changes

Change Details Files
Refactor SHT21 sensor communication and data conversion
  • Introduce Mode enum with registerAddress for temperature/humidity
  • Replace mutable constants with static final fields
  • Convert rawToTemp/rawToRH to static methods returning Double
  • Enhance getRaw to use readBulk, handle empty data and checksum errors
app/src/main/java/io/pslab/communication/sensors/SHT21.java
Update SensorDataFetch to use new SHT21 API and simplify data storage
  • Replace List buffers with single Double fields
  • Use setMode and getRaw returning Double instead of selectParameter and List
  • Adjust chart entries and UI updates to use primitive float values
app/src/main/java/io/pslab/sensors/SensorSHT21.java
Enhance ThermometerDataFragment parameter parsing and logging
  • Remove hardcoded TEMPERATURE string and use TAG constant
  • Switch from Integer.valueOf to Integer.parseInt for sensorType
  • Replace e.printStackTrace with Log.e
  • Use setMode(Enum) when initiating SHT21
app/src/main/java/io/pslab/fragment/ThermometerDataFragment.java
Clean up CommunicationHandler internals and logging
  • Remove unused mWriteBuffer and drivers field
  • Localize drivers list variable
  • Standardize Log.v calls to use TAG
app/src/main/java/io/pslab/communication/CommunicationHandler.java
Document I2C.readBulk and improve exception logging in SensorActivity
  • Add Javadoc for readBulk documenting parameters and return
  • Replace printStackTrace in SensorActivity with Log.w for sensor scanning errors
app/src/main/java/io/pslab/communication/peripherals/I2C.java
app/src/main/java/io/pslab/activity/SensorActivity.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

github-actions bot commented Jul 6, 2025

@AsCress AsCress merged commit 9b7826a into fossasia:development Jul 7, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Unexpected problem or unintended behavior in app java Pull requests that update Java code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants