Snorktracker
 All Data Structures Files Functions Variables Macros Pages
tracker.ino
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 */
24 #include <SoftwareSerial.h>
25 #include <ArduinoOTA.h>
26 #include <FS.h>
27 
28 #define SIM808_CONNECTED
29 
30 #include "Config.h"
31 #define USE_CONFIG_OVERRIDE
32 #ifdef USE_CONFIG_OVERRIDE
33  #include "ConfigOverride.h"
34 #endif
35 
36 #include "Utils.h"
37 #include "StringList.h"
38 #include "Gps.h"
39 #include "Options.h"
40 #include "Data.h"
41 #include "Voltage.h"
42 #include "DeepSleep.h"
43 #include "WebServer.h"
44 #include "GsmPower.h"
45 #include "GsmGps.h"
46 #include "SmsCmd.h"
47 #include "Mqtt.h"
48 #include "BME280.h"
49 
50 
51 #define PIN_POWER D3
52 #define PIN_BME_GRND D4
53 #define BME_ADDRESS 0x77
54 #define PIN_TX D5
55 #define PIN_RX D6
56 
59 MyVoltage myVoltage(myOptions, myData);
60 MyDeepSleep myDeepSleep(myOptions, myData);
61 MyWebServer myWebServer(myOptions, myData);
62 MyBME280 myBME280(myOptions, myData, PIN_BME_GRND, BME_ADDRESS);
63 
64 #ifdef SIM808_CONNECTED
66  MyGsmGps myGsmGps(myOptions, myData, PIN_RX, PIN_TX);
67  MySmsCmd mySmsCmd(myGsmGps, myOptions, myData);
68  MyMqtt myMqtt(myGsmGps.gsmClient, myOptions, myData);
69 #else
70  MyMqtt myMqtt(MyWebServer::server.wifiClient(), myOptions, myData);
71 #endif
72 
73 bool gsmHasPower = false;
74 bool isStarting = false;
75 bool isStopping = false;
76 
83 void yield(void)
84 {
85  ESP.wdtFeed();
86 }
87 
92 void myDebugInfo(String info, bool fromWebserver, bool newline)
93 {
94  static bool lastNewLine = true;
95 
96  if (newline || lastNewLine != newline) {
97  String secs = String(secondsSincePowerOn()) + F(": ");
98 
99  myData.logInfos.addTail(secs + info);
100  if (!lastNewLine) {
101  Serial.println("");
102  }
103  Serial.println(secs + info);
104  } else {
105  String tmp = myData.logInfos.removeTail();
106 
107  Serial.print(tmp + info);
108  myData.logInfos.addTail(tmp + info);
109  }
110  lastNewLine = newline;
111 
112  if (!fromWebserver) {
116  }
117  delay(1);
118  yield();
119 }
120 
123 {
127  delay(1);
128  yield();
129 }
130 
133 {
134  return myData.secondsSincePowerOn();
135 }
136 
139 void setup()
140 {
141  Serial.begin(115200);
142  delay(1000);
143 
144  MyDbg(F("Start SnorkTracker ..."));
145 
146 #ifdef SIM808_CONNECTED
147  myGsmPower.begin();
148 #endif
149  SPIFFS.begin();
150  myOptions.load();
151  myVoltage.begin();
152 
153  // Back to deep sleep?
154  myDeepSleep.begin();
155 
156  // no deep sleep!
157  myWebServer.begin();
158  myMqtt.begin();
159 #ifdef SIM808_CONNECTED
160  mySmsCmd.begin();
161 #endif
162  myBME280.begin();
163 }
164 
174 void loop()
175 {
178 
180 
181  if (myData.isOtaActive) {
182  ArduinoOTA.handle();
183  }
184 
185 #ifndef SIM808_CONNECTED
186  if (myOptions.isMqttEnabled) {
188  }
189 
190  if (!myMqtt.waitingForMqtt()) {
191  if (myDeepSleep.haveToSleep()) {
192  WiFi.disconnect();
193  WiFi.mode(WIFI_OFF);
194  yield();
195  myDeepSleep.sleep();
196  }
197  }
198 #else
199  if (!myData.consoleCmds.isEmpty()) {
200  String cmd = myData.consoleCmds.removeHead();
201 
202  myGsmGps.sendAT(cmd);
203  }
204 
205  // Starting gsm ?
206  if (myOptions.powerOn && !gsmHasPower) {
207  if (!isStarting && !isStopping) {
208  isStarting = true;
209  myGsmPower.on();
210  gsmHasPower = true;
211  if (!myGsmGps.begin()) {
212  myGsmGps.stop();
213  myGsmPower.off();
214  gsmHasPower = false;
215  }
216  isStarting = false;
217  }
218  }
219 
220  // Stopping gsm ?
221  if (!myOptions.powerOn && gsmHasPower) {
222  if (!isStarting && !isStopping) {
223  isStopping = true;
224  myGsmGps.stop();
225  myGsmPower.off();
226  gsmHasPower = false;
227  isStopping = false;
228  }
229  }
230 
231  // Do gsm processing.
232  if (gsmHasPower && !isStarting && !isStopping) {
234 
235  // No sms or mqtt if we are waiting for a gps position.
236  // Otherwise we are sending invalid gps values.
237  if (!myGsmGps.waitingForGps()) {
238  if (myOptions.isSmsEnabled) {
240  }
241  if (myOptions.isMqttEnabled) {
243  }
244  }
245  }
246 
247  // Deep Sleep?
248  // (No deep sleep if we are waiting for a valid gps position).
250  if (myDeepSleep.haveToSleep()) {
251  if (myData.isGsmActive) {
252  myGsmGps.stop();
253  }
254  myGsmPower.off();
255  WiFi.disconnect();
256  WiFi.mode(WIFI_OFF);
257  yield();
258  myDeepSleep.sleep();
259  }
260  }
261 
262 #endif
263 
264  yield();
265  delay(10);
266 }
bool readValues()
Definition: BME280.h:73
bool begin()
Definition: BME280.h:61
void myDelayLoop()
Definition: tracker.ino:122
bool load()
Definition: Options.h:108
void sleep(bool start=true)
Definition: DeepSleep.h:106
bool sendAT(String cmd)
Definition: GsmGps.h:244
bool begin()
Definition: WebServer.h:124
bool gsmHasPower
Is the DC-DC modul switched on?
Definition: tracker.ino:73
MyWebServer myWebServer(myOptions, myData)
The Webserver.
TinyGsmClient gsmClient
Gsm client interface.
Definition: GsmGps.h:41
StringList consoleCmds
open commands to send to the sim808 module
Definition: Data.h:97
MyOptions myOptions
The global options.
Definition: tracker.ino:57
void setup()
Definition: tracker.ino:139
bool isStopping
Are we in a stopping process?
Definition: tracker.ino:75
bool stop()
Definition: GsmGps.h:219
Definition: Data.h:27
bool isEmpty()
Definition: StringList.h:66
StringList logInfos
received sim808 answers or other logs
Definition: Data.h:98
bool waitingForGps()
Definition: GsmGps.h:238
bool begin()
Definition: Mqtt.h:152
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
#define PIN_POWER
power on/off to DC-DC LM2596
Definition: tracker.ino:51
String removeHead()
Definition: StringList.h:123
void handleClient()
Definition: GsmGps.h:168
void on()
Definition: GsmPower.h:63
bool haveToSleep()
Definition: DeepSleep.h:90
bool powerOn
Is the GSM power from the DC-DC modul switched on?
Definition: Options.h:41
void handleClient()
Definition: WebServer.h:184
void readVoltage()
Definition: Voltage.h:67
#define PIN_RX
Receive-pin to the sim808 TX.
Definition: tracker.ino:55
void handleClient()
Definition: SmsCmd.h:80
Definition: Mqtt.h:54
#define PIN_BME_GRND
Ground pin to the BME280 module.
Definition: tracker.ino:52
#define PIN_TX
Transmit-pin to the sim808 RX.
Definition: tracker.ino:54
MyDeepSleep myDeepSleep(myOptions, myData)
Helper class for deep sleeps.
MyGsmPower myGsmPower(myData, PIN_POWER)
Helper class to switch on/off the sim808 power.
MyGsmGps myGsmGps(myOptions, myData, PIN_RX, PIN_TX)
sim808 gsm/gps communication class.
#define BME_ADDRESS
BME280 port address (Default 0x77, China 0x76)
Definition: tracker.ino:53
MyBME280 myBME280(myOptions, myData, PIN_BME_GRND, BME_ADDRESS)
Helper class for the BME280 sensor communication.
void myDebugInfo(String info, bool fromWebserver, bool newline)
Definition: tracker.ino:92
void handleClient()
Definition: Mqtt.h:161
void yield(void)
Definition: tracker.ino:83
long secondsSincePowerOn()
Definition: tracker.ino:132
String removeTail()
Definition: StringList.h:138
MyVoltage myVoltage(myOptions, myData)
Helper class for deep sleeps.
bool isSmsEnabled
Is the sms check functionality active?
Definition: Options.h:42
void loop()
Definition: tracker.ino:174
MyMqtt myMqtt(myGsmGps.gsmClient, myOptions, myData)
Helper class for the mqtt communication via gsm.
void addTail(String newInfo)
Definition: StringList.h:112
MySmsCmd mySmsCmd(myGsmGps, myOptions, myData)
sms controller class for the sms handling.
bool isMqttEnabled
Should the system connect to a MQTT server?
Definition: Options.h:54
bool begin()
Definition: GsmGps.h:84
bool begin()
Definition: Voltage.h:55
bool isStarting
Are we in a starting process?
Definition: tracker.ino:74
bool isGsmActive
Is the sim808 modul connected to a gsm network?
Definition: Data.h:68
bool begin()
Definition: GsmPower.h:55
long secondsSincePowerOn()
Definition: Data.h:187
bool begin()
Definition: SmsCmd.h:73
bool waitingForMqtt()
Definition: Mqtt.h:135
void off()
Definition: GsmPower.h:74
MyData myData
The global collected data.
Definition: tracker.ino:58
bool isOtaActive
Is OverTheAir update active?
Definition: Data.h:65
bool begin()
Definition: DeepSleep.h:59