#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLK4O-h8Z6"
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
const int trigPin = 5;
const int echoPin = 18;
#define Merah 14
#define Kuning 22
#define Hijau 13
long duration;
float distanceCm;
float distanceInch;
const int DHT_PIN = 15;
DHTesp dhtSensor;
char auth[] = "5vpqk1kTnwQSNM7C5Z41WeksUoe7nlus";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
Serial.println("Test");
}
void sendSensor()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
Blynk.virtualWrite(V0, data.temperature); //mengirimkan data ke Virtual pin di Blynk Cloud
Blynk.virtualWrite(V1, data.humidity); //mengirimkan data ke Virtual pin di Blynk Cloud
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timer.setInterval(2000L, sendSensor);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop()
{
Blynk.run();
timer.run();
// 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;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Blynk.virtualWrite(V2, distanceCm);
delay(1000);
}