Skip to content

Instantly share code, notes, and snippets.

@itsmebirdie
Last active March 26, 2025 10:23
Show Gist options
  • Save itsmebirdie/5efe13afa33db847445f137d56cab6b7 to your computer and use it in GitHub Desktop.
Save itsmebirdie/5efe13afa33db847445f137d56cab6b7 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <HTTPClient.h>
#include <TinyGPSPlus.h>
// Wi-Fi credentials
const char* ssid = "MurgaDealers";
const char* password = "ptuz42069";
// Firebase URL
const char* serverName = "https://myperro-gps-post-test-default-rtdb.asia-southeast1.firebasedatabase.app/test.json";
// Define RX and TX pins for the GPS module
#define RXD2 22
#define TXD2 21
TinyGPSPlus gps; // Create an instance of TinyGPSPlus
HardwareSerial gpsSerial(2); // Use Serial2 for GPS communication
void setup() {
// Initialize serial communication
Serial.begin(115200);
gpsSerial.begin(9600, SERIAL_8N1, RXD2, TXD2);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
}
void loop() {
// Check for GPS data availability
while (gpsSerial.available() > 0) {
char c = gpsSerial.read();
gps.encode(c); // Process GPS data
// Output satellite count regularly
Serial.print("Satellites connected: ");
Serial.println(gps.satellites.value());
if (gps.location.isValid()) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();
Serial.print("Latitude: ");
Serial.println(latitude);
Serial.print("Longitude: ");
Serial.println(longitude);
// Send valid GPS data to Firebase
sendToFirebase(latitude, longitude, gps.satellites.value());
} else {
Serial.println("Waiting for valid GPS signal...");
}
delay(2000); // Sleep for 2 seconds before the next update
}
}
void sendToFirebase(float latitude, float longitude, int satellites) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Start HTTP connection
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
// Prepare JSON payload with latitude, longitude, and satellite count
String jsonPayload = "{\"latitude\":" + String(latitude) + ",\"longitude\":" + String(longitude) + ",\"satellites\":" + String(satellites) + "}";
// Send POST request
int httpResponseCode = http.POST(jsonPayload);
// Handle response
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// End HTTP connection
http.end();
} else {
Serial.println("Wi-Fi disconnected!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment