#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
#define RELAY3_PIN 21 // Added relay pin
#define RELAY4_PIN 19 // Added relay pin
#define RELAY5_PIN 18 // Added relay pin
BlynkTimer timer;
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(RELAY3_PIN, OUTPUT); // Set as output for relay 3
pinMode(RELAY4_PIN, OUTPUT); // Set as output for relay 4
pinMode(RELAY5_PIN, OUTPUT); // Set as output for relay 5
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;
}
// Convert the temperature to a range of 0-100
int temperature_0_100 = map(t, -40, 80, 0, 100);
Blynk.virtualWrite(V6, h);
Blynk.virtualWrite(V3, temperature_0_100);
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);
}
BLYNK_WRITE(V3) { // Added for relay 3
int relay3_state = param.asInt();
digitalWrite(RELAY3_PIN, relay3_state);
}
BLYNK_WRITE(V4) { // Added for relay 4
int relay4_state = param.asInt();
digitalWrite(RELAY4_PIN, relay4_state);
}
BLYNK_WRITE(V5) { // Added for relay 5
int relay5_state = param.asInt();
digitalWrite(RELAY5_PIN, relay5_state);
}