-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABoatMon.ino
428 lines (356 loc) · 11 KB
/
ABoatMon.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Greg Cope <gregcope@gmail.com>
* see; https://github.com/gregcope/ABoatMon
*
* Allot of code pinched from;
* https://github.com/LowPowerLab/RFM69/blob/master/Examples/MotionMote/MotionMote.ino
*
* TODO
* Look at https://github.com/cdl1051/DS18B20_NROO/blob/master/DS18B20.h
* https://lowpowerlab.com/forum/projects/temperature-sensing-with-ds18b20/msg18040/#msg18040
* https://lowpowerlab.com/forum/moteino/improvedoptimized-ds18b201wire-read/msg14975/#msg14975
* s
*
*/
// External includes
// https://github.com/mikalhart/TinyGPSPlus/releases
#include <TinyGPS++.h>
//#include <NMEAGPS.h>
// internal classes/includes
//#include "Config.h"
#include "Button.h"
#include "Sleep.h"
#include "Gps.h"
//#include "Gns.h"
#include "Lipo.h"
#include "Vcc.h"
#include "Modem.h"
#include "Led.h"
#include "Temp.h"
// PIN defines
#define MEGA_LED 15
#define D12_GPS_ENABLE 12
#define BUTTON_LED 26
#define BUTTON_SWITCH 25
#define BILGE_SWITCH 3 // Other line from BilgeSwitch to GND
//#define LIPO_VOLTAGE_DIVIDER 24 // D24 is same as A0
const int LIPO_VOLTAGE_DIVIDER = 0;
// DS18B20
// Red connects to 27, Blue/Black connects to ground and Yellow/White (data) to 28
#define TEMP_POWER 27 // PA3
#define TEMP_DATA 28 // PA4
#define VCC_12V_24V_VOLTAGE_DIVIDER A5
#define VCC_12V_24V_ENABLE 19
#define FONA_KEY 14
#define FONA_PS 18
#define FONA_NETSTAT 13
//oled https://lowpowerlab.com/forum/moteinomega/moteino-(mega)-oled-and-u8g/msg7305/#msg7305
#define OLED_SDA 17
#define OLED_SCL 16
#define OLED_POWER 20
// Static defines
#define FOURMIN_CYCLES 30 // 8 sec sleep * 30 cycles = 240 secs or 4 mins
#define HOUR_CYCLES 450 // 8 sec sleep * 450 cyles == 3600 secs or 1 hour
#define DAILY_CYCLES 10800 // 8 sec sleep * 10800 cycles == 86400 secs or 1 day
//#define INITIAL_GPS_FIX_TIMEOUT_MSECS 300000 // time to try and get a fix in msecs is 300 secs, or 5 mins
//#define INITIAL_GPS_FIX_TIMEOUT_MSECS 600000 // time to try and get a fix in msecs is 600 secs, or 10 mins
#define INITIAL_GPS_FIX_TIMEOUT_MSECS 900000 // time to try and get a fix in msecs is 900 secs, or 15 mins
#define INITIAL_GPS_FIX_TIMEOUT_MSECS 1200000 // time to try and get a fix in msecs is 1200 secs, or 20 mins
//#define UPDATE_GPS_FIX_TIMEOUT_MSECS 15000 // 15 secs
#define UPDATE_GPS_FIX_TIMEOUT_MSECS 60000 // 60 secs
#define UPDATE_GPS_NUMBER_OF_FIXES 30 // 10 secs
// Alarm settings
#define LOW_TEMP_ALARM 2 // or could be 0 or -1
#define HIGH_TEMP_ALARM 85
#define REG_OFF_VOLTS 12.8
#define REG_ON_VOLTS 13
#define VCC_LOW_ALARM 11.2
#define LIPO_BATTERY_ALARM 3.1
// debug functions
#define DEBUG(input) {Serial.print(input); Serial.flush();}
#define DEBUGln(input) {Serial.println(input); Serial.flush();}
// Objects
// tinyGPS is a nmea feed parser
Sleep sleep;
Gps gps(D12_GPS_ENABLE);
//Gns gns(D12_GPS_ENABLE);
Lipo lipo(LIPO_VOLTAGE_DIVIDER);
Vcc vcc(VCC_12V_24V_ENABLE, VCC_12V_24V_VOLTAGE_DIVIDER);
Modem modem(FONA_KEY);
Led megaLed(MEGA_LED);
Led switchLed(BUTTON_LED);
Temp temp(TEMP_POWER, TEMP_DATA);
Button bilgeSwitch(BILGE_SWITCH);
Button switchButton(BUTTON_SWITCH);
// Cycle Vars
unsigned long cycleCount = 0;
//byte fourMinCycleCount = 0;
//unsigned int hourCycleCount = 0;
//unsigned long NOW = 0;
// GPS / Nmea vars
//unsigned long nmeaSentenceTimeOutMs = 0;
//boolean nmeaSentenceTimeoutReached = false;
//double fixLat = 181; // invalid Lat
//double fixLng = 181; // invalid Lat
//double alarmLat = 181; // invalid Lat
//double alarmLng = 181; // invalid Lat
double orgLat = 0; // Last recorded Lat
double orgLon = 0; // Last recorded lon
double newLat = 0;
double newLon = 0;
double distance = 10000; // distance moved (default)
// LipoBattery, Vcc and Temp variables & buffer strings
float lipoVolts = 0;
char lipoStr[10];
float vccVolts= 0;
char vccStr[10];
char tempStr[11];
float tempInC;
char bilgeStr[4];
char latStr[30]; // lat string
char lonStr[30]; // lon string
char distanceStr[30];
//char disStr[6]; // in 99999m distance in 24hrs ... would not work in a fast yacht!
// Message varriables
//String messageStr = "";
char messageStr[200];
char dateTimeStr[19];
boolean sendLipoMessage = false;
boolean sendHighTempMessage = false;
boolean sendLowTempMessage = false;
boolean sendBilgeMessage = false;
boolean sendNoGpsFixMessage = false;
boolean sendGeoFenceMessage = false;
boolean sendVccMessage = false;
boolean sendDailyMessageFlag = false;
//
// Code from here on ...
//
void setup() {
Serial.begin(9600);
Serial.flush();
DEBUGln("setup Start 15");
modem.on();
//modemOnMillis = millis();
temp.init();
//yep burn CPU for 1/2 sec... to let stuff settle
delay(500);
gps.init();
// gns.init(); // don't need a neoGPS init - not using antenna output
gps.getInitialFix(INITIAL_GPS_FIX_TIMEOUT_MSECS);
//gns.getInitialFix(INITIAL_GPS_FIX_TIMEOUT_MSECS);
DEBUGln("setup Done");
}
void loop() {
// start of loop
megaLed.on();
DEBUGln("loop ...");
// assign some defaults
sprintf(lipoStr, "LIPO:5.0v");
sprintf(vccStr, "VCC:50.0v");
sprintf(tempStr, "TEMP:99.9c");
sprintf(bilgeStr, "B:F");
tempInC = -100;
// do checks
doShortChecks();
doHourlyChecks();
sendDailyMessage();
// if we need to
sendMessage();
// done
megaLed.off();
sleep.kip8Secs();
}
void checkLipo(void) {
// read lipo battery
lipoVolts = lipo.read();
// check if alarm
if ( lipoVolts <= LIPO_BATTERY_ALARM ) {
sendLipoMessage = true;
}
// format string ... even if we do not need it
dtostrf(lipoVolts,3,1,lipoStr);
//DEBUG("lipoVolts: ");
//DEBUGln(lipoVolts);
}
void checkVcc(void) {
// read Vcc and enable regulator
// do this each short iteration as we
// will have to wait for temp anyway
// so might either micro sleep or do something useful!
vccVolts = vcc.read();
DEBUG("vcc is: ");
DEBUG(vccVolts);
DEBUGln("V");
// check reg
if ( vccVolts > REG_ON_VOLTS ) {
vcc.regOn();
} else if (vccVolts < REG_OFF_VOLTS ) {
vcc.regOff();
}
if (vccVolts < VCC_LOW_ALARM ) {
//DEBUGln("Vcc is too low!!!");
sendVccMessage = true;
// disbale regulator
}
// format string ... even if we do not need it
dtostrf(vccVolts,3,1,vccStr);
//DEBUG("vccVolts: ");
//DEBUGln(vccVolts);
}
void checkBilge(void) {
// check bilge switch
if ( bilgeSwitch.isClosed() ) {
// oh no ....
sendBilgeMessage = true;
sprintf(bilgeStr, "B:T");
}
//DEBUG("bilgeStr: ");
//DEBUGln(bilgeStr);
}
void checkReadTemp(void) {
// blocking read temp
tempInC = temp.read();
if ( tempInC >= HIGH_TEMP_ALARM ) {
sendHighTempMessage = true;
DEBUGln("HIGH TEMP ALARM");
} else if ( tempInC <= LOW_TEMP_ALARM ) {
sendLowTempMessage = true;
DEBUGln("LOW TEMP ALARM");
}
dtostrf(tempInC,3,1,tempStr);
//DEBUG("tempStr: ");
//DEBUGln(tempStr);
}
boolean doShortChecks(void) {
// function to do short checks each time
// returns wether to send a message
DEBUGln("doShortChecks");
checkAsyncTemp();
checkLipo();
checkVcc();
checkBilge();
checkReadTemp();
checkLocation();
// update cyclecount and return
cycleCount++;
return true;
}
void checkAsyncTemp(void) {
// kick off temp conversion
temp.startConvert();
}
//boolean doLongChecks(void) {
// function to do long checks
// returns weather to send a message
//if ( cycleCount <= FOURMIN_CYCLES ) {
// if not time yet, return false
//return false;
//}
// otherwise time to do long checks
//DEBUGln("doLongChecks: ");
//checkLocation();
//gps.getUpdatedFix(UPDATE_GPS_FIX_TIMEOUT_MSECS, UPDATE_GPS_NUMBER_OF_FIXES);
//return true;
//}
boolean doHourlyChecks(void) {
// function to do hourly checks
// returns wether to send a message
if ( cycleCount <= HOUR_CYCLES ) {
DEBUG("cycleCount is: ");
DEBUG(cycleCount);
DEBUGln(", doHourlyChecks : Not running now!");
// if not time yet, return false
return false;
}
// time to do hour checks
DEBUGln("doHourlyChecks: ");
checkLocation();
// TODO: calc number of cycles left to get to hourly checks
// send a message?
return true;
}
boolean sendDailyMessage(void) {
if ( cycleCount <= DAILY_CYCLES ) {
DEBUG("cycleCount is: ");
DEBUG(cycleCount);
DEBUGln(", sendDailyMessage : Not running now!");
// if not time yet, return false
return false;
}
// function to do daily checks
// always returns true as we want to send a message
DEBUGln("set sendDailyMessageFlag to true");
// send a message?
sendDailyMessageFlag = true;
return true;
}
void checkLocation(void) {
// update location
// TODO: Logic if no updated fix
gps.getUpdatedFix(UPDATE_GPS_FIX_TIMEOUT_MSECS, UPDATE_GPS_NUMBER_OF_FIXES);
// check location vs last location
// work out distance(orgLat, orgLon, lat, lon);
//newLat = gps.getLat();
//newLon = gps.getLon();
gps.getLocation(newLat, newLon);
// distance = gps.distanceMoved(orgLat, orgLon);
// distance = gps.haversine(newLat, newLon, orgLat, orgLon);
gps.distanceMoved(orgLat, orgLon, distance);
// save where we are now
orgLat = newLat;
orgLon = newLon;
// convert Double to string
// https://ukhas.org.uk/guides:common_coding_errors_payload_testing
dtostrf(newLat, 10, 6, latStr); //first number is length, last is numbers after decimal
dtostrf(newLon, 10, 6, lonStr);
dtostrf(distance, 9,2, distanceStr);
}
void sendMessage(void) {
// check to send a message?
// basically ANY alarms OR daily message
if ( !sendLipoMessage || !sendHighTempMessage || !sendLowTempMessage || !sendBilgeMessage || !sendNoGpsFixMessage || !sendGeoFenceMessage || !sendVccMessage || !sendDailyMessageFlag ) {
// no need to send a message
//return;
}
// send a message!!!!
// like 20190127T20:20:20-LIPO:5.0v-VCC:50.0v-TEMP:99.9c,BILGE:OK,LAT:NNNNN:LON:YYYYYY:DIS:NNNNNm
//Get a dateTime String
sprintf(dateTimeStr, gps.getdateTime());
// going to try and pinch some power;
vcc.regOn();
//Put the message together
// first 1 is API version
// https://docs.rockblock.rock7.com/docs/compressing-your-data
// RockBLOCK 9603 would be nice
// basically send ints 13.7v should be 137, ie drop the V, and decimal
sprintf(messageStr, "'1,%s,%sv,%sv,%sc,%s,%s,%s,%sm'", dateTimeStr, lipoStr, vccStr, tempStr, bilgeStr, latStr, lonStr, distanceStr);
removeSpaces(messageStr);
DEBUG("Message is: ");
DEBUGln(messageStr);
// message sent tidy up
// done swich off regulator
vcc.regOff();
// clear flags
sendLipoMessage = false;
sendHighTempMessage = false;
sendLowTempMessage = false;
sendBilgeMessage = false;
sendNoGpsFixMessage = false;
sendGeoFenceMessage = false;
sendVccMessage = false;
sendDailyMessageFlag = false;
// done
}
// https://www.geeksforgeeks.org/remove-spaces-from-a-given-string/
void removeSpaces(char *str) {
// To keep track of non-space character count
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}