Commit d2a785b1 authored by user2684's avatar user2684
Browse files

Add option to measure battery level via a pin #23

parent 1d24f1f1
...@@ -915,6 +915,15 @@ void NodeManager::setRetries(int value) { ...@@ -915,6 +915,15 @@ void NodeManager::setRetries(int value) {
void NodeManager::setBatteryReportCycles(int value) { void NodeManager::setBatteryReportCycles(int value) {
_battery_report_cycles = value; _battery_report_cycles = value;
} }
void NodeManager::setBatteryInternalVcc(bool value) {
_battery_internal_vcc = value;
}
void NodeManager::setBatteryPin(int value) {
_battery_pin = value;
}
void NodeManager::setBatteryVoltsPerBit(float value) {
_battery_volts_per_bit = value;
}
#endif #endif
#if SLEEP_MANAGER == 1 #if SLEEP_MANAGER == 1
void NodeManager::setSleepMode(int value) { void NodeManager::setSleepMode(int value) {
...@@ -1145,6 +1154,10 @@ void NodeManager::before() { ...@@ -1145,6 +1154,10 @@ void NodeManager::before() {
#endif #endif
} }
#endif #endif
#if POWER_MANAGER == 1
// set analogReference to internal if measuring the battery through a pin
if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL);
#endif
// setup individual sensors // setup individual sensors
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
if (_sensors[i] == 0) continue; if (_sensors[i] == 0) continue;
...@@ -1300,7 +1313,9 @@ void NodeManager::_process(const char * message) { ...@@ -1300,7 +1313,9 @@ void NodeManager::_process(const char * message) {
// BATTERY: return the battery level // BATTERY: return the battery level
else if (strcmp(message, BATTERY) == 0) { else if (strcmp(message, BATTERY) == 0) {
// measure the board vcc // measure the board vcc
float volt = _getVcc(); float volt = 0;
if (_battery_internal_vcc || _battery_pin == -1) volt = _getVcc();
else volt = analogRead(_battery_pin) * _battery_volts_per_bit;
// calculate the percentage // calculate the percentage
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100; int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
if (percentage > 100) percentage = 100; if (percentage > 100) percentage = 100;
...@@ -1311,7 +1326,7 @@ void NodeManager::_process(const char * message) { ...@@ -1311,7 +1326,7 @@ void NodeManager::_process(const char * message) {
Serial.print(" P="); Serial.print(" P=");
Serial.println(percentage); Serial.println(percentage);
#endif #endif
#if BATTERY_MANAGER == 1 && BATTERY_SENSOR == 1 #if BATTERY_SENSOR == 1
// report battery voltage // report battery voltage
MyMessage battery_msg(BATTERY_CHILD_ID, V_VOLTAGE); MyMessage battery_msg(BATTERY_CHILD_ID, V_VOLTAGE);
_send(battery_msg.set(volt, 2)); _send(battery_msg.set(volt, 2));
......
...@@ -595,6 +595,12 @@ class NodeManager { ...@@ -595,6 +595,12 @@ class NodeManager {
void setBatteryMax(float value); void setBatteryMax(float value);
// after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10) // after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10)
void setBatteryReportCycles(int value); void setBatteryReportCycles(int value);
// 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)
void setBatteryInternalVcc(bool value);
// 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)
void setBatteryPin(int value);
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
void setBatteryVoltsPerBit(float value);
#endif #endif
#if SLEEP_MANAGER == 1 #if SLEEP_MANAGER == 1
// 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 // 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 { ...@@ -644,6 +650,9 @@ class NodeManager {
float _battery_min = 2.6; float _battery_min = 2.6;
float _battery_max = 3.3; float _battery_max = 3.3;
int _battery_report_cycles = 10; int _battery_report_cycles = 10;
bool _battery_internal_vcc = true;
int _battery_pin = -1;
float _battery_volts_per_bit = 0.003363075;
int _cycles = 0; int _cycles = 0;
float _getVcc(); float _getVcc();
#endif #endif
......
...@@ -126,6 +126,12 @@ Node Manager comes with a reasonable default configuration. If you want/need to ...@@ -126,6 +126,12 @@ Node Manager comes with a reasonable default configuration. If you want/need to
void setBatteryMax(float value); void setBatteryMax(float value);
// after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10) // after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10)
void setBatteryReportCycles(int value); void setBatteryReportCycles(int value);
// 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)
void setBatteryInternalVcc(bool value);
// 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)
void setBatteryPin(int value);
// if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
void setBatteryVoltsPerBit(float value);
#endif #endif
#if SLEEP_MANAGER == 1 #if SLEEP_MANAGER == 1
// 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 // 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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment