//blynk iot auth token , wifi ssid and password
#define BLYNK_TEMPLATE_ID "TMPL3l7hgvV0o"
#define BLYNK_TEMPLATE_NAME "Project"
#define BLYNK_AUTH_TOKEN "2eMU1aQ0T44yecj16aaqSEQN5iPrx8hH"
char ssid[]="Wokwi-GUEST";
char password[]="";
//header files
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <ESP32Servo.h>
//esp pins naming
#define DHTPIN 4
#define servopin 2
#define ledpin 5
#define tripin 27
#define echopin 26
//dht sensor type
#define DHTTYPE DHT22
//dht and servo sensor object creation
DHT dht (DHTPIN, DHTTYPE);
Servo servo;
//variables for data store
float temperature;
float humidity;
int duration;
float distance;
void setup() {
// put your setup code here, to run once:
//serial communcation setup with 115200 baut rate
Serial.begin(115200);
//blynk connection and wifi connection with auth , ssid, password
Blynk.begin(BLYNK_AUTH_TOKEN,ssid,password);
//different pinmode configuration (defining which is input or output)
pinMode(ledpin,OUTPUT);
pinMode(tripin, OUTPUT);
pinMode(echopin, INPUT);
//begin dht
servo.attach(servopin,500,2400);
dht.begin();
}
void loop() {
//reading temperature and humidity form dht sensor and displaying on serial monitor
temperature=dht.readTemperature();
humidity=dht.readHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
//finding time duration taked by sound to reach the object
delayMicroseconds(2);
digitalWrite(tripin, HIGH);
delayMicroseconds(10);
digitalWrite(tripin, LOW);
duration=pulseIn(echopin,HIGH);
//find distance of object based on time taken by sound and displaying
distance=duration*0.034/2;
Serial.print("distance:");
Serial.print(distance);
Serial.println(" cm");
Serial.println("____________________________________________");
//delay 1sec
delay(1000);
//sending temp , hum , dis into blynk iot app (v1,v2,v3) are virtural pin in blynk
Blynk.virtualWrite(V1,temperature);
Blynk.virtualWrite(V2,humidity);
Blynk.virtualWrite(V4,distance);
// Blynk server in sync and run background communication between esp and blynk
Blynk.run();
}
// receiving servo motor position from blynk
BLYNK_WRITE(V3){
// received value store in pos variable
int pos=param.asInt();
//changing servo position based on pos varibale value
servo.write(pos);
}
//receiving led state value from blynk
BLYNK_WRITE(V0){
bool ledstate=param.asInt();
digitalWrite(ledpin,ledstate);
}