Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Macros | Functions | Variables
tracker.ino File Reference
#include <SoftwareSerial.h>
#include <ArduinoOTA.h>
#include <FS.h>
#include "Config.h"
#include "ConfigOverride.h"
#include "Utils.h"
#include "StringList.h"
#include "Gps.h"
#include "Options.h"
#include "Data.h"
#include "Voltage.h"
#include "DeepSleep.h"
#include "WebServer.h"
#include "GsmPower.h"
#include "GsmGps.h"
#include "SmsCmd.h"
#include "Mqtt.h"
#include "BME280.h"

Go to the source code of this file.

Macros

#define SIM808_CONNECTED
 
#define USE_CONFIG_OVERRIDE
 Switch to use ConfigOverride.
 
#define PIN_POWER   D3
 power on/off to DC-DC LM2596
 
#define PIN_BME_GRND   D4
 Ground pin to the BME280 module.
 
#define BME_ADDRESS   0x77
 BME280 port address (Default 0x77, China 0x76)
 
#define PIN_TX   D5
 Transmit-pin to the sim808 RX.
 
#define PIN_RX   D6
 Receive-pin to the sim808 TX.
 

Functions

MyMqtt myMqtt (myGsmGps.gsmClient, myOptions, myData)
 Helper class for the mqtt communication via gsm.
 
void yield (void)
 
void myDebugInfo (String info, bool fromWebserver, bool newline)
 
void myDelayLoop ()
 
long secondsSincePowerOn ()
 
void setup ()
 
void loop ()
 

Variables

MyOptions myOptions
 The global options.
 
MyData myData
 The global collected data.
 
MyVoltage myVoltage (myOptions, myData)
 Helper class for deep sleeps.
 
MyDeepSleep myDeepSleep (myOptions, myData)
 Helper class for deep sleeps.
 
MyWebServer myWebServer (myOptions, myData)
 The Webserver.
 
MyBME280 myBME280 (myOptions, myData, PIN_BME_GRND, BME_ADDRESS)
 Helper class for the BME280 sensor communication.
 
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.
 
MySmsCmd mySmsCmd (myGsmGps, myOptions, myData)
 sms controller class for the sms handling.
 
bool gsmHasPower = false
 Is the DC-DC modul switched on?
 
bool isStarting = false
 Are we in a starting process?
 
bool isStopping = false
 Are we in a stopping process?
 

Detailed Description

Main file with setup() and loop() functions

Definition in file tracker.ino.

Function Documentation

void loop ( )

Main loop function. Read the power supply voltage. Checks for console inputs and send them to the SIM808 modul. Checks for OTA activities. Start or stop the gsm and/or gps activities. Check for receiving sms and process them. Checks the gps and send the overall information to a mqtt server if needed. Starts the deep sleep mode if needed.

Definition at line 174 of file tracker.ino.

References MyGsmGps::begin(), MyData::consoleCmds, gsmHasPower, MyGsmGps::handleClient(), MySmsCmd::handleClient(), MyMqtt::handleClient(), MyWebServer::handleClient(), MyDeepSleep::haveToSleep(), StringList::isEmpty(), MyData::isGsmActive, MyOptions::isMqttEnabled, MyData::isOtaActive, MyOptions::isSmsEnabled, isStarting, isStopping, myBME280, myDeepSleep, myGsmGps, myGsmPower, myMqtt(), mySmsCmd, myVoltage, myWebServer, MyGsmPower::off(), MyGsmPower::on(), MyOptions::powerOn, MyBME280::readValues(), MyVoltage::readVoltage(), StringList::removeHead(), MyGsmGps::sendAT(), MyDeepSleep::sleep(), MyGsmGps::stop(), MyGsmGps::waitingForGps(), MyMqtt::waitingForMqtt(), and yield().

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
void sleep(bool start=true)
Definition: DeepSleep.h:106
bool sendAT(String cmd)
Definition: GsmGps.h:244
bool gsmHasPower
Is the DC-DC modul switched on?
Definition: tracker.ino:73
MyWebServer myWebServer(myOptions, myData)
The Webserver.
StringList consoleCmds
open commands to send to the sim808 module
Definition: Data.h:97
MyOptions myOptions
The global options.
Definition: tracker.ino:57
bool isStopping
Are we in a stopping process?
Definition: tracker.ino:75
bool stop()
Definition: GsmGps.h:219
bool isEmpty()
Definition: StringList.h:66
bool waitingForGps()
Definition: GsmGps.h:238
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
void handleClient()
Definition: SmsCmd.h:80
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.
MyBME280 myBME280(myOptions, myData, PIN_BME_GRND, BME_ADDRESS)
Helper class for the BME280 sensor communication.
void handleClient()
Definition: Mqtt.h:161
void yield(void)
Definition: tracker.ino:83
MyVoltage myVoltage(myOptions, myData)
Helper class for deep sleeps.
bool isSmsEnabled
Is the sms check functionality active?
Definition: Options.h:42
MyMqtt myMqtt(myGsmGps.gsmClient, myOptions, myData)
Helper class for the mqtt communication via gsm.
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 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 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
void myDebugInfo ( String  info,
bool  fromWebserver,
bool  newline 
)

Overwritten Debug Function It logs all the debug calls to the console string-list And call a refresh of the webserver for not blocking the system.

Definition at line 92 of file tracker.ino.

References StringList::addTail(), MyWebServer::handleClient(), MyData::logInfos, myWebServer, StringList::removeTail(), secondsSincePowerOn(), and yield().

Referenced by MyDbg().

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 }
MyWebServer myWebServer(myOptions, myData)
The Webserver.
StringList logInfos
received sim808 answers or other logs
Definition: Data.h:98
void handleClient()
Definition: WebServer.h:184
void yield(void)
Definition: tracker.ino:83
long secondsSincePowerOn()
Definition: tracker.ino:132
String removeTail()
Definition: StringList.h:138
void addTail(String newInfo)
Definition: StringList.h:112
MyData myData
The global collected data.
Definition: tracker.ino:58
void myDelayLoop ( )

Overwritten delay loop for refreshing the webserver on waiting processes.

Definition at line 122 of file tracker.ino.

References MyWebServer::handleClient(), myWebServer, and yield().

Referenced by MyDelay().

123 {
127  delay(1);
128  yield();
129 }
MyWebServer myWebServer(myOptions, myData)
The Webserver.
void handleClient()
Definition: WebServer.h:184
void yield(void)
Definition: tracker.ino:83
long secondsSincePowerOn ( )

Returns the seconds since power up (not since last deep sleep).

Definition at line 132 of file tracker.ino.

References MyData::secondsSincePowerOn().

Referenced by MyDeepSleep::begin(), MyGsmGps::getGps(), MyGsmGps::getGpsFromGsm(), MyGsmGps::handleClient(), MyMqtt::handleClient(), MyDeepSleep::haveToSleep(), myDebugInfo(), secondsElapsed(), secondsElapsedAndUpdate(), and MyDeepSleep::sleep().

133 {
134  return myData.secondsSincePowerOn();
135 }
long secondsSincePowerOn()
Definition: Data.h:187
MyData myData
The global collected data.
Definition: tracker.ino:58
void setup ( )

Main setup function. This is also called after every deep sleep. Do the initialization of every sub-component.

Definition at line 139 of file tracker.ino.

References MyGsmPower::begin(), MyDeepSleep::begin(), MyVoltage::begin(), MyBME280::begin(), MySmsCmd::begin(), MyMqtt::begin(), MyWebServer::begin(), MyOptions::load(), myBME280, MyDbg(), myDeepSleep, myGsmPower, myMqtt(), mySmsCmd, myVoltage, and myWebServer.

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 }
bool begin()
Definition: BME280.h:61
bool load()
Definition: Options.h:108
bool begin()
Definition: WebServer.h:124
MyWebServer myWebServer(myOptions, myData)
The Webserver.
MyOptions myOptions
The global options.
Definition: tracker.ino:57
bool begin()
Definition: Mqtt.h:152
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
MyDeepSleep myDeepSleep(myOptions, myData)
Helper class for deep sleeps.
MyGsmPower myGsmPower(myData, PIN_POWER)
Helper class to switch on/off the sim808 power.
MyBME280 myBME280(myOptions, myData, PIN_BME_GRND, BME_ADDRESS)
Helper class for the BME280 sensor communication.
MyVoltage myVoltage(myOptions, myData)
Helper class for deep sleeps.
MyMqtt myMqtt(myGsmGps.gsmClient, myOptions, myData)
Helper class for the mqtt communication via gsm.
MySmsCmd mySmsCmd(myGsmGps, myOptions, myData)
sms controller class for the sms handling.
bool begin()
Definition: Voltage.h:55
bool begin()
Definition: GsmPower.h:55
bool begin()
Definition: SmsCmd.h:73
bool begin()
Definition: DeepSleep.h:59
void yield ( void  )

***** IMPORTANT ***** It seems that the yield function of ESP does not feed the watch dog timer. So we overwrite the yield() function here. If not the program crashed on some waits. ***** IMPORTANT *****

Definition at line 83 of file tracker.ino.

Referenced by loop(), myDebugInfo(), MyDelay(), and myDelayLoop().

84 {
85  ESP.wdtFeed();
86 }