Akshaya Viswanathan
Published

Temperature Data Logger and Predictor with Anomaly Detection

A device used to measure temperature over a period of time and predict the temperature after certain period of time.

BeginnerProtip4 hours356
Temperature Data Logger and Predictor with Anomaly Detection

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×3
Temperature Sensor
Temperature Sensor
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
Repl

Story

Read more

Code

Polynomial Regression

JavaScript
For predicting the temperature using graph
SetChartLibrary('google_chart');
SetChartTitle('Polynomial Regression');
SetChartType('predictionGraph');
SetAxisName('time_stamp','temp');
mul(0.097);
plotChart('time_stamp','temp');

Temperature Sensor Indication using Twilio

Python
Will indicate us with a message when temperature value crosses threshold value.
SID ="AC9ba8bXXXXXXX" 
AUTH_TOKEN ="29177eXXXXXXXXXXXXXc10ccf75c509"
FROM_NUMBER = "+12058968099"
TO_NUMBER = "+9173XXXXXXX80"
API_KEY = "95XXXXXXXXXX28f-XXXX-XX69-XXXX-189759XXXXXXX"
DEVICE_ID = "BOLTXXXXXXXXX"
from boltiot import Sms, Bolt
import json, time

minimum_limit = 300
maximum_limit = 600  


mybolt = Bolt( API_KEY,DEVICE_ID)
sms = Sms(SID, AUTH_TOKEN, TO_NUMBER, FROM_NUMBER)


while True: 
    print ("Reading sensor value")
    response = mybolt.analogRead('A0') 
    data = json.loads(response) 
    print("Sensor value is: " + str(data['value']))
    try: 
        sensor_value = int(data['value']) 
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            print("Making request to Twilio to send a SMS")
            response = sms.send_sms("The Current temperature sensor value is " +str(sensor_value))
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))
    except Exception as e: 
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)

Anomaly detection using Twilio

Python
indicates us when temperature increases or decreases suddenly.
SID ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
AUTH_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' 
FROM_NUMBER = '+12058968099'
TO_NUMBER = '+91XXXXXXXXXXXXX'
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXX'
DEVICE_ID = 'BOLTXXXXXXXXXXX'
FRAME_SIZE = 10
MUL_FACTOR = 6
import json, time, math, statistics
from boltiot import Sms, 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]

mybolt = Bolt(API_KEY,DEVICE_ID)
sms = Sms(SID,AUTH_TOKEN,TO_NUMBER,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 ("This is the value "+data['value'])
    sensor_value=0
    try:
        sensor_value = int(data['value'])
    except e:
        print("There was an error while parsing the response: ",e)
        continue

    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

    try:
        if sensor_value > bound[0] :
            print ("The Temperature level increased suddenly. Sending a SMS.")
            response = sms.send_sms("Temperture level increased")
            print("This is the response ",response)
        elif sensor_value < bound[1]:
            print ("The Temperature level is decreased suddenly. Sending a SMS .")
            response = sms.send_sms("Temperature level decreased")
            print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

Akshaya Viswanathan
1 project • 0 followers

Comments