Riddhi Varshney
Published

Temperature Alert System

The alert is generated due to variations in the fridge temperature using message(Twilio), email(Mailgun), sound(buzzer) and light(LED).

BeginnerFull instructions provided501
Temperature Alert System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Temperature Sensor
Temperature Sensor
×1
LED (generic)
LED (generic)
×1
Buzzer
Buzzer
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
SMS Messaging API
Twilio SMS Messaging API
Mailgun
Ubuntu
Ubuntu

Story

Read more

Code

Twilio configuration file

Python
SID="xxxxxxxxx"         # Present in Twilio dashboard
AUTH_TOKEN="xxxxxxxx"   # Also present in Twilio dashboard
FROM_NUMBER="+xxxxxxxx" # The number alloted on account creation
TO_NUMBER="+xxxxxxxx"   # Number where you want to recieve SMS
API_KEY="xxxxxxxx"      # Api key on bolt cloud
DEVICE_ID="BOLTxxxxx"   # From bolt cloud
FRAME_SIZE=10           # For z-score analysis(value can be changed)
MUL_FACTOR=6            # For z-score analysis(value can be changed)

Main program

Python
import conf, json, time, math, statistics
from boltiot import Sms, Email, Bolt                # Sms and Email files included to send sms and email 

def compute_bounds(history_data,frame_size,factor): # Function to compute bounds for Z-score analysis
    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)    # Variables to call the configuration files
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER) # Object created to send sms
mailer = Email(email_conf.MAILGUN, email_conf.SANDBOX, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)    # Object created to send email
history_data=[]

while True:
    response = mybolt.analogRead('A0')  # Input data from the sensor
    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'])
        Temperature = (100*sensor_value)/1024    
    except e:
        print("There was an error while parsing the response: ",e)
        continue

    bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)   # Function call for bounds
    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

    print("Low bound is {}".format(bound[1]))    # To print the bounds
    print("High bound is {}".format(bound[0]))
   
    try:    # Conditions to send the alert
        if sensor_value > bound[1] :      
            print ("The temperature level has deacreased. Sending an SMS.")
            response = sms.send_sms("The fridge is malfunctioning.The current temperature in degree celcius is"+str(Temperature))
            print("This is the response " +str(response))
            led=digitalWrite('2','HIGH')
            buzzer=digitalWrite('3','HIGH')
            time.sleep(0.001)
            led=digitalWrite('2','LOW')
            buzzer=digitalWrite('3','LOW')
            print("Making request to Mailgun to send an email")
            response1 = mailer.send_email("Alert", "The fridge is malfunctioning, the temperature in degree celcius is " +str(Temperature))
            response_text = json.loads(response1.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
        elif sensor_value < bound[0]:
            print ("The light level decreased suddenly. Sending an SMS.")
            response = sms.send_sms("Someone turned off the lights.The current temperature in degree celcius is"+str(Temperature))
            print("This is the response "+str(response))
            led=digitalWrite('2','HIGH')
            buzzer=digitalWrite('3','HIGH')
            time.sleep(0.001)
            led=digitalWrite('2','LOW')
            buzzer=digitalWrite('3','LOW')
            print("Making request to Mailgun to send an email")
            response1 = mailer.send_email("Alert", "Someone opened the fridge, the temperature in degree cecius is " +str(Temperature))
            response_text = json.loads(response1.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
        
        except Exception as e:
            print("Error",e)

Mailgun configuration file

Python
MAILGUN_API_KEY = "xxxxxxxx" # Present in Mailgun dashboard
SANDBOX_URL=  "xxxxxxxx"     # Also present in Mailgun dashboard
SENDER_EMAIL ="xxxxxxxx"     # test@your SANDBOX_URL in Mailgun Dashboard
RECIPIENT_EMAIL = "xxxxxxxx" # Email Id to recieve the mail
API_KEY = "xxxxxxxx"         # Api key on bolt cloud
DEVICE_ID = "xxxxxxxx"       # From bolt cloud

Credits

Riddhi Varshney
1 project • 0 followers

Comments