#define BLYNK_TEMPLATE_ID "TMPL6PanQTJbt"
#define BLYNK_TEMPLATE_NAME "esp32"
#define BLYNK_AUTH_TOKEN "bnNHxu7_0lBp_gPvXXzLngyP_9m1Z7gk"
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#define BLYNK_PRINT Serial
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHT_PIN 15
#define trigPin 27
#define echoPin 26
#define ldrPin 32
DHTesp dhtSensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Blynk.begin(auth, ssid, pass);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int humidity, temperature;
tempNhum(humidity,temperature);
Serial.println(humidity);
Serial.println(temperature);
int distance;
dist(distance);
Serial.println(distance);
int lux;
light(lux);
Serial.println(lux);
Blynk.virtualWrite(V0, humidity);
Blynk.virtualWrite(V1, temperature);
Blynk.virtualWrite(V2, distance);
Blynk.virtualWrite(V3, lux);
delay(1000);
}
void tempNhum(int &humidity, int &temperature){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if(isnan(data.humidity)||isnan(data.temperature)){
Serial.println("Failed to read from DHT22!!");
humidity =-1;
temperature =-1;
}else{
humidity = data.humidity;
temperature = data.temperature;
}
}
void dist(int &distance){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pulse duration
int duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration / 58;
}
void light(int &lux){
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
int analogValue = analogRead(ldrPin);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
}