Aditya
Published

Temperature Monitoring System

Our project monitors temperature and alerts the person by sending mails, messages, and tweets when temperature crosses threshold values.

IntermediateFull instructions provided8 hours843
Temperature Monitoring System

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

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
mailgun
digital ocean
SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Code

conf.py

Python
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)

email_conf.py

Python
import email_conf, json, time
from boltiot import Email, Bolt

minimum_limit = 300 #the minimum threshold of light value 
maximum_limit = 600 #the maximum threshold of light value 


mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)


while True: 
    print ("Reading sensor value")
    response = mybolt.analogRead('A0') 
    data = json.loads(response) 
    print ("Sensor value is: " + str(data['value']))
    try: 
        sensor_value = int(data['value']) 
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            print("Making request to Mailgun to send an email")
            response = mailer.send_email("Alert", "The Current temperature sensor value is " +str(sensor_value))
            response_text = json.loads(response.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
    except Exception as e: 
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)

temp_sms.py

Python
import conf, json, time
from boltiot import Sms, Bolt
import json, time

minimum_limit = 300
maximum_limit = 600  


mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)


while True: 
    print ("Reading sensor value")
    response = mybolt.analogRead('A0') 
    data = json.loads(response) 
    print("Sensor value is: " + str(data['value']))
    try: 
        sensor_value = int(data['value']) 
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            print("Making request to Twilio to send a SMS")
            response = sms.send_sms("The Current temperature sensor value is " +str(sensor_value))
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))
    except Exception as e: 
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)

anamoly_detection.py

Python
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)
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

Aditya

Aditya

1 project • 0 followers

Comments