RITIK MANISH MEHTA
Published

Temperature maintainer check for dispensary room

To keep a check that the temperature doesn't rise over 20°C else the medicines may spoil.

IntermediateFull instructions provided2 hours25
Temperature maintainer check for dispensary room

Things used in this project

Story

Read more

Custom parts and enclosures

Door opened deliberately to test the working of the project

When the door is opened the room temperature will rise leading to temperature increase alert and message from Twilio. This makes sure that the system is tested and proves its originality as providing a validation

Alert message for dispensary kept open

The main objective of Twilio is to make a system that can alert the staff incharge on time so that the risk is controlled

Schematics

Circuit connection using LM35 to Bolt Wifi modue

Jumper wires and breadboard are used to connect the LM35 with the bolt Wifi module. The connections are pretty much simple. The left pin ( supply ) of the LM35 must be connected to 5V pin of the Wifi module. Also The middle pin (output) must be connected to A0 and the ground pin of the sensor must be connected to ground of bolt module.

Code

Medicines are safe or not

Python
This code is used for a variety of Iot projects and I do acknowledge to have used it with minor changes.
code explanation:
Here's a line-by-line explanation :
import conf, json, time, math, statistics
This line imports the necessary modules:
conf: A configuration module likely containing API keys and other configuration data.
json: A module to work with JSON data.
time: A module to work with time-related functions.
math: A module providing access to mathematical functions.
statistics: A module to perform statistical operations.

from boltiot import Sms, Bolt
This imports the Sms and Bolt classes from the boltiot library, which are used to interact with the Bolt IoT device and send SMS messages.


def compute_bounds(history_data, frame_size, factor):
This line defines a function named compute_bounds which takes three parameters:
history_data: A list of past data points.
frame_size: The number of data points to consider for calculating bounds.
factor: A multiplier for the Z-score calculation.

if len(history_data) < frame_size:
return None
If the length of history_data is less than frame_size, the function returns None because there isn't enough data to compute the bounds.

if len(history_data) > frame_size:
del history_data[0:len(history_data)-frame_size]
If history_data has more elements than frame_size, this line removes the oldest data points to maintain the size of history_data equal to frame_size.


Mn = statistics.mean(history_data)
Calculates the mean of history_data and stores it in the variable Mn.

Variance = 0
for data in history_data:
Variance += math.pow((data - Mn), 2)
Initializes Variance to 0 and then calculates the variance of history_data by summing the squared differences between each data point and the mean.


Zn = factor * math.sqrt(Variance / frame_size)
Calculates the Z-score (Zn) using the factor and the standard deviation of history_data.


High_bound = history_data[frame_size - 1] + Zn
Low_bound = history_data[frame_size - 1] - Zn
Calculates the high and low bounds by adding and subtracting the Z-score from the latest data point in history_data.


return [High_bound, Low_bound]
Returns the high and low bounds as a list.

mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
Creates an instance of the Bolt class with the API key and device ID from the conf module.


sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
Creates an instance of the Sms class with the SID, authentication token, and phone numbers from the conf module.


history_data = []
Initializes history_data as an empty list to store past data points.

frame = 5
factor = 2
Sets the frame size to 5 and the factor to 2 for the Z-score calculation.

while True:
Starts an infinite loop to continuously read data and perform calculations.
response = mybolt.analogRead('A0')
data = json.loads(response)
Reads the analog value from pin A0 on the Bolt device and parses the JSON 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
Checks if the data retrieval was successful. If not, it prints an error message, waits for 10 seconds, and continues to the next iteration of the loop.


sensor_value = 0
try:
sensor_value = int(data['value']) / 10.24
except Exception as e:
print("There was an error while parsing the response: ", e)
continue
Tries to convert the sensor value from the response to an integer and scales it down by dividing by 10.24. If an error occurs, it prints the error and continues to the next iteration.


print("This is the current temperature ", sensor_value)
Prints the current temperature value.

bound = compute_bounds(history_data, frame, factor)
if not bound:
required_data_count = frame - len(history_data)
print("Not enough data to compute Z-score. Need ", required_data_count, " more data points")
history_data.append(sensor_value)
time.sleep(10)
continue
Calls the compute_bounds function to get the high and low bounds. If the bounds are None (not enough data), it calculates how many more data points are needed, prints a message, adds the current sensor_value to history_data, waits for 10 seconds, and continues to the next iteration.


try:
if sensor_value > bound[0]:
print("The temperature has increased suddenly. Sending an SMS.")
response = sms.send_sms("Someone has opened the fridge door")
print("This is the response ", response)
history_data.append(sensor_value)
except Exception as e:
print("Error", e)
If the current sensor_value exceeds the high bound, it prints a message, sends an SMS, and prints the response. It then adds the current sensor_value to history_data. If an error occurs, it prints the error message.


time.sleep(10)
Waits for 10 seconds before the next iteration of the loop.
import conf, 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(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]

frame=5
factor=2

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

    sensor_value=0
    try:
        sensor_value = int(data['value'])/10.24
    except e:
        print("There was an error while parsing the response: ",e)
        continue

    print ("This is the current temperature ", sensor_value)

    bound = compute_bounds(history_data,frame,factor)
    if not bound:
        required_data_count=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'])/10.24)
        time.sleep(10)
        continue

    try:
        if sensor_value > bound[0] :
            print ("The temperature has increased suddenly. Sending an SMS.")
            response = sms.send_sms("Someone has opened the fridge door")
            print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

RITIK MANISH MEHTA
1 project • 0 followers
Thanks to Adhyoksh Jyoti.

Comments