-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add ACS712 current sensor usermod #5668
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
czQery
wants to merge
3
commits into
wled:main
Choose a base branch
from
czQery:main
base: main
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
−0
Open
Changes from 1 commit
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #include "wled.h" | ||
|
|
||
| #ifdef WLED_DISABLE_MQTT | ||
| #error "This user mod requires MQTT to be enabled." | ||
| #endif | ||
|
|
||
| class ACS712 : public Usermod { | ||
| private: | ||
| bool initPin = false; | ||
| bool initMQTT = false; | ||
| float current = 0; | ||
| float lastCurrent = 0; | ||
| double sumCurrent = 0; | ||
| unsigned long lastTime = 0; | ||
| String currentTopic = ""; | ||
|
|
||
| bool enabled = false; | ||
| int8_t pin = -1; | ||
| uint8_t currentRatio = 100.0; | ||
| uint16_t resolution = 4095.0; | ||
| int16_t offset = 0; | ||
|
|
||
| static const char _name[]; | ||
| static const char _enabled[]; | ||
| static const char _current[]; | ||
| static const char _resolution[]; | ||
| static const char _offset[]; | ||
|
|
||
|
|
||
| void _mqttInitialize() { | ||
| currentTopic = String(mqttDeviceTopic) + "/current"; | ||
|
|
||
| String ha = String("homeassistant/sensor/") + mqttClientID + "/" + FPSTR(_current) + "/config"; | ||
| StaticJsonDocument<1024> json; | ||
|
|
||
| json[F("name")] = serverDescription+String(" Current"); | ||
| json[F("state_topic")] = currentTopic; | ||
| json[F("device_class")] = FPSTR(_current); | ||
| json[F("unique_id")] = String(mqttClientID); | ||
| json[F("unit_of_measurement")] = F("mA"); | ||
|
|
||
| String jsonSer; | ||
| serializeJson(json, jsonSer); | ||
| mqtt->publish(ha.c_str(), 0, true, jsonSer.c_str()); | ||
| } | ||
|
|
||
| public: | ||
| void setup() override { | ||
| } | ||
|
|
||
| void loop() override { | ||
| if (!enabled || strip.isUpdating() || pin == -1 || (millis() - lastTime < 5000)) return; | ||
| if (!initPin) { | ||
| pinMode(pin, INPUT); | ||
| initPin = true; | ||
| } | ||
|
|
||
| if (!initMQTT && WLED_MQTT_CONNECTED) { | ||
| _mqttInitialize(); | ||
| initMQTT = true; | ||
| } | ||
|
|
||
| sumCurrent = 0; | ||
| for(int i = 0; i < 100; i++){ | ||
| current = analogRead(pin); | ||
|
|
||
| current = (current - (resolution / 2.0f)) * (5000.0f / (resolution * currentRatio)); | ||
| current = current * 1000; | ||
| current = current + offset; | ||
|
|
||
| sumCurrent += current; | ||
| } | ||
|
|
||
| current = sumCurrent/100; | ||
| if (initMQTT) mqtt->publish(currentTopic.c_str(), 0, true, String((current+lastCurrent)/2.0).c_str()); | ||
|
|
||
| lastCurrent = current; | ||
| lastTime = millis(); | ||
| } | ||
|
|
||
| void addToConfig(JsonObject &root) override { | ||
| JsonObject top = root.createNestedObject(FPSTR(_name)); | ||
| top[FPSTR(_enabled)] = enabled; | ||
| top["pin"] = pin; | ||
| top[FPSTR(_current)] = currentRatio; | ||
| top[FPSTR(_resolution)] = resolution; | ||
| top[FPSTR(_offset)] = offset; | ||
| } | ||
|
|
||
| bool readFromConfig(JsonObject& root) override { | ||
| JsonObject top = root[FPSTR(_name)]; | ||
| bool configComplete = !top.isNull(); | ||
| configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled, enabled); | ||
| configComplete &= getJsonValue(top["pin"], pin, pin); | ||
| configComplete &= getJsonValue(top["current"], currentRatio, currentRatio); | ||
| configComplete &= getJsonValue(top["resolution"], resolution, resolution); | ||
| configComplete &= getJsonValue(top["offset"], offset, offset); | ||
|
|
||
| return configComplete; | ||
| } | ||
|
|
||
| void appendConfigData() override { | ||
| oappend(F("dd=addDropdown('")); | ||
| oappend(String(FPSTR(_name)).c_str()); | ||
| oappend(F("','")); | ||
| oappend(String(FPSTR(_current)).c_str()); | ||
| oappend(F("');")); | ||
| oappend(F("addOption(dd,'5A',185.0);")); | ||
| oappend(F("addOption(dd,'20A',100.0);")); | ||
| oappend(F("addOption(dd,'30A',66.0);")); | ||
| oappend(F("addInfo('")); | ||
| oappend(String(FPSTR(_name)).c_str()); | ||
| oappend(F(":")); | ||
| oappend(String(FPSTR(_offset)).c_str()); | ||
| oappend(F("',1,'<i>(use to calibrate 0mA)</i>');")); | ||
| } | ||
| }; | ||
|
|
||
| const char ACS712::_name[] PROGMEM = "ACS712"; | ||
| const char ACS712::_enabled[] PROGMEM = "enabled"; | ||
| const char ACS712::_current[] PROGMEM = "current"; | ||
| const char ACS712::_resolution[] PROGMEM = "resolution"; | ||
| const char ACS712::_offset[] PROGMEM = "offset"; | ||
|
|
||
| static ACS712 acs712; | ||
| REGISTER_USERMOD(acs712); | ||
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,5 @@ | ||
| { | ||
| "name": "ACS712", | ||
| "build": { "libArchive": false }, | ||
| "dependencies": {} | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.