Bishal Chakraborty
Published © GPL3+

Temperature Monitoring & Alert System Using Bolt IoT

It is a project to measure temperature inside refrigerator by bolt wifi connected to LM35 sensor to get notified when anomaly occurs.

IntermediateProtip5 hours5,775
Temperature Monitoring & Alert System Using Bolt IoT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 Temperature Sensor
×1
Jumper Wires Male to Female
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Digital Ocean

Story

Read more

Code

conf.py

Python
These are the API keys from Twilio, Mailgun, Bolt to help the program to run successfully.
#For Privacy i use 'XXXXXXXXX'
#You can find SSID in your Twilio Dashboard
SSID = 'AC7ae69b9370XXXXXXXXXX6978d3ce4ce8'
#You can find AUTH TOKEN on your Twilio Dashboard
AUTH_TOKEN = 'eb4563dfdXXXXXXXXXXadde03ae817bb'
#FROM_NUMBER,This is the no. generated by Twilio. 
FROM_NUMBER = '+16XXXXXXX80'
#TO_NUMBER,This is your number
TO_NUMBER = '+91XXXXXXXX03'
FROM_WHATSAPP = 'whatsapp:+14XXXXXXX86'
TO_WHATSAPP = 'whatsapp:+91XXXXXXXX03'
#This is the private API key which you can find on your Mailgun Dashboard
MAILGUN_API_KEY = '4beed33869c0a7de8b97XXXXXXXXXXXX-060550c6-e591c150'
#SANDBOX_URL,You can find this on your Mailgun Dashboard
SANDBOX_URL= 'sandbox12b9e8227bf34c118XXXXXXXXXXXXXXX.mailgun.org'
#SENDER_EMAIL,This would be test@your SANDBOX_URL
SENDER_EMAIL = 'test@sandboxXXXXXXXXXXXXXXX18ccec4718337b3f6.mailgun.org'
#RECIPIENT_EMAIL,Enter your Email ID Here
RECIPIENT_EMAIL = 'cbXXXXXX@gmail.com'
#API_KEY,This is your Bolt Cloud account API key
API_KEY = '499d00d0-81f6-XXXX-XXXX-7caaecca6e93'
#DEVICE_ID,This is the ID of your Bolt device
DEVICE_ID = 'BOLTXXXXXXX6'
FRAME_SIZE = 10
MUL_FACTOR = 6

anomaly_detection.py

Python
This code detects anomaly and send SMS, Whatsapp message and Email alert as notification.
import conf, json, time, math, statistics
from boltiot import Sms, Bolt, Email


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)
sms_whatsapp = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_WHATSAPP, conf.FROM_WHATSAPP)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
history_data=[]

while True:
    response = mybolt.analogRead('A0')
    response1 = mybolt.analogRead('A0')
    response2 = 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

    sensor_value1 = int(data['value'])
    sensor_value1 = sensor_value1/10.24
    print ("The current Temparature of your Refrigarator is "+ str(sensor_value1)+" degree celsious. And the Sensor Value is "+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(5)
        continue

    try:
        if sensor_value > bound[0] :
            sensor_value1 = sensor_value/10.24
            print ("The Temparature level has been INCREASED suddenly.Sending SMS")
            response = sms.send_sms("Someone Opened the fridge door. The Current temperature is " + str(sensor_value1)+ " degree celsious")
            response1 = mailer.send_email("RED Alert", "Someone opened the fridge door. Because The Temparature of your Refrigarator has been INCREASED suddenly. The Current temperature is " + str(sensor_value1)+" degree celsious")
            response2 = sms_whatsapp.send_sms("Someone opened the fridge door. Because The Temparature of your Refrigarator has been INCREASED suddenly. The Current temperature sensor value is " + str(sensor_value1)+ " degree celsious")
            print("This is the response for SMS ",response)
            print("This is the response for EMAIL ",response1)
            print("This is the response for WHATSAPP ",response2)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(5)

Credits

Bishal Chakraborty

Bishal Chakraborty

1 project • 6 followers

Comments