#include <WiFi.h>
#include <PubSubClient.h> // Library for MQTT
#include <LiquidCrystal_I2C.h>
// WiFi Configuration
const char* ssid = "Wokwi-GUEST"; // Your WiFi SSID
const char* password = ""; // Your WiFi Password
// MQTT Configuration
const char* mqttServer = "mqtt3.thingspeak.com"; // MQTT Broker address
const int mqttPort = 1883; // MQTT Port
const char* mqttUser = "Hyk5PAkMMzkHJCMfBQkcGRE"; // MQTT Username
const char* mqttPassword = "NczuHZThketxmD96Q7p3UwwZ"; // MQTT Password
const char* mqttClientID = "Hyk5PAkMMzkHJCMfBQkcGRE"; // MQTT Client ID
const char* mqttTopic = "channels/2751055/publish/fields/field1"; // MQTT Topic for publishing data
WiFiClient espClient;
PubSubClient client(espClient);
// Pin Definitions for Light Sensor and LEDs
#define LIGHT_SENSOR_PIN 32 // Photoresistor Sensor Pin
#define LED_YELLOW_PIN 14 // Yellow LED Pin
#define LED_GREEN_PIN 12 // Green LED Pin
#define LED_ORANGE_PIN 27 // Orange LED Pin
#define LED_RED_PIN 26 // Red LED Pin
// LCD I2C Address and Dimensions
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
// Start Serial Communication
Serial.begin(9600);
// Set LED pins to OUTPUT mode
pinMode(LED_YELLOW_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_ORANGE_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting...");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
lcd.setCursor(0, 1);
lcd.print(".");
}
Serial.println("\nWiFi Connected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
// Connect to MQTT Broker
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect(mqttClientID, mqttUser, mqttPassword)) {
Serial.println("Connected to MQTT!");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
delay(5000); // Retry after 5 seconds
}
}
}
void loop() {
// Ensure MQTT connection stays alive
if (!client.connected()) {
reconnectMQTT();
}
client.loop();
// Read analog value from light sensor
int analogValue = analogRead(LIGHT_SENSOR_PIN);
// Print analog value to Serial Monitor
Serial.print("Analog Value = ");
Serial.println(analogValue);
// Update the LCD display
lcd.setCursor(0, 0);
lcd.print("Analog: ");
lcd.print(analogValue);
// Determine light status and control LEDs
String lightStatus = "";
if (analogValue < 40) {
lcd.setCursor(0, 1);
lcd.print("Gelap ");
lightStatus = "Gelap";
controlLEDs(LED_RED_PIN);
} else if (analogValue < 800) {
lcd.setCursor(0, 1);
lcd.print("Redup ");
lightStatus = "Redup";
controlLEDs(LED_YELLOW_PIN);
} else if (analogValue < 2000) {
lcd.setCursor(0, 1);
lcd.print("Terang ");
lightStatus = "Terang";
controlLEDs(LED_GREEN_PIN);
} else if (analogValue < 3200) {
lcd.setCursor(0, 1);
lcd.print("Cukup Terang ");
lightStatus = "Cukup Terang";
controlLEDs(LED_ORANGE_PIN);
} else {
lcd.setCursor(0, 1);
lcd.print("Sangat Terang");
lightStatus = "Sangat Terang";
controlLEDs(0); // Turn off all LEDs
}
// Send sensor data to ThingSpeak via MQTT
String payload = String(analogValue); // Send sensor value
if (client.publish(mqttTopic, payload.c_str())) {
Serial.println("Data Sent to ThingSpeak");
} else {
Serial.println("Failed to send data");
}
// Wait before reading again
delay(10000); // Delay for 10 seconds
}
// Function to handle LED control based on light status
void controlLEDs(int activeLED) {
// Turn off all LEDs first
digitalWrite(LED_YELLOW_PIN, LOW);
digitalWrite(LED_GREEN_PIN, LOW);
digitalWrite(LED_ORANGE_PIN, LOW);
digitalWrite(LED_RED_PIN, LOW);
// Turn on the active LED
if (activeLED != 0) {
digitalWrite(activeLED, HIGH);
}
}
// Function to handle MQTT reconnection
void reconnectMQTT() {
while (!client.connected()) {
Serial.print("Reconnecting to MQTT...");
if (client.connect(mqttClientID, mqttUser, mqttPassword)) {
Serial.println("Reconnected to MQTT");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
delay(5000); // Retry every 5 seconds
}
}
}