Rid
Published © GPL3+

PAAS - Patients' Assistance Alert System

A Wireless/IoT emergency bell system for COVID field hospitals

IntermediateFull instructions provided3 hours682
PAAS - Patients' Assistance Alert System

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
IR Proximity Sensor
Digilent IR Proximity Sensor
×1

Software apps and online services

Flask for python
Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Bolt Python Library
Bolt IoT Bolt Arduino library

Story

Read more

Schematics

Circuit connections

Circuit connections for the device

Code

Directory_structure.txt

Plain text
Directory structure of the project
directory structure:

Project_directory
 templates
    base.html
    devices.html
    edit.html
    index.html
    manage.html
    new_device.html
   
 app.py
   
 device_code.py
   
 devices.sqlite

base.html

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    {% block head %} {% endblock %}
</head>
<body>
    <nav class="navbar navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Navigate</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample01" aria-controls="navbarsExample01" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      
        <div class="collapse navbar-collapse" id="navbarsExample01">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <a class="nav-link" href="/">PAAS Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="/devices">Device Status<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="/devices/new">Add New Device<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="/manage">Manage Devices<span class="sr-only">(current)</span></a>
            </li>
          </ul>
        </div>
      </nav>
    <div class = 'container'>
    {% block body %} {% endblock %}

    <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.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    </div>
</body>
</html>

devices.html

HTML
{% extends 'base.html' %}

{% block head %} 
<meta http-equiv="refresh" content="5" >
<title>Device status</title>
{% endblock %}

{% block body %} 
<div>
    <br>
    <h1 style='display:inline'>Device status</h1>
    <a style='display:inline' class='btn btn-success float-right' href='/devices/new' >+ Add device</a>
    <br>
</div>
<hr>
    {% for device in devices %}
    <h2>Patient number {{ device.id }}</h2>
        {% if device.response == '1' %}
        <br>
        <p ></p>
        <div style='white-space:pre-wrap'class="alert alert-danger" role="alert">
            <b>The patient requires assistance.</b><a style='display:inline' class ='btn btn-danger float-right' href='/devices/attend/{{device.id}}'>Attend</a>          
        </div>
        </p>
        {% else %}
        <div style='white-space:pre-wrap' class="alert alert-success" role="alert">
            <b>No help needed. </b>
        </div>
        <br>
        {% endif %}
        <hr>
    {% endfor %}   

{% endblock %}

edit.html

HTML
{% extends 'base.html' %}

{% block head %} 
<title>Edit Device</title>
{% endblock %}

{% block body %} 
    <br>
    <h1>Edit Device</h1>
        <hr>      
        <form action = '/devices/edit/{{device.id}}' method='POST'>
            <label for="device_id">Device id</label>
            <input class="form-control" type="text" name = 'device_id' id = 'device_id' value = '{{device.device_id}}'>
            <br>
            <label for="api_key">API key</label> 
            <input class="form-control" type="text" name = 'api_key' id = 'api_key' value = '{{device.api_key}}'>
            <br>
            <input type='submit'value = 'Save'>
        <hr>
{% endblock %}

index.html

HTML
{% extends 'base.html' %}

{% block head %} 
<title>Home</title>
{% endblock %}

{% block body %}
<br> 
<h1>PAAS - Central alarm monitoring system</h1>
<br>

<main role="main">
  <div class="jumbotron">
    <div class="col-sm-8 mx-auto">
      <h3>Welcome to the central alarm monitoring system</h3>
      <p>
        <a class="btn btn-primary" href="/devices" role="button">View device status &raquo;</a>
      </p>
    </div>
  </div>
</main>

{% endblock %}

manage.html

HTML
{% extends 'base.html' %}

{% block head %} 
<title>Manage devices</title>
{% endblock %}

{% block body %} 
    <br>
    <h1>Manage Devices</h1>
    <br>
    <br>
    {% for device in devices %}
        <h2 style='display:inline' >Device {{ device.id }}</h2>
        <a style='display:inline' class ='btn btn-danger float-right' href='/devices/delete/{{device.id}}'>Delete</a>
        <a style='display:inline' class = 'btn btn-primary  float-right mr-2' href='/devices/edit/{{device.id}}'>Edit</a>
        <br>
        <p style='display:inline' >{{ device.device_id }}</p>
        <hr>
        <br>        
    {% endfor %} 
{% endblock %}

new_device.html

HTML
{% extends 'base.html' %}

{% block head %} 
<title>New device</title>
{% endblock %}

{% block body %} 
    <br>
    <h1>Add new device</h1>

        <hr>
        <form action = '/devices' method='POST'>
            <label for="device_id">Device id</label>
            <input class="form-control" type="text" name = 'device_id' id = 'device_id' placeholder = 'Enter Device id'>
            <br>
            <label for="api_key">API key</label> 
            <input class="form-control" type="text" name = 'api_key' id = 'api_key' placeholder = 'Enter API key'>
            <br>
                      
            <br>
            <input type='submit'value = 'Add device'>
    
        </form>
        <hr>
{% endblock %}

app.py

Python
Flask app
#import necessary libraries
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

#create flask app
app = Flask(__name__)

#configure database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///devices.sqlite'
db = SQLAlchemy(app)

class device_response(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    device_id = db.Column(db.String(100), nullable = False)
    response = db.Column(db.Text, nullable = True, default = 'N/A')
    api_key = db.Column(db.String(20), nullable = False) 

    def __repr__(self):
        return 'device_response' + str(self.id)

#create routes for individual webpages
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/devices', methods=['GET','POST'])
def devices():
    if request.method == 'POST':
        device_device_id = request.form['device_id']
        device_api_key = request.form['api_key']
        new_device = device_response(device_id=device_device_id, api_key=device_api_key,response='0')
        db.session.add(new_device)
        db.session.commit()
        return redirect('/devices')
    else:
        all_devices = device_response.query.order_by(device_response.id).all()
        return render_template('devices.html',devices=all_devices)

@app.route('/devices/delete/<int:id>')
def delete(id):
    device = device_response.query.get_or_404(id)
    db.session.delete(device)
    db.session.commit()
    return redirect('/devices')

@app.route('/devices/edit/<int:id>',methods=['GET','POST'])
def edit(id):
    device = device_response.query.get_or_404(id)
    if request.method=='POST':
        device.device_id=request.form['device_id']
        device.api_key=request.form['api_key']
        db.session.commit()
        return redirect('/devices')
    else:
        return render_template('edit.html',device = device)

@app.route('/devices/attend/<int:id>',methods=['GET','POST'])
def attend(id):
    device = device_response.query.get_or_404(id)
    device.response=0
    db.session.commit()
    return redirect('/devices')

@app.route('/devices/new',methods=['GET','POST'])
def new_device():
    if request.method=="POST":
        device.device_id=request.form['device_id']
        device.api_key=request.form['api_key']
        new_device = device_response(device_id=device_device_id, response=device_response, api_key=device_api_key)
        db.session.add(new_device)
        db.session.commit()
        return redirect('/devices')
    else:
        return render_template('new_device.html')

@app.route('/manage', methods = ['GET','POST'])
def manage_device():
    all_devices = device_response.query.order_by(device_response.id).all()
    return render_template('manage.html',devices=all_devices)


if __name__=="__main__":
    app.run(debug=True)

device_code.py

Python
Code for fetching data from arduino via Bolt over internet
#import required libraries
import sqlite3                                                                          
from boltiot import Bolt                                                       
import json, time, sqlite3

#connect to sql database
conn = sqlite3.connect('devices.sqlite')
cur = conn.cursor()

sqlquery='SELECT device_id,api_key FROM device_response ORDER BY id'

#empty dictionary for holding Device id and API keys of Bolt devices
bolt_devices={}

#empty dictionary to hold Bolt objects
mybolt_devices={}

while True:
    #populating bolt_devices dictionary with data obtained from the database
    for row in cur.execute(sqlquery):
        (device_id,api_key)=(row)
        if device_id not in bolt_devices.keys():
            bolt_devices[device_id]=api_key
    
    #creating Bolt objects for all bolt devices available, opening Bolt serial communication
    for device_id,api_key in bolt_devices.items():
        mybolt=Bolt(api_key,device_id)
        
        if device_id not in mybolt_devices.keys():
            mybolt_devices[device_id]=mybolt

    #reading data from individual Bolt devices
    for device_id,mybolt in mybolt_devices.items():
        response = mybolt_devices[device_id].digitalRead('0')
        data = json.loads(response)
        obst = data['value'].rstrip()
        #print(data) #print for troubleshooting
        cur.execute('SELECT response FROM device_response WHERE device_id=?',(device_id,))
        query=cur.fetchall()
        
        if query[0][0]=='0':
            if obst=='0':
                cur.execute('UPDATE device_response SET response = "1" WHERE device_id =?', (device_id,))
                conn.commit()              
                        
            else:
                cur.execute('UPDATE device_response SET response = "0" WHERE device_id =?', (device_id,))
                conn.commit()
            
            time.sleep(5)
        
        else:pass
        #print(query) #print for troubleshooting
        time.sleep(10)                  
           

Credits

Rid

Rid

2 projects • 2 followers

Comments