Skip to content

Commit d2a785b

Browse files
author
user2684
committed
Add option to measure battery level via a pin #23
1 parent 1d24f1f commit d2a785b

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

NodeManagerTemplate/NodeManager.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ void NodeManager::setRetries(int value) {
915915
void NodeManager::setBatteryReportCycles(int value) {
916916
_battery_report_cycles = value;
917917
}
918+
void NodeManager::setBatteryInternalVcc(bool value) {
919+
_battery_internal_vcc = value;
920+
}
921+
void NodeManager::setBatteryPin(int value) {
922+
_battery_pin = value;
923+
}
924+
void NodeManager::setBatteryVoltsPerBit(float value) {
925+
_battery_volts_per_bit = value;
926+
}
918927
#endif
919928
#if SLEEP_MANAGER == 1
920929
void NodeManager::setSleepMode(int value) {
@@ -1145,6 +1154,10 @@ void NodeManager::before() {
11451154
#endif
11461155
}
11471156
#endif
1157+
#if POWER_MANAGER == 1
1158+
// set analogReference to internal if measuring the battery through a pin
1159+
if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL);
1160+
#endif
11481161
// setup individual sensors
11491162
for (int i = 0; i < 255; i++) {
11501163
if (_sensors[i] == 0) continue;
@@ -1300,7 +1313,9 @@ void NodeManager::_process(const char * message) {
13001313
// BATTERY: return the battery level
13011314
else if (strcmp(message, BATTERY) == 0) {
13021315
// measure the board vcc
1303-
float volt = _getVcc();
1316+
float volt = 0;
1317+
if (_battery_internal_vcc || _battery_pin == -1) volt = _getVcc();
1318+
else volt = analogRead(_battery_pin) * _battery_volts_per_bit;
13041319
// calculate the percentage
13051320
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
13061321
if (percentage > 100) percentage = 100;
@@ -1311,7 +1326,7 @@ void NodeManager::_process(const char * message) {
13111326
Serial.print(" P=");
13121327
Serial.println(percentage);
13131328
#endif
1314-
#if BATTERY_MANAGER == 1 && BATTERY_SENSOR == 1
1329+
#if BATTERY_SENSOR == 1
13151330
// report battery voltage
13161331
MyMessage battery_msg(BATTERY_CHILD_ID, V_VOLTAGE);
13171332
_send(battery_msg.set(volt, 2));

NodeManagerTemplate/NodeManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,12 @@ class NodeManager {
595595
void setBatteryMax(float value);
596596
// after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10)
597597
void setBatteryReportCycles(int value);
598+
// if true, the battery level will be evaluated by measuring the internal vcc without the need to connect any pin, if false the voltage divider methon will be used (default: true)
599+
void setBatteryInternalVcc(bool value);
600+
// if setBatteryInternalVcc() is set to false, the analog pin to which the battery's vcc is attached (https://www.mysensors.org/build/battery) (default: -1)
601+
void setBatteryPin(int value);
602+
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
603+
void setBatteryVoltsPerBit(float value);
598604
#endif
599605
#if SLEEP_MANAGER == 1
600606
// define if the board has to sleep every time entering loop (default: IDLE). It can be IDLE (no sleep), SLEEP (sleep at every cycle), WAIT (wait at every cycle
@@ -644,6 +650,9 @@ class NodeManager {
644650
float _battery_min = 2.6;
645651
float _battery_max = 3.3;
646652
int _battery_report_cycles = 10;
653+
bool _battery_internal_vcc = true;
654+
int _battery_pin = -1;
655+
float _battery_volts_per_bit = 0.003363075;
647656
int _cycles = 0;
648657
float _getVcc();
649658
#endif

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ Node Manager comes with a reasonable default configuration. If you want/need to
126126
void setBatteryMax(float value);
127127
// after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10)
128128
void setBatteryReportCycles(int value);
129+
// if true, the battery level will be evaluated by measuring the internal vcc without the need to connect any pin, if false the voltage divider methon will be used (default: true)
130+
void setBatteryInternalVcc(bool value);
131+
// if setBatteryInternalVcc() is set to false, the analog pin to which the battery's vcc is attached (https://www.mysensors.org/build/battery) (default: -1)
132+
void setBatteryPin(int value);
133+
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
134+
void setBatteryVoltsPerBit(float value);
129135
#endif
130136
#if SLEEP_MANAGER == 1
131137
// define if the board has to sleep every time entering loop (default: IDLE). It can be IDLE (no sleep), SLEEP (sleep at every cycle), WAIT (wait at every cycle

0 commit comments

Comments
 (0)