Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Voltage.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #define ANALOG_FACTOR 0.03
24 
25 
28 class MyVoltage
29 {
30 protected:
33 
35 
36 public:
37  MyVoltage(MyOptions &options, MyData &data);
38 
39  bool begin();
40 
41  void readVoltage();
42 };
43 
44 /* ******************************************** */
45 
48  : myOptions(options)
49  , myData(data)
50  , lowPowerStartSec(0)
51 {
52 }
53 
56 {
57  MyDbg(F("MyVoltage::begin"));
58  pinMode(A0, INPUT);
59  myData.voltage = ANALOG_FACTOR * analogRead(A0); // Volt
61  lowPowerStartSec = millis() / 1000;
62 }
63 
68 {
69  bool isLowPower = false;
70  long currSec = millis() / 1000;
71 
72  myData.voltage = ANALOG_FACTOR * analogRead(A0); // Volt
74 
75  if (myData.isLowPower && !isLowPower) { // Change to high power
76  long lowPowerSec = currSec - lowPowerStartSec;
77 
78  myData.rtcData.lowPowerActiveTimeSec += lowPowerSec;
79  if (myData.isPowerOn) {
80  myData.rtcData.lowPowerPowerOnTimeSec += lowPowerSec;
81  }
82  MyDbg((String) F("Change to high power (V): ") + String(myData.voltage, 2));
83  }
84  if (!myData.isLowPower && isLowPower) { // Change to low power
85  lowPowerStartSec = currSec;
86  MyDbg((String) F("Change to low power (V): ") + String(myData.voltage, 2));
87  }
88  myData.isLowPower = isLowPower;
89 }
double powerSaveModeVoltage
Minimum voltage to stay always alive.
Definition: Options.h:50
long lowPowerPowerOnTimeSec
Timestamp of the last deep sleep start.
Definition: Data.h:43
class MyData::RtcData rtcData
Data to store in the RTC memory.
MyOptions myOptions
The global options.
Definition: tracker.ino:57
Definition: Data.h:27
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
void readVoltage()
Definition: Voltage.h:67
long lowPowerActiveTimeSec
Timestamp of the last deep sleep start.
Definition: Data.h:42
long lowPowerStartSec
Switch off timestamp.
Definition: Voltage.h:34
bool isPowerOn
Is the power of the sim808 switched on?
Definition: Data.h:66
double voltage
Current supply voltage.
Definition: Data.h:74
MyVoltage(MyOptions &options, MyData &data)
Definition: Voltage.h:47
MyOptions & myOptions
Reference to global options.
Definition: Voltage.h:31
MyData & myData
Reference to global data.
Definition: Voltage.h:32
#define ANALOG_FACTOR
Factor to the analog voltage divider.
Definition: Voltage.h:23
bool begin()
Definition: Voltage.h:55
MyData myData
The global collected data.
Definition: tracker.ino:58
bool isLowPower
Is the power below min voltage?
Definition: Data.h:67