#define BLYNK_TEMPLATE_ID "TMPL6mL6jxXFl"
#define BLYNK_TEMPLATE_NAME "WOKWI Blynk"
#define BLYNK_AUTH_TOKEN "EFdiwFYXWY4eHVPs_9syu4SORZ6ObR2p"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// 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;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
DHT dht(DHTPIN, DHTTYPE);
// BlynkTimer initialize
BlynkTimer timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
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 parameters - Token, ssid, password
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
// 2 parameters - 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 parameters - 1. pin #, 2. Value
Blynk.virtualWrite(V1, distanceCm);
}
// From Blynk to Wowki we need to write the LED status
// to write we always use this method (Blynk method)
BLYNK_WRITE(V0) {
// get the status of the virtual switch V0
int status = param.asInt();
digitalWrite(led, status);
}
void DHTData(){
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
Blynk.virtualWrite(V2, h);
}
void loop() {
Blynk.run();
timer.run();
DHTData();
}