Twitter icon
Facebook icon
LinkedIn icon
Google icon
Reddit icon
StumbleUpon icon
Del.icio.us icon

Temperature Monitoring System with Buzzer Alert for Anomaly

Added to IoTplaybook or last updated on: 11/11/2019
Temperature Monitoring System with Buzzer Alert for Anomaly

Story

Let us suppose you want to monitor any room on the basis of its temperature. If you want the temperature to regulate between some threshold values and also you want to detect if the flow of regulation breaks or we can say there is a sudden rise or fall in the temperature at any instant, this project is for you.

The main concept is sensing data using LM 35 temperature sensor, sending the data on cloud using Bolt wifi module, modulating the data and calculating Z-score threshold values, detecting any anomaly if the temperature crosses the threshold and sending an alert signal if the anomaly is detected.

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
 
× 1

Bolt

Temperature Sensor
Temperature Sensor
 
× 1

Newark

Buzzer
Buzzer
 
× 1

Newark

Adafruit

Breadboard (generic)
Breadboard (generic)
 
× 1

Newark

SparkFun

 
Male/Male Jumper Wires
 
× 5

Newark

Adafruit

USB A to Micro-B Cable
Digilent USB A to Micro-B Cable
 
× 1

Digilent

Software apps and online services

Snappy Ubuntu Core
Snappy Ubuntu Core
 
  Ubuntu

Let us discuss in more detail.

This is LM 35

fig:1

fig:1

It has 3 terminals: Vcc, output and ground

This is Bolt wifi module

fig:2

fig:2

This is the Buzzer used for alerting the authorities in case of anomaly detection.

fig:3

fig:3

The positive side of buzzer is connected to '0' GPIO pin of wifi-module. The negative side is connected to ground of the module. The Vcc side of LM-35 is connected to 5V pin, Ouput side to 'A0' pin and ground side to ground of the module.

The complete circuit can be viewed from the cover photo of this project. Also we need to power the wifi-module using any power source like laptop, adapter or power bank.

Working: The LM-35 would first sense the temperature of the room to be monitored. Then Z-score threshold values would be calculated. Z-score threshold depend on the past values of the temperature values. It basically changes based on the trend of the past values. It is used to detect any sudden anomaly in the trend of any variable changing with time. If there is large and sudden rise or fall in the temperature values, I would get an alert by the sound of the buzzer. For sudden rise the buzzer would sound after 2 beeps. For sudden fall the buzzer would sound after 1 beep. Also if the temperature crosses the given minimum or maximum value, the buzzer would sound normally.

Now comes the coding part. Code can be viewed from the figure below.

Explanation: First we are importing libraries like boltiot to take reading from the wifi-module, json to convert into json (Javascript Object Notation) format etc. After that, declaration of api_key and deice_id of the module is done. Frame size and multiplication factor are used to calculate Z-score threshold values which is calculated in the function compute_bounds which returns the upper and lower Z-score threshold values. Min_Threshold and Max_Threshold are the min and max values of the allowed temperature in the room. After that response is being read from wifi-module. It is converted into json format in data variable. Z-score threshold values are calculated and stored in a list variable bound. Now I performed the 4 condition. Also there is pause of 8 seconds between any consecutive reading of the sensor.

Code

buz.py - Python

 

from boltiot import Bolt
import json, time, math, statistics
api_key = "c3c7aecf-6aec-45e2-bfca-98158b0cb085"
device_id  = "BOLT3853118"
FRAME_SIZE = 4
MUL_FACTOR = 6
Min_Threshold = 280
Max_Threshold = 250

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(api_key, device_id)
history_data = []
while True:
	response = mybolt.analogRead('A0')
	data = json.loads(response)
	if data['success']!=1:
		print("There was an error")
		time.sleep(10)
		continue
	print("The value of temperature is ", data['value'])
	sensor_value = 0
	z = int(data['value'])
	try:
		sensor_value = z
	except e:
		print("There was an error", e)
	bound = compute_bounds(history_data,FRAME_SIZE,MUL_FACTOR)
	if not bound:
	    required_data_count=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("\nZ-score lower bound:", bound[0])
	print("Z-score upper bound", bound[1])
	try:
	    if sensor_value > bound[0]:
	        print("The light level increased suddenly. 2 beep buzzer.")
	        mybolt.digitalWrite('0','HIGH')
	        time.sleep(1)
	        mybolt.digitalWrite('0','LOW')
	        time.sleep(1)
	        mybolt.digitalWrite('0','HIGH')
	        time.sleep(1)
	        mybolt.digitalWrite('0','LOW')
	        time.sleep(1)
	        mybolt.digitalWrite('0','HIGH')
	        time.sleep(3)
	        mybolt.digitalWrite('0','LOW')
	    elif sensor_value < bound[1]:
	        print("The light level decreased suddenly. 1 beep buzzer.")
	        mybolt.digitalWrite('0','HIGH')
	        time.sleep(1)
	        mybolt.digitalWrite('0','LOW')
	        time.sleep(1)
	        mybolt.digitalWrite('0','HIGH')
	        time.sleep(3)
	        mybolt.digitalWrite('0','LOW')
	    elif sensor_value < Min_Threshold:
	    	print("The light level is less than Min_Threshold. Normal buzzer.")
	    	mybolt.digitalWrite('0','HIGH')
	    	time.sleep(4)
	    	mybolt.digitalWrite('0','LOW') 
	    elif sensor_value > Max_Threshold:
        	print("The light level is more than Max_Threshold. Normal buzzer.")
        	mybolt.digitalWrite('0','HIGH')
        	time.sleep(4)  
        	mybolt.digitalWrite('0','LOW')
	except e:
		print("There was an error", e)
	history_data.append(sensor_value)
	time.sleep(8)

Credits

Hardik Aggarwal

Hardik Aggarwal

 

Avnet

This content is provided by our content partner Avnet, a global technology solutions provider with end-to-end ecosystem capabilities. Visit them online for more great content like this.

This article was originally published at Avnet. It was added to IoTplaybook or last modified on 11/11/2019.
Verticals: