Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Data Structures | Macros | Functions
Utils.h File Reference

Go to the source code of this file.

Data Structures

class  SerialOut
 

Macros

#define POLY   0xedb88320
 CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order.
 

Functions

long secondsSincePowerOn ()
 
bool secondsElapsed (long &lastCheckSec, const long &intervalSec)
 
bool secondsElapsedAndUpdate (long &lastCheckSec, const long &intervalSec)
 
long crc32 (long crc, unsigned char *buf, size_t len)
 
void myDebugInfo (String info, bool isWebServer, bool newline)
 
void MyDbg (String info, bool fromWebServer=false, bool newline=true)
 
void myDelayLoop ()
 
void MyDelay (long millisDelay)
 
String WifiGetRssiAsQuality (int rssi)
 
String TextToUrl (String data)
 
String TextToXml (String data)
 
String Trim (const String &data, const String &chars)
 
String formatInterval (long secs)
 
bool scanInterval (String interval, long &secs)
 
void SetupOTA ()
 

Detailed Description

A collection of utility functions.

Definition in file Utils.h.

Function Documentation

long crc32 ( long  crc,
unsigned char *  buf,
size_t  len 
)

Simple crc function. Can multiple called but the first time crc should be 0.

Definition at line 77 of file Utils.h.

References POLY.

Referenced by MyData::RtcData::getCRC().

78 {
79  crc = ~crc;
80  while (len--) {
81  crc ^= *buf++;
82  for (int k = 0; k < 8; k++)
83  crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
84  }
85  return ~crc;
86 }
#define POLY
CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order.
Definition: Utils.h:74
String formatInterval ( long  secs)

Helper function to format seconds to x days hours:minutes:seconds

Definition at line 191 of file Utils.h.

Referenced by MyMqtt::handleClient(), MyWebServer::handleLoadInfoInfo(), MyWebServer::handleLoadMainInfo(), and MyWebServer::handleLoadSettingsInfo().

192 {
193  char buff[255];
194 
195  int days = secs / 60 / 60 / 24;
196  int hours = (secs / 60 / 60) % 24;
197  int minutes = (secs / 60) % 60;
198  int seconds = secs % 60;
199 
200  buff[0] = '\0';
201  if (days <= 0) {
202  sprintf(buff, "%02d:%02d:%02d", hours, minutes, seconds);
203  } else {
204  sprintf(buff, "%d %02d:%02d:%02d", days, hours, minutes, seconds);
205  }
206  return buff;
207 }
void MyDbg ( String  info,
bool  fromWebServer = false,
bool  newline = true 
)
void myDebugInfo ( String  info,
bool  fromWebserver,
bool  newline 
)

This function has to be overwritten to implement the handle of debug informations.

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 MyDelay ( long  millisDelay)

Replacement with background calls when we have to wait. Replace the delay function.

Definition at line 104 of file Utils.h.

References myDelayLoop(), and yield().

Referenced by MyGsmGps::begin(), MyWebServer::begin(), MyMqtt::handleClient(), MyWebServer::loadRestart(), and MyGsmPower::on().

105 {
106  long m = millis();
107 
108  while (millis() - m < millisDelay) {
109  myDelayLoop();
110  yield();
111  delay(1);
112  }
113 }
void yield(void)
Definition: tracker.ino:83
void myDelayLoop()
Definition: tracker.ino:122
void myDelayLoop ( )

This function has to be overwritten to implement background delay calls.

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
bool scanInterval ( String  interval,
long &  secs 
)

Helper function to scan a interval information '[days] hours:minutes:seconds'

Definition at line 210 of file Utils.h.

References Trim().

Referenced by MyWebServer::GetOption().

211 {
212  int first = -1;
213 
214  interval = Trim(interval, F(" "));
215  first = interval.indexOf(F(":"));
216  if (first != -1) {
217  String daysString;
218  String hoursString;
219  String minutesString;
220  String secondsString;
221  long days = 0;
222  long hours = 0;
223  long minutes = 0;
224  long seconds = 0;
225 
226  int second = interval.indexOf(F(":"), first + 1);
227 
228  if (second != -1) {
229  int space = interval.indexOf(F(" "));
230 
231  if (space != -1 && space < first) {
232  daysString = interval.substring(0, space);
233  hoursString = interval.substring(space + 1, first);
234  } else {
235  hoursString = interval.substring(0, first);
236  }
237  minutesString = interval.substring(first + 1, second);
238  secondsString = interval.substring(second + 1);
239 
240  days = atol(daysString.c_str());
241  hours = atol(hoursString.c_str());
242  minutes = atol(minutesString.c_str());
243  seconds = atol(secondsString.c_str());
244 
245  if (days >= 0 &&
246  hours >= 0 && hours <= 23 &&
247  minutes >= 0 && minutes <= 59 &&
248  seconds >= 0 && seconds <= 59) {
249  secs = 0;
250  secs += days * 24 * 60 * 60;
251  secs += hours * 60 * 60;
252  secs += minutes * 60;
253  secs += seconds;
254  return true;
255  }
256  }
257  }
258  return false;
259 }
String Trim(const String &data, const String &chars)
Definition: Utils.h:168
bool secondsElapsed ( long &  lastCheckSec,
const long &  intervalSec 
)

Checks if the intervalSec is from the last checkIntervalSec elapsed

Definition at line 52 of file Utils.h.

References secondsSincePowerOn().

Referenced by MyGsmGps::handleClient(), MyMqtt::handleClient(), and MyMqtt::waitingForMqtt().

53 {
54  long currentSec = secondsSincePowerOn();
55 
56  if (lastCheckSec == 0 || (currentSec - lastCheckSec > intervalSec)) {
57  return true;
58  }
59  return false;
60 }
long secondsSincePowerOn()
Definition: tracker.ino:132
bool secondsElapsedAndUpdate ( long &  lastCheckSec,
const long &  intervalSec 
)

Checks if the intervalSec is from the last checkIntervalSec elapsed and if true it sets the lastCheckSec value

Definition at line 63 of file Utils.h.

References secondsSincePowerOn().

Referenced by MyGsmGps::handleClient(), MySmsCmd::handleClient(), and MyBME280::readValues().

64 {
65  long currentSec = secondsSincePowerOn();
66 
67  if (lastCheckSec == 0 || (currentSec - lastCheckSec > intervalSec)) {
68  lastCheckSec = currentSec;
69  return true;
70  }
71  return false;
72 }
long secondsSincePowerOn()
Definition: tracker.ino:132
long secondsSincePowerOn ( )

This function has to be overwritten to return the seconds since power up (not since last deep sleep).

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 SetupOTA ( )

Helper function to start the OTA functionality of the ESP.

Definition at line 264 of file Utils.h.

References MyDbg().

Referenced by MyWebServer::loadUpdate().

265 {
266  MyDbg(F("StartOTA"));
267 
268  ArduinoOTA.setHostname("SnorkTracker");
269 
270  ArduinoOTA.setPort(8266);
271 
272  ArduinoOTA.onStart([]() {
273  MyDbg(F("OTA Start"));
274  });
275  ArduinoOTA.onEnd([]() {
276  MyDbg(F("\nOTA End"));
277  });
278  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
279  MyDbg("OTA Progress: " + String(progress / (total / 100)));
280  });
281  ArduinoOTA.onError([](ota_error_t error) {
282  MyDbg("OTA Error[%u]: " + error);
283  if (error == OTA_AUTH_ERROR) MyDbg(F("OTA Auth Failed"));
284  else if (error == OTA_BEGIN_ERROR) MyDbg(F("OTA Begin Failed"));
285  else if (error == OTA_CONNECT_ERROR) MyDbg(F("OTA Connect Failed"));
286  else if (error == OTA_RECEIVE_ERROR) MyDbg(F("OTA Receive Failed"));
287  else if (error == OTA_END_ERROR) MyDbg(F("OTA End Failed"));
288  });
289 }
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
String TextToUrl ( String  data)

Convert Text to html - URL. Should be decoded with encodeURIComponent() in JavaScript And replace every invalid xml char with '?'.

Definition at line 137 of file Utils.h.

Referenced by MyWebServer::handleLoadConsoleInfo().

138 {
139  data.replace(F("%"), F("%25"));
140  data.replace(F("&"), F("%26"));
141  data.replace(F("<"), F("%3C"));
142  data.replace(F(">"), F("%3E"));
143 
144  for (int i = 0; i < data.length(); i++) {
145  bool validChar = (data[i] == 0x09 || data[i] == 0x0A || data[i] == 0x0D || (data[i] >= 0x20 && data[i] <= 0xFF));
146 
147  if (!validChar) {
148  data[i] = '?';
149  }
150  }
151  return data;
152 }
String TextToXml ( String  data)

Helper HTML text conversation function for special character.

Definition at line 156 of file Utils.h.

Referenced by MyWebServer::AddIntervalInfo(), MyWebServer::AddOption(), and MyWebServer::AddTableTr().

157 {
158  data.replace(F("&"), F("&amp;"));
159  data.replace(F("<"), F("&lt;"));
160  data.replace(F(">"), F("&gt;"));
161  data.replace(F("\""), F("&quot;"));
162  return data;
163 }
String Trim ( const String &  data,
const String &  chars 
)

Trims the data string on the left and right side every occurrence of a char from chars.

Definition at line 168 of file Utils.h.

Referenced by MyGsmSim808::getSMS(), and scanInterval().

169 {
170  String ret = data;
171 
172  for (int i = 0; i < ret.length(); i++) {
173  if (chars.indexOf(ret[i]) != -1) {
174  ret.remove(i, 1);
175  i--;
176  continue;
177  }
178  break;
179  }
180  for (int i = data.length() - 1; i >= 0; i--) {
181  if (chars.indexOf(ret[i]) != -1) {
182  ret.remove(i, 1);
183  continue;
184  }
185  break;
186  }
187  return ret;
188 }
String WifiGetRssiAsQuality ( int  rssi)

Conversion of the RSSI value to a quality value.

Definition at line 119 of file Utils.h.

Referenced by MyWebServer::begin(), MyMqtt::handleClient(), MyWebServer::handleLoadInfoInfo(), and MyWebServer::handleLoadMainInfo().

120 {
121  int quality = 0;
122 
123  if (rssi <= -100) {
124  quality = 0;
125  } else if (rssi >= -50) {
126  quality = 100;
127  } else {
128  quality = 2 * (rssi + 100);
129  }
130  return String(quality);
131 }