element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs A Bluetooth Based In/Out Board for the Raspberry Pi 3
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mikedavis
  • Date Created: 23 Sep 2016 5:55 PM Date Created
  • Views 594 views
  • Likes 3 likes
  • Comments 0 comments
  • bluetooth
  • python
  • flask
  • raspberry pi
Related
Recommended

A Bluetooth Based In/Out Board for the Raspberry Pi 3

mikedavis
mikedavis
23 Sep 2016

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!

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube