Navneet
Published © GPL3+

Remote Patient and Air Quality Monitoring System

This project aims at measuring parameters of a patient such as his or her heart rate, temperature, humidity, and the air quality using IoT.

IntermediateFull instructions providedOver 1 day3,387
Remote Patient and Air Quality Monitoring System

Things used in this project

Story

Read more

Schematics

SAMPLE SCREENSHOTS

SAMPLE SCREENSHOTS

SAMPLE SCREENSHOTS

SAMPLE SCREENSHOTS

Code

Main program

Python
Launches flask webserver
import RPi.GPIO as GPIO
MQpin=4
GPIO.setmode(GPIO.BCM)
GPIO.setup(MQpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
import Adafruit_DHT
from flask import Flask, render_template, request, send_file, make_response
gpio=17
sensor=Adafruit_DHT.DHT11
app = Flask(__name__)


import sqlite3



# Retrieve data from database

def getData():

        conn=sqlite3.connect('DHT.db')
        curs=conn.cursor()
        for row in curs.execute("SELECT * FROM DHT_DATA2 ORDER BY timestamp DESC LIMIT 1"):

                time = str(row[0])

                temp = row[1]

                hum = row[2]

                pulse = row[3]
        conn.close()

        return time, temp, hum, pulse

def getHistData (numSamples):
        conn=sqlite3.connect('DHT.db')
        conn=sqlite3.connect('DHT.db')
        curs=conn.cursor()
        curs.execute("SELECT * FROM DHT_data2 ORDER BY timestamp DESC LIMIT "+str(numSamples))
        data = curs.fetchall()
        dates = []
        temps = []
        hums = []
        pulse = []
        for row in reversed(data):
                dates.append(row[0])
                temps.append(row[1])
                hums.append(row[2])
                pulse.append(row[3])
        return dates, temps, hums, pulse
def maxRowsTable():
        conn=sqlite3.connect('DHT.db')
        curs=conn.cursor()
        for row in curs.execute("select COUNT(temp) from  DHT_data2"):
                maxNumberRows=row[0]
        return maxNumberRows
# define and initialize global variables
global numSamples
numSamples = maxRowsTable()
if (numSamples > 101):
    numSamples = 100
# main route
@app.route("/")
def index():
        if(GPIO.input(MQpin)==1):
                warning="PPM detected, air contaminated"
        if(GPIO.input(MQpin)==0):
                warning="Air is clean, safe for breathing"
        chum,ctemp=Adafruit_DHT.read_retry(sensor,gpio)
        time, temp, hum ,pulse = getData()
        templateData = {
        'time'  : time,
        'temp'  : temp,
        'hum'   : hum,
        'pulse'   :pulse,
        'ctemp'   :ctemp,
        'chum'    :chum,
        'warning' :warning,
        'numSamples'   :numSamples
        }
        return render_template('index.html', **templateData)
@app.route('/', methods=['POST'])
def my_form_post():
    global numSamples
    numSamples = int (request.form['numSamples'])
    numMaxSamples = maxRowsTable()
    if (numSamples > numMaxSamples):
        numSamples = (numMaxSamples-1)
    time, temp, hum, pulse = getData()
    templateData = {
                'time'  : time,
                'temp'  : temp,
                'hum'   : hum,
                'pulse' : pulse,
                'numSamples'    : numSamples
        }
    return render_template('index.html', **templateData)
@app.route('/plot/temp')
def plot_temp():
        times, temps, hums, pulses = getHistData(numSamples)
        ys = temps
        fig = Figure()
        axis = fig.add_subplot(1, 1, 1)
        axis.set_title("Temperature [°C]")
        axis.set_xlabel("Samples")
        axis.grid(True)
        xs = range(numSamples)
        axis.plot(xs, ys)
        canvas = FigureCanvas(fig)
        output = io.BytesIO()
        canvas.print_png(output)
        response = make_response(output.getvalue())
        response.mimetype = 'image/png'
        return response
@app.route('/plot/hum')
def plot_hum():
        times, temps, hums, pulses = getHistData(numSamples)
        ys = temps
        fig = Figure()
        axis = fig.add_subplot(1, 1, 1)
        axis.set_title("Temperature [°C]")
        axis.set_xlabel("Samples")
        axis.grid(True)
        xs = range(numSamples)
        axis.plot(xs, ys)
        canvas = FigureCanvas(fig)
        output = io.BytesIO()
        canvas.print_png(output)
        response = make_response(output.getvalue())
        response.mimetype = 'image/png'
        return response
@app.route('/plot/hum')
def plot_hum():
        times, temps, hums, pulses = getHistData(numSamples)
        ys = hums
        fig = Figure()
        axis = fig.add_subplot(1, 1, 1)
        axis.set_title("Humidity [%]")
        axis.set_xlabel("Samples")
        axis.grid(True)
        xs = range(numSamples)
        axis.plot(xs, ys)
        canvas = FigureCanvas(fig)
        output = io.BytesIO()
        canvas.print_png(output)
        response = make_response(output.getvalue())
        response.mimetype = 'image/png'
        return response
@app.route('/plot/pulse')
def plot_pulse():
        times, temps, hums, pulses = getHistData(numSamples)
        ys = pulses
        fig = Figure()
        axis = fig.add_subplot(1, 1, 1)
        axis.set_title("Heart pulse [°C]")
        axis.set_xlabel("Samples")
        axis.grid(True)
        xs = range(numSamples)
        axis.plot(xs, ys)
        canvas = FigureCanvas(fig)
        output = io.BytesIO()
        canvas.print_png(output)
        response = make_response(output.getvalue())
        response.mimetype = 'image/png'
        return response

if __name__ == "__main__":

   app.run(host='0.0.0.0', port=80, debug=False)

Web Page(HTML)

HTML
<!DOCTYPE html>

   <head>

      <title>ENVIRONMENT MONITORING SYSTEM</title>

      <link rel="stylesheet" href='../static/style.css'/>

   </head>



   <body>

                <h1>DATA FROM SENSORS </h1>

                <h3> TEMPERATURE &nbsp; = {{ temp}} oC</h3>

                <h3> HUMIDITY (Rel.) =  {{ hum }} %</h3>

                <h3> PULSE  =  {{ pulse }} </h3>

                <h3> AIR STATUS  =  {{ warning }} </h3>

                 <h3> CURRENT TEMPERATURE &nbsp; = {{ctemp}} oC</h3>

                <h3> CURRENT HUMIDITY (Rel.) =  {{ chum }} %</h3>

                <hr>

                <h3> Last Sensors Reading: {{ time }} ==> <a href="/"class="button">REFRESH</a></h3>

                <hr>
                <h3> PLOT OF DATA OVER TIME PERIOD </h3>
                 <p> Enter number of samples to retrieve:
                 <form method="POST">
                <input name="numSamples" value= {{numSamples}}>
                <input type="submit">
</form></p>
        <hr>
        <img src="/plot/temp" alt="Image Placeholder" width="33%">
        <img src="/plot/hum" alt="Image Placeholder" width="33%">
        <img src="/plot/pulse" alt="Image Placeholder" width="33%">




   </body>

</html>

CSS Code

CSS
CSS code
body{
        background: white;
        color: black;
        padding:1%;
}
.button {
        font: bold 15px Arial;
        text-decoration: none;
        background-color: #EEEEEE;
        color: #333333;
        padding: 2px 6px 2px 6px;
        border-top: 1px solid #CCCCCC;
        border-right: 1px solid #333333;
        border-bottom: 1px solid #333333;
        border-left: 1px solid #CCCCCC;
}
img{
        display: display: inline-block;
}

Credits

Navneet

Navneet

2 projects • 5 followers
Interested in IoT, and Embedded systems,Image Processing

Comments