Aadarsh
Published

Anomaly Temperature Detector

Want to get notified in your email if your Freezer is not working properly. Then this device will do it so.

IntermediateFull instructions provided5 hours333
Anomaly Temperature Detector

Things used in this project

Hardware components

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

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Linux subsystem for windows
Mailgun

Story

Read more

Code

Polynomial Regression Product

JavaScript
setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);//for converting the value recieved by sensor to temperature unit we need to multiply it by 0.0977.
plotChart('time_stamp','temp');

Configuration File

Python
MAILGUN_API_KEY= 'c46e376f604c352d*******b064705612-*********-4591ce69' #can be found in your mailgun account

SANDBOX_URL= ' sandboxdba7620ba3d2469ab59fe4**********.mailgun.org' #can be found in your mailgun account

SENDER_EMAIL= 'test@sandboxdba7620ba**********fe44f85f14856.mailgun.org'  #email sent from this id. can be found in the mailgun account.

RECIPIENT_EMAIL= 'aadarsh@*******.com'  #email to which mail is to be sent

API_KEY= 'aadfd682-****-4ca1-****-457****4d6a1' #your bolt api key, can be found from your cloud page.

DEVICE_ID= 'BOLT******'  #device id of your bolt device.
FRAME_SIZE= 4
MUL_FACTOR= 3

Anomaly detection

Python
   
import conf, json, time, math, statistics
from boltiot import Email, 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)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
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(10)
        continue

    print ("This is the value "+data['value'])
    sensor_value=0
    try:
        sensor_value = int(data['value'])
    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 ("Someone opened the fridge. Sending an EMAIL.")
            response = mailer.send_email("Alert","Someone Opened the door of fridge")
            print("This is the response ",response)
        elif sensor_value < bound[1]:
            print ("Someone changed the settings of fridge.. Sending an EMAIL.")
            response = mailer.send_email("Alert","Someone Changed the settings of Fridge")
            print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

Aadarsh
1 project • 0 followers

Comments