Prerit Wahi
Published

Food Industry Smart Temperature Monitoring System

Monitoring temperature. Using SMS, LED and buzzer alerts when temperature crosses threshold. Graphical prediction of temperature with time.

IntermediateFull instructions provided441
Food Industry Smart Temperature Monitoring System

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Temperature Sensor
Temperature Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×1
Buzzer
Buzzer
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Male/Male Jumper Wires
×6

Software apps and online services

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

Story

Read more

Schematics

Circuit Diagram

Code

Python Code

Python
from boltiot import Sms, Bolt  # Import Sms and Bolt class from boltiot library
import json, time, math, statistics  # Import json library to handle JSON objects and import time library.
# Import math and statistics library for calculating the Z-score and the threshold boundaries

def compute(history_data, frame_size, factor):  # Define a function, which takes 3 input variables: hisotry_data, frame_size and factor
    if len(history_data)<(frame_size):
        return None

    if len(history_data)>(frame_size):
        del history_data[0:len(history_data)-(frame_size)]
    # Above code checks whether enough data has been accumulated to calculate the Z-score.Iif there is too much data,
    # then the code deletes the older data
    Mn = statistics.mean(history_data)  # Calculates the mean (Mn) value of the collected data points
    Variance = 0
    for data in history_data:
        Variance += math.pow((data - Mn), 2)
    # Above code helps to calculate the Variance of the data points
    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]
    # Above code calculates Z score (Zn) for the data and use it to calculate the upper and lower threshold bounds
    # It is required to checks if a new data point is normal or anomalous

max_limit = 204.8  # Maximum threshold value of temperature (20 degree C)
min_limit = 102.4  # Minimum threshold value of temperature (10 degree C)

# Bolt credentials
API_KEY = 'This is your Bolt Cloud API key which you will find on your Bolt Dashboard'
DEVICE_ID = 'This is your Bolt Device ID which you will find on your Bolt Dashboard'

# Credentials required to send SMS
SID = 'This is your Twilio Account SID which you will find on your Twilio Dashboard'
AUTH_TOKEN = 'This is your Twilio Auth Token which you will find on your Twilio Dashboard'
FROM_NUMBER = 'This is the number generated by Twilio which you find on your Twilio Dashboard'
TO_NUMBER = 'This is your number. Remember to add your country code at the beginning'

FRAME_SIZE = 5
MUL_FACTOR = 3
history_data=[]

mybolt = Bolt(API_KEY, DEVICE_ID)  # Create object so that Bolt Cloud can identify your device
sms = Sms(SID, AUTH_TOKEN, TO_NUMBER, FROM_NUMBER)  # Create object to send an Sms

while True:  # Will continuously monitor the temperature
    print("Reading sensor value")
    response = mybolt.analogRead('A0')  # Continuously fetches the temperature value from LM35 at the A0 pin
    data = json.loads(response)  # Load the JSON data sent by the cloud using Python's json library
    sensor_value = int(data['value'])  # Access the JSON values and also converts the sensor reading to integer data type for comparing the temperature range
    temp = 100 * sensor_value / 1024  # Covert raw temperature reading to degree celcius
    print (temp)
    print("Sensor value is: {0} degree Celcius".format(str(temp)))  # print current temperature value
    mybolt.digitalWrite(1, 'LOW')  # Turn OFF LED connected at pin 1
    mybolt.digitalWrite(4, 'LOW')  # Turn OFF buzzer at pin 4
    try:
        sensor_value = int(data['value'])
        if sensor_value > max_limit or sensor_value < min_limit:  # checks if threshold values have been crossed or not for temperature readings
            mybolt.digitalWrite(1, 'HIGH')  # Turn ON LED at pin 1
            mybolt.digitalWrite(4, 'HIGH')  # Turn ON buzzer at pin 4
            print('Making request to Twilio to send an SMS')
            response = sms.send_sms("ALERT! The temperature threshold has been exceeded! The current temperature is " + str(temp))  # Send SMS alert to the operator
            print("Response received from Twilio is: " + str(response))  # Response from Twilio will be stored inside the `response` variable
            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)  # Wait for 10 seconds and then repeat the loop

    if data['success'] != 1:  #Checks if valid data is generated or not
        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'])

    bound = compute(history_data, FRAME_SIZE, MUL_FACTOR)
    if not bound:
        required_data_count = int(FRAME_SIZE) - len(history_data)  # Checks if there is required data or not
        print("Not enough data to compute Z-score. Need ", required_data_count, " more data points")
        history_data.append(int(data['value']))  # Add interger data values to the history_data
        time.sleep(10)  # Wait for 10 sec
        continue
    try:
        if sensor_value > bound[0]:  #Sudden increase in temperature
            mybolt.digitalWrite(1, 'HIGH')  # Turn ON LED at pin 1
            mybolt.digitalWrite(4, 'HIGH')  # Turn ON buzzer at pin 4
            print("Sending an SMS.")
            response = sms.send_sms("Alert! Check temperature control ASAP. Temperature increased suddenly")
            print("This is the response ", response)
            mybolt.digitalWrite(1, 'LOW')  # Turn OFF LED at pin 1
            mybolt.digitalWrite(4, 'LOW')  # Turn OFF buzzer at pin 4
        elif sensor_value < bound[1]: #Sudden decrease in temperature
            print("Sending an SMS")
            response = sms.send_sms("Alert! Check temperature control ASAP. Temperature decreased suddenly.")
            print("This is the response ", response)
        history_data.append(sensor_value);  # Add sensor_value to history_data
    except Exception as e:
        print("Error occured", e)
    time.sleep(10)

Code for Temperature Prediction

JavaScript
setChartLibrary('google-chart'); /* Set the chart library to be used to visualize data. Here google-chart library is being used */
setChartTitle('Temperature Monitoring'); // Set the title of the graph
setChartType('predictionGraph'); /* Set the type of chart to be plotted. Here line-graph is used */
setAxisName('time','temp'); // Set the display name for each of the graph axis
mul(0.0977); //gives temprature reading in degree celcius
plotChart('time_stamp','temp'); /* Plots points in form of table. Display the graph on the screen for the given variables. time_stamp is the time of data collection and 'temp' is variable for A0 pin set in hardware configuration */

Credits

Prerit Wahi
1 project • 0 followers
I am a GET in Collins Aerospace.

Comments