#include <ESP32Servo.h>
const int hujanPin = 35;
const int cahayaPin = 34;
const int servoPin = 2;
const int merah = 4;
const int hijau = 22;
Servo servo;
// Array with LEDs that should be lit up one by one
uint8_t leds_cycling[] = { 21, 19, 18 };
// Array with LEDs that should be controlled from ThingsBoard, one by one
uint8_t leds_control[] = { 26, 33, 25 };
uint8_t pin = 15;
// Main application loop delay
int quant = 20;
// Initial period of LED cycling.
int led_delay = 1000;
// Period of sending a temperature/humidity data.
int send_delay = 2000;
// Time passed after LED was turned ON, milliseconds.
int led_passed = 0;
// Time passed after temperature/humidity data was sent, milliseconds.
int send_passed = 0;
// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;
// LED number that is currenlty ON.
int current_led = 0;
void setup() {
Serial.begin(115200);
pinMode(15, OUTPUT);
pinMode(4, OUTPUT);
pinMode(22, OUTPUT);
servo.attach(servoPin, 500, 2400);
}
void loop()
{
delay(quant);
led_passed += quant;
send_passed += quant;
// Check if next LED should be lit up
if (led_passed > led_delay) {
// Turn off current LED
digitalWrite(leds_cycling[current_led], LOW);
led_passed = 0;
current_led = current_led >= 2 ? 0 : (current_led + 1);
// Turn on next LED in a row
digitalWrite(leds_cycling[current_led], HIGH);
}
// Check if it is a time to send DHT22 temperature and humidity
const float GAMMA = 0.7;
const float RL10 = 50;
int sensorCahaya = analogRead(cahayaPin); // Baca nilai sensor
int sensorHujan = analogRead(hujanPin);
float voltage = sensorCahaya / 4096. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Cahaya: ");
Serial.println(lux);
Serial.print("Sensor Hujan: ");
Serial.println(sensorHujan);
// sensor hujan = hujan nilai besar >>
if(lux > 30000 || sensorHujan > 900){
digitalWrite(15, HIGH);
servo.write(90);
Serial.println("Tutup Canopy");
}
// sensor hujan = tidak hujan nilai kecil <<
else if (lux < 30000 || sensorHujan < 900){
digitalWrite(15, LOW);
servo.write(180);
Serial.println("Buka Canopy");
}
delay(1000);
}