Ananya rout
Published

Temperature Alert via Telegram and Buzzer

Temperature Alert via Telegram and Buzzer It is a project to measure temperature of indoor things.

BeginnerProtip5 hours293
Temperature Alert via Telegram and Buzzer

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Buzzer
Buzzer
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Temperature Sensor
Temperature Sensor
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Spyder-Python
Telegram app

Story

Read more

Schematics

Temperature Alert via Telegram and Buzzer schematic

Temperature Alert via Telegram
and Buzzer
It is a project to measure temperature of indoor things or in industries using
BOLT Wifi module, LM35, Buzzer and alert using Telegram

Code

Temperature Alert via Telegram and Buzzer

Python
It is a project to measure temperature of indoor things or in industries using
BOLT Wifi module, LM35, Buzzer and alert using Telegram
import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation

from boltiot import Bolt        # importing Bolt from boltiot module
import conf                     # config file

mybolt = Bolt(conf.bolt_api_key, conf.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 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):
    """Sends message via Telegram"""
    url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": conf.telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "GET",
            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:
    # Step 1
    sensor_value = get_sensor_value_from_pin("A0")    
    print("The current temperature value is:", (100*sensor_value/1024), "degree Celsius")
    
    # Step 2
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(10)
        continue
    
    # Step 3
    if sensor_value >= conf.threshold:
        print("Sensor value has exceeded threshold")
        response = mybolt.digitalWrite('0','HIGH')
        time.sleep(10)
        message = "Alert! Temperature value has exceeded " + str(conf.threshold*100/1024) + \
                  ". The current value is " + str(sensor_value*100/1024)
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)
        response = mybolt.digitalWrite('0','LOW')

    # Step 4
    time.sleep(10)

Credits

Ananya rout
1 project • 0 followers

Comments