MySensors Library & Examples  2.3.2-36-g2e00bf6
BatteryPoweredSensorInternal.ino
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 <[email protected]>
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 
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 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: BatteryPoweredSensorInternal.ino:55
presentation
void presentation()
Node presentation.
Definition: BatteryPoweredSensorInternal.ino:49
hwCPUVoltage
uint16_t hwCPUVoltage(void)
setup
void setup()
Called after node initialises but before main loop.
Definition: BatteryPoweredSensorInternal.ino:45
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
sendBatteryLevel
bool sendBatteryLevel(const uint8_t level, const bool requestEcho=false)
MySensors.h
API declaration for MySensors.