Skip to content

Commit 65f9465

Browse files
authored
Add internal battery measurement example (#1470)
* Add internal battery measurement example * Use existing hwCPUVoltage() * Make debug output prettier
1 parent ce21a30 commit 65f9465

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
The MySensors Arduino library handles the wireless radio link and protocol
3+
between your home built sensors/actuators and HA controller of choice.
4+
The sensors forms a self healing radio network with optional repeaters. Each
5+
repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
network topology allowing messages to be routed to nodes.
7+
8+
Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
9+
Copyright (C) 2013-2020 Sensnology AB
10+
Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
12+
Documentation: http://www.mysensors.org
13+
Support Forum: http://forum.mysensors.org
14+
15+
This program is free software; you can redistribute it and/or
16+
modify it under the terms of the GNU General Public License
17+
version 2 as published by the Free Software Foundation.
18+
19+
*******************************
20+
21+
DESCRIPTION
22+
23+
This is an example that demonstrates how to report the battery level for a sensor
24+
using the internal measurement method.
25+
Instructions for measuring battery capacity are available here:
26+
http://www.mysensors.org/build/battery
27+
28+
*/
29+
30+
// Enable debug prints to serial monitor
31+
#define MY_DEBUG
32+
33+
// Enable and select radio type attached
34+
#define MY_RADIO_RF24
35+
//#define MY_RADIO_NRF5_ESB
36+
//#define MY_RADIO_RFM69
37+
//#define MY_RADIO_RFM95
38+
39+
#include <MySensors.h>
40+
41+
uint32_t SLEEP_TIME = 900000; // sleep time between reads (seconds * 1000 milliseconds)
42+
int oldBatteryPcnt = 0;
43+
#define FULL_BATTERY 3 // 3V for 2xAA alkaline. Adjust if you use a different battery setup.
44+
45+
void setup()
46+
{
47+
}
48+
49+
void presentation()
50+
{
51+
// Send the sketch version information to the gateway and Controller
52+
sendSketchInfo("Battery Meter", "1.0");
53+
}
54+
55+
void loop()
56+
{
57+
// get the battery Voltage
58+
long batteryMillivolts = hwCPUVoltage();
59+
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
60+
#ifdef MY_DEBUG
61+
Serial.print("Battery voltage: ");
62+
Serial.print(batteryMillivolts / 1000.0);
63+
Serial.println("V");
64+
Serial.print("Battery percent: ");
65+
Serial.print(batteryPcnt);
66+
Serial.println(" %");
67+
#endif
68+
69+
if (oldBatteryPcnt != batteryPcnt) {
70+
sendBatteryLevel(batteryPcnt);
71+
oldBatteryPcnt = batteryPcnt;
72+
}
73+
sleep(SLEEP_TIME);
74+
}

0 commit comments

Comments
 (0)