//program code for Street light fault monitor system
#include <WiFi.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//LiquidCrystal_I2C lcd(0x27, 20, 4);
#define BUZZER 23
int ldr1 = 19;
int ldr2 = 18;
int ldr3 = 15;
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi password
WiFiClient client;
const char* server = "api.thingspeak.com";
// ThingSpeak credentials
unsigned long myChannelNumber = 2713475; // Replace with your ThingSpeak channel number
const char* myWriteAPIKey = "7MC65PG54VDEHESU"; // Replace with your ThingSpeak Write API Key
void setup()
{
lcd.init();
lcd.backlight();
pinMode(ldr1, INPUT);
pinMode(ldr2, INPUT);
pinMode(ldr3, INPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
WiFi.mode(WIFI_STA);
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop()
{
int data1 = analogRead(ldr1);
Serial.println("ldr1 value=" +String(data1));
int data2 = analogRead(ldr2);
Serial.println("ldr2 value=" +String(data2));
int data3 = analogRead(ldr3);
Serial.println("ldr3 value=" +String(data3));
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Street Light Fault");
lcd.setCursor(1, 1);
lcd.print("Detection Project");
if(data1>1000 && data2>1000)
{
if(data3>1000)
{
Serial.println("All light are fault");
lcd.setCursor(0, 3);
lcd.print("All Lights are Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data1);
ThingSpeak.setField(2, data2);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else
{
Serial.println("light 1 & 2 fault");
lcd.setCursor(2, 3);
lcd.print("Light 1 & 2 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data1);
ThingSpeak.setField(2, data2);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
}
else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
}
else if(data2>1000 && data3>1000)
{
if(data1>1000)
{
Serial.println("All light are fault");
lcd.setCursor(0, 3);
lcd.print("All Lights are Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data1);
ThingSpeak.setField(2, data2);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else
{
Serial.println("light 2 & 3 fault");
lcd.setCursor(2, 3);
lcd.print("Light 2 & 3 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(2, data2);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
}
else if(data3>1000 && data1>1000)
{
if(data2>1000)
{
Serial.println("All light are fault");
lcd.setCursor(0, 3);
lcd.print("All Lights are Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data1);
ThingSpeak.setField(2, data2);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else
{
Serial.println("light 1 & 3 fault");
lcd.setCursor(2, 3);
lcd.print("Light 1 & 3 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data3);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
}
else if(data1>1000)
{
Serial.println("Light 1 Fault");
lcd.setCursor(3, 3);
lcd.print("Light 1 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(1, data1);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else if (data2>1000)
{
Serial.println("Light 2 Fault");
lcd.setCursor(3, 3);
lcd.print("Light 2 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(2, data2);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else if (data3>1000){
Serial.println("Light 3 Fault");
lcd.setCursor(3, 3);
lcd.print("Light 3 Fault");
digitalWrite(BUZZER, HIGH);
// set the fields with the values
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
else
{
Serial.println("NO Fault");
lcd.setCursor(2, 3);
lcd.print("No Fault ocuured");
digitalWrite(BUZZER, LOW);
// set the fields with the values
ThingSpeak.setField(1, data1);
ThingSpeak.setField(2, data2);
ThingSpeak.setField(3, data3);
int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (responseCode == 200)
{
Serial.println("Data sent to ThingSpeak successfully");
} else
{
Serial.println("Error sending data to ThingSpeak: " + String(responseCode));
}
delay(20000); // Send data every 20 seconds*/
}
delay(6000);
}