Maneet Kaur
Published

Automated Street Light System

This is a model for automated street light which switches ON the street light when the sunlight goes down and turns it OFF after sunrises.

IntermediateFull instructions provided2 hours14,062
Automated Street Light System

Story

Read more

Schematics

Circuit Diagram

Code

automated_street_light.py

Python
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 23 23:32:06 2019

@author: hp
"""
import time #for causing delay in seconds
import json #library for handling json data
import requests #for making HTTP requests
from boltiot import Bolt #importing Bolt from boltiot module

bolt_api_key="********" #This is your Bolt Cloud API Key
device_id="BOLT****" #This is the ID of your bolt device
telegram_chat_id="@****" #This is the channel ID which we have got when creating the channel on Telegram
telegram_bot_id="bot*****"#This is the ID of the telegram bot we had got when creating the bot
threshold=512 #This is the threshold beyond which the alert should be sent

mybolt=Bolt(bolt_api_key,device_id)
def get_sensor_value_from_pin(pin):
    """Returns the sensor value.
    Returns -999 if request fails"""
    try:
        response=mybolt.analogRead(pin)
        data=json.loads(response)
        if data["success"]!=1:
            print("Request not successful")
            print("This is the response->",data)
            return -999
        sensor_value=int(data["value"])
        return sensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999
def send_telegram_message(message):
    """Sends message via Telegram"""
    url="https://api.telegram.org/"+telegram_bot_id+"/sendMessage"
    data={"chat_id":telegram_chat_id,
          "text":message}
    try:
        response=requests.request(
                "GET",
                url,
                params=data
                )
        print("This is the Telegram response")
        print(response.text)
        telegram_data=json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occured in sending the alert message via Telegram")
        print(e)
        return False
while True:
        sensor_value=get_sensor_value_from_pin("A0")
        print("The current sensor value is: ",sensor_value)
        if sensor_value==-999:
            print("Request was unsuccessful.Skipping.")
            time.sleep(10)
            continue
        elif sensor_value>=threshold:
            print('Lights are OFF')
            mybolt.digitalWrite('4','LOW')
       
        elif sensor_value<threshold:
           print('Lights are ON')
           mybolt.digitalWrite('4','HIGH')
        
           message="Alert! LDR value has decreased below the set threshold "+str(threshold)+\
           ".The current value is: "+str(sensor_value)+". The lights have been switched ON:-)"
           telegram_status=send_telegram_message(message)
           print("This is the Telegram status: ",telegram_status)
        time.sleep(12)
            
            
                
   
        

Credits

Maneet Kaur
1 project • 6 followers

Comments