-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraccar.ino
203 lines (161 loc) · 3.74 KB
/
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
#define SerialMon Serial
// Set serial for AT commands (to the module)`
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
String FINALLATI="0",FINALLOGI="0",FINALSPEED="0";
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "Your-APN";
const char gprsUser[] = "";
const char gprsPass[] = "";
const char server[] = "Your Traccar Server IP";
const int port = 5055;
String myid = "Traccar ID";
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#define DUMP_AT_COMMANDS
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
HttpClient http(client, server, port);
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
void modemPowerOn()
{
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1000); //Datasheet Ton mintues = 1S
digitalWrite(PWR_PIN, HIGH);
}
void modemPowerOff()
{
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1500); //Datasheet Ton mintues = 1.2S
digitalWrite(PWR_PIN, HIGH);
}
void modemRestart()
{
modemPowerOff();
delay(1000);
modemPowerOn();
}
void enableGPS(void)
{
Serial.println("Start positioning . Make sure to locate outdoors.");
Serial.println("The blue indicator light flashes to indicate positioning.");
modem.sendAT("+SGPIO=0,4,1,1");
if (modem.waitResponse(10000L) != 1)
{
DBG(" SGPIO=0,4,1,1 false ");
}
modem.enableGPS();
}
void disableGPS(void)
{
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1)
{
DBG(" SGPIO=0,4,1,0 false ");
}
modem.disableGPS();
}
void send_data(float lat, float lon)
{
String FINALLATI="0",FINALLOGI="0",FINALSPEED="0";
String SerialData="";
SerialData = String(lat,6);
String SerialData1="";
SerialData1 = String(lon,6);
FINALLATI=SerialData;
FINALLOGI=SerialData1;
int err = http.post("/?id="+myid+"&lat="+FINALLATI+"&lon="+FINALLOGI+"");
if (err != 0)
{
SerialMon.println(F("failed to connect"));
delay(10000);
return;
}
int status = http.responseStatusCode();
if (!status)
{
delay(10000);
return;
}
String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);
// Shutdown
http.stop();
SerialMon.println(F("Server disconnected bye bye will connect soon"));
}
void setup()
{
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
delay(10000);
modemPowerOn();
Serial.println("Initializing modem...");
if (!modem.restart())
{
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3)
{
modem.simUnlock(GSM_PIN);
}
}
void loop()
{
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass))
{
SerialMon.println(" fail");
delay(30000);
return;
}
SerialMon.println(" success");
if (modem.isGprsConnected())
{
SerialMon.println("GPRS connected");
}
enableGPS();
float lat, lon;
while (1)
{
if (modem.getGPS(&lat, &lon))
{
send_data(lat, lon);
}
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(30000);
}
}