#define BLYNK_TEMPLATE_ID "TMPLgnDa13z2"
#define BLYNK_DEVICE_NAME "Quickstart Device"
#define BLYNK_AUTH_TOKEN "DXGdIpx79xs0_QxhG5NAoVaHZFvlR43r"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
if (value == 1){
digitalWrite(2, HIGH);}
else {digitalWrite(2, LOW);}
// Update state
Blynk.virtualWrite(V1, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
Serial.println("Connected");
}
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(V1, data.temperature); //mengirimkan data ke Virtual pin di Blynk Cloud
Blynk.virtualWrite(V2, data.humidity);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(2, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Blynk.begin(auth, ssid, pass);
timer.setInterval(2000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}