-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinkItOne-Traccar.ino
325 lines (290 loc) · 8.54 KB
/
LinkItOne-Traccar.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
// --------------TRACCAR LINKIT ONE CLIENT------------------
// USAGE: SET PARAMETERS, PROGRAM THE BOARD, INSERT GPRS WEB ENABLED SIM, CONNECT GSM & GPS ANTENNAS
// LOCATION UPDATE IS PROVIDED VIA GPRS CONNECTION, USING OSMAND PROTOCOL,INFO AT: https://www.traccar.org/osmand/
// GPRS MOBILE WEB MUST BE INITIALIZED WITH CARRIER APN, CARRIER APN USERNAME & CARRIER APN PASSWORD, LAST TWO CAN BE OMITTED.
// REMEBER TO REGISTER USERID ON YOUR TRACCAR SERVER TO MATCH CLIENT USERID DEFINED IN PARAMETERS
// HAVE FUN
//
// AUTHOR: LIMON93
#include <LTask.h>
#include <vmthread.h>
#include <stddef.h>
#include <LGPS.h>
#include <LGSM.h>
#include <math.h>
#include <LDateTime.h>
#include <LStorage.h>
#include <LFlash.h>
#include <LGPRSClient.h>
#include <LGPRSServer.h>
#include <LGPRS.h>
#include "struct.h"
#define LEDGPS 13
//PARAMETERS
#define UPDATE_FREQ 10 // INSERT CLIENT POSITION UPDATE INTERVAL IN SECONDS
char USERID[20] = "123456789"; // INSERT YOUR TRACCAR SERVER USER ID
char server[] = "demo.traccar.org"; //INSERT YOUR SERVER ADDRESS (aaa.com or xxx.xxx.xxx.xxx)
int port = 5055; // INSERT YOUR SERVER PORT
char APN[]="internet.com"; //INSERT YOUR MOBILE GPRS INTERNET APN
char APNUSER[]=""; //INSERT YOUR MOBILE GPRS INTERNET APN USERNAME, LEAVE BLANK IF NONE
char APNPASS[]="";//INSERT YOUR MOBILE GPRS INTERNET APN PASSWORD, LEAVE BLANK IF NONE
gpsSentenceInfoStruct info;
LGPRSClient client;
char buff[512];
static double getDoubleNumber(const char *s)
{
char buf[10];
unsigned char i;
double rev;
i=getComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=atof(buf);
return rev;
}
static unsigned char getComma(unsigned char num,const char *str){
unsigned char i,j = 0;
int len=strlen(str);
for(i = 0;i < len;i ++){
if(str[i] == ',')
j++;
if(j == num)
return i + 1;
}
return 0;
}
static float getFloatNumber(const char *s){
char buf[10];
unsigned char i;
float rev;
i=getComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=atof(buf);
return rev;
}
static float getIntNumber(const char *s){
char buf[10];
unsigned char i;
float rev;
i=getComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=atoi(buf);
return rev;
}
void parseGPGGA(const char* GPGGAstr){
if(GPGGAstr[0] == '$'){
int tmp;
tmp = getComma(1, GPGGAstr);
MyGPSPos.hour = (GPGGAstr[tmp + 0] - '0') * 10 + (GPGGAstr[tmp + 1] - '0');
MyGPSPos.minute = (GPGGAstr[tmp + 2] - '0') * 10 + (GPGGAstr[tmp + 3] - '0');
MyGPSPos.second = (GPGGAstr[tmp + 4] - '0') * 10 + (GPGGAstr[tmp + 5] - '0');
//get time
sprintf(buff, "UTC time %02d:%02d:%02d", MyGPSPos.hour, MyGPSPos.minute, MyGPSPos.second);
Serial.print(buff);
//get lat/lon coordinates
float latitudetmp;
float longitudetmp;
tmp = getComma(2, GPGGAstr);
latitudetmp = getFloatNumber(&GPGGAstr[tmp]);
tmp = getComma(4, GPGGAstr);
longitudetmp = getFloatNumber(&GPGGAstr[tmp]);
// need to convert format
convertCoords(latitudetmp, longitudetmp, MyGPSPos.latitude, MyGPSPos.longitude);
//get lat/lon direction
tmp = getComma(3, GPGGAstr);
if(GPGGAstr[tmp] == 'N')
{ // do nothing
}
else if(GPGGAstr[tmp] == 'S')
{
MyGPSPos.latitude = -MyGPSPos.latitude;
}
else
{
sprintf(buff, "Error: found %i(%c). Expected N or S.", GPGGAstr[tmp], GPGGAstr[tmp]);
Serial.println(buff);
}
MyGPSPos.latitude_dir = (GPGGAstr[tmp]);
tmp = getComma(5, GPGGAstr);
if(GPGGAstr[tmp] == 'E')
{ // do nothing
}
else if(GPGGAstr[tmp] == 'W')
{
MyGPSPos.longitude = -MyGPSPos.longitude;
}
else
{
sprintf(buff, "Error: found %i(%c). Expected E or W.", GPGGAstr[tmp], GPGGAstr[tmp]);
Serial.println(buff);
}
MyGPSPos.longitude_dir = (GPGGAstr[tmp]);
tmp = getComma(6, GPGGAstr);
MyGPSPos.fix = getIntNumber(&GPGGAstr[tmp]);
sprintf(buff, " - GPS fix quality = %d", MyGPSPos.fix);
Serial.print(buff);
//get satellites in view
tmp = getComma(7, GPGGAstr);
MyGPSPos.num = getIntNumber(&GPGGAstr[tmp]);
sprintf(buff, " - %d satellites", MyGPSPos.num);
Serial.println(buff);
tmp = getComma(9, GPGGAstr);
MyGPSPos.alt = getIntNumber(&GPGGAstr[tmp]);
}
else{
Serial.println("No GPS data");
}
}
void convertCoords(float latitude, float longitude, float &lat_return, float &lon_return){
int lat_deg_int = int(latitude/100); //extract the first 2 chars to get the latitudinal degrees
int lon_deg_int = int(longitude/100); //extract first 3 chars to get the longitudinal degrees
// must now take remainder/60
// this is to convert from degrees-mins-secs to decimal degrees
// so the coordinates are "google mappable"
float latitude_float = latitude - lat_deg_int * 100; //remove the degrees part of the coordinates - so we are left with only minutes-seconds part of the coordinates
float longitude_float = longitude - lon_deg_int * 100;
lat_return = lat_deg_int + latitude_float / 60 ; //add back on the degrees part, so it is decimal degrees
lon_return = lon_deg_int + longitude_float / 60 ;
}
void GetGPSPos(void){
Serial.println("--- LGPS loop ---");
LGPS.getData(&info);
parseGPRMC((const char*)info.GPRMC);
LGPS.getData(&info);
parseGPGGA((const char*)info.GPGGA);
//check fix
//if GPS fix is OK
if ( MyGPSPos.fix == GPS || MyGPSPos.fix == DGPS || MyGPSPos.fix == PPS ){
//set a flag
MyFlag.fix3D = true;
}
else{
//reset flag
MyFlag.fix3D = false;
}
sprintf(buff, "Current position is : https://www.google.com/maps?q=%2.6f,%3.6f Alititude=%d", MyGPSPos.latitude,MyGPSPos.longitude,MyGPSPos.alt);
Serial.println(buff);
Serial.println();
}
void parseGPRMC(const char* GPRMCstr)
{
double spd;
int tmp, hour, minute, second, date, num ;
if(GPRMCstr[0] == '$')
{
tmp = getComma(7, GPRMCstr);
spd = getDoubleNumber(&GPRMCstr[tmp]);
sprintf(buff, "Speed(knots) = %5.2f", spd);
Serial.println(buff);
MyGPSPos.spd=spd;
}
else
{
Serial.println("Unable to get speed data.");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// set I/O direction
pinMode(LEDGPS, OUTPUT);
delay(5000);
Serial.println("Limon93Studio Traccar client.");
// GPS power on
LGPS.powerOn();
Serial.println("GPS powered ON.");
// set default value for GPS (needed for default led status)
MyGPSPos.fix = Error;
LTask.remoteCall(createThread1, NULL);
// GSM setup
while(!LSMS.ready()){
delay(1000);
Serial.println("Insert SIM");
}
Serial.println("SIM ready.");
pinMode(LEDGPS, OUTPUT);
Serial.println("Setup done.");
while (!LGPRS.attachGPRS(APN,APNUSER,APNPASS))
{
delay(500);
}
GetGPSPos();
delay(30000);
}
void loop() {
GetGPSPos();
if (MyFlag.fix3D){
Serial.println("Periodic update.");
char path[200];
sprintf(path,"/?id=%s&lat=%2.6f&lon=%3.6f&altitude=%d&speed=%d",USERID,MyGPSPos.latitude, MyGPSPos.longitude,MyGPSPos.alt,MyGPSPos.spd);
if (client.connect(server, port))
{
Serial.println("Connected to network and server.");
// Make a HTTP request:
client.print("GET ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println("Done.\n");
}
else
{
// if you didn't get a connection to the server:
Serial.println("Connection failed.");
}
delay(UPDATE_FREQ*1000);
}
else {Serial.println("Waiting for 3D GPS fix..."); delay(1000);}
}
boolean createThread1(void* userdata) {
// The priority can be 1 - 255 and default priority are 0
// the arduino priority are 245
vm_thread_create(thread_ledgps, NULL, 255);
return true;
}
//----------------------------------------------------------------------
//!\brief THREAD LED GPS
//----------------------------------------------------------------------
VMINT32 thread_ledgps(VM_THREAD_HANDLE thread_handle, void* user_data){
for (;;){
switch(MyGPSPos.fix){
case Invalid:
// blink led as pulse
digitalWrite(LEDGPS, HIGH);
delay(500);
digitalWrite(LEDGPS, LOW);
delay(500);
break;
case GPS:
case DGPS:
case PPS:
case RTK:
case FloatRTK:
case DR:
case Manual:
case Simulation:
// blink led as slow pulse
digitalWrite(LEDGPS, HIGH);
delay(150);
digitalWrite(LEDGPS, LOW);
delay(2850);
break;
case Error:
// Fast blinking led
digitalWrite(LEDGPS, HIGH);
delay(100);
digitalWrite(LEDGPS, LOW);
delay(100);
break;
}
}
return 0;
}