0% found this document useful (0 votes)
25 views

Fast Sending Data To Server Loop

Uploaded by

gamerboy18teen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Fast Sending Data To Server Loop

Uploaded by

gamerboy18teen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <WiFi.

h>
#include <HardwareSerial.h>
#include <HTTPClient.h>

// Replace with your network credentials


const char* ssid = "thrasher";
const char* password = "wildfire45";

// Replace with your server's IP address


const char* serverName = "http://192.168.1.2/gps_project/submit_data.php";

// Initialize HardwareSerial for GPS communication


HardwareSerial mySerial(2);

void setup() {
Serial.begin(115200);
mySerial.begin(9600, SERIAL_8N1, 16, 17); // RX2 (GPIO16) and TX2 (GPIO17)

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
}

void loop() {
String nmeaSentence = "";

// Check WiFi connection


if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected. Reconnecting...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Reconnected to Wi-Fi");
}

while (mySerial.available() > 0) {


char c = mySerial.read();
if (c == '\n') {
if (nmeaSentence.startsWith("$")) {
sendToServer(nmeaSentence);
}
nmeaSentence = "";
} else {
nmeaSentence += c;
}
}

// Add a small delay to avoid potential overloading of the server


delay(100);
}

void sendToServer(const String& nmea) {


if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Sending raw NMEA sentence


String postData = "nmea=" + nmea;

int httpResponseCode = http.POST(postData);

if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error: " + String(httpResponseCode));
}

http.end();
} else {
Serial.println("Error: Not connected to Wi-Fi");
}
}

You might also like