Akash Auti
Published

Temperature Alert using bolt module

This project can be used in various places where you need to maintain the temperature below a specific value.

IntermediateShowcase (no instructions)1 hour466
Temperature Alert using bolt module

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Buzzer
Buzzer
×1
Temperature Sensor
Temperature Sensor
×1

Story

Read more

Schematics

Circuit connection

connect the lm35 sensor and the buzzer as shown in the photo uploaded below to the bolt module.
Just remember the color of the wire and connect accordingly.

img_20190707_154234_pvUjSsWrCI.jpg

Code

Telegram_alert

Python
I have written this code in the VMware Workstation 12 .
#This is the conf.py code used in the main code written below
BOLT_API_KEY = "Enter your bolt api key here"             
DEVICE_ID = "Enter the device id from your bolt cloud"                  
telegram_chat_id = "@XXXX"        
telegram_bot_id = "botXXXX"          
threshold = 250                 






#Create another python file telegram_alert to write the main code here

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):
    """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 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(10)
        continue
    
  
    if sensor_value >= conf.threshold:
        print("Sensor value has exceeded threshold")
        response = mybolt.digitalWrite('0','HIGH')
        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)
        response = digitalWrite('0','LOW')

  
    time.sleep(10)

Credits

Akash Auti

Akash Auti

2 projects • 1 follower

Comments