Pratheek Reddy
Published

Reading Lights using Bolt IOT

Have you ever been so engrossed in your work that you forget to turn on your reading lights?

IntermediateFull instructions provided2 hours293
Reading Lights using Bolt IOT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
LDR, 1 Mohm
LDR, 1 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Telegram
Ubuntu
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

circuit connections

Code

Telegram alert code

Python
import requests                 
import json                     
import time                     

from boltiot import Bolt        
import conf                     

mybolt = Bolt(conf.bolt_api_key, conf.device_id)

def get_sensor_value_from_pin(pin):
        try:
        response = mybolt.analogRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            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):
       url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": conf.telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        print("This is the Telegram URL")
        print(url)
        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 occurred 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 unsuccessfull. Skipping.")
        time.sleep(20)
        continue
    
        if sensor_value < conf.threshold:
        print("Sensor value has exceeded threshold")
        message = "Alert! Your eyes are getting strained .Please switch on                          reading lights.
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)

       time.sleep(20)

Credits

Pratheek Reddy
1 project • 1 follower

Comments