Maria Noaptes
Published © GPL3+

Environment monitoring system

This project is meant to be a prototype for a system which monitors the temperature and the humidity in the house.

BeginnerShowcase (no instructions)5 hours363
Environment monitoring system

Things used in this project

Hardware components

Raspberry Pi 3 Model A+
Raspberry Pi 3 Model A+
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Breadboard (generic)
Breadboard (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×6
Male/Male Jumper Wires
×3
Through Hole Resistor, 5.1 kohm
Through Hole Resistor, 5.1 kohm
×1
Through Hole Resistor, 150 ohm
Through Hole Resistor, 150 ohm
×2
LED (generic)
LED (generic)
×2

Story

Read more

Schematics

Connections

Code

Client side

HTML
The client interface that provides the exact measures of these environment conditions is available in the local network by accessing the https://localhost:5000/ link in a browser(if you are in the Raspberry Pi OS; if not, replace localhost with the IP assigned to your Raspberry Pi in the local network).
<html>
    <head>
        <script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>
        <title>Climate</title>
        <!-- css code for styling the table and background -->
        <style>
            body {
                background-color: #92f9d1;
                }
            table {
                padding: 20;
                align: center;
                horizontal-align: middle;
                vertical-align: middle;
                border: 4px solid black;
                }
            table.center {
                font-size:40px;
                margin-left:auto;
                margin-right:auto;
                }
        </style>
    </head>
    <body>
	<!-- table for displaying the results provided by the sensor --> 
        <br /><br /><br />
        <table class='center'>
          <tr>
            <td> <div>Temperature: </div> </td>
            <td> <div id='Temperature'></div></td>
          </tr>
          <tr> <td> <div>Humidity: </div> </td>
            <td> <div id='Humidity'></div></td>
          </tr>
        </table>
	<!-- explanatory paragraphs that provide information about the behavior of the leds -->
        <br /><br /><br />
        <p> If temperature is bigger than 25ºC the red led will light up. </p>
        <p> If humidity is bigger than 60% the blue led will light up. </p>

    </body>
<script>
   //jquery call when page is completely loaded
   $(document).ready(function(){
     //setting a call for the refresh function at every 1000 ms
     setInterval(refreshFunction,1000);
   });
   function refreshFunction(){
       //call of the /refresh resource of the server, result returned in the e variable as json
       $.getJSON('/refresh', function(e){
       //setting the content of temperature and humidity in the table by providing their id in the jquery call
       $('#Temperature').html(e.temperature+"ºC");
       $('#Humidity').html(e.humidity+"%");
     });
   }
</script>
</html>

Server side code

Python
# import library bottle (used for API implementation)
from bottle import route, run, template, request

# import libraries for pin values and sensor handling
import RPi.GPIO as GPIO
import Adafruit_DHT

# set mode of numbering for the pins of the development board as BCM (GPIO numbering)
GPIO.setmode(GPIO.BCM)

#name the type of sensor used
type = Adafruit_DHT.DHT11

#declare the pin used by the sensor in GPIO form
dht11 = 25
hum = 20
temp = 21

#set the sensor as INPUT
GPIO.setup(dht11, GPIO.IN)

#set temperature and humidity pins as OUTPUTS
GPIO.setup(hum, GPIO.OUT)
GPIO.setup(temp, GPIO.OUT)

#define the route for the main page
@route('/')
def index():
#at the '/' route we will return the index.html template that is in the views folder
return template('index.html')
#define two variables that will store the last reading
buffer1 = 0
buffer2 = 0
@route('/refresh')
#define a function to send data to client
def refresh():
global buffer1
global buffer2
#make the reading
humidity, temperature = Adafruit_DHT.read(type, dht11)

GPIO.output(temp, GPIO.HIGH)
GPIO.output(temp, GPIO.HIGH)
# check if temperature reached the 25 degrees threshold
# if so, light up the red led
if(buffer1>25):
GPIO.output(temp, GPIO.HIGH)
else:
GPIO.output(temp, GPIO.LOW)
# check if temperature reached the 60% threshold
# if so, light up the blue led
if(buffer2>60):
GPIO.output(hum, GPIO.HIGH)
else:
GPIO.output(hum, GPIO.LOW)

#we will send the new values if they are not null else the previous
#readings will be send
if humidity is not None and temperature is not None:
buffer1 = temperature
buffer2 = humidity

#return the values to the client
return {'temperature':temperature, 'humidity':humidity}
else:
#return the values to the client
return {'temperature':buffer1, 'humidity':buffer2}
#we will run the app on port 5000
run(host = '0.0.0.0', port = '5000')
#clean the used ports
GPIO.cleanup()
# import library bottle (used for API implementation)
from bottle import route, run, template, request

# import libraries for pin values and sensor handling
import RPi.GPIO as GPIO
import Adafruit_DHT

# set mode of numbering for the pins of the development board as BCM (GPIO numbering)
GPIO.setmode(GPIO.BCM)

#name the type of sensor used
type = Adafruit_DHT.DHT11

#declare the pin used by the sensor in GPIO form
dht11 = 25
hum = 20
temp = 21

#set the sensor as INPUT
GPIO.setup(dht11, GPIO.IN)

#set temperature and humidity pins as OUTPUTS
GPIO.setup(hum, GPIO.OUT)
GPIO.setup(temp, GPIO.OUT)

#define the route for the main page
@route('/')
def index():
    #at the '/' route we will return the index.html template that is in the views folder
    return template('index.html')
#define two variables that will store the last reading
buffer1 = 0
buffer2 = 0
@route('/refresh')
#define a function to send data to client
def refresh():
    global buffer1
    global buffer2
    #make the reading
    humidity, temperature = Adafruit_DHT.read(type, dht11)

    GPIO.output(temp, GPIO.HIGH)
    GPIO.output(temp, GPIO.HIGH)
    # check if temperature reached the 25 degrees threshold
    # if so, light up the red led
    if(buffer1>25):
        GPIO.output(temp, GPIO.HIGH)
    else:
        GPIO.output(temp, GPIO.LOW)
    # check if temperature reached the 60% threshold
    # if so, light up the blue led
    if(buffer2>60):
        GPIO.output(hum, GPIO.HIGH)
    else:
        GPIO.output(hum, GPIO.LOW)

    #we will send the new values if they are not null else the previous
    #readings will be send
    if humidity is not None and temperature is not None:
        buffer1 = temperature
        buffer2 = humidity

        #return the values to the client
        return {'temperature':temperature, 'humidity':humidity}
    else:
        #return the values to the client
        return {'temperature':buffer1, 'humidity':buffer2}
#we will run the app on port 5000
run(host = '0.0.0.0', port = '5000')
#clean the used ports
GPIO.cleanup()

Credits

Maria Noaptes
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.