//Include Library Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "6e33d0b1-2f80-4728-88b6-beadde3e0ae0";
const char SSID[] = "Wokwi-GUEST"; // Network SSID (name)
const char PASS[] = ""; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = "Gm6lEaX@FubJR1d7QEgWCb#6?"; // Secret device password
String status;
int led = 16;
int ldrpin = 34;
float resistance; // Deklarasikan variabel resistance
float voltage; // Deklarasikan variabel voltage
int lampStatus = 0;
const float GAMMA = 0.7;
const float RL10 = 50;
float analogValue = analogRead(ldrpin);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
LiquidCrystal_I2C lcd(0x27, 20, 4);
// PWM Settings for LED
int ledBrightness = 0; // Brightness value (0-255)
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(status, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(analogValue, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(lux, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(lampStatus, READ, ON_CHANGE, NULL);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup()
{
pinMode(led, OUTPUT);
pinMode(ldrpin, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(7, 0);
lcd.print("RAIHAN");
lcd.setCursor(2, 1);
lcd.print("RAMANDHA SAPUTRA");
lcd.setCursor(4, 3);
lcd.print("41422110039");
delay(3000);
lcd.clear();
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop()
{
ArduinoCloud.update();
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
float analogValue = analogRead(ldrpin);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Analog Value : ");
Serial.println(analogValue);
voltage = analogValue * (3.3 / 4096); // Hitung nilai tegangan
resistance = 2000 * voltage / (1 - voltage / 3.3); // Sesuaikan ini dengan rumus yang benar
int light = analogRead(ldrpin);
float voltage = light / 4096. * 5;
float resistance = 2000 * voltage / (1- voltage /5);
// Tampilkan nilai lux pada Serial Monitor
Serial.print("Lux : ");
Serial.println(lux, 2); // Tampilkan nilai lux dengan dua desimal
lcd.setCursor(4, 0);
lcd.print("STATUS LAMPU");
// Variabel untuk status teks (On/Off)
//Lampu Menyala 50%
if (analogValue >= 928 && analogValue < 3092)
{
int nyala = map(light, 0, 4096, 0,128);
ledBrightness = 128; // 50% brightness
analogWrite(led, nyala); // Atur kecerahan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Redup");
lcd.setCursor(1, 3);
lcd.print("Lampu menyala: 50%");
delay(2000);
lcd.clear();
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Redup");
lampStatus = 50;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
status = "On"; // Status teks untuk lampu 50% menyala
Serial.print("Status Lampu : ");
Serial.println(status);
Serial.print("-----------------------------");
Serial.println();
}
// Kondisi lampu menyala 100%
else if (analogValue >= 3092)
{
int nyala = map(light, 0, 4096, 0,255);
ledBrightness = 255; // 100% brightness
analogWrite(led, ledBrightness); // Atur kecerahan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Gelap");
lcd.setCursor(1, 3);
lcd.print("Lampu menyala:100%");
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Gelap");
lampStatus = 100;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
status = "On"; // Status teks untuk lampu 100% menyala
Serial.print("Status Lampu : ");
Serial.println(status);
Serial.print("-----------------------------");
Serial.println();
delay(2000);
lcd.clear();
}
// Kondisi lampu mati
else
{
ledBrightness = 0; // Lampu mati
analogWrite(led, ledBrightness); // Matikan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Terang");
lcd.setCursor(5, 3);
lcd.print("Lampu Mati");
lampStatus = 0;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Terang");
status = "Off"; // Status teks untuk lampu mati
Serial.print("Status Lampu : ");
Serial.println(status);
Serial.print("-----------------------------");
Serial.println();
delay(2000);
lcd.clear();
}
}