Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
gandla akhil
Published

Temperature Monitoring System (Smart Refrigerator)

Build this system to monitor the temperature and send out SMS alerts if a temperature goes beyond using Z-score anomaly.

BeginnerShowcase (no instructions)3 hours521
Temperature Monitoring System (Smart Refrigerator)

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
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Code

confy.py

Python
SSID = 'AC201XXXXXXXXXXXXXXXXXXXXXXXX1ac73ae' 
AUTH_TOKEN = '82757dXXXXXXXXXXXXXXXa6ca56' 
FROM_NUMBER = '+12XXXXXXXX42'
TO_NUMBER = '+91XXXXXXXX5'
API_KEY = '471a1bb2-2cXXXXXXXXXXXXXXXX5a2'
DEVICE_ID = 'BOLTXXXXXX20'
FRAME_SIZE = 10
MUL_FACTOR = 6

pewdiction

JavaScript
setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);
plotChart('time_stamp','temp'); 

anamoly.py

Python
The main code
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

gandla akhil

gandla akhil

1 project • 0 followers

Comments