-
Notifications
You must be signed in to change notification settings - Fork 5
/
MiniSmartTracker.ino
390 lines (295 loc) · 11.1 KB
/
MiniSmartTracker.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
#include <TinyGPS++.h>
#include <string.h>
#include <Arduino.h>
#include <ArduinoQAPRS.h>
#include <SPI.h>
#include <OneWire.h>
#include "config.h"
// The TinyGPS++ object
TinyGPSPlus gps;
OneWire ds(6);
char * packet_buffer = " \n ";
char gradbuf[4];
const char from_addr[] = MYCALL;
const char dest_addr[] = DEST_ADDR;
const char relays[] = RELAY;
const int analogInPin = A0;
int ledPin = 13;
const byte highSpeed = 60; // High speed
const byte lowSpeed = 20; // Low speed
unsigned long previousMillis = 0;
float latitude = 0.0;
float longitude = 0.0;
// Initial lat/lng pos, change to your base station coordnates
float lastTxLat = HOME_LAT;
float lastTxLng = HOME_LON;
float lastTxdistance;
int previousHeading, currentHeading = 0;
unsigned int mCounter = 0;
unsigned int txCounter = 0;
unsigned long txTimer = 0;
unsigned long lastTx = 0;
unsigned long lastRx = 0;
unsigned long txInterval = 80000L; // Initial 80 secs internal
void setup()
{
Serial.begin(9600);
Serial.println("WAIT!");
txTimer = millis();
delay(100);
pinMode(ledPin, OUTPUT);
QAPRS.init(3, 2);
delay(2000);
}
void loop()
{
int headingDelta = 0;
while (Serial.available())
{
gps.encode(Serial.read());
}
///////////////// Triggered by location updates ///////////////////////
if ( gps.location.isUpdated() ) {
lastTxdistance = TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
lastTxLat,
lastTxLng);
latitude = gps.location.lat();
longitude = gps.location.lng();
// Get headings and heading delta
currentHeading = (int) gps.course.deg();
if ( currentHeading >= 180 ) {
currentHeading = currentHeading-180;
}
headingDelta = (int) ( previousHeading - currentHeading ) % 360;
} // endof gps.location.isUpdated()
///////////////// Triggered by time updates ///////////////////////
// Update LCD every second
if ( gps.time.isUpdated() ) {
// Change the Tx internal based on the current speed
// This change will not affect the countdown timer
// Based on HamHUB Smart Beaconing(tm) algorithm
if ( gps.speed.kmph() < 5 ) {
txInterval = 300000; // Change Tx internal to 5 mins
} else if ( gps.speed.kmph() < lowSpeed ) {
txInterval = 60000; // Change Tx interval to 60
} else if ( gps.speed.kmph() > highSpeed ) {
txInterval = 20000; // Change Tx interval to 20 secs
} else {
// Interval inbetween low and high speed
txInterval = (highSpeed / gps.speed.kmph()) * 20000;
} // endif
} // endof gps.time.isUpdated()
////////////////////////////////////////////////////////////////////////////////////
// Check for when to Tx packet
////////////////////////////////////////////////////////////////////////////////////
lastTx = millis() - txTimer;
// Only check the below if locked satellites < 3
if ( (gps.satellites.value() > 3) and (gps.location.age() < 3000) ) {
if ( lastTx > 5000 ) {
// Check for heading more than 25 degrees
if ( (headingDelta < -25 || headingDelta > 25) && lastTxdistance > 5 ) {
if (TxtoRadio()) {
lastTxdistance = 0; // Ensure this value is zero before the next Tx
previousHeading = currentHeading;
}
} // endif headingDelta
} // endif lastTx > 5000
if ( lastTx > 10000 ) {
// check of the last Tx distance is more than 600m
if ( lastTxdistance > 600 ) {
if ( TxtoRadio() ) {
lastTxdistance = 0; // Ensure this value is zero before the next Tx
}
} // endif lastTxdistance
} // endif lastTx > 10000
if ( lastTx >= txInterval ) {
// Trigger Tx Tracker when Tx interval is reach
// Will not Tx if stationary bcos speed < 5 and lastTxDistance < 20
if ( lastTxdistance > 20 ) {
TxtoRadio();
} // endif lastTxdistance > 20
//Debug info
Serial.print("sentencesWithFix=");Serial.println(gps.sentencesWithFix());
Serial.print("Sentences that failed checksum=");
Serial.println(gps.failedChecksum());
//end DEBUG info
} // endif of check for lastTx > txInterval
} // Endif check for satellites
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
//формирование пакета APRS и его отправка
boolean TxtoRadio(void) {
char tmp[10];
float latDegMin, lngDegMin = 0.0;
String latOut, lngOut, cmtOut = "";
unsigned int Mem = freeRam();
float Volt = (float) readVcc();
digitalWrite(13, HIGH);
lastTxLat = gps.location.lat();
lastTxLng = gps.location.lng();
if ( lastTx > 6000 ) { // This prevent ANY condition to Tx below 6 secs
latDegMin = convertDegMin(lastTxLat);
lngDegMin = convertDegMin(lastTxLng);
// Convert Lat float to string
dtostrf(fabs(latDegMin), 2, 2, tmp );
// latOut.concat("lla"); // set latitute command
// Append 0 if Lat less than 10
if ( fabs(lastTxLat) < 10 ) {
latOut.concat("0");
}
latOut.concat(tmp); // Actual Lat in DDMM.MM
// Determine E or W
if (latDegMin >= 0) {
latOut.concat("N");
} else if (latDegMin < 0) {
latOut.concat("S");
}
cmtOut.concat("!");
cmtOut.concat(latOut);
cmtOut.concat("/");
// Convert Lng float to string
dtostrf(fabs(lngDegMin), 2, 2, tmp );
// lngOut.concat("llo"); // set longtitute command
// Append 0 if Lng less than 100
if ( ( fabs(lastTxLng) < 100) ) {
lngOut.concat("0"); // set longtitute command
}
// Append 0 if Lng less than 10
if ( fabs(lastTxLng) < 10 ) {
latOut.concat("0"); // Append 0 if Lng less than 10
}
lngOut.concat(tmp); // Actual Lng in DDDMM.MM
// Determine E or W
if (lngDegMin >= 0) {
lngOut.concat("E");
} else if (latDegMin < 0) {
lngOut.concat("W");
}
cmtOut.concat(lngOut);
//aprs = "!" + lat + ns + "/" + lon + ew + ">" + "/A=000" + alti + " Bat=" + outputValue + "V" + " Sat=" + sat;
cmtOut.concat(">");
cmtOut.concat(padding((int) gps.course.deg(),3));
cmtOut.concat("/");
cmtOut.concat(padding((int)gps.speed.mph(),3));
cmtOut.concat("/A=");
cmtOut.concat(padding((int)gps.altitude.feet(),6));
cmtOut.concat(" ");
cmtOut.concat("V=");
cmtOut.concat(Volt);
cmtOut.concat(" ");
cmtOut.concat("Sat=");
cmtOut.concat(gps.satellites.value());
cmtOut.concat(" ");
#ifdef TEMP_TX
cmtOut.concat("T=");
cmtOut.concat(getTemp());
cmtOut.concat(" ");
#endif
cmtOut.concat(COMMENT);
// convert string to char_array
int str_len = cmtOut.length() + 1;
char char_array[str_len];
cmtOut.toCharArray(char_array, str_len);
// send via packet_buffer
packet_buffer = char_array;
Serial.print("Send: "),Serial.println(packet_buffer);
QAPRS.send(from_addr, CALL_SSID, dest_addr, '0', relays, packet_buffer);
// Reset all tx timer
txInterval = 80000;
txTimer = millis();
lastTx = 0;
digitalWrite(13, LOW);
// Tx success, return TRUE
return 1;
} else {
return 0;
}// endif lastTX > 6000
} // endof TxtoRadio()
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
float convertDegMin(float decDeg) {
float DegMin;
int intDeg = decDeg;
decDeg -= intDeg;
decDeg *= 60;
DegMin = ( intDeg*100 ) + decDeg;
return DegMin;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
float readVcc() {
float sensorValue = 0;
float outputValue = 0;
//измерение напряжения
delay(10);
sensorValue = analogRead(analogInPin);
outputValue = float(analogRead(analogInPin)) / 204.6;
outputValue = (outputValue) * 10;
return outputValue;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
String padding( int number, byte width ) {
String result;
// Prevent a log10(0) = infinity
int temp = number;
if (!temp) { temp++; }
for ( int i=0;i<width-(log10(temp))-1;i++) {
result.concat('0');
}
result.concat(number);
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
int freeRam() {
#if defined(__arm__) && defined(TEENSYDUINO)
char top;
return &top - reinterpret_cast<char*>(sbrk(0));
#else // non ARM, this is AVR
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//измерение температуры
int getTemp(void)
{
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return 0;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return 0;
}
if ( addr[0] != 0x10) {
return 0;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1);
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
int HighByte, LowByte, TReading, Tce;
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
Tce = TReading/2;
return (Tce);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////