vivek kunachi
Published

Smart Temperature Monitoring System with Fan Controlling

We can control the Fan, either the program will do it for you. Based on the room temperature. And send an SMS when Temperature is too High.

IntermediateProtip4 hours433
Smart Temperature Monitoring System with Fan Controlling

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Temperature Sensor
Temperature Sensor
×1
Power MOSFET N-Channel
Power MOSFET N-Channel
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Mini Fan
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
ubuntu OS
Flask
Bootstrap
Chrome
SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Schematics

Full Circuit Diagram

Schematic Diagram

Code

Configuration File

Python
This file contains the sensitive data for the main program. And should be saved in the "MyProject" Folder
SSID = 'You can find SID in your Twilio Dashboard' 
AUTH_TOKEN = 'You can find  on your Twilio Dashboard' 
FROM_NUMBER = 'This is the no. generated by Twilio. You can find this on your Twilio Dashboard'
TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning'
API_KEY = 'This is your Bolt Cloud accout API key'
DEVICE_ID = 'This is the ID of your Bolt device'
FRAME_SIZE = 10 
MUL_FACTOR = 6

Interface Program

HTML
Save it as "index.html" in the Folder MyProject/Templates
Also used Bootstrap code in this program
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Bolt iot</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>

  </head>
  <body>
    <div class="" align="center">
    <form class="" action="/ON" method="post">
      <p><input type="submit" name="" value="ON" class="btn btn-success btn-lg" ></p>
    </form>

    <form class="" action="/OFF" method="post">
      <p><input type="submit" name="" value="OFF" class="btn btn-danger btn-lg"></p>
    </form>

    <form class="" action="/AUTO" method="post">
      <p><input type="submit" name="" value="AUTO" class="btn btn-dark btn-lg"></p>
    </form>
    </div>
  </body>
</html>

Backend Program for the Web Page

Python
Save this as "app.py" in the MyProject Folder
import conf, json, time, math, statistics
from boltiot import Sms, Bolt
from flask import Flask, request, render_template, url_for, redirect

app = Flask(__name__)

def compute_bounds(history_data,frame_size,factor):
    if len(history_data)<frame_size :
        return None

    else:
        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]

#creating an objects
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]

#The Threshold
min_limit = 25
max_limit = 35

#the below loop will collect the necessary data points for the Z-score Analysis
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
    temp1 = int(data['value'])
    temp1 = (100*temp1)/1024
    print ("This is the value "+str(temp1))
    sensor_value=0
    try:
        sensor_value = temp1
    except e:
        print("There was an error while parsing the response: ",e)
        continue

    bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_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(temp1)
        time.sleep(5)
        continue
    if bound:
        break

#The backend Prograam for the buttons
@app.route('/', methods = ['GET','POST'])
def index():
    return render_template('index.html')

#Turns ON the fan
@app.route('/ON', methods = ['GET','POST'])
def ON():
    response1 = mybolt.digitalWrite('1', 'HIGH')
    print(response1)
    return render_template('index.html')

#Turns OFF the fan
@app.route('/OFF', methods = ['GET','POST'])
def OFF():
    response1 = mybolt.digitalWrite('1', 'LOW')
    print(response1)
    return render_template('index.html')

#The below code will automatically turns on or off the fan
@app.route('/AUTO', methods = ['GET','POST'])
def AUTO():

    #this loop will continuesly checks for values per 5 minutes and takes the decision whether to turn on the fan or not.
    while True:
        #reading the value of the temperature sensor.
        try:
            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
            temp1 = int(data['value'])
            #converting the value
            temp1 = (100*temp1)/1024
            print ("This is the value "+str(temp1))
            sensor_value=0
            try:
                sensor_value = temp1
            except e:
                print("There was an error while parsing the response: ",e)
                continue

            #the below if statement will send a msg to the phone if the temperature increases suddenly (in the Chances of fire)
            if sensor_value > bound[0] :
                print ("The temperature level increased suddenly!! \n Sending an SMS.")
                response = sms.send_sms("Alert!!! \n Chances of fire in the room... \n temperature detected " + str(sensor_value))
                print("Response received from Twilio is: " + str(response))
                print("Status of SMS at Twilio is :" + str(response.status))
                history_data.append(sensor_value);

            #the below code will turns on the fan
            if sensor_value > max_limit:
                print ("This is the value "+str(sensor_value))
                print("Turning on the fan \n or \n Already turned on")
                response1 = mybolt.digitalWrite('1', 'HIGH')
                print(response1)

            #this is the normal mode it will turn off the fan if its on
            elif sensor_value < max_limit and sensor_value > min_limit:
                print("temperature is normal")
                print ("This is the value "+str(sensor_value))
                print("Turning off the fan \n or \n Already turned off")
                response1 = mybolt.digitalWrite('1', 'LOW')
                print(response1)

            #this will execute when the temperature goes down.. Just a remainder to stay warm... :)
            else:
                print("It's too cold here!!! Get some warm Cloths... \n The Current temperature sensor value is " +str(sensor_value))
                response1 = mybolt.digitalWrite('1', 'LOW')
                print(response1)
        except Exception as e:
            print ("Error",e)
        time.sleep(300)
        return render_template('index.html')




if __name__ == '__main__':
    app.run()

Credits

vivek kunachi
1 project • 0 followers

Comments