#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6PZZ2PIty"
#define BLYNK_TEMPLATE_NAME "Andhika Frasha Haryanda"
#define BLYNK_AUTH_TOKEN "NtIFoxHXNq7OVf7vF9rw4DP2qJomNlc7"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
DHT dht(4, DHT22);
float temperature ;
float humidity;
const int trigPin = 5;
const int echoPin = 18;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define LED 13
int SW_State=0;
BLYNK_WRITE (V0)
{
SW_State = param.asInt();
if (SW_State == 1)
{
digitalWrite(LED, HIGH);
Serial.println("LAMPU HIDUP");
Blynk.virtualWrite(V1, HIGH);
}
else
{
digitalWrite(LED, LOW);
Serial.println("LAMPU MATI");
Blynk.virtualWrite(V1, LOW);
}
}
void climateRoutine() {
byte h1 = dht.readHumidity(); // f1 and h1 are celsius and humidity readings
// byte t1 = dhtA.readTemperature(true); // for temperature in farenheits
byte t1 = dht.readTemperature(); // from DHT/A
Blynk.virtualWrite(V1, t1);
Blynk.virtualWrite(V2, h1);
// Set Virtual Pin 0 frequency to PUSH in Blynk app
// Blynk.virtualWrite(V1, h1); // Set Virtual Pin 1 frequency to PUSH in Blynk app
}
void bacasensorjarak(){
// 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);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
Blynk.virtualWrite(V3, distanceCm);
}
void setup()
{
pinMode(LED, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
climateRoutine();
bacasensorjarak();
delay(1000);
// timer.run();
}