MySensors Library & Examples  2.3.2-62-ge298769
BatteryPoweredSensor.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-2022 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  * Instructions for measuring battery capacity on A0 are available here:
25  * http://www.mysensors.org/build/battery
26  *
27  */
28 
29 
30 
31 // Enable debug prints to serial monitor
32 #define MY_DEBUG
33 
34 // Enable and select radio type attached
35 #define MY_RADIO_RF24
36 //#define MY_RADIO_NRF5_ESB
37 //#define MY_RADIO_RFM69
38 //#define MY_RADIO_RFM95
39 //#define MY_PJON
40 
41 #include <MySensors.h>
42 
43 int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
44 
45 uint32_t SLEEP_TIME = 900000; // sleep time between reads (seconds * 1000 milliseconds)
46 int oldBatteryPcnt = 0;
47 
48 void setup()
49 {
50  // use the 1.1 V internal reference
51 #if defined(__AVR_ATmega2560__)
52  analogReference(INTERNAL1V1);
53 #else
54  analogReference(INTERNAL);
55 #endif
56 }
57 
59 {
60  // Send the sketch version information to the gateway and Controller
61  sendSketchInfo("Battery Meter", "1.0");
62 }
63 
64 void loop()
65 {
66  // get the battery Voltage
67  int sensorValue = analogRead(BATTERY_SENSE_PIN);
68 #ifdef MY_DEBUG
69  Serial.println(sensorValue);
70 #endif
71 
72  // 1M, 470K divider across battery and using internal ADC ref of 1.1V
73  // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
74  // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
75  // 3.44/1023 = Volts per bit = 0.003363075
76 
77  int batteryPcnt = sensorValue / 10;
78 
79 #ifdef MY_DEBUG
80  float batteryV = sensorValue * 0.003363075;
81  Serial.print("Battery Voltage: ");
82  Serial.print(batteryV);
83  Serial.println(" V");
84 
85  Serial.print("Battery percent: ");
86  Serial.print(batteryPcnt);
87  Serial.println(" %");
88 #endif
89 
90  if (oldBatteryPcnt != batteryPcnt) {
91  // Power up radio after sleep
92  sendBatteryLevel(batteryPcnt);
93  oldBatteryPcnt = batteryPcnt;
94  }
95  sleep(SLEEP_TIME);
96 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: BatteryPoweredSensor.ino:64
presentation
void presentation()
Node presentation.
Definition: BatteryPoweredSensor.ino:58
setup
void setup()
Called after node initialises but before main loop.
Definition: BatteryPoweredSensor.ino:48
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.