-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpsfinalcode.ino
More file actions
96 lines (80 loc) · 2.96 KB
/
gpsfinalcode.ino
File metadata and controls
96 lines (80 loc) · 2.96 KB
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
#include <WiFi.h>
#include <HTTPClient.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#define WIFI_SSID "POCO M6 Pro 5G"
#define WIFI_PASSWORD "12344321"
const int GPS_RX_PIN = 4;
const int GPS_TX_PIN = 2;
TinyGPSPlus gps;
HardwareSerial gpsSerial(1);
const char* serverName = "https://script.google.com/macros/s/AKfycbzPmt_Aq2FajHi89F9kKvVcyokQTqoLzUeCyo_uN7H2zR2cvNUBjB9KPZU-jechysLPCA/exec";
void setup() {
Serial.begin(115200);
gpsSerial.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("GPS module is ready...");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
if (gps.location.isUpdated()) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();
float altitude = gps.altitude.meters();
float speed = gps.speed.kmph();
int satellites = gps.satellites.value();
String date = String(gps.date.month()) + "/" + String(gps.date.day()) + "/" + String(gps.date.year());
String time = String(gps.time.hour()) + ":" + String(gps.time.minute()) + ":" + String(gps.time.second());
Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
Serial.print("Altitude: ");
Serial.println(altitude);
Serial.print("Speed: ");
Serial.println(speed);
Serial.print("Satellites: ");
Serial.println(satellites);
Serial.print("Date: ");
Serial.println(date);
Serial.print("Time: ");
Serial.println(time);
Serial.println();
Serial.print("Connecting to server: ");
Serial.println(serverName);
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "latitude=" + String(latitude, 6) +
"&longitude=" + String(longitude, 6) +
"&altitude=" + String(altitude) +
"&speed=" + String(speed) +
"&satellites=" + String(satellites) +
"&date=" + date +
"&time=" + time;
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String response = http.getString();
Serial.print("Server response: ");
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
} else {
Serial.println("Error in WiFi connection");
}
delay(1000);
}