Making an In/Out Board with the Raspberry Pi 3

I work at St. Petersburg College in Tarpon Springs.  The library here is a very dynamic student-centered place where a lot of students come for extra help and tutoring.  I hold about 1/3 of my office hours in the Science Room here.  I thought it would be nice to have a dynamic display board outside the room that would show what tutors are present and what kind of help you can get.  So I followed this tutorial for making an in/out board with a Python program, and then I added Flask, so it could be served to the internet.

Materials

  • Raspberry Pi 3 with built-in bluetooth (an older Pi could be used, but you will need to add bluetooth functionality to it).
  • Reliable internet connection.  I recommend a wired connection.

Step 1 – Install bluetooth

You will want to make sure that the bluetooth module is installed and working.

sudo apt-get update
sudo apt-get install python-pip python-dev ipython
sudo apt-get install bluetooth libbluetooth-dev
sudo pip install pybluez

Use the command below:

hcitool dev

The Pi will scan for any discoverable bluetooth devices like phones and return their bluetooth addresses.

hcitool_dev

Additionally, the program below will use the bluetooth library to look for a specific address. You would put it in at ‘Your Bluetooth Address’. This link will help you find the bluetooth address of your  Android phone.  The way its written, it looks for the specific address.  It prints the time, and whether you are in or out.

 
import bluetooth
import time

print "In/Out Board"

while True:
  print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S")
  result = bluetooth.lookup_name('Your Bluetooth Address', timeout=5)
  if (result != None):
    print "Mike is in"
  else:
    print "Mike is out"

  time.sleep(60)

 

The next thing to do is set up the Raspberry Pi to be a local web server.  Ultimately the python program will need to report to the web that someone is in or out.  For this I used Flask, and this tutorial was very helpful.

Step 2 – Install Flask

The installation process is pretty easy.  First you need to make sure PIP is installed.

sudo apt-get install python-pip

Then you will install Flask

sudo pip install flask

This will install the web server software on the Raspberry Pi. It takes about 2 minutes to complete with a good internet connection, and you’ll see all kinds of fancy stuff scroll by.

When this is done, you can use the program below to make sure it works.  Create a program called hello-world.py by typing

nano hello-world.py

This is the program that you will want to copy and past into hello-world.py.


from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

To run the program:

sudo python hello-world.py

 

Point your browser to the IP address of the Raspberry Pi (found using ifconfig), and you will see this.

hello-world

The terminal will show activity on the server.  So if you refresh the page, you will see requests being made on the server.  Pretty cool!

flask

Step 3 – Program for the In/Out Board

The program below will take the code for making a python In/Out board, and run it as a web-application. Open a file and call it hello-blue.py

nano hello-blue.py

 

Copy and paste this program into it.


from flask import Flask, render_template
import datetime
import bluetooth
import time

app = Flask(__name__)

@app.route("/blue/")
def btooth():
   result = bluetooth.lookup_name('YOUR BLUETOOTH ADDRESS')
   if (result != None):
     output =  "Mike is in"
   else:
     output =  "Mike is out"

   templateData = {
        'title' : 'In or Out',
        'output' : output
        }
   return render_template('blue.html', **templateData)

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=80, debug=True)

This program, when it is run will serve up a webpage at http://IPADDRESS/blue that will say whether Mike is in or out.  We just need a couple more things to make it work, because it uses an html page template.

Create a folder called templates, and create a file called blue.html.

 
mkdir templates
cd templates
nano blue.html

In that blue.html file you are going to copy the html code below and paste it in.


<!DOCTYPE html>
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <h1>In or Out</h1>
    <h2>{{ output }}</h2>
  </body>
</html>

So when the hello-blue.py program is running, it will generate a string ‘In or Out’ that will be put into the template html file where the {{ title }} tag appears.  Additionally, whatever the output of the bluetooth scan gives will appear in the {{ output }} tag.

To run the program, back out of the templates folder and type

sudo python hello-blue.py

When this is done, you can go to the web and type in the IP address of the Raspberry Pi to get a screen that looks like this.  One thing to notice is that the page will take a few seconds to respond.  The reason for that is the length of time it takes for the bluetooth scan to be completed.

capture2

So that is pretty much it.  The rest is styling, and functionalizing.  Once I have a completed product I will post a screen shot.  I was pretty pleased with this proof-of-concept, and our tutoring coordinator is excited for the help!

 

Leave a Reply

Your email address will not be published. Required fields are marked *