MySensors Library & Examples  2.3.2
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-2019 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 
40 #include <MySensors.h>
41 
42 int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
43 
44 uint32_t SLEEP_TIME = 900000; // sleep time between reads (seconds * 1000 milliseconds)
45 int oldBatteryPcnt = 0;
46 
47 void setup()
48 {
49  // use the 1.1 V internal reference
50 #if defined(__AVR_ATmega2560__)
51  analogReference(INTERNAL1V1);
52 #else
53  analogReference(INTERNAL);
54 #endif
55 }
56 
58 {
59  // Send the sketch version information to the gateway and Controller
60  sendSketchInfo("Battery Meter", "1.0");
61 }
62 
63 void loop()
64 {
65  // get the battery Voltage
66  int sensorValue = analogRead(BATTERY_SENSE_PIN);
67 #ifdef MY_DEBUG
68  Serial.println(sensorValue);
69 #endif
70 
71  // 1M, 470K divider across battery and using internal ADC ref of 1.1V
72  // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
73  // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
74  // 3.44/1023 = Volts per bit = 0.003363075
75 
76  int batteryPcnt = sensorValue / 10;
77 
78 #ifdef MY_DEBUG
79  float batteryV = sensorValue * 0.003363075;
80  Serial.print("Battery Voltage: ");
81  Serial.print(batteryV);
82  Serial.println(" V");
83 
84  Serial.print("Battery percent: ");
85  Serial.print(batteryPcnt);
86  Serial.println(" %");
87 #endif
88 
89  if (oldBatteryPcnt != batteryPcnt) {
90  // Power up radio after sleep
91  sendBatteryLevel(batteryPcnt);
92  oldBatteryPcnt = batteryPcnt;
93  }
94  sleep(SLEEP_TIME);
95 }
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
API declaration for MySensors.
bool sendBatteryLevel(const uint8_t level, const bool requestEcho=false)
void setup()
Called after node initialises but before main loop.
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
void presentation()
Node presentation.
void loop()
Main loop.