#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino.h>

const char* ssid = "Wokwi-GUEST";
const char* password = "";
int generateRandomTemperature() {
  return random(18, 81); // تولید عدد تصادفی بین 18 تا 80
  }
struct Coordinate {
  double latitude;
  double longitude;;
};



int currentIndex = 0;


void setup() {
  Serial.begin(115200);

  // اتصال به شبکه Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connection to WiFi...");
  }
  Serial.println("Connected to WiFi");

   

  
}

void loop() {
  // کدی که در حلقه اصلی برنامه اجرا می‌شود
   if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
Coordinate coordinates[] = {
  {26.5295, 53.963},
  {26.52947, 53.96373},
  {26.5294, 53.9641},
  {26.52945, 53.96421},
  {26.52934, 53.9654},
  {26.52925, 53.96671},
  {26.52916, 53.96805},
  {26.52902, 53.96953},
  {26.5289, 53.9717},
  {26.5285, 53.9765},
  {26.5274, 53.9897},
  {26.527, 53.9949},
  {26.5268, 53.9989}
};

    http.begin("https://sashiraz.info/api/v1/fMOIXyBGUFPU0MsusEEf/telemetry");
    http.addHeader("Content-Type", "application/json");

    int temperature = generateRandomTemperature();
   // String jsonPayload = "{\"temperature\":" + String(temperature) + "}";
   float latitude = coordinates[currentIndex].latitude;
  float longitude = coordinates[currentIndex].longitude;
String jsonPayload = "{\"latitude\":" + String(latitude, 4) + ", \"longitude\":" + String(longitude, 4) + "}";  
  Serial.println(jsonPayload);

  currentIndex++;
  if (currentIndex >= sizeof(coordinates) / sizeof(coordinates[0])) {
    currentIndex = 0;
  }


    int httpResponseCode = http.POST(jsonPayload);

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println(httpResponseCode);
      Serial.println(response);
    } else {
      Serial.println("Error on HTTP request");
    }

    http.end();
  }

  delay(300);
}