Skip to content

Bmp280 #4

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Bmp280 #4

wants to merge 2 commits into from

Conversation

Hahihula
Copy link
Owner

@Hahihula Hahihula commented May 13, 2025

Summary by CodeRabbit

  • New Features

    • Added a new example for reading temperature and humidity from an AHT20 sensor.
    • Added a new example demonstrating combined use of BMP280 and AHT20 sensors for temperature, pressure, and humidity readings on an ESP microcontroller.
  • Documentation

    • Updated the README with new example entries for AHT20 and BMP280_AHT20 board usage, including build and flash instructions.

Copy link

coderabbitai bot commented May 13, 2025

Walkthrough

Two new example programs, aht20 and bmp280_aht20_board, were added for interfacing with AHT20 and BMP280 sensors over I2C on ESP microcontrollers. The README was updated to reference these examples and provide build/flash instructions. The examples demonstrate sensor initialization, data acquisition, compensation, and error handling.

Changes

File(s) Change Summary
README.md Added two new example entries (aht20, bmp280_aht20_board) with paths and cargo espflash usage.
examples/aht20.rs New example: Reads temperature/humidity from AHT20 sensor via I2C, includes sensor init and error handling.
examples/bmp280_aht20_board.rs New example: Reads and compensates data from BMP280 (pressure/temp) and AHT20 (temp/humidity) sensors; includes calibration parsing, compensation functions, address detection, and error handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ESP_MCU
    participant AHT20
    participant BMP280

    User->>ESP_MCU: Power on and start example
    ESP_MCU->>AHT20: Initialize sensor via I2C
    ESP_MCU->>BMP280: Detect and initialize sensor via I2C
    loop Every 2 seconds
        ESP_MCU->>AHT20: Trigger measurement
        ESP_MCU->>AHT20: Read temperature/humidity
        ESP_MCU->>BMP280: Read raw temperature/pressure
        ESP_MCU->>ESP_MCU: Compensate/calibrate BMP280 readings
        ESP_MCU->>User: Print sensor data
    end
Loading

Poem

🐇
Two new examples hop into view,
Sensors to read, with code so true.
AHT20 and BMP280, side by side,
Data and diagnostics, in bytes they confide.
README now guides with steps so clear—
Flash and run, your sensors are here!
🌱

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (7)
examples/aht20.rs (2)

33-33: Consider removing commented-out code.

This commented-out LED initialization code doesn't appear to be used or relevant to the AHT20 sensor example.

-    // let mut led = Output::new(peripherals.GPIO8, Level::High, config);

48-57: Consider more graceful error handling instead of panicking.

While panicking on initialization failure is acceptable for an example, in production code, you might want to use a more graceful error handling approach.

 match i2c.write(AHT20_ADDR, &[CMD_INIT, INIT_PARAM1, INIT_PARAM2]) {
     Ok(_) => {
         println!("AHT20 sensor initialized!");
     }
     Err(e) => {
-        panic!("Failed to initialize AHT20: {:?}", e);
+        println!("Failed to initialize AHT20: {:?}", e);
+        // Handle error: retry, wait, or use default values
     }
 }
examples/bmp280_aht20_board.rs (5)

1-6: Documentation needs to mention both sensors.

The file header only mentions the BMP280 sensor, but the example clearly works with both BMP280 and AHT20 sensors.

-//! Reads a BMP280 pressure and temperature sensor
+//! Reads from BMP280 pressure/temperature and AHT20 humidity/temperature sensors
 //!
 //! The following wiring is assumed:
 //! - SDA => GPIO8
 //! - SCL => GPIO9

25-26: Fix typo in constant naming comment.

There's a typo in the comment for AHT20 constants.

-// ATH20 constants
+// AHT20 constants
 const AHT20_ADDR: u8 = 0x38; // I2C address of AHT20

104-114: Non-critical error handling could be improved.

Similar to the other example, consider using more graceful error handling for non-critical errors, especially since the code already has the "Will try to continue anyway..." message.


212-216: Consider moving AHT20 measurement constants to the top.

The AHT20 measurement command constants are defined inside the loop. For better code organization, consider moving them to the top with the other constants.

 // ATH20 constants
 const AHT20_ADDR: u8 = 0x38; // I2C address of AHT20
 const CMD_INIT: u8 = 0xBE; // Initialize command
 const INIT_PARAM1: u8 = 0x08;
 const INIT_PARAM2: u8 = 0x00;
+const CMD_MEASURE: u8 = 0xAC; // Trigger measurement command
+const MEASURE_PARAM1: u8 = 0x33;
+const MEASURE_PARAM2: u8 = 0x00;

 // ...
 
 loop {
     // ...
-    // AHT20 constants for measurement
-    const CMD_MEASURE: u8 = 0xAC;
-    const MEASURE_PARAM1: u8 = 0x33;
-    const MEASURE_PARAM2: u8 = 0x00;

333-338: Consider logging raw calculated pressure for debugging.

When an invalid pressure is detected, it might be helpful to log the raw calculated value for debugging purposes.

 if p < 30000 || p > 110000 {
     // Invalid pressure range (300-1100 hPa is normal on Earth)
     // Return a sentinel value or use previous valid reading
-    println!("Warning: Invalid pressure calculation: {} Pa", p);
+    println!("Warning: Invalid pressure calculation: {} Pa (raw value: {})", p, raw_pressure);
     return 101325; // Standard pressure in Pa as fallback
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b479c33 and 220a190.

📒 Files selected for processing (3)
  • README.md (1 hunks)
  • examples/aht20.rs (1 hunks)
  • examples/bmp280_aht20_board.rs (1 hunks)
🔇 Additional comments (11)
README.md (1)

38-42: LGTM! Documentation additions are clear and consistent.

The new entries for AHT20 and BMP280 sensor examples follow the same format as the existing examples, making it easy for users to understand how to build and flash these examples to their ESP32-C3 boards.

examples/aht20.rs (6)

1-6: LGTM! Clear documentation for pin connections.

The file header provides clear information about the expected wiring configuration for the AHT20 sensor.


20-27: LGTM! Well-defined constants with clear naming.

Sensor-specific constants are properly defined with descriptive names, making the code more maintainable and easier to understand.


37-47: LGTM! I2C initialization with proper error handling.

The I2C bus is properly initialized with appropriate frequency, pin assignments, and error handling.


76-103: LGTM! Robust handling of sensor calibration status.

The code properly checks the calibration status and implements a recovery mechanism with soft reset when needed.


111-119: LGTM! Correct bit manipulation for raw sensor data extraction.

The bit operations to extract 20-bit humidity and temperature values from the sensor response conform to the AHT20 datasheet specifications.


121-131: LGTM! Proper data validation before presenting to the user.

The code validates the sensor readings against expected ranges before displaying them, which helps prevent displaying nonsensical values from potential communication errors.

examples/bmp280_aht20_board.rs (4)

76-82: LGTM! Smart address detection for BMP280.

The code intelligently detects the BMP280 address, which makes the example more robust as the sensor may use different I2C addresses depending on hardware configuration.


135-157: LGTM! Comprehensive sensor configuration with fallback handling.

The sensor configuration includes both measurement settings and filter settings, with appropriate error handling that allows the program to continue even if configuration fails.


254-272: LGTM! Comprehensive status reporting for all sensor combinations.

The code handles all possible combinations of sensor data availability, providing clear output in each case.


343-396: LGTM! Excellent address detection function with thorough error reporting.

The BMP280 address detection function is well-implemented with clear status reporting at each step and a sensible fallback option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant