Skip to content

Add ACS712 current sensor usermod#5668

Open
czQery wants to merge 3 commits into
wled:mainfrom
czQery:main
Open

Add ACS712 current sensor usermod#5668
czQery wants to merge 3 commits into
wled:mainfrom
czQery:main

Conversation

@czQery
Copy link
Copy Markdown

@czQery czQery commented Jun 4, 2026

Adds support for the ACS712 current sensor.
Sends current in mA to MQTT every 5 seconds.
Allows specifying:

  • Type of sensor: 5A, 20A, 30A
  • Resolution of ADC
  • Offset to compensate for inaccuracies

Summary by CodeRabbit

  • New Features
    • Support for AC current measurement using ACS712 sensors.
    • Periodic sampling with smoothing and retained MQTT publishing of averaged current readings.
    • Home Assistant discovery payloads for automatic sensor integration (published after MQTT connectivity).
    • Configurable calibration parameters (ratio, resolution, offset) with persistent storage and UI configuration entries.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 4, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d7a1dfde-4bf1-4a29-bce9-8c36766d4208

📥 Commits

Reviewing files that changed from the base of the PR and between f5228fd and c37db7c.

📒 Files selected for processing (1)
  • usermods/ACS712/ACS712.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • usermods/ACS712/ACS712.cpp

Walkthrough

This PR adds a new WLED usermod (ACS712) that measures AC current via an analog pin, applies configurable calibration (currentRatio, resolution, offset), samples and averages readings every 5s, and publishes retained numeric readings plus Home Assistant MQTT discovery. MQTT is required at compile time.

Changes

ACS712 Current Measurement Usermod

Layer / File(s) Summary
Class structure and HA discovery initialization
usermods/ACS712/ACS712.cpp
Defines the ACS712 usermod class with a compile-time guard requiring MQTT support. Private fields store configuration (currentRatio, resolution, offset) and state. HA discovery initialization builds and publishes a retained JSON payload describing the current sensor to Home Assistant's discovery topic.
Runtime current measurement and MQTT publishing
usermods/ACS712/ACS712.cpp
The loop() method enforces throttling (5-second intervals) and gating conditions (enabled, strip updated, valid pin). On first execution, the GPIO pin is initialized. The core logic samples 100 analog readings, applies calibration math (offset, ratio, resolution), computes the average, and publishes the retained current value to the MQTT data topic while updating internal timing and state.
Configuration persistence and UI generation
usermods/ACS712/ACS712.cpp
addToConfig() serializes enabled/pin/calibration parameters to JSON; readFromConfig() deserializes and validates them with fallback defaults; appendConfigData() generates a dropdown and calibration guidance text including the configured offset.
Module registration and library metadata
usermods/ACS712/ACS712.cpp, usermods/ACS712/library.json
Defines PROGMEM string constants for MQTT topic prefixes and UI labels. Instantiates and registers the usermod via REGISTER_USERMOD. library.json provides the library manifest with the ACS712 name, disabled libArchive, and empty dependencies.

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers:

  • willmmiles
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add ACS712 current sensor usermod' accurately and concisely describes the main change: introducing a new ACS712 usermod for current measurement in WLED.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
usermods/ACS712/ACS712.cpp

usermods/ACS712/ACS712.cpp:1:10: fatal error: 'wled.h' file not found
1 | #include "wled.h"
| ^~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/c37db7c141e3bd95a5668d14a91845760c548308-f196ec0aeab1c1bf/tmp/clang_command_.tmp.f642f2.txt
++Contents of '/tmp/coderabbit-infer/c37db7c141e3bd95a5668d14a91845760c548308-f196ec0aeab1c1bf/tmp/clang_command_.tmp.f642f2.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free"
"-clear-ast-bef

... [truncated 1042 characters] ...

/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-fdeprecated-macro" "-ferror-limit" "19" "-fgnuc-version=4.2.1"
"-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/f196ec0aeab1c1bf/file.o" "-x" "c++"
"usermods/ACS712/ACS712.cpp" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@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: 1

🧹 Nitpick comments (1)
usermods/ACS712/ACS712.cpp (1)

8-117: ⚡ Quick win

Use 2-space indentation (no tabs) to match project C++ style.

This file currently uses tabs in the class body; please reindent to 2 spaces for guideline compliance and consistency.

As per coding guidelines, **/*.cpp and usermods/**/*.{cpp,h} require 2-space indentation with no tabs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@usermods/ACS712/ACS712.cpp` around lines 8 - 117, Reformat the file to
replace tabs with 2-space indentation throughout the class body so it follows
project C++ style; update indentation for the private member declarations and
all methods (_mqttInitialize, setup, loop, addToConfig, readFromConfig,
appendConfigData) and their internal blocks, ensuring no tabs remain and each
indent level uses exactly two spaces; preserve existing code and line structure,
only change whitespace.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@usermods/ACS712/ACS712.cpp`:
- Around line 52-55: The config-derived values pin, currentRatio and resolution
must be validated and clamped in readFromConfig(JsonObject& root) (and before
any use in loop/setup) so they cannot be 0 or out-of-range when passed to
pinMode() or analogRead() or used in division; update readFromConfig to clamp
pin into a sane ADC pin range (or set to -1/disabled if invalid), enforce
currentRatio and resolution to be positive non-zero (fallback to defaults if
invalid), and ensure any places that call pinMode(pin, INPUT) or analogRead(pin)
(references to pin) and any current math (division using
currentRatio/resolution) check the validated values (or return early) to avoid
divide-by-zero and invalid pin operations.

---

Nitpick comments:
In `@usermods/ACS712/ACS712.cpp`:
- Around line 8-117: Reformat the file to replace tabs with 2-space indentation
throughout the class body so it follows project C++ style; update indentation
for the private member declarations and all methods (_mqttInitialize, setup,
loop, addToConfig, readFromConfig, appendConfigData) and their internal blocks,
ensuring no tabs remain and each indent level uses exactly two spaces; preserve
existing code and line structure, only change whitespace.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 165ec204-3c2b-40d2-9b32-63f9c4c58dde

📥 Commits

Reviewing files that changed from the base of the PR and between be264fb and 9cd0570.

📒 Files selected for processing (2)
  • usermods/ACS712/ACS712.cpp
  • usermods/ACS712/library.json

Comment thread usermods/ACS712/ACS712.cpp Outdated
@softhack007 softhack007 added the usermod usermod related label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

usermod usermod related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants