Tarun Singh Negi
Published

Monitoring of Temperature and Its Alert

The device which notifies via email or SMS for any sudden change in temperature or if it goes out of the defined threshold.

IntermediateFull instructions provided2 hours2,728
Monitoring of Temperature and Its Alert

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
Awesome Wifi Module by Bolt IoT
×1
Temperature Sensor
Temperature Sensor
LM35 series are precision integrated-circuit temperature sensors.
×1
Jumper wires (generic)
Jumper wires (generic)
Jumper wires are simply wires that have connector pins at each end, allowing them to be used to connect two points to each other without soldering.
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt Cloud platform for connecting and managing Bolt IoT devices
Bolt IoT Android App
Bolt IoT Android App
Link your Bolt devices to your Bolt Cloud account.
SMS Messaging API
Twilio SMS Messaging API
Cloud communications platform for building SMS, Voice & Messaging applications
Mailgun
Cloud communications platform for sending the Emails
Oracle VM Virtual Box Or Digital Occean

Story

Read more

Schematics

LM35 connection to Bolt

How to connect LM35 to Bolt Wifi Module

Code

anomaly_detection.py

Python
For anomaly detection using Z score analysis
import conf, email_conf, json, time, math, statistics
from boltiot import Sms, 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]


minimum_limit = 7 #can be change
maximum_limit = 40 #can be change

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)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)

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("The sensor value is"+(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, 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 compute Z-score. Need", required_data_count, "more data points")
        history_data.append(int(data['value']))
        time.sleep(10)
        continue

    try:
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            response = mailer.send_email("Alert", "The Current temperature is beyond the threshold ")
        if sensor_value > bound[0]:
            print("The temperature increased suddenly.Sending Sms.")
            response = sms.send_sms("Someone opened the chamber")
            print("This is the response", response)
        elif sensor_value < bound[1]:
            print("The temperature decreased suddenly. Sending an email.")
            response = mailer.send_email("Someone opened the chamber")
            print("This is the response", response)
        history_data.append(sensor_value)
    except Exception as e:
        print("Error", e)
    time.sleep(10)

conf.py

Python
For information of Twilio and Bolt cloud
SSID = 'You can find SSID in your Twilio Dashboard' 
AUTH_TOKEN = 'You can find  on your Twilio Dashboard' 
FROM_NUMBER = 'This is the no. generated by Twilio. You can find this on your Twilio Dashboard'
TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device'
FRAME_SIZE = 10
MUL_FACTOR = 6

email_conf.py

Python
For Mailgun
MAILGUN_API_KEY = 'your Mailgun private API'
SANDBOX_URL = 'your sandbox url'
SENDER_EMAIL = 'test@your sandbox url'
RECIPIENT_EMAIL = 'your mail id'
API_KEY = 'Bolt API key'
DEVICE_ID = 'Your Device ID'
FRAME_SIZE = 10
MUL_FACTOR = 6

Credits

Tarun Singh Negi

Tarun Singh Negi

2 projects • 5 followers
a student and want to learn a lot of things

Comments