// #include <TinyGPS++.h>
// #include <WiFi.h>
// #include <HTTPClient.h>
// const char* ssid = "your_SSID"; // Replace with your WiFi SSID
// const char* password = "your_PASSWORD"; // Replace with your WiFi Password
// const char* serverPath = "http://yourserver.com/api/location"; // Replace with your server path
// TinyGPSPlus gps; // GPS object
// HardwareSerial gpsSerial(1); // Serial port for GPS communication (use UART1)
// void setup() {
// Serial.begin(115200);
// gpsSerial.begin(9600, SERIAL_8N1, 16, 17); // GPIO16 (RX), GPIO17 (TX) for GPS
// WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi");
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println(" Connected!");
// }
// void loop() {
// // Process incoming GPS data
// while (gpsSerial.available() > 0) {
// gps.encode(gpsSerial.read());
// }
// if (gps.location.isUpdated()) {
// float latitude = gps.location.lat();
// float longitude = gps.location.lng();
// Serial.print("Latitude: "); Serial.println(latitude, 6);
// Serial.print("Longitude: "); Serial.println(longitude, 6);
// // Send location to the server
// if (WiFi.status() == WL_CONNECTED) {
// HTTPClient http;
// http.begin(serverPath);
// http.addHeader("Content-Type", "application/json");
// String jsonPayload = "{\"latitude\":" + String(latitude, 6) + ", \"longitude\":" + String(longitude, 6) + "}";
// int httpResponseCode = http.POST(jsonPayload);
// if (httpResponseCode > 0) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// } else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// http.end();
// } else {
// Serial.println("WiFi not connected");
// }
// }
// delay(5000); // Send location every 5 seconds
// }
//above code is works if the device actually has gps module
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverUrl = "https://iot-project-newone.vercel.app/api/cycle/location";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "latitude=" + String(29.8083283) + "&longitude=" + String(76.3962184) + "&id=6723f3689536e67a03e90dfd";
// Send POST request
// Serial.println(jsonMessage);
int httpResponseCode = http.POST(httpRequestData);
Serial.println(httpResponseCode);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Server Response: " + response);
} else {
Serial.println("Error sending request: " + String(httpResponseCode));
}
http.end();
}
delay(10000); // Send data every second
}