#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#define LDR_PIN 34 // LDR connected to pin 34 (change as needed)
#define NTC_PIN 35 // NTC connected to pin 35 (change as needed)
#define LED_PIN 15 // LED connected to pin 15 (change as needed)
#define BUZZER_PIN 2 // Buzzer connected to pin 2 (change as needed)
#define RELAY_PIN 32 // Relay connected to pin 32 (change as needed)
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const float GAMMA = 0.7;
const float RL10 = 50;
const float SERIES_RESISTOR = 10000.0; // 10k ohm series resistor
const float NOMINAL_RESISTANCE = 10000.0; // 10k ohms at 25 degrees Celsius
const float NOMINAL_TEMPERATURE = 25.0; // 25 degrees Celsius
const float BETA_COEFFICIENT = 3950.0; // Beta coefficient
const float ADC_MAX = 4095.0; // ADC max value for ESP32
const float SUPPLY_VOLTAGE = 3.3; // Supply voltage
LiquidCrystal_I2C lcd(0x27, 20, 4);
WiFiClient client;
unsigned long myChannelNumber = 2810103; // Replace with your ThingSpeak channel number
const char *myWriteAPIKey = "675MAW1VEFWJP4GI"; // Replace with your ThingSpeak Write API Key
void setup() {
// Start the serial communication
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize ThingSpeak
ThingSpeak.begin(client);
pinMode(LDR_PIN, INPUT);
pinMode(NTC_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
lcd.init();
lcd.backlight();
// Start with relay and buzzer off, and LED off
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
// Run Blynk to maintain communication
Blynk.run();
// Read the analog value from LDR
int ldrValue = analogRead(LDR_PIN);
float voltage = ldrValue / 4095.0 * 5.0;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Read the analog value from NTC
int ntcValue = analogRead(NTC_PIN);
float celsius = 1 / (log(1 / (4095.0 / ntcValue - 1)) / BETA_COEFFICIENT + 1.0 / 298.15) - 273.15;
// Display the LDR value and light condition on LCD and Serial Monitor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lux: ");
lcd.print(lux);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(celsius);
lcd.print(" C");
Serial.print("Lux: ");
Serial.println(lux);
Serial.print("Temperature: ");
Serial.println(celsius);
// Control LED, Relay, and Buzzer based on LDR value
if (lux > 500) {
lcd.setCursor(0, 2);
lcd.print("Terang");
Serial.println("Kondisi: Terang");
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
} else {
lcd.setCursor(0, 2);
lcd.print("Gelap");
Serial.println("Kondisi: Gelap");
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
tone(BUZZER_PIN, 1000);
}
// Check if temperature exceeds 30°C
if (celsius > 30) {
lcd.setCursor(0, 3);
lcd.print("Suhu Tinggi");
Serial.println("Kondisi: Suhu Tinggi");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000);
digitalWrite(RELAY_PIN, HIGH);
}
// Send data to Blynk
Blynk.virtualWrite(V1, lux);
Blynk.virtualWrite(V2, celsius);
// Send data to ThingSpeak
ThingSpeak.setField(1, lux); // Field 1 for Lux
ThingSpeak.setField(2, celsius); // Field 2 for Temperature
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("ThingSpeak update successful");
} else {
Serial.println("ThingSpeak update failed, HTTP error code: " + String(x));
}
delay(5000); // Adjust the delay as needed
}