Raspberry Pi Robot - Android Remote Control (Bluetooth)#
Remote control of a Raspberry Pi robot from an Android device using Bluetooth.
The Raspberry Pi code was developed using a Raspberry Pi 3 Model B and:
The Android app was developed with MIT App Inventor and should run on any Android smartphone or tablet equipped with Bluetooth.
Bluetooth needs to be installed and configured on the Raspberry Pi.
First, ensure the Bluetooth packages are installed:
sudo apt-get install bluez python-bluez
It was previously recommended that PNAT be disabled by adding the line
DisablePlugins = pnat to /etc/bluetooth/main.conf, however that key is not recognised by the current version of bluetoothd (5.23) and the underlying Debian bug appears to have been resolved.
The Bluetooth daemon needs to run in compatibility mode so that the serial port service can be enabled. This can be done by adding -C to the
bluetoothd line in /etc/systemd/system/dbus-org.bluez.service
Create a directory named
/home/pi/boot and copy the three Python scripts into it. If the scripts are installed to a different directory, the file paths within the scripts should be modified accordingly.
Call bootscript-bt.py at boot time, for example by adding this line to cron with
sudo crontab -e
@reboot python /home/pi/boot/bootscript-bt.py >/home/pi/boot/bootscript-bt.log 2>/home/pi/boot/bootscript-bt.err
The script bootscript-bt.py starts the robot Bluetooth server and the shutdown monitor. This script is optional, but may be useful for headless operation.
The script shutdownbutton.py waits for a button to be pressed, then halts the Raspberry Pi. This script is optional, but may be useful for headless operation.
The Bluetooth server script roboserver-bt.py initialises the Raspberry Pi hardware, then starts a Bluetooth serial port service to listen for commands to control the robot from a connected device. The server returns robot status information (from the LEDs, ultrasound unit and line follower unit) encoded in JSON format.
The script may need to be adapted depending on how the hardware components are connected; in particular, when using EduKit 1 and EduKit 3 simultaneously, it is necessary to use different GPIO pins for EduKit 1 than are shown in the CamJam worksheets.
The script first flashes the LEDs, then illuminates the yellow LED while waiting for a Bluetooth client to connect.
The green LED is illuminated when a Bluetooth connection is initiated by the client Android device, and the server is ready to accept commands. The supported commands can be seen by inspecting the roboserver-bt.py script.
The red LED is illuminated whenever the motors are active. The yellow LED is illuminated, accompanied by an audible warning from the buzzer, when an object is detected within 10 cm of the ultrasound unit.
The roboclient_bt.apk file is a packaged Android app that should run successfully on any recent Android device equipped with Bluetooth. ‘Sideloading’ of apps needs to be enabled in order to install the client app. On most devices there is a checkbox under Settings -> Security that refers to installing apps from unknown or untrusted sources. Some devices will prompt the user to change the setting when an attempt is first made to sideload an app.
The Android device must be paired with the Raspberry Pi before starting the app for the first time. This can be done by making the Android device discoverable and then using
bluetoothctl on the Raspberry Pi:
pi@raspberrypi:~ $ bluetoothctl [NEW] Controller AA:AA:AA:AA:AA:AA raspberrypi [default] [NEW] Device BB:BB:BB:BB:BB:BB Nexus 7 [bluetooth]# pair BB:BB:BB:BB:BB:BB Attempting to pair with BB:BB:BB:BB:BB:BB [CHG] Device BB:BB:BB:BB:BB:BB Paired: yes Pairing successful [bluetooth]# exit pi@raspberrypi:~ $
Alternatively, make the Raspberry Pi discoverable, and initiate pairing from the Android device:
sudo hciconfig hci0 piscan
Start the app and click the Connect button to establish a Bluetooth connection with the Raspberry Pi. On successful connection, the status message on the app changes to show the distance detected by the ultrasound module.
Sliding the Raspberry Pi icon around on the blue canvas controls the motors, and affords full control of speed and direction. The robot stops automatically when the icon is released. The slider below the canvas controls the maximum motor speed.
The buttons at the bottom sound the buzzer, speak a preset phrase, and flash the LEDs.
MIT App Inventor
The roboclient_bt.aia file contains the MIT App Inventor source from which the app is built. This file is not required to use the app.
MIT App Inventor allows Android apps to be easily built using drag-and-drop interfaces for both design and coding. Further information is available at:
Once an account has been created on App Inventor, roboclient_bt.aia can be imported as a new project in order to examine and modify the code. The app can then be rebuilt and downloaded locally as an APK file, or installed directly to the Android device via a QR code.
Gerard Kelly, November 2016