Wednesday, 27 May 2026

AI-Based Automatic Street Light Control with Traffic Prediction

AI-Based Automatic Street Light Control with Traffic Prediction Agentic IoT System using ESP32 + AI + n8n Automation + Telegram Voice Alerts + Google Sheets + ThingSpeak
1. Project Overview This project is an AI-powered smart street lighting system that automatically controls street lights based on: Traffic density Ambient light conditions Motion detection AI-based prediction Cloud analytics The system uses: Espressif Systems ESP32 microcontroller PIR motion sensors LDR light sensor AI prediction logic n8n workflow automation Telegram voice alerts Google Sheets logging ThingSpeak cloud dashboard The system reduces: Electricity wastage Manual maintenance Urban energy costs while enabling: Smart city automation Predictive street lighting Remote monitoring Voice-based AI notifications 2. Key Features Smart Features ✅ Automatic ON/OFF street lights ✅ Traffic density prediction ✅ AI-based energy optimization ✅ Cloud monitoring dashboard ✅ Telegram alerts with voice notification ✅ Google Sheets data logging ✅ ThingSpeak live analytics ✅ n8n automation workflow ✅ Real-time sensor monitoring ✅ ESP32 WiFi IoT control 3. System Architecture +----------------+ | LDR Sensor | +----------------+ | v +----------------+ | ESP32 | | AI Prediction | +----------------+ | | | | | | v v v PIR1 PIR2 Relay Module | | | v | Street Lights | v WiFi Internet | v +----------------------+ | n8n | | Automation Workflow | +----------------------+ | | | | | | v v v Telegram Google ThingSpeak Alerts Sheets Dashboard 4. Components List Component Quantity Purpose ESP32 Dev Board 1 Main controller PIR Motion Sensor 2 Vehicle detection LDR Sensor 1 Day/Night sensing Relay Module 1 Street light control LEDs / Street Lamp Model 4 Demonstration 220Ω Resistors 4 LED protection Breadboard 1 Prototyping Jumper Wires Several Connections 5V Adapter 1 Power supply WiFi Network 1 Cloud communication Telegram Bot 1 Notifications Google Sheet 1 Data storage ThingSpeak Channel 1 Dashboard 5. Circuit Schematic Diagram +------------------+ | ESP32 | | | LDR -------->| GPIO34 | PIR1 ------->| GPIO26 | PIR2 ------->| GPIO27 | Relay ------>| GPIO25 | | | +------------------+ Relay Module: COM -> AC Supply NO -> Street Light GND -> Common Ground LED Street Lights connected through relay 6. Working Principle Daytime LDR detects sunlight Street lights remain OFF Nighttime LDR senses darkness ESP32 activates monitoring mode Traffic Detection PIR sensors detect vehicle movement AI logic estimates traffic intensity AI Prediction Predicts: Peak traffic hours Energy consumption Lighting duration Automation Data sent to: Telegram Google Sheets ThingSpeak 7. Flowchart START | Initialize ESP32 | Read LDR Value | Is it Dark? / \ NO YES | | Lights Read PIR Sensors OFF | | Vehicle Detected? / \ NO YES | | Dim Lights Full Brightness | Send Data to Cloud | AI Prediction | Telegram Voice Alert | Repeat 8. ESP32 Source Code (Arduino IDE) #include #include const char* ssid = "YOUR_WIFI"; const char* password = "YOUR_PASSWORD"; #define LDR_PIN 34 #define PIR1 26 #define PIR2 27 #define RELAY 25 String webhook = "YOUR_N8N_WEBHOOK"; void setup() { Serial.begin(115200); pinMode(PIR1, INPUT); pinMode(PIR2, INPUT); pinMode(RELAY, OUTPUT); WiFi.begin(ssid, password); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println("WiFi Connected"); } void loop() { int ldr = analogRead(LDR_PIN); int pir1 = digitalRead(PIR1); int pir2 = digitalRead(PIR2); bool dark = ldr < 2000; bool traffic = pir1 || pir2; if(dark && traffic){ digitalWrite(RELAY, HIGH); } else{ digitalWrite(RELAY, LOW); } if(WiFi.status() == WL_CONNECTED){ HTTPClient http; http.begin(webhook); http.addHeader("Content-Type", "application/json"); String jsonData = "{"; jsonData += "\"ldr\":" + String(ldr) + ","; jsonData += "\"traffic\":" + String(traffic) + ","; jsonData += "\"light\":" + String(dark); jsonData += "}"; int response = http.POST(jsonData); Serial.println(response); http.end(); } delay(5000); } 9. AI Traffic & Power Prediction Logic Prediction Parameters The AI engine predicts: Vehicle density Energy usage Peak traffic periods Lighting duration Future electricity demand Simple AI Formula Traffic score: Traffic Score= 2 PIR1+PIR2 ​ Power consumption estimation: Power Consumption=Light_ON_Time×Wattage Prediction model: IF traffic high: Increase brightness ELSE: Dim lights Advanced AI Enhancements Future upgrades may use: TensorFlow Lite Edge AI Historical analytics Reinforcement learning 10. n8n Automation Workflow Using n8n automation platform. Workflow Steps Webhook Trigger | v Receive ESP32 JSON | +----> Google Sheets | +----> ThingSpeak Update | +----> Telegram Alert | +----> Voice Message 11. n8n Workflow JSON { "nodes": [ { "parameters": { "path": "street-light" }, "name": "Webhook", "type": "n8n-nodes-base.webhook" }, { "parameters": { "operation": "append" }, "name": "Google Sheets", "type": "n8n-nodes-base.googleSheets" }, { "parameters": { "chatId": "YOUR_CHAT_ID", "text": "Traffic detected. Street lights activated." }, "name": "Telegram", "type": "n8n-nodes-base.telegram" } ] } 12. Telegram Bot Setup Using Telegram BotFather Steps Open Telegram Search: @BotFather Create new bot: /newbot Copy Bot Token Add token into n8n Telegram node 13. Telegram Voice Notification Automation Voice Alert Example "Warning! Heavy traffic detected. Street lights switched to high brightness mode." n8n Voice Flow Webhook | Text-to-Speech API | Telegram Send Audio Recommended TTS APIs: Google Cloud Text-to-Speech ElevenLabs 14. Google Sheets Integration Using Google Sheets Logged Parameters Time LDR Traffic Light Status 10:30 PM 1800 HIGH ON Steps Create Google Sheet Enable Google Sheets API Connect credentials in n8n Append rows automatically 15. ThingSpeak Cloud Dashboard Setup Using ThingSpeak Create Channel Fields Field Description Field 1 LDR Value Field 2 Traffic Count Field 3 Light Status Dashboard Widgets Real-time graphs Traffic trends Power analytics AI prediction charts 16. AI Agentic IoT Concept This project becomes an Agentic AI IoT System because: ESP32 senses environment AI predicts conditions n8n automates decisions Telegram communicates alerts Cloud stores intelligence The system acts autonomously with minimal human intervention. 17. Future Enhancements AI Improvements TensorFlow Lite Micro Edge AI on ESP32 Camera-based traffic detection YOLO object detection Smart City Features Automatic fault detection Solar-powered operation Smart energy billing Adaptive brightness Cloud Expansion Firebase integration AWS IoT Core MQTT broker Grafana dashboards Security HTTPS encryption Secure MQTT Device authentication 18. Deployment Guide Hardware Deployment Install poles with PIR sensors Waterproof ESP32 enclosure Connect relay to street lamps Software Deployment Upload ESP32 code Configure WiFi Setup n8n server Connect Telegram API Create ThingSpeak dashboard Testing Simulate darkness Trigger PIR motion Verify cloud updates Check Telegram alerts 19. Applications Smart cities Highway lighting Parking areas Industrial zones Campus roads Smart villages 20. Advantages ✅ Energy saving ✅ Reduced maintenance ✅ AI-based automation ✅ Real-time monitoring ✅ Low operational cost ✅ Remote accessibility ✅ Scalable architecture 21. Conclusion This project demonstrates a complete AI-powered Agentic IoT Smart Street Lighting System integrating: ESP32 AI prediction n8n automation Telegram voice alerts Google Sheets ThingSpeak analytics The system intelligently manages street lights using environmental sensing and predictive analytics, making it suitable for future smart city infrastructure.

No comments:

Post a Comment

AI-Based ECG and Heart Disease Prediction System

AI-Based ECG & Heart Disease Prediction System Agentic IoT using ESP32 + AI + n8n Automation + Telegram Voice Alerts + Google Sheets + T...