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

const char* ssid = "Wokwi-GUEST";
const char* password = "";
int generateRandomTemperature() {
  return random(18, 81); // تولید عدد تصادفی بین 18 تا 80
  }
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;

    http.begin("http://demo.thingsboard.io/api/v1/NV2svV22GI34NCf0xFTc/telemetry");
    http.addHeader("Content-Type", "application/json");

    int temperature = generateRandomTemperature();
    String jsonPayload = "{\"temperature\":" + String(temperature) + "}";

    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(5000);
}