Ashwin Jain
Published © GPL3+

Ambient light sensor using bolt and telegram app

A project that adjusts the brightness of led using LDR and sends telegram message when light becomes dim so that user can adjust brightness

IntermediateFull instructions provided1 hour733
Ambient light sensor using bolt and telegram app

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
5 mm LED: Red
5 mm LED: Red
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×4

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Snappy Ubuntu Core
Snappy Ubuntu Core
telegram

Story

Read more

Schematics

circuit for making this project

Ambient sensor by ashwin

use this diagram to make hardware configurations for the project Ambient sensor

Code

Amhardware_conf.html

HTML
<html>
    <head>
        <title>Ambient light sensor</title>
        <script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
        <script>
        setKey('{{ApiKey}}','{{Name}}');
        </script>
    </head>
    <body bgcolor="#000000">
        <center>
        <font color="ffffff"><h1>Ambient light sensor </h1></font>
        <font color="90ee90"><h4>Though light is adjusted according to surrounding lights but still you can adjust light by following buttons:</h4></font>
        <button onclick="digitalWrite(1, 'LOW');">OFF</button>
        <button onclick="analogWrite(1, 50);">1</button>
        <button onclick="analogWrite(1, 150);">2</button>
        <button onclick="analogWrite(1, 175);">3</button>
        <button onclick="digitalWrite(1, 'HIGH');">4</button>
        </center>
    </body>
</html>

Ambientlight.py

Python
import requests

import json

import time


from boltiot import Bolt

import conf2

mybolt=Bolt(conf2.api_key,conf2.device_id)


def get_value(pin):

	try:

		response=mybolt.analogRead(pin)

		data=json.loads(response)

		if data["success"]!=1:

			print("request unsucessful with response->",data)

			return -999

		sensor_value=int(data["value"])

		return sensor_value

	except Exception as e:

		print("Something went wrong")

		print(e)

		return -999

def send_telegram_message(message):

	url="https://api.telegram.org/" + conf2.telegram_bot_id + "/sendMessage"

	data= {

		"chat_id" : conf2.telegram_chat_id,

		"text" : message

	}

	try:

		response = requests.request("POST",url,params=data)

		print("this is response",response.text," from ",url)

		telegram_data=json.loads(response.text)

		return telegram_data["ok"]

	except Exception as e:

		print("an error occured while sendind data to telegram")

		print(e)

		return False


while True:

	sensor_value=get_value("A0")

	
	if sensor_value == -999:

		print("request unsuccessful")

		time.sleep(10)

		continue


	if sensor_value <= (conf2.lowlimit):

		message = "alert! light is very dim , visit https://cloud.boltiot.com/control?name=BOLT291173 and adjust light according to your convinience. current sensed brightness is " + str(sensor_value)

		telegram_status=send_telegram_message(message)

		print("this is telegram status: ",telegram_status)

		mybolt.analogWrite("1",5)

		time.sleep(3600)

	else :

		if sensor_value <= (conf2.upperlimit):

			mybolt.analogWrite("1",sensor_value)

			time.sleep(10)

		else:
			mybolt.analogWrite("1","HIGH")

			time.sleep(10)

conf2.py

Python
api_key="*********************************"	 # replace * with api key 
device_id="BOLT******"                       # replace * with device id of bolt
telegram_chat_id="@************"	           # replace * with telegram channel id
telegram_bot_id="bot**********************"  # replace * with telegram bot id

lowlimit=10

upperlimit=255

Credits

Ashwin Jain

Ashwin Jain

1 project • 0 followers
Thanks to BOLT IOT.

Comments