-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpstracker_sim800l_thingspeak_webhost.ino
130 lines (106 loc) · 3.75 KB
/
gpstracker_sim800l_thingspeak_webhost.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
// THIS PROJECT WAS MADE BY github.com/waritsriyadi
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
SoftwareSerial sim800lSerial(10, 11); // RX, TX for SIM800L
SoftwareSerial gpsSerial(3, 2); // RX, TX for GPS
TinyGPSPlus gps;
const char* SERVER_NAME = "YOUR_WEBHOST000_WEB"; // Example: motorcylealertsystem.000webhostapp.com
const int SERVER_PORT = 80;
const String API_PATH = "/gpsdata.php";
const char* thingspeakApiKey = "YOUR_API_KEY";
void setup() {
Serial.begin(9600);
sim800lSerial.begin(9600);
gpsSerial.begin(9600);
sendATcommand("AT", "OK", 2000);
sendATcommand("AT+CMGF=1", "OK", 2000);
delay(1000);
}
void loop() {
Serial.println("Finding GPS...");
smartdelay_gps(1000);
String latitude, longitude, speedKmph;
if (gps.location.isValid()) {
latitude = String(gps.location.lat(), 6);
longitude = String(gps.location.lng(), 6);
speedKmph = String(gps.speed.kmph(), 6);
Serial.print("Latitude: ");
Serial.println(latitude);
Serial.print("Longitude: ");
Serial.println(longitude);
Serial.print("Speed: ");
Serial.print(speedKmph);
Serial.println(" km/h");
uploadToThingSpeak(latitude, longitude, speedKmph);
uploadToWebhost(latitude, longitude);
}
delay(15000); // Sesuaikan delay ini berdasarkan kebutuhan Anda
}
void uploadToThingSpeak(const String& latitude, const String& longitude, const String& speedKmph) {
Serial.println("Uploading to ThingSpeak...");
sim800lSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");
delay(500);
ShowSerialData();
sim800lSerial.println("AT+CIPSEND");
delay(500);
ShowSerialData();
String str = "GET /update?api_key=" + String(thingspeakApiKey) +
"&field1=" + latitude +
"&field2=" + longitude +
"&field3=" + speedKmph;
sim800lSerial.println(str);
delay(1000);
ShowSerialData();
sim800lSerial.println((char)26); // Sending
delay(5000);
ShowSerialData();
sim800lSerial.println("AT+CIPSHUT"); // Close the connection
delay(100);
ShowSerialData();
Serial.println("Done Uploading to ThingSpeak...");
}
void uploadToWebhost(const String& latitude, const String& longitude) {
Serial.println("Uploading to Webhost000..");
String url, temp;
url = "http://YOUR_WEBHOST000_WEB/gpsdata.php?lat="; // Example: http://motorcylealertsystem.000webhostapp.com/gpsdata.php?lat=
url += latitude;
url += "&lng=";
url += longitude;
delay(300);
sendATcommand("AT+CFUN=1", "OK", 2000);
sendATcommand("AT+CGATT=1", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"YOUR_APN_HERE_DONT_FORGET_THIS\"", "OK", 2000);
sendATcommand("AT+SAPBR=1,1", "OK", 2000);
sendATcommand("AT+HTTPINIT", "OK", 2000);
sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 1000);
sim800lSerial.print("AT+HTTPPARA=\"URL\",\"");
sim800lSerial.print(url);
sendATcommand("\"", "OK", 1000);
sendATcommand("AT+HTTPACTION=0", "0,200", 1000);
sendATcommand("AT+HTTPTERM", "OK", 1000);
sendATcommand("AT+CIPSHUT", "SHUT OK", 1000);
Serial.println("Done Uploading to Webhost000..");
}
void ShowSerialData() {
while (sim800lSerial.available() != 0)
Serial.write(sim800lSerial.read());
delay(5000);
}
static void smartdelay_gps(unsigned long ms) {
unsigned long start = millis();
do {
while (gpsSerial.available())
gps.encode(gpsSerial.read());
} while (millis() - start < ms);
}
void sendATcommand(const char* command, const char* expectedResponse, int timeout) {
sim800lSerial.println(command);
unsigned long start = millis();
while (millis() - start < timeout) {
if (sim800lSerial.find(const_cast<char*>(expectedResponse))) {
return;
}
}
}
// THIS PROJECT WAS MADE BY github.com/waritsriyadi