Skip to content

Latest commit

 

History

History
216 lines (175 loc) · 7.94 KB

readme.adoc

File metadata and controls

216 lines (175 loc) · 7.94 KB

How To: Bluetooth control of Raspberry Pi

A simple daemon which accepts commands from a paired bluetooth client.

Install this code

On your robot:

cd ~
mkdir pydir
cd pydir
wget --no-check-certifate https://github.com/javatechs/RxCmd/archive/master.zip
unzip master.zip
mv RxCmd-master/pydir/* RxCmd-master/daemon-rxcmd.sh .
rm -rf RxCmd-master

Install Python’s bluez package on your Pi and PC

$ sudo apt-get install python-bluez bluez python-gobject python-dbus python-daemon

Discover the hciX address of your Pi

$ hcitool dev

Devices:
	hci0	xx:xx:xx:xx:xx:xx

Discover the address of your phone/laptop

  • Make sure Bluetooth is enabled on the phone/laptop.

  • Make sure Bluetooth on your phone/pc/laptop is discoverable.

  • On an Android phone, go to settings→Bluetooth. Make device discoverable (visible). The device only remains discoverable for a short time.

./finddevice.py

Add pc/laptop bluetooth as trusted to the Pi

  • Temporarily:

bluez-simple-agent hci# xx:xx:xx:xx:xx:xx
  • Permanently:

sudo bluez-test-device trusted xx:xx:xx:xx:xx:xx yes

Add phone as trusted Bluetooth device

Make Pi visible

$ hostname
prsg2
$ sudo hciconfig hci0 piscan
$ hciconfig -a
...
UP RUNNING PSCAN ISCAN
...

$ bluez-simple-agent

Initiate pairing on phone

  • Select settings→Bluetooth→SCAN.

  • Look for the host name from the hostname command above.

  • Click to connect with device. On the phone a dialog box appears. 'Bluetooth Pairing Request', Confirm passkey is 999999 to pair with prsg. DO NOT CLICK 'OK' ON PHONE yet. Back to the Pi.

Confirm passkey and accept pairing on Pi

$ bluez-simple-agent
not registered
RequestConfirmation (/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX, 999999)
Confirm passkey (yes/no) yes
^C
  • Use the Ctrl+C to terminate bluez-simple-agent.

Complete pairing on phone

  • NOW press OK on the phone to complete pairing on phone.

Send commands from pc/laptop to Pi

Start Daemon on Pi

  • Daemon implements start/stop/restart. To start daemon on Pi, type:

$ ./daemon-rxcmd.py start

Start the command client on your pc/laptop

  • Type this command with the address of the Pi:

./rxcmd.py xx:xx:xx:xx:xx:xx

Pi screen should read:

$ ./daemon-rxcmd.py start
Accepted connection from  ('xx:xx:xx:xx:xx:xx', 1)

On pc/laptop,

You can now enter commands:

Talking to: rosrov1-0. Hit enter to send, 'esc' to quit
command001

PI now reads

$ ./daemon-rxcmd.py start
Accepted connection from  ('xx:xx:xx:xx:xx:xx', 1)
received [command001]

Alternate CLI only bluetooth howto

On the robot itself (not your phone) execute the following:

bluetoothctl

You should now be in bluetooth command prompt. Now type the following to enable scanning:

scan on

Then look for devices that are available:

devices

You should see something like the below - the one I’m interested in is my phone, so copy the device id to your clipboard (that’s the string with all the colons in it):

[bluetooth]# devices
Device 03:1C:7D:E4:62:12 BOOGYWOOGY 625s
Device BD:32:B2:E1:1C:83 JDPHONE

Now trust your device with the following command:

[bluetooth]# trust BD:32:B2:E1:1C:83

Then run the following commands and get the expected output shown:

[bluetooth]# discoverable on
Changing discoverable on succeeded
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# agent NoInputNoOutput
Agent registered
[bluetooth]# default-agent
Default agent request successful

At this point go to your phone bluetooth settings and find the robot and try to pair with it. It should prompt you to enter a pin on the phone - pick any number combination you like (I used 0000) and enter it on your phone. The phone will then talk back to the robot and the robot should prompt you to enter the same pin:

[NEW] Device BD:32:B2:E1:1C:83 JDPHONE
Request PIN code
[agent] Enter PIN code: 0000

at that point you’ll get more output suddenly. You can now exit out of the bluetooth console.

That’s it, you’re paired. You can now use the Vicky app on your phone!

Running the Vicky app

To get bluetooth app on android phone

  • Go to Google Drive/PRSG/RoboMagellan - there should be a file app-kinetic-debug.apk - go ahead and download it to your phone and run it (You may be prompted to go into settings and enable "Untrusted Sources" - you need to do that to install an app this way).

  • Make sure phone’s bluetooth is turned on

  • Go into your settings/bluetooth settings and make sure your phone is discoverable

Once daemon-rxcmd.py is running on your robot and you are bluetooth paired to it from your phone. On your phone:

  • Launch the vicky app

  • Click the green bluetooth button at the top and from the list of devices, choose your robot

    • You should see a message about being connected

  • Use back arrow on phone to back out of vicky app

  • Reopen vicky app. You should now have a series of options such as reboot robot, roslaunch, reboot pixhawk, etc.

You can additionally click on the ROS looking icon and connect via wifi if you know the IP Address/port combination from the vicky app.

init script

Note, these steps presume you have run the "Install this code" section and that your paths are correct. If you have changed things, you may need to edit daemon-rxcmd.py and daemon-rxcmd.sh accordingly

As robot user check /home/<user>/pydir:

  • Check that daemon-rxcmd.py stdout, stderr and pid locations are correct, or modify according to your paths

  • Check that the paths/files listed in the variables section in daemon-rxcmd.sh are correct or modify according to your paths

  • Check that the user listed below the path variables section in daemon-rxcmd.sh is correct

  • Then execute these commands:

sudo su
cd /etc/init.d
cp /home/<user>/pydir/daemon-rxcmd.sh .
chmod 755 daemon-rxcmd.sh
update-rc.d daemon-rxcmd.sh defaults
mv /etc/rc2.d/S01daemon-rxcmd.sh /etc/rc2.d/S05daemon-rxcmd.sh
mv /etc/rc3.d/S01daemon-rxcmd.sh /etc/rc3.d/S05daemon-rxcmd.sh
mv /etc/rc4.d/S01daemon-rxcmd.sh /etc/rc4.d/S05daemon-rxcmd.sh
mv /etc/rc5.d/S01daemon-rxcmd.sh /etc/rc5.d/S05daemon-rxcmd.sh

That’s it! You should now successfully start/stop the daemon for bluetooth on stop/start/reboot