Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
TANMOY DEY
Published © GPL3+

Capstone Project - Temperature Anomaly Detection

This IoT device detects sudden change in temperature of the cooling storage using a temperature sensor (LM35) and Bolt Wifi Module.

IntermediateFull instructions provided4 hours195
Capstone Project - Temperature Anomaly Detection

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Temperature Sensor
Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

Schematic Diagram

Code

anomaly_detection.py

Python
This is the main Code that will detect anomaly detection
import conf, json, time, math, statistics, requests
from boltiot import Bolt

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( "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      

def compute_bounds(history_data,frame_size,factor):
    if len(history_data)<frame_size :      
        return None    
    if len(history_data)>frame_size :     
        del history_data[0:len(history_data)-frame_size]
    Mn=statistics.mean(history_data)
    Variance=0 
    for data in history_data: 
        Variance += math.pow((data-Mn),2)
    Zn = factor * math.sqrt(Variance/frame_size)    
    High_bound = history_data[frame_size-1]+Zn
    Low_bound = history_data[frame_size-1]-Zn
    return [High_bound,Low_bound] 


mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)

history_data=[]


while True:    
    response = mybolt.analogRead('A0')
    data = json.loads(response)  
    if data['success'] != 1:     
        print("There was an error while retriving the data.")   
        print("This is the error:"+data['value'])       
        time.sleep(5)   
        continue  
    
    print ("The sensor value is : "+data['value'])
    sensor_value=0  
    try:      
        sensor_value = int(data['value'])
        t = sensor_value/10.24
        T = round(t, 2)
        degree = u"\N{DEGREE SIGN}"
    except e: 
        print("There was an error while parsing the response: ",e)    
        continue  
    
    bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR) 
    if not bound:    
        required_data_count=conf.FRAME_SIZE-len(history_data)   
        print("Not enough data to compute Z-score. Need ",required_data_count," more data points")     
        history_data.append(int(data['value']))    
        time.sleep(5)     
        continue
    
    try:   
        if sensor_value > bound[0] :
            print("Temperature increased suddenly. Sending Alert!") 
            message = "Alert! Someone has opened the chamber, The current temperature is " + str(T) + str(degree) + "C "     
            telegram_status = send_telegram_message(message)        
            print("This is the Telegram status:", telegram_status) 
 
        elif sensor_value < bound[1]:
             print("Temperature decreased suddenly. Sending Alert!") 
             message = "Please! Adjust the temperature, The current temperature is " + str(T) + str(degree) + "C "     
             telegram_status = send_telegram_message(message)       
             print("This is the Telegram status:", telegram_status)
        history_data.append(sensor_value);   
    
    except Exception as e:      
        print ("Error",e)  
    time.sleep(10)

conf.py

Python
telegram_chat_id = "@Temp_2002_bot"        # This is the channel ID of the created                                             Telegram channel.Paste after @ symbol.#

telegram_bot_id = "bot5089549443:AAEk9cqU1zj92waCpPp88Od0H-on5-Q_H2M"                           # This is the bot ID of the created Telegram Bot.Paste after bot text.

API_KEY = "********************************"
DEVICE_ID = "BOLT********"
FRAME_SIZE = 10
MUL_FACTOR =6

Credits

TANMOY DEY
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.