Diana Buliga
Published

Smart Room

Smart Room for measurement of temperature and humidity.

BeginnerShowcase (no instructions)166
Smart Room

Things used in this project

Hardware components

5 mm LED: Red
5 mm LED: Red
×1
LED, Blue
LED, Blue
×1
5 mm LED: Green
5 mm LED: Green
×1
Breadboard (generic)
Breadboard (generic)
×1
Micro SD Card 16GB
×1
Through Hole Resistor, 5.1 kohm
Through Hole Resistor, 5.1 kohm
×1
Through Hole Resistor, 110 ohm
Through Hole Resistor, 110 ohm
×2
Resistor 330 ohm
Resistor 330 ohm
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×7
Male/Male Jumper Wires
×8

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Python 3
Flask

Story

Read more

Schematics

Scheme

Code

script.py

Python
import RPi.GPIO as GPIO
import Adafruit_DHT
import time
import threading
import smtplib
from email.mime.text import MIMEText
from flask import Flask, render_template

dht11 = 17
redLed = 3
greenLed = 5
blueLed = 7

refreshTimer = 30   # 3600 - for 1h

constTemperature = 28
constHumidity = 55

email = 'smartRoom1306B@gmail.com'
password = '13smartRoom06B'
receiver = 'butnarumariana26@gmail.com'

mutex = threading.Lock()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(redLed,GPIO.OUT)
GPIO.setup(greenLed,GPIO.OUT)
GPIO.setup(blueLed,GPIO.OUT)

app = Flask(__name__)

humidity = 0
temperature = 0

@app.route('/')
def index():
    return render_template('index.html', recHumidity = str(humidity), recTemperature = str(temperature))

@app.route('/', methods=['POST'])
def readDataFromSensor():
        global humidity
        global temperature
        humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, dht11)
            
        message = ''
        messageEmail = ''
        
        if humidity is not None and temperature is not None:
            message = 'Temperature = ' + str(temperature) + '*C  Humidity = ' + str(humidity) + '%\n'
            
            if humidity <= constHumidity and temperature <= constTemperature:
                messageEmail = message + 'Temperature and humidity are normal'
                print(messageEmail)
                
                GPIO.output(redLed, 1)
                GPIO.output(blueLed, 0)
                GPIO.output(greenLed, 0)
                
            elif humidity > constHumidity and temperature <= constTemperature:
                messageEmail = message + 'The humidity is too high, the dehumidifier starts!'
                print(messageEmail)
                
                GPIO.output(redLed, 0)
                GPIO.output(blueLed, 1)
                GPIO.output(greenLed, 0)
                
                sendEmail(messageEmail)
                
            elif humidity <= constHumidity and temperature > constTemperature:
                messageEmail = message + 'The temperature is too high, the AC starts!'
                print(messageEmail)
                
                GPIO.output(redLed, 0)
                GPIO.output(blueLed, 0)
                GPIO.output(greenLed, 1)
                
                sendEmail(messageEmail)
                
            elif humidity > constHumidity and temperature > constTemperature:
                messageEmail = message + 'The temperature and humidity are too high, the AC and the dehumidifier are turned on!'
                print(messageEmail)
                
                GPIO.output(redLed, 0)
                GPIO.output(blueLed, 1)
                GPIO.output(greenLed, 1)
                
                sendEmail(messageEmail)
        else:
            message = 'Failed to get reading. Try again!'
            print(message)
        return render_template('index.html', recHumidity = str(humidity), recTemperature = str(temperature))

def sendEmail(message):
    global mutex

    mutex.acquire()

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email, password)

    emailMessage = MIMEText(message, 'plain')
    emailMessage['Subject'] = 'Temperature and humidity ratio.'

    server.sendmail(email, receiver, emailMessage.as_string())
    server.quit()

    print('Email sent successfully!')

    mutex.release()
    
if __name__ == '__main__':
    GPIO.output(redLed, 1)
    app.run(host = '0.0.0.0')
    readDataFromSensor()
    GPIO.output(redLed, 0)
    GPIO.cleanup()

interfata.html

HTML
<html>
    <head>
        <title> Smart Room </title>

        <style>
            * {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            }

            h1{
				padding-top:30px;
                text-align: center;
                color: white;
				padding-bottom: 100px
            }

            p {
                font-size: 20px;
                text-align: center;
                color: white;
            }

            header, body {
                background-color: #282d32;
            }

            .footer-dark {
                padding:20px 0;
                color:#f0f9ff;
                background-color:#282d32;
            }

            .footer-dark .copyright {
                text-align:center;
                opacity:0.8;
                font-size:16px;
                margin-bottom:0;
	      padding-top:300px;
            }

            .button {
                background-color: #04AA6D;
                color: white;
                padding: 14px 20px;
                margin: 0 auto;
                display: block;
                border: none;
                cursor: pointer;
                width: 10%;
                font-size: 17px;
            }

            .center {
                display: block;
                margin-left: auto;
                margin-right: auto;
            }         
        </style>

        <meta name='viewport' content='width=device-width, initial-scale=1'>
        <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
    </head>

    <body>
        <h1>
            Smart Room Project
        </h1>

        <p id='Temperature'>
            <b>Temperatura:</b> {{recTemperature}} *C
        </p>
		<br>
        <p id='Humidity'>
            <b>Umiditate:</b> {{recHumidity}} %
        </p>
		<br><br>
		<form action="/" method="POST">
			<button type="submit" class="button">Refresh</button>
		</form>
	</body>

    <footer>
        <footer class="footer-dark">
			<p class="copyright">
                Made by: <br>
                <b>
                    Butnaru Silviu - 1306B<br>
                    Paraschiv Vlad - 1306B<br>
                    Buliga Diana - 1306B<br>
                    Iluca Alexandru - 1306B<br>
                </b>
            </p>
        </footer>
    </footer>
	
</html>

Credits

Diana Buliga
1 project • 0 followers

Comments