/*************************************************************
SMART DEVICES WITH BLYNK APP CONTROL With Brightness of lux
*************************************************************/
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPLhqpIZ9uT"
#define BLYNK_TEMPLATE_NAME "IoTApp"
#define BLYNK_AUTH_TOKEN "VGzfIy493beaUCIY6grUUPIhX4ahLMV3"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
// include data
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
// WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// constant to count of lux
const float GAMMA = 0.7;
const float RL10 = 50;
// declare servo for functions
Servo myservo;
// data management and send data from many value of lux and light
void sendData() {
// declare analogread into light variable
int light = analogRead(36);
//declare type of float variable into volt / tegangan
float voltage = light / 4095. * 5;
//declare type of float variable into resistance
float resistance = 2000 * voltage / (1 - voltage / 5);
//declare type of float variable into lux or light (sun)
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// declare type of string variable into night
char* night = "night";
// declare type of string variable into night
char* day = "day";
Blynk.virtualWrite(V2, light); // send data a light into of blynk
Blynk.virtualWrite(V36, lux); // send data a lux into of blynk
Serial.print("Sending data...");
Serial.print(" analog: ");
Serial.print(light);
Serial.print(" lux: ");
Serial.print(lux);
if(lux < 10){
myservo.write(90);
delay(1000);
digitalWrite(2, HIGH); // turn on LED
Blynk.virtualWrite(V0, 255); // turn on LED in blynk
Blynk.virtualWrite(V3, night);
Serial.print(", Close Windows");
Serial.println(", It's night now");
}
else {
digitalWrite(2, LOW); // turn off LED
Blynk.virtualWrite(V0, 0); // turn off LED in blynk
myservo.write(0);
delay(1000);
Blynk.virtualWrite(V3, day);
Serial.print(", Open Windows");
Serial.println(", It's day now");
}
}
// declare function of timer
BlynkTimer timer;
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V0
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
// switch to close windows and turn on lamp
if(pinValue == 255)
{
// myservo.write(0);
// delay(1000);
// myservo.write(45);
// delay(1000);
myservo.write(90);
delay(1000);
digitalWrite(2, HIGH);
// Serial.println("Close Windows");
// Serial.println("Lamp is On");
}
// switch to open windows and turn off lamp
else
{
digitalWrite(2, LOW);
// myservo.write(90);
// delay(1000);
// myservo.write(90);
// delay(1000);
myservo.write(0);
delay(1000);
// Serial.println("Open Windows");
// Serial.println("Lamp is Off");
}
}
// first or begin condition
void beginCondition()
{
myservo.write(0);
}
// condition setup
void setup()
{
// Debug console
pinMode(36, INPUT);
pinMode(2, OUTPUT);
myservo.attach(13);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
beginCondition();
timer.setInterval(1L, sendData);
}
// condition loop
void loop()
{
Blynk.run();
timer.run();
}