Bluetooth

This first section will use a simple bluetooth dongle with the Pi.

To install:

sudo apt-get update ## Updates the list of available packages

sudo apt-get install bluetooth bluez blueman ## install bluetooth packages

## note that "bluez" seems to have replaced "bluez-utils" that is mentioned on web sites to install.

lsusb ## this command will show all the devices connected to the usb ports.

/etc/init.d/bluetooth status # checks bluetooth device

Should respond with:

[ ok ] bluetooth is running.

hcitool scan ## scans for available bluetooth devices

Scanning ...

20:15:10:19:93:94 HC-06

sudo l2ping -c 1 20:15:10:19:93:94

# number after -c <N> indicates how many times to ping

Now, set up the bluetooth configuration so that the dongle can talk to the discovered device:

sudo /usr/bin/emacs -nw /etc/bluetooth/rfcomm.conf

Add the following code to this file:

rfcomm1 {

bind yes;

device 20:15:10:19:93:94;

channel 1;

comment "connection to Bluetooth HC-06 serial module";

}

Save file...

sudo rfcomm bind all

correct bind command may be:

sudo rfcomm bind rfcomm1 20:15:10:19:93:94 1

Set up minicom:

sudo apt-get install minicom

Add python serial package:

sudo apt-get install python-serial

AT Commands to PAIR a HC-05 to another HC-5 device

Set up the Slave:

at+reset ## is this needed? Not sure what reset does other than reset PIO pins (Not used in HC05_ProgSlave code)

at+orgl ## set to original defaults

at+uart=38400,1,0 ## set baud

at+role=0 # 0=slave, 1=master

at+rmaad # DELETE devices in pair list (not used in HC05 ProgSlave code since ORGL was issued).

at+pswd=1234 # set password (default is 1234) (Not used in HC05_ProgSlave code - left at default.)

at+name=tinkXX # set device name

at+addr? <<<< WRITE THIS DOWN!!!!

at+cmode=1 ## Use cmode=0 to specify the address to connect to. Use cmode=1 to connect to anything that tries.

at+init << Be sure to do this to put the device in SPP mode!!

Set up the MASTER:

at+reset # reset ## Is this needed??

at+orgl # Set back to original defaults

at+uart=38400,1,0 # 9600 baud, 1 stop bit, no parity

at+role=1 # 0=slave, 1=master

at+rmaad # DELETE devices in pair list

at+pswd=1234 # set password, use at+pswd? to get passwd

at+name=foo # set the name of the device

at+addr? # get the address of the device

at+cmode=1 # Allow to connect to ANY device - start with this, then set to cmode0 after pairing

at+init << Be sure to do this!! # if it comes back with ERROR:(17), then it is already in SPP mode. Do this before at+inqm.

at+inqm=0,10,20 # set INQ mode - std mode, get first 10 devices, default after 20 sec.

at+inq # get addresses - will answer with nnn:nn:nnnn,<type>,<signal>

# at+inq does NOT have (?) after command. Will not return unless there is a BT device that is in slave mode and NOT in AT mode avail.

at+rname? nnn,nn,nnnn # get the name for the device to verify right device to set - Command is as specified. at+rname?[space][xxx,yy,zzzz]

at+pair=nnn,nn,nnn,20 # pair the two devices, with 20 second timeout for pairing

at+bind=nnn,nn,nnnn # bind the devices

at+cmode=0 # Only connect to bound devices.

at+link=nnn,nn,nnnn # link the devices.

Alternate: at+fsad=nnn,nn,nnnn # check if device is in pair list, and then link. Response will be "FAIL" if device is not in pair list.

at+inqc # cancel linked device??

at+disc # disconnect device??

"BT_Echo" code for Arduino:

Source: https://myraspberryandme.wordpress.com/2013/11/20/bluetooth-serial-communication-with-hc-05/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()

{

// Open serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for Leonardo only

}

Serial.println("Ready!");

// set the data rate for the SoftwareSerial port

// for HC-05 use 38400 when poerwing with KEY/STATE set to HIGH on power on

mySerial.begin(9600);

}

void loop() // run over and over

{

if (mySerial.available())

Serial.write(mySerial.read());

if (Serial.available())

mySerial.write(Serial.read());

}

Plan:

    • Set up slave