#define BLYNK_TEMPLATE_ID "TMPLPAypn2ac"
#define BLYNK_DEVICE_NAME "LDR to BLYNK"
#define BLYNK_AUTH_TOKEN "YdjazJZv1HM9dxERPb4LQcCJpm0Nr7Oo"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#define virtualPinLed V1
#define virtualPinSuhu V2
#define virtualPinHumidity V3
#define virtualPinLux V4
DHTesp dhtSensor; //memanggil library
BlynkTimer timer;
const float GAMMA = 0.7; //nilai konstanta gamma
const float RL10 = 50; //nilai R saat 10 lux
const int DHT_PIN = 15; //pin sensor DHT22
const int photoresistorPin = 35;
const int ledPin = 25;
const float batasSuhu = 50;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void sendSensor()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("==================================");
//mengirimkan data ke Virtual pin di Blynk Cloud
Blynk.virtualWrite(virtualPinSuhu, data.temperature);
Blynk.virtualWrite(virtualPinHumidity, data.humidity);
//mengkonfigurasi coding Luxmeter
int bacaanAnalog = analogRead(photoresistorPin); //membaca luaran analog sensor di pin AO
float voltage = (bacaanAnalog / 4095.0) * 5; //konversi fraksi tegangan menuju max 5V
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)); //dari rumus https://lagacemichel.com/tag/photoresistor-2/
Serial.print("Lux: ");
Serial.println(lux);
Blynk.virtualWrite(virtualPinLux, lux); //menyuruh Blynk mengubah nilai virtual pin
if (data.temperature > batasSuhu) //jika suhu lebih besar dari batas
{
digitalWrite(ledPin, HIGH); //nyalakan led indikator
Blynk.virtualWrite(virtualPinLed, HIGH); //update nilai virtual pin ke Blynk
} else
{
digitalWrite(ledPin, LOW);
Blynk.virtualWrite(virtualPinLed, LOW);
}
}
void setup()
{
Serial.begin(9600); //memulai serial monitor
Blynk.begin(auth, ssid, pass); //memulai Blynk
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); //menginisiasi dht sensor
timer.setInterval(1000L, sendSensor); //mengeset interval waktu dari fungsi
pinMode(ledPin, OUTPUT); //led sebagai luaran
}
void loop()
{
Blynk.run();
timer.run();
}