Ijas Saleem
Published

Temperature Monitoring System Using Bolt IoT

A simple project to monitor the temperature in a refrigerator and send alerts to our e-mail if temperature rises or falls down.

IntermediateFull instructions provided1,268
Temperature Monitoring System Using Bolt IoT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 IC
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

Circuit

Code

Main program

Python
This is the main program that when run does the temperature monitor. It requires another file called "conf.py" which contains the varies configurations required.
import conf
from boltiot import Email, Bolt
import json, time, math, statistics

max_val = conf.max_thresh
min_val = conf.min_thresh

def valintemp(sensor_val):
  return str(round(sensor_val*100 / 1024,2))+"˚C"


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:
  print("\nReading value from sensor")
  response = mybolt.analogRead('A0')
  sensor_val = int(json.loads(response)['value'])
  print("Sensor value is " + valintemp(sensor_val))


  try:
    if sensor_val > max_val:
      print("Temp above threshold. Sending alert!!!")
      mailer.send_email("Alert from temp sensor","The current temperature value ("+ valintemp(sensor_val) +") is above the threshold by "+ valintemp(sensor_val-max_val))

    elif sensor_val < min_val:
      print("Temp below threshold. Sending alert!!!")
      mailer.send_email("Alert from temp sensor","The current temperature value ("+ valintemp(sensor_val) +") is below the threshold by "+ valintemp(min_val - sensor_val))

  except Exception as e:
    print("Error",e)

  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(sensor_val)
        time.sleep(10)
        continue
  
  #else:
    #print("Current value - "+str(sensor_val)+"["+str(bound[1])+"-"+str(bound[0])+"]")

  try:
        if sensor_val > bound[0] :
            print ("The temp level increased suddenly. Sending an E-mail.")
            mailer.send_email("Alert from temp sensor","Someone has opened the fridge!!!")
            
        elif sensor_val < bound[1]:
            print ("The temp level decreased suddenly. Sending an E-mail.")
            mailer.send_email("Alert from temp sensor","Temp of fridge has reduced suddenly!!!!")
            
        history_data.append(sensor_val);
  except Exception as e:
        print ("Error",e)

  time.sleep(10)

conf.py

Python
This is the configuration file.
MAILGUN_API_KEY = 'XXXXX' 
SANDBOX_URL= 'XXXX' 
SENDER_EMAIL = 'XXXXX'
RECIPIENT_EMAIL = 'XXXXX'
API_KEY = 'XXXXX'  #bolt api key
DEVICE_ID = 'XXXX' #bolt device id
min_thresh = XX  # min threshold 
max_thresh = XX  # max threshold
frame_size = X   # frame size for Z-score analysis
mul_factor = X   # multipilcation factor for Z-score analysis

Credits

Ijas Saleem
2 projects • 0 followers

Comments