Aditya Jaiswal
Published © GPL3+

Bolt Temperature Anomaly Predictor and Alerting System

Getting SMS and email alert if the temperature changes anomalously or suddenly by means of any mishappening.

IntermediateFull instructions provided4 hours936
Bolt Temperature Anomaly Predictor and Alerting System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Temperature Sensor
Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
SMS Messaging API
Twilio SMS Messaging API
MailGun
Bolt IoT Android App
Bolt IoT Android App

Story

Read more

Schematics

LM35 Internal Circuit Diagram

This Circuit diagram shows how an LM35 convert change in temperature to change in Voltage. Temperature is directly proportional to Voltage.

Code

The Main Code

Python
This is the main working code. Inside the code, there is an import of conf.py which is the file that contains all the confidential credentials and is not to be shared. Kindly make that file on your own.
from boltiot import Bolt, Sms, Email
import json
import conf
import statistics
import math
import time


def get_bound(history_dat, frame_size, mul_factor):
    if len(history_dat) < frame_size:
        return None
    if len(history_dat) > frame_size:
        del history_data[0:len(history_data)-frame_size]

    mean__ = statistics.mean(history_dat)

    variance_ = 0
    for data1 in history_data:
        variance_ += math.pow((data1-mean__), 2)

    z_score = mul_factor*(math.sqrt(variance_/frame_size))

    up_lim = history_dat[frame_size-1] + z_score
    low_lim = history_dat[frame_size-1] - z_score

    return [up_lim, low_lim]


my_bolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL,
               conf.RECIPIENT_EMAIL)
history_data = []
while True:
    response = my_bolt.analogRead("A0")
    data = json.loads(response)
    sensor_value = 0
    try:
        sensor_reading = float(data["value"])
        if data["success"] != 1:
            print("Request not successful")
            print("This is the response:   ", data)
        sensor_value = sensor_reading/10.24
        print("The Current Temperature is {}".format(sensor_value))

    except Exception as err:
        print("Something went wrong while returning the sensor value")
        print(err)

    bounds = get_bound(history_data, conf.FRAME_SIZE, conf.MUL_FACTOR)

    try:
        if not bounds:
            req_data_count = conf.FRAME_SIZE - len(history_data)
            print("Not enough data to compute Z-score Analysis. Need {} more data points".format(req_data_count))
            history_data.append(sensor_value)
            time.sleep(10)
            continue

        if sensor_value > bounds[0]:
            print("The temperature has increased suddenly. Requesting Twillio to Send an SMS")
            response1 = sms.send_sms("Temperature has suddenly increased to {}.\n Someone has opened the fridge door".
                                     format(sensor_value))
            response_text1 = json.loads(response1.text)
            print("Response Received from Twillio: ", str(response_text1["message"]))
            print("Making request to Mailgun to send an email")
            response2 = mailer.send_email("Alert", "The Current temperature sensor value is {}. Someone has opened the "
                                                   "fridge door".format(sensor_value))
            response_text2 = json.loads(response2.text)
            print("Response received from Mailgun is: " + str(response_text2['message']))
        elif sensor_value < bounds[1]:
            print("The temperature has decreased suddenly. Requesting Twillio to Send an SMS")
            response1 = sms.send_sms("Temperature has suddenly dropped to {}. \n Someone has changed the configuration"
                                     .format(sensor_value))
            response_text1 = json.loads(response1.text)
            print("Response Received from Twillio: ", str(response_text1["message"]))
            print("Making request to Mailgun to send an email")
            response2 = mailer.send_email("Alert", "The Current temperature sensor value is {}. Someone has changed the"
                                                   "configuration".format(sensor_value))
            response_text = json.loads(response2.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
        history_data.append(sensor_value)
    except Exception as error_msg:
        print("Error: ", error_msg)

    time.sleep(10)

Credits

Aditya Jaiswal

Aditya Jaiswal

1 project • 1 follower

Comments