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
Bluetooth Unleashed Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Bluetooth Unleashed Design Challenge
  • More
  • Cancel
Bluetooth Unleashed Design Challenge
Blog Bluetooth Challenge - Home Security System - Blog Post III
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: kedwards@swbell.net
  • Date Created: 17 May 2018 7:32 PM Date Created
  • Views 922 views
  • Likes 6 likes
  • Comments 3 comments
Related
Recommended

Bluetooth Challenge - Home Security System - Blog Post III

kedwards@swbell.net
kedwards@swbell.net
17 May 2018

Hello everyone,

I am still waiting for my Bluetooth devices. However, I decided that I should test the Bluetooth adapter on the BeagleBone Black Wireless in case the adapter was defective. I did not want to wait too long if I had to replace the BeagleBone Black. I used a HC-06 Bluetooth device, from a previous robotics project, to communicate with the BeagleBone Black Bluetooth adapter. The HC-06 was attached to an Arduino Uno.

 

I ran into one problem. I had to fix an SAP driver error that occurred while checking the Bluetooth service status (Section I). I decided to use Python to access the Bluetooth adapter on the BeagleBone Black. Therefore, I installed the PyBluez libraries.

 

I wrote a “Hello World” Python program on the BeagleBone Black to send a message, via Bluetooth, to the Arduino Uno. I wrote an Arduino sketch to receive and display the message sent by the BeagleBone Black. Running the programs verified that the Bluetooth adapter on the BeagleBone Black was functioning properly.

 

I forgot to discuss securing the Root user last week. So, I added the discussion in the Security section.

 

I wrote a tutorial for each task I completed. If you are interested in the details you can view the following sections below. The sections are:

 

Section I – Pair the BeagleBone Black Bluetooth Adapter to the HC-06

Section II - Install the PyBluez Development Libraries

Section III – Create the Bluetooth Application Software

Section IV – Connect the HC-06 to the Arduino

Section V – Test the Bluetooth Connection

Section VI - Security

 

This week I will take inventory to see what remaining items I need to order. Since I did not do it last week, I will design the main module for the controller program.

 

Talk to you soon.

 

Quote of the Moment -

It’s not broke. It just doesn’t work.

-- My 15-year-old granddaughter.

 

 

 

 

Section I - Pair the BeagleBone Black Bluetooth adapter to the HC-06

 

First, we need to check to see if the Bluetooth service is running.

Run “sudo service bluetooth status” (Figure 1).

You should see: Active: active (running) If you see: Active: inactive (dead)

Run “sudo service bluetooth start”

  Then run “sudo service bluetooth status” again.

 

image

  Figure 1.

 

Note that I received the following error when displaying the Bluetooth status:

beaglebone bluetoothd[2604]: Sap driver initialization failed.

beaglebone bluetoothd[2604]: sap-server: Operation not permi...)

To get rid of the error we need to edit the /etc/systemd/system/bluetooth.target.wants/bluetooth.service file. The example in Figure 2 uses nano since it is easy to use. By the way my favorite editor is vi. It is a more powerful editor than nano. However, vi has a steep learning curve. Hence, nano is my editor of choice for displaying examples.

 

  1. Run “sudo nano /etc/systemd/system/bluetooth.target.wants/bluetooth.service” (Figure 2).
  2. Change the line : ExecStart=/usr/lib/bluetooth/bluetoothd to: ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
  3. Enter Ctrl-O to save the file. When the file name is displayed press the Enter key.
  4. Then enter Ctrl-X to exit nano
  5. Run “sudo systemctl daemon-reload” to reload systemd.
  6. Run “sudo service bluetooth restart” to restart the Bluetooth service.
  7. Run “sudo service bluetooth status”. Note that the error message is gone (Figure 3).

 

image

  Figure 2.

 

image

  Figure 3.

 

 

Not that we have the Bluetooth service running we can pair with another Bluetooth device. In this example we will pair with a HC-06 Bluetooth device attached to an Arduino Uno. We will use the bluetoothctl utility to pair our device.

 

  1. Run “bluetoothctl” (Figure 4).
  2. To ensure the Bluetooth device is powered on. Enter “power on”.
  3. Enter “devices” to see available Bluetooth devices that the BeagleBone knows exist.
  4. If the target device is not displayed enter “scan on”. Any nearby Bluetooth devices will be displayed.
    1. Note the MAC address of the device to pair to. In this example, we will pair to the HC-06 Bluetooth device.
  5. We need to turn on the agent so that we can enter the PIN code when requested. Enter “agent on”.
  6. We can now pair the devices. Enter “pair 20:16:12:12:58:43”
  7. Enter “1234” to the prompt: [agent] Enter PIN code:
    1. The following messages are displayed:
      1. [CHG] Device 20:16:12:12:58:43 Paired: yes
      2. Pairing successful
  8. Enter “exit” to terminate the bluetoothctl utility.

 

image

  Figure 4.

 

 

Section II – Install the PyBluez Development Libraries

 

Now that the BeagleBone and the Arduino are paired we need to install the libraries needed to create the Bluetooth program. We are going to use PyBluez development libraries, so we need to install the libraries on our BeagleBone Black. First, we need to ensure that our system is up-to-date. Therefore, we need to run “sudo apt-get update” (Figure 5) and “sudo apt-get upgrade” (Figure 6). Remember to answer “Y” to the message: Do you want to continue? [Y/n].

 

image

   Figure 5.

 

image

  Figure 6.

 

 

Now that our BeagleBone Black is up-to-date we need to install PyBluez libraries. We need to verify that the build-essential libraries are up-to-date.
 
Run “sudo apt-get install build-essential python-dev libbluetooth-dev” (Figure 7).
 
 
image
  Figure 7.

 

Once the build-essential libraries are up-to-date we can install PyBluez libraries. Run “sudo pip install pybluez” (Figure 8).
 
image
  Figure 8.

 

 
 
Section III – Create the Bluetooth Application Software

 

Now let us see if we can use the BeagleBone Black’s Bluetooth adapter to communicate with another Bluetooth device. In your favorite editor, create a file called rfcomm-client.py. Then add the following code into the file:

 

 
import bluetooth
 
bd_addr = "20:16:12:12:58:43"
port = 1
 
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sendcount=sock.send("Hello World!!!")
print(sendcount)
sock.close()

 

Figure 9. rfcomm-client.py

 

 

This program just sends a hello world message to another Bluetooth device. The device address is hard-coded in the program. (bd_addr = "20:16:12:12:58:43"). This is the MAC address of the HC-06 device attached to the Arduino Uno. The port number is also hard-coded to 1 (port = 1 ).  A socket is created (sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )). Then a connection to the HC-06 is established (sock.connect((bd_addr, port))). The text “Hello World!!!” is sent to the Arduino via the socket (sendcount=sock.send("Hello World!!!")). The number of bytes sent is stored in the sendcount variable. The byte count is displayed on the terminal (print(sendcount)).  The socket is closed and the program is terminated (sock.close()).

 

We also need a program on the Arduino to accept the data. If you do not have the Arduino IDE installed on your PC visit the following site:

 

https://www.arduino.cc/en/Guide/HomePage

 

Create a new Arduino sketch (Arduino’s term for s program). Call it BBBWTest.ino. Add the following code to the file:
 
 
#include <SoftwareSerial.h>
SoftwareSerial BTserial(4, 2);

 

char recd_dat[81];

 

void setup(){
  BTserial.begin(9600);
  Serial.begin(9600);
  Serial.println("Start Test");
} // End setup()

 

void loop(){
  for (int i = 0; i < 80; i++){
      recd_dat[i] = 0;
  } // End for (int i = 0; i < 80; i++)

 

  if( BTserial.available()) {
    BTserial.readBytes(recd_dat,80);
    Serial.print("From BeagleBone - ");
    Serial.println(recd_dat);
  } // End if( BTserial.available())              

 

} // End loop()

 

Figure 10. BBBWTest.ino

 

 

Instead of using the default serial ports on the Arduino Uno, we are going to use GPIO pins 4 and 2. Therefore we need to include the software serial library (#include <SoftwareSerial.h>).  Next, we create a softwareSerial object called BTserial that uses GPIO pins 4 and 2 (SoftwareSerial BTserial(4, 2);). We create a character array to hold the data received from the BeagleBone Black (char recd_dat[81];).

 

In the setup function we are going to initialize the Bluetooth serial ports (BTserial.begin(9600);) and the default serial ports (Serial.begin(9600);). The default Serial port is used to display the data sent from the BeagleBone Black. The last thing we do in the setup function is display a message to denote the sketch has started (Serial.println("Start Test");).

 

The first thing we do in the main loop is initialize the receive buffer to ensure there is no garbage in our array buffer:

 

  for (int i = 0; i < 80; i++){
      recd_dat[i] = 0;
  } // End for (int i = 0; i < 80; i++)

 

The sketch will check if any data has been sent from the BeagleBone Black (if( BTserial.available()){). If no data is found the sketch will go to the top of the loop and execute the code again. If data was sent the sketch will read up to 80 bytes and store the data in the recd_dat buffer (BTserial.readBytes(recd_dat,80);). The sketch will display the data on the serial monitor:

 

Serial.print("From BeagleBone - ");
    Serial.println(recd_dat);

 

The sketch will go to the top of the loop and execute the code again. This process will continue until the power is disconnected from the Arduino or until another sketch is loaded into the Arduino.

 

 

 

Section IV – Connect the HC-06 to the Arduino

 

Now, insert the HC-06 into a breadboard (Figure 11) and wire it to the Arduino.
 
  • Connect a jumper wire from the receive pin on the HC-06 to the transmit pin on the Arduino (pin 2).
  • Connect a jumper wire from the transmit pin on the HC-06 to the receive pin on the Arduino (pin 4).
  • Connect a jumper wire from the ground pin on the HC-06 to the ground pin on the Arduino.
  • Connect a jumper wire from the VCC pin on the HC-06 to the 5-volt pin on the Arduino.

 

image

 

  Figure 11.
 
 
 
Section V – Test the Bluetooth Connection

 

We are ready to test our Bluetooth connection. First, we need to load the BBBWTest sketch so it can wait for a transmission from the BeagleBone Black.
 
Double click on the BBBWTest.ino file (Figure 12).
 
Click on the Upload button (Figure 13) to load and execute the BBBWTest sketch.

 


image

  Figure 12.

 

image

  Figure 13.

 

We need to start the Serial Monitor so we can display the data passed by the BeagleBone Black. In the Arduino IDE, click on the Tools menu and select Serial Monitor (Figure 14). The Serial Monitor is displayed (Figure 15).
 
image
  Figure 14.
 
image
  Figure 15.
 
Create a Putty session to the BeagleBone Black. Then execute the rfcomm-client python program.
 
Run “sudo python rfcomm-client.py” (Figure 16).
 
As shown in Figure 16, the BBBWTest sketch displays “Start Test” in the Serial Monitor. When the rfcomm-client python program is started, it will connect to the Bluetooth device attached to the Arduino and send it the “Hello World!!!” text. When the BBBWTest sketch receives the text, it will display it in the Serial Monitor (From BeagleBone – Hello World!!!).
 
We now know that the Bluetooth adaptor on the BeagleBone Black is working correctly.
 
image
  Figure 16.
 
 
 
Section VI – Security
 
I do not know how I forgot to add this last time but I did. Debian version 8 (Jessie) installs without a root password. Make sure you give it one.

 

Logon as root. Once you enter root as the username Debian will log you on (Figure 17).
 
image
  Figure 17.
 
Run “passwd”. Notice that Debian will not ask you for your old password because there is not one (Figure 18).
 
Enter your new root password twice.

 

image

  Figure 18.

 

Your root password is now secure however, you can lock down the root username even more. There is no reason to logon as root remotely once you are finished installing the Debian operating system. So, let us turn off remote root logon.
 
We need to edit the /etc/ssh/sshd_config file. Read the /etc/ssh/sshd_config file into your favorite editor. The example in Figure 19 uses nano since it is easy to use.
 
  1. Run “nano /etc/ssh/sshd_config”
  2. Press Ctrl-W
  3. Enter PermitRootLogin and then press the Enter key to perform the search.
  4. Change yes to no (Figure 20).
  5. Enter Ctrl-O to save the file. When the file name is displayed press the Enter key.
  6. Then enter Ctrl-X to exit nano.
  7. The change will not take effect until you restart the SSH server.
    1. Run “/etc/init.d/ssh restart” (Figure 21).

 

 

image

  Figure 19.

 

image

   Figure 20.

 

image

  Figure 21.

 

Now Debian will not allow you to logon as Root remotely (Figure 22).
 
image
  Figure 22.

  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 7 years ago +1
    Hi Keith, Great project, and excellent detail on how to use the Python API for Bluetooth comms! Bookmarked : )
  • sunnyiut
    sunnyiut over 7 years ago +1
    hello Keith, nice detail blog. liked the 'security' section.
  • sunnyiut
    sunnyiut over 7 years ago

    hello Keith,

     

    nice detail blog.

    liked the 'security' section.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 7 years ago

    Hi Keith,

     

    Great project, and excellent detail on how to use the Python API for Bluetooth comms!

    Bookmarked : )

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 7 years ago

    kedwards@swbell.net

    Very nice update on your design challenge project.  It takes quite a bit of code and setup to say "Hello World" these days.

    I look forward to your updates and wish you luck on your project.

     

    I love the quote!

    Gene

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
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