shoaib aslam shaik
Published © GPL3+

Temperature Alert via telegram

Using LED and Telegram, to get alerts if temperature increases more than given threshold

BeginnerFull instructions provided2 hours724
Temperature Alert via telegram

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Breadboard (generic)
Breadboard (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
LED (generic)
LED (generic)
×1
Temperature Sensor
Temperature Sensor
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Code

Temperature_alert_system

Python
LED glows when the temperature exceeds threshold value and telegram alert will be sent
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 is not successfull")
            print("This was response->", data)
            return -999
        sensor_value = int(data["value"])
        return sensor_value
    except Exception as e:
        print("Error while 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:
    # Step 1
    sensor_value = get_sensor_value_from_pin("A0")    
    print("The current sensor value is:", sensor_value)
    
    # Step 2
    if sensor_value == -999:
        print("Request was not unsuccessfull. Skipping the step.")
        time.sleep(10)
        continue
    
    # Step 3
    if sensor_value >= conf.threshold:
        print("Sensor value has exceeded threshold")
        print ("Turning On LED")
        response2 = mybolt.digitalWrite('0','HIGH')
        print (response2)
        message = "Alert! Sensor value has exceeded " + str(conf.threshold) + \
                  ". The current value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)

    # Step 4
    time.sleep(10)
    print ("Turning Off LED")
    response3 = mybolt.digitalWrite('0','LOW')
    print (response3)

Credits

shoaib aslam shaik
1 project • 0 followers

Comments