Keya Shukla
Published © GPL3+

Temperature Monitoring and Alert System

This project is a know-all window to the world of IoT. Combining all major concepts of this vast field, this project sums IoT perfctly.

IntermediateFull instructions provided5 hours1,136
Temperature Monitoring and Alert System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Bolt IoT LM35 Temperaure Sensor
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
android usb cable
×1

Software apps and online services

Mailgun
DigitalOcean
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Code

email_conf.py

Python
The code given below is the email_conf.py file. This file stores all the credentials required throughout the code.
mailgun_api_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx-xxxxxxxx'
sandbox_url='sandboxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org'
sender_email='test@sandboxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org'
recipient_email='xxxxxxx@xxxxx.com'
api_key='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
device_id='BOLTxxxxxxx'
frame_size=10
mul_factor=6

CapstoneProject.py

Python
This file includes all the working code of the temperature sensor. It performs Z-score analysis and sends the email alert through Mailgun.
import json
import time
import email_conf
import math,statistics
from boltiot import Bolt,Email

mybolt=Bolt(email_conf.api_key,email_conf.device_id)

def set_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]
        Mean=statistics.mean(history_data)
        Variance=0
        for data in history_data:
                Variance+=math.pow((data-Mean),2)
                Zn=factor*math.sqrt(Variance/frame_size)
                HB=history_data[frame_size-1]+Zn
                LB=history_data[frame_size-1]-Zn
                return [HB,LB]

mailer=Email(email_conf.mailgun_api_key,email_conf.sandbox_url,email_conf.sender_email,email_conf.recipient_email)
history_data=[]

while(True):
        response=mybolt.analogRead('AO')
        data=json.loads(response)
        if data['success']!='1':
                print("There was an error while retrieving 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=set_bounds(history_data,email_conf.frame_size,email_conf.mul_factor)
        if not bound:
                required_data_count=email_conf.frame_size-len(history_data)
                print("Not enough data to comput Z-Score. Need ",required_data_count," more data points")
                history_data.append(int(data['value']))
                time.sleep(10)
                continue
        sensor_temp=sensor_value*0.097
        try:
                if(sensor_value>bound[0]):
                        print("The temperature has suddenly increased. Sending an email")
                        response=mailer.send_email("Alert. The temperaure 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 email")
                        response=mailer.send_email("Alert. The temperaure has suddenly decreased", "The current tempeorature 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 temperaure value has crossed the limit.Sending an email.")
                response=mailer.send_email("Alert. The temperature value 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

Keya Shukla

Keya Shukla

2 projects • 0 followers

Comments