added MQTT support, Battery voltage to Info, circuit diagram to readme, minor fixes

This commit is contained in:
itCarl 2021-09-02 15:41:19 +02:00
parent 54f4658dae
commit f368bbec32
5 changed files with 86 additions and 20 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

View File

@ -2,16 +2,25 @@
This Usermod allows you to monitor the battery level of your battery powered project. This Usermod allows you to monitor the battery level of your battery powered project.
You can see the battery level in the `info modal` right under the `estimated current`. You can see the battery level and voltage in the `info modal`.
For this to work the positive side of the (18650) battery must be connected to pin `A0` of the d1mini/esp8266 with a 100k ohm resistor (see [Useful Links](#useful-links)). For this to work the positive side of the (18650) battery must be connected to pin `A0` of the d1mini/esp8266 with a 100k ohm resistor (see [Useful Links](#useful-links)).
If you have a esp32 board it is best to connect the positive side of the battery to ADC1 (GPIO32 - GPIO39) If you have a esp32 board it is best to connect the positive side of the battery to ADC1 (GPIO32 - GPIO39)
<p align="center">
<img width="300" src="assets/battery_info_screen.png">
</p>
## Installation ## Installation
define `USERMOD_BATTERY_STATUS_BASIC` in `my_config.h` define `USERMOD_BATTERY_STATUS_BASIC` in `my_config.h`
### Basic wiring diagram
<p align="center">
<img width="300" src="assets/battery_connection_schematic_01.png">
</p>
### Define Your Options ### Define Your Options
* `USERMOD_BATTERY_STATUS_BASIC` - define this (in `my_config.h`) to have this user mod included wled00\usermods_list.cpp * `USERMOD_BATTERY_STATUS_BASIC` - define this (in `my_config.h`) to have this user mod included wled00\usermods_list.cpp
@ -45,6 +54,11 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3.
* https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/ * https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
## Change Log ## Change Log
2021-09-02
* added "Battery voltage" to info
* added circuit diagram to readme
* added MQTT support, sending battery voltage
* minor fixes
2021-08-15 2021-08-15
* changed `USERMOD_BATTERY_MIN_VOLTAGE` to 2.6 volt as default for 18650 batteries * changed `USERMOD_BATTERY_MIN_VOLTAGE` to 2.6 volt as default for 18650 batteries

View File

@ -29,7 +29,7 @@
#endif #endif
// the frequency to check the battery, 1 minute // the frequency to check the battery, 30 sec
#ifndef USERMOD_BATTERY_MEASUREMENT_INTERVAL #ifndef USERMOD_BATTERY_MEASUREMENT_INTERVAL
#define USERMOD_BATTERY_MEASUREMENT_INTERVAL 30000 #define USERMOD_BATTERY_MEASUREMENT_INTERVAL 30000
#endif #endif
@ -53,7 +53,8 @@ class UsermodBatteryBasic : public Usermod
int8_t batteryPin = USERMOD_BATTERY_MEASUREMENT_PIN; int8_t batteryPin = USERMOD_BATTERY_MEASUREMENT_PIN;
// how often to read the battery voltage // how often to read the battery voltage
unsigned long readingInterval = USERMOD_BATTERY_MEASUREMENT_INTERVAL; unsigned long readingInterval = USERMOD_BATTERY_MEASUREMENT_INTERVAL;
unsigned long lastTime = 0; unsigned long nextReadTime = 0;
unsigned long lastReadTime = 0;
// battery min. voltage // battery min. voltage
float minBatteryVoltage = USERMOD_BATTERY_MIN_VOLTAGE; float minBatteryVoltage = USERMOD_BATTERY_MIN_VOLTAGE;
// battery max. voltage // battery max. voltage
@ -68,6 +69,7 @@ class UsermodBatteryBasic : public Usermod
// mapped battery level based on voltage // mapped battery level based on voltage
long batteryLevel = 0; long batteryLevel = 0;
bool initDone = false; bool initDone = false;
bool initializing = true;
// strings to reduce flash memory usage (used more than twice) // strings to reduce flash memory usage (used more than twice)
@ -82,6 +84,19 @@ class UsermodBatteryBasic : public Usermod
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
float truncate(float val, byte dec)
{
float x = val * pow(10, dec);
float y = round(x);
float z = x - y;
if ((int)z == 5)
{
y++;
}
x = y / pow(10, dec);
return x;
}
public: public:
@ -107,6 +122,9 @@ class UsermodBatteryBasic : public Usermod
pinMode(batteryPin, INPUT); pinMode(batteryPin, INPUT);
#endif #endif
nextReadTime = millis() + readingInterval;
lastReadTime = millis();
initDone = true; initDone = true;
} }
@ -129,26 +147,38 @@ class UsermodBatteryBasic : public Usermod
{ {
if(strip.isUpdating()) return; if(strip.isUpdating()) return;
unsigned long now = millis();
// check the battery level every USERMOD_BATTERY_MEASUREMENT_INTERVAL (ms) // check the battery level every USERMOD_BATTERY_MEASUREMENT_INTERVAL (ms)
if (now - lastTime >= readingInterval) { if (millis() < nextReadTime) return;
// read battery raw input
rawValue = analogRead(batteryPin);
// calculate the voltage nextReadTime = millis() + readingInterval;
voltage = (rawValue / adcPrecision) * maxBatteryVoltage ; lastReadTime = millis();
initializing = false;
// translate battery voltage into percentage // read battery raw input
/* rawValue = analogRead(batteryPin);
the standard "map" function doesn't work
https://www.arduino.cc/reference/en/language/functions/math/map/ notes and warnings at the bottom
*/
batteryLevel = mapf(voltage, minBatteryVoltage, maxBatteryVoltage, 0, 100);
lastTime = now; // calculate the voltage
voltage = (rawValue / adcPrecision) * maxBatteryVoltage ;
// check if voltage is within specified voltage range
voltage = voltage<minBatteryVoltage||voltage>maxBatteryVoltage?-1.0f:voltage;
// translate battery voltage into percentage
/*
the standard "map" function doesn't work
https://www.arduino.cc/reference/en/language/functions/math/map/ notes and warnings at the bottom
*/
batteryLevel = mapf(voltage, minBatteryVoltage, maxBatteryVoltage, 0, 100);
// SmartHome stuff
if (WLED_MQTT_CONNECTED) {
char subuf[64];
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/voltage"));
mqtt->publish(subuf, 0, false, String(voltage).c_str());
} }
} }
@ -163,9 +193,31 @@ class UsermodBatteryBasic : public Usermod
JsonObject user = root["u"]; JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u"); if (user.isNull()) user = root.createNestedObject("u");
JsonArray battery = user.createNestedArray("Battery level"); // info modal display names
battery.add(batteryLevel); JsonArray batteryPercentage = user.createNestedArray("Battery level");
battery.add(F(" %")); JsonArray batteryVoltage = user.createNestedArray("Battery voltage");
if (initializing) {
batteryPercentage.add((nextReadTime - millis()) / 1000);
batteryPercentage.add(" sec");
batteryVoltage.add((nextReadTime - millis()) / 1000);
batteryVoltage.add(" sec");
return;
}
if(batteryLevel < 0) {
batteryPercentage.add(F("invalid"));
} else {
batteryPercentage.add(batteryLevel);
}
batteryPercentage.add(F(" %"));
if(voltage < 0) {
batteryVoltage.add(F("invalid"));
} else {
batteryVoltage.add(truncate(voltage, 2));
}
batteryVoltage.add(F(" V"));
} }