#define BLYNK_TEMPLATE_ID "TMPL6Ygimrq19"
#define BLYNK_TEMPLATE_NAME "gas leakage monitoring"
#define BLYNK_AUTH_TOKEN "kHzIAm6qKv-G4qZ60UwrptqOmMmn9trr"

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

#define RELAY1_PIN 23
#define RELAY2_PIN 22

BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();

  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(RELAY2_PIN, OUTPUT);

  timer.setInterval(2500L, sendSensor);
}

void loop() {
  Blynk.run();
  timer.run();
}

void sendSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // قم بتحويل درجة الحرارة إلى نطاق 0-100
  int temperature_0_100 = map(t, -40, 80, 0, 100);  // نطاق درجة حرارة DHT22 هو -40 إلى 80

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V3, temperature_0_100);  // إرسال القيمة المحولة إلى Blynk
  Serial.print("Temperature (0-100): ");
  Serial.print(temperature_0_100);
  Serial.print(" Humidity: ");
  Serial.println(h);

  if (t > 30) {
    Blynk.logEvent("temp_alert", "Temp above 30 degrees");
  }
}

BLYNK_WRITE(V1) {
  int relay1_state = param.asInt();
  digitalWrite(RELAY1_PIN, relay1_state);
}

BLYNK_WRITE(V2) {
  int relay2_state = param.asInt();
  digitalWrite(RELAY2_PIN, relay2_state);
}
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module