#define BOTtoken "7075784432:AAFHqptWCMPvcZLNeTfJre239blBfn2EF4c"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// WiFiClientSecure client;
// UniversalTelegramBot bot(BOTtoken, client);
int led[] = {18, 5}; // Street light LED pins
int ir[] = {27 }; // IR motion sensor pins
int ldr[] = {33 }; // LDR sensor pins
unsigned long previousMillis = 0;
const long interval = 60000; // Interval for sending message (in milliseconds)
unsigned long bot_lasttime = 0;
const unsigned long BOT_MTBS = 1000; // Mean time between scan messages
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
String chat_id = "123456"; // bot.messages[i].chat_id;
String text = "/status"; // bot.messages[i].text;
String from_name = "Wokwi User"; // bot.messages[i].from_name;
if (from_name == "") {
from_name = "Guest";
}
if (text == "/start") {
String welcome = "Welcome to the Street Light Monitoring System, " + from_name + ".\n";
welcome += "Commands:\n";
welcome += "/status : Get system status\n";
Serial.println(welcome);
// bot.sendMessage(chat_id, welcome);
}
if (text == "/status") {
String status = "Street light system is operational.\n";
for (int j = 0; j < 3; j++) {
status += "LED " + String(j + 1) + " is " + (digitalRead(led[j]) ? "ON" : "OFF") + "\n";
}
Serial.println(status);
// bot.sendMessage(chat_id, status);
}
}
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(led[i], OUTPUT);
pinMode(ir[i], INPUT);
pinMode(ldr[i], INPUT);
// Setup LEDC for PWM
ledcSetup(i, 5000, 8); // Channel, frequency, resolution
ledcAttachPin(led[i], i); // Attach channel to pin
}
// Simulate WiFi connection
Serial.println("Connecting to WiFi...");
delay(1000);
Serial.println("WiFi connected");
// client.setInsecure();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
sendTelegramMessageToAll("Street light system is operational.");
previousMillis = currentMillis;
}
for (int i = 0; i < 3; i++) {
handleLEDControl(i);
}
if (millis() - bot_lasttime > BOT_MTBS) {
int numNewMessages = 1; // Simulate new message
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = 0; // Simulate no more new messages
}
bot_lasttime = millis();
}
}
void handleLEDControl(int index) {
int irState = digitalRead(ir[index]); // Read IR sensor state
int ldrValue = analogRead(ldr[index]); // Read LDR sensor value
if (ldrValue < 500) { // If light level is low
ledcWrite(index, 128); // Turn on LED at 50% brightness
if (irState == HIGH) { // If motion is detected
ledcWrite(index, 255); // Turn on LED at 100% brightness
}
} else {
ledcWrite(index, 0); // Turn off LED
}
// Check if there is a problem: IR detects motion, LDR detects darkness, but LED is off
if (ldrValue < 500 && irState == HIGH && ledcRead(index) == 0) {
sendTelegramMessageToAll("Problem detected: LED " + String(index + 1) + " should be ON but it's OFF.");
delay(1000); // Add a delay to prevent spamming messages
}
}
void sendTelegramMessageToAll(String message) {
// Simulate sending a Telegram message
Serial.print("Sending message to Telegram: ");
Serial.println(message);
}