siddhi s
Published

Temperature Monitoring and Alert System

Monitor the temperature of a system by checking any sudden change or out of range temperature, and informing the user.

IntermediateFull instructions provided844
Temperature Monitoring and Alert System

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
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
SMS Messaging API
Twilio SMS Messaging API
Digital ocean
Mailgun

Story

Read more

Code

Temperature monitoring system code.

Python
Two python files are required. The first one - 'conf.py' will contain requirements for configuring online services and the second one - 'temp_anomaly.py' is the code for temperature monitoring.
#conf.py:
SSID='AC7bcbf9580b096b0e60d8ce8008c3f0e0'
AUTH_TOKEN='523bba9708236c5657d0ed9d6cb30598'
FROM_NUMBER='+1321XXXX701'
TO_NUMBER='+91XXXXXXXXXX'
API_KEY='XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
DEVICE_ID='BOLTXXXXXXX'
FRAME_SIZE = 10
MUL_FACTOR = 6
MAILGUN_API_KEY= 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXX-XXXXXXXX'
SANDBOX_URL ='sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org'
SENDER_EMAIL='test@sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org'
RECIPIENT_EMAIL='XXXXXXXXXXX@XXXXX.com'

#temp_anomaly.py:
import json, time, conf, 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 Exception as 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

  sensor_temp = sensor_value*0.0977
  try:
    if sensor_value > bound[0]:
      print("The temperature has suddenly increased. Sending an sms.")
      response = sms.send_sms("temperature of the system has sudddenly increased")
      print("This is the response",response)

      print("Making request to Mailgun to send an email")
      response = mailer.send_email("Alert. The temperature has suddenly increased", "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']))

    elif sensor_value < bound[1]:
      print("The temperature has suddenly decreased. sending an sms.)
      response = sms.sms_send("temperature of the system has suddenly decreased")
      print("This is the response",response)

      print("Making request to Mailgun to send an email")
      response = mailer.send_email("Alert. The temperature has suddenly decreased", "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']))
    history_data.append(sensor_value)
  except Exception as e:
    print ("Error",e)
  try:
    if sensor_temp > 10
      print("The temperature value has crossed the limit. Sending an SMS")
      response = sms.send_sms("temperature of the system has crossed the limit")
      print("This is the response",response)

      print("Making request to Mailgun to send an email")
      response = mailer.send_email("Alert. The temperature has crossed the limit", "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",e)
  time.sleep(10)

Credits

siddhi s
1 project • 0 followers

Comments