esphome icon indicating copy to clipboard operation
esphome copied to clipboard

Complete Rewrite for supporting newer SM300D2 V2 with auto legacy support.

Open Duker4444 opened this issue 5 months ago • 2 comments

What does this implement/fix?

Receive "Preamble Error" from data alignment errors.

This is a complete rewrite to better handle the data parsing

The updated sm300d2.h and sm300d2.cpp fix the const mismatch error by making sm300d2_checksum_ const-correct. The buffer-based processing strategy should resolve previous synchronization timeouts and handle extra bytes (e.g., timestamps).

New Approach: Reads all UART data into a std::vector<uint8_t> buffer, scans for preambles (0x3C or 0x01), extracts complete packets (17 or 19 bytes), and processes them. Partial packets are retained for the next cycle.

Improvements: Eliminates timeouts by removing delay-based logic. Handles continuous data streams and multiple packets per cycle. Retains partial packets, ensuring no data loss. Skips invalid bytes (e.g., timestamps like 3F:12:62:F6:A9), preventing buffer overflow.

Extra Bytes Handling: Issue: Logs showed extra bytes (e.g., 3F:12:62:F6:A9) after 19-byte packets, causing misalignment. Change: Non-preamble bytes are skipped during buffer scanning, effectively discarding timestamps or invalid data.

Sensor Value Validation - Unrealistic Value Checks: Issue: Logs showed unrealistic values (e.g., PM10=7424 µg/m³, temperature=10°C). Change: Added validation in process_packet: PM2.5/PM10: ≤ 1000 µg/m³ (MAX_REASONABLE_PM). Temperature: -40°C to 80°C. Humidity: 0% to 100%.

This should improve reliability in the output data.

Fixes

  • fixes (https://community.home-assistant.io/t/sm300d2-issues/482833)

Test Environment

  • [ ] ESP32 -New SM300D2 Hardware Rev v2 with 19 byte output

Example entry for config.yaml:

##Example ESPHome Config:

uart: tx_pin: GPIO17 rx_pin: GPIO16 baud_rate: 9600

rx_buffer_size: 256

debug: direction: RX dummy_receiver: false after: delimiter: "\n" sequence: - lambda: UARTDebug::log_hex(direction, bytes, ':');

sensor:

  • platform: sm300d2 co2: name: "Office CO2 Value" formaldehyde: name: "Office Formaldehyde Value" tvoc: name: "Office TVOC Value" pm_2_5: name: "Office PM2.5 Value" pm_10_0: name: "Office PM10 Value" temperature: name: "Office Temperature Value" humidity: name: "Office Humidity Value" update_interval: 15s
I've fully tested this against the newer Hardware, however I allowed room for backwards compatibility. 

Duker4444 avatar Jun 16 '25 21:06 Duker4444

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 17.08%. Comparing base (15c5dd2) to head (09f4713).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #9107   +/-   ##
=======================================
  Coverage   17.08%   17.08%           
=======================================
  Files        1168     1168           
  Lines       48860    48860           
  Branches     5866     5866           
=======================================
  Hits         8350     8350           
  Misses      39933    39933           
  Partials      577      577           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Jun 16 '25 21:06 codecov-commenter

I just updated the files for clang formatting. I think they are good to go.. This is my first release on Git. So My apologies if i ask questions.. I've fully tested the code and is running successfully on the device submitted.

Duker4444 avatar Jun 16 '25 23:06 Duker4444

This Pull request has stalled.. Will Resubmit the pull request.

Duker4444 avatar Jun 26 '25 18:06 Duker4444