#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2JNF2asf"
#define BLYNK_AUTH_TOKEN "95YQ2MNdbtqmBu6zhEG7SKOIst_LOfpR"
#define LED1 25
#define LED2 33
#define LDR 34
const float GAMMA = 0.7;
const float RL10 = 50;
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#include <ESP32Servo.h>
const int DHT_PIN = 15;
DHTesp dhtSensor;
Servo myServo;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
void sendSensor(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Blynk.virtualWrite(V1, data.temperature);
Blynk.virtualWrite(V0, data.humidity); //mengirimkan data ke Virtual pin di Blynk Cloud
int analogValue = analogRead(LDR);
float voltage = analogValue * 5/4095.0; //jika di arduino rumus konversinya analaogValue / 1023.*5
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Lux: ");
Serial.println(lux);
Blynk.virtualWrite(V2, lux);
if(lux>=50){
digitalWrite(LED2, LOW);
Blynk.virtualWrite(V4, 0);
}
else {
digitalWrite(LED2, HIGH);
Blynk.virtualWrite(V4, 1);
}
}
BLYNK_WRITE(V3){
int pinVal = param.asInt();
digitalWrite(LED1, pinVal);
}
BLYNK_WRITE(V5){
int btnServo = param.asInt();
if(btnServo==1){
myServo.write(180);
}
else{
myServo.write(0);
}
}
void setup(){
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timer.setInterval(1000L, sendSensor);
myServo.attach(2);
}
void loop(){
Blynk.run();
timer.run();
}