Hello Everyone, I'm working on a project where I'm using an Arduino to gather GPS coordinates and then send them to a database via a server. However, I'm facing an issue where the GPS coordinates get truncated during transmission. I'm not really expert on this and I really need your help.
Below is my arduino code
#include "WiFiEsp.h"
#include <TinyGPS++.h>
// Define your Wi-Fi network credentials
const char* ssid = "..."; // Replace with your Wi-Fi network SSID
const char* password = "12345678"; // Replace with your Wi-Fi network password
// Define your server details
const char* serverAddress = "192.168.234.21"; // Replace with your server's IP address
const int serverPort = 80; // Assuming your server is running on port 80
WiFiEspClient client; // Create a client object to communicate with the server
TinyGPSPlus gps; // Create a TinyGPS++ object to handle GPS data
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
Serial1.begin(9600); // Initialize hardware serial communication with ESP module
Serial2.begin(9600); // Initialize hardware serial communication with GPS module
// Initialize ESP module
WiFi.init(&Serial1);
// Connect to Wi-Fi
connectToWiFi();
}
void loop() {
// Update GPS data
while (Serial2.available() > 0) {
gps.encode(Serial2.read());
// Check if GPS data is valid
if (gps.location.isValid()) {
// Store latitude and longitude as doubles
double latitude = gps.location.lat();
double longitude = gps.location.lng();
Serial.print("Latitude: ");
Serial.println(latitude, 15);
Serial.print("Longitude: ");
Serial.println(longitude, 15);
// Convert latitude and longitude to strings
String latitudeString = String(latitude, 15);
String longitudeString = String(longitude, 15);
// Send GPS data to the server
sendDataToServer(latitudeString, longitudeString);
delay(10000);
}
}
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
// Attempt to connect to Wi-Fi network
int status = WiFi.begin(ssid, password);
int attempts = 0;
while (status != WL_CONNECTED && attempts < 5) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
status = WiFi.begin(ssid, password);
attempts++;
}
if (status == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect to WiFi");
}
}
void sendDataToServer(String latitude, String longitude)
{
// Check if client is connected to the server
if (client.connect(serverAddress, serverPort))
{
Serial.println("Connected to server.");
// Construct the HTTP POST request
String postRequest = "POST /Arduino_server/arduino_gps.php HTTP/1.1\r\n";
postRequest += "Host: " + String(serverAddress) + "\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
// Calculate content length
long int contentLength = latitude.length() + longitude.length() + 40; // "latitude=" and "longitude=" prefixes length
postRequest += "Content-Length: " + String(contentLength) + "\r\n";
postRequest += "Connection: close\r\n\r\n";
postRequest += "latitude=" + latitude + "&longitude=" + longitude;
// Send the POST request
client.println(postRequest);
Serial.println("Request sent");
Serial.println("Latitude: " + latitude);
Serial.println("Longitude: " + longitude);
// Wait for the server response
delay(20000);
// Read and print the server response
while (client.available())
{
char c = client.read();
Serial.print(c);
}
// Close the connection
client.stop();
}
else
{
Serial.println("Connection to server failed");
}
}
And this is the output of my code
```
Latitude: 8.655395507812500
Longitude: 123.420051574707031
Connected to server.
Request sent
Latitude: 8.655395500000000
Longitude: 123.420050000000000
```
I don't know what to do and I don't know how to fix this problem