Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
utpal dutta
Published

Temperature Monitoring & Anomaly detection system: TempGuard

TempGuard not only ensures that the temperature is maintained within the optimal range but detects any anomaly and sends immediate alert.

IntermediateFull instructions provided5 hours204
Temperature Monitoring & Anomaly detection system: TempGuard

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

Software apps and online services

Octonion Digital Ocean cloud
Python
putty

Story

Read more

Schematics

Connection for LM35

Image depicts the connection for LM35 temp sensor.

Code

Anomaly_detection.py

Python
This file has the main code. Apart from this file there is an another file from which the configuration details has been fetched. Such as , API keys and ids etc.
import conf, json, time, math, statistics
from boltiot import Sms, Bolt
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)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]

while True:
    response = mybolt.analogRead('A0')
    data = json.loads(response)
    temp_val = int(data['value'])
    temp = 100*temp_val/1024
    if data['success'] != 1:
        print("There was an error while retriving the data.")
        print("This is the error:"+str(temp))
        time.sleep(10)
        continue

    print ("This is the value "+ str(temp))
    sensor_value=0
    try:
        sensor_value = temp
    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(10)
        continue

    try:
        if sensor_value > bound[0] :
            print ("Anomaly detected. Sending an SMS.")
            response = sms.send_sms("Temp going up. Alert! Alert!")
            print("This is the response ",response)
        elif sensor_value < bound[1]:
            print ("Alert! Alert! anomaly detected. Temperature going down abnormally.")
            response = sms.send_sms("Alert! Anomaly detected. Temp going down abnormally")
            print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

utpal dutta
1 project • 0 followers

Comments