// #define BLYNK_TEMPLATE_ID "TMPL650TKoGCD"
// #define BLYNK_TEMPLATE_NAME "Wokwi Blynk"
// #define BLYNK_AUTH_TOKEN "yt-dQA1Id9a4RJmWS4hiSyqSyfHiDejc"
// #define SOUND_SPEED 0.034
// #define CM_TO_INCH 0.393701
// #define DHT22_PIN 21 // ESP32 pin GPIO21 connected to DHT22 sensor
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// #include <DHT.h>
// DHT dht22(DHT22_PIN, DHT22);
// // Name of the router and password
// const char* ssid = "Wokwi-GUEST";
// const char* password = "";
// const int led = 14;
// const int trigPin = 19;
// const int echoPin = 18;
// long duration;
// float distanceCm;
// float distanceInch;
// //BlynkTimer initialize
// BlynkTimer timer;
// void setup() {
// // put your setup code here, to run once:
// Serial.begin(115200);
// pinMode(led, OUTPUT);
// pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
// pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// // Start wifi - 2 parameters (ssid & password)
// WiFi.begin(ssid, password);
// Serial.println("Connecting to WiFi");
// //Connection status
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// // Connection Info
// Serial.println("");
// Serial.println("WiFi connected!");
// Serial.print("IP Address: ");
// Serial.println(WiFi.localIP());
// //3 parameter - Token, ssid, password
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
// //2 parameter- 1. time(L-ms), 2. Method name
// timer.setInterval(1000L, DistanceData);
// }
// void DistanceData() {
// // Clears the trigPin
// digitalWrite(trigPin, LOW);
// delayMicroseconds(2);
// // Sets the trigPin on HIGH state for 10 micro seconds
// digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin, LOW);
// // Reads the echoPin, returns the sound wave travel time in microseconds
// duration = pulseIn(echoPin, HIGH);
// // Calculate the distance
// distanceCm = duration * SOUND_SPEED/2;
// // Convert to inches
// distanceInch = distanceCm * CM_TO_INCH;
// // Prints the distance in the Serial Monitor
// Serial.print("Distance (cm): ");
// Serial.println(distanceCm);
// //write Distance Data into Blynk
// //2 parameter - 1.pin, 2.Value
// Blynk.virtualWrite(V1, distanceCm);
// }
// //From Blynk to Wokwi we need to write the LED status
// //to write we always use this method
// BLYNK_WRITE(V0){
// //get the status of the virtual switch V0
// int status = param.asInt();
// digitalWrite(led, status);
// }
// void loop() {
// Blynk.run();
// timer.run();
// //delay(1000);
// }
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Define constants
#define DHTPIN 15
#define DHTTYPE DHT22
#define BUTTON_PIN 13
#define SERVO_PIN 26
#define WIFI_SSID "TMPL6WIaI86Rc"
#define BLYNK_TEMPLATE_NAME "Automatic Dress Hanger"
// #define WIFI_PASSWORD "your_wifi_password"
// #define BLYNK_AUTH_TOKEN "your_blynk_auth_token"
#define BLYNK_TEMPLATE_ID "TMPL650TKoGCD"
#define BLYNK_TEMPLATE_NAME "Wokwi Blynk"
#define BLYNK_AUTH_TOKEN "yt-dQA1Id9a4RJmWS4hiSyqSyfHiDejc"
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
//char auth[] = "FeA5DzSLXSmMVX7L4D3P_BBulUoGVPAu"; // Replace with your actual Blynk auth token
// char ssid[] = "Wokwi-GUEST"; // Wokwi simulation Wi-Fi
// char pass[] = "";
// Initialize objects
DHT dht(DHTPIN, DHTTYPE); // DHT sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD 16x2 I2C display
Servo servo; // Servo motor
// Virtual Pins for Blynk
#define TEMP_PIN V2
#define HUM_PIN V3
#define WEATHER_STATE_PIN V1
// Global variables
float temperature = 0.0;
float humidity = 0.0;
int weatherState = 0; // 0: Sunny, 1: Rainy
// Blynk setup
BlynkTimer timer;
// WiFi connection
void connectToWiFi() {
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Blynk.config(BLYNK_AUTH_TOKEN);
}
// Read DHT sensor and send data to Blynk
void readDHTSensor() {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(TEMP_PIN, temperature);
Blynk.virtualWrite(HUM_PIN, humidity);
}
// Control the servo based on weather state (pushbutton)
void controlServoAndLED() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH) {
// Rainy condition: move servo inside, LED ON
servo.write(0); // Servo moves to the inside position (example value)
Blynk.virtualWrite(WEATHER_STATE_PIN, "Rainy");
digitalWrite(LED_BUILTIN, HIGH); // Turn LED on
} else {
// Sunny condition: move servo outside, LED OFF
servo.write(90); // Servo moves to the outside position (example value)
Blynk.virtualWrite(WEATHER_STATE_PIN, "Sunny");
digitalWrite(LED_BUILTIN, LOW); // Turn LED off
}
}
// LCD display function
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temperature) + "C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(humidity) + "%");
if (weatherState == 1) {
lcd.setCursor(0, 1);
lcd.print("Rainy");
} else {
lcd.setCursor(0, 1);
lcd.print("Sunny");
}
}
// Setup function
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
servo.attach(SERVO_PIN); // Attach servo
dht.begin(); // Start DHT sensor
lcd.begin(); // Start LCD
lcd.backlight(); // Turn on backlight
connectToWiFi();
timer.setInterval(2000L, readDHTSensor); // Read DHT every 2 seconds
timer.setInterval(1000L, controlServoAndLED); // Check button every 1 second
timer.setInterval(1000L, updateLCD); // Update LCD every second
}
// Loop function
void loop() {
Blynk.run();
timer.run();
}