We are in part 2 of tutorial series of Building Raspberry Pi Robot controlled by Android App via bluetooth! If you want to jump back and forth or missed any of my previous tutorials – here is the consolidated list.
All Tutorials to build Raspberry Pi Robot controlled by Android App via Bluetooth
- Build Raspberry PI Robot controlled by Android App via Bluetooth
- Programming the Raspberry Pi Robot for Bluetooth with Python
- Creating Xamarin Android App for connecting to Raspberry Pi Robot
- Final Bluetooth controlled Rasbperry Pi Robot in Action
If you are directly starting from this tutorial, everything will sound new. I recommend you to first build basic robot by following below tutorial series:
Tutorial to build basic Pi Robot
- Understanding Raspberry PI and Robotics
- Assembling Raspberry PI Robot and all electronics
- Programming Raspberry PI Robot with Python
- Final Raspberry PI robot in Action
First, let’s understand what is Bluetooth and why we need it for connecting mobile phone and Pi 3.
What is Bluetooth and why we selected Bluetooth?
Bluetooth is a remote innovation standard for trading information over short separations (utilizing short-wavelength UHF radio waves in the ISM band from 2.4 to 2.485 GHz) from settled and cell phones, and building individual range systems (PANs). Concocted by telecom merchant Ericsson in 1994, it was initially imagined as a remote contrasting option to RS-232 information links.
In our tutorial, we are going to use it for standard data transmission. In our first tutorial, we created robot and controlled using computer. We want to now build something that can be controlled wireless. There is inbuilt Bluetooth in Raspberry Pi 3 which makes it ideal to go for Bluetooth protocol for wireless connectivity.
Step 1: Enable Bluetooth on the Raspberry Pi 3 and Pair android phone
The Pi 3 comes with bluetooth and a couple of straightforward advances will influence it to work. I read you may need to refresh Raspbian, however I didn’t need to. In the event of some unforeseen issue, here are the commands to make it work:
Run the commands one by one and it will upgrade Pi 3 and install bluetooth, bluez and python-bluez if missing. If installed it will not reinstall again and will give an alert message.
Below commands can be provided using Putty – connect to Pi 3 via SSH:
sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install bluetooth sudo apt-get install bluez sudo apt-get install python-bluez
Next step will be to turn on Bluetooth and pair the mobile phone. You can also do this using Raspbian GUI if you are aware of it else continue working with SSH. Here are the commands:
#this will turn on bluetooth control mode, ie [bluetooth]# sudo bluetoothctl agent on default-agent
On your Android phone, first turn on Bluetooth and make it discoverable. Then, go to the Bluetooth settings by opening up the Settings app, then Bluetooth. The page should say “[Your Device] is visible to nearby devices while Bluetooth settings is open.”. Now on the Pi, issue this command:
scan on #You will see the MAC Addresses of nearby devices Discovery started [NEW] Controller FF:YY:ZZ:AA:BB:CC #Your Pi's MAC Address [NEW] Device DD:XX:FF:GG:HH:II #Your Android's MAC Address #To pair the phone, replace MAC Address with your own pair DD:XX:FF:GG:HH:II
You will see a 6 digit number show up on your android phone and also the Pi getting some information about it. Sort in Yes on the Pi to finish the pairing. Your android phone likely won’t associate on the grounds that the Pi is combined yet doesn’t have any administrations to deal with the association. You will see below:
[agent] Confirm passkey 111111 (yes/no): yes [CHG] Device DD:XX:FF:GG:HH:II Modalias: bluetooth:xxxx [CHG] Device DD:XX:FF:GG:HH:II UUIDs: &amp;lt;bunch of numbers&amp;gt; [CHG] Device DD:XX:FF:GG:HH:II Paired: Yes Pairing successful [CHG] Device DD:XX:FF:GG:HH:II Connected: No [CHG] Device DD:XX:FF:GG:HH:II Trusted: Yes
Step 2: Programming Raspberry Pi to send data
First we will write very simple program to turn LED ON and OFF by android phone. Here is the program that will do the trick. We are going to use Pin 40 for connecting the LED. I have used 1K resistor so that LED doesn’t get damaged. Here goes the code:
import bluetooth import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(40, GPIO.OUT) GPIO.setwarnings(False) server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) port = 1 server_socket.bind(("", port)) server_socket.listen(1) client_socket, address = server_socket.accept() print "Accepted connection from ", address try: while 1: data = client_socket.recv(1024) print "Received: %s" % data if (data == "0"): GPIO.output(40, 0) if (data == "1"): GPIO.output(40, 0) finally: print("Cleaning Up!") GPIO.cleanup() client_socket.close() server_socket.close()
The code is very easy to understand:
- We import the Bluetooth namespace by import bluetooth
- We initialise the socket and use the Bluetooth protocol RFCOMM. It is a simple set of transport protocols, made on top of the L2CAP protocol, providing emulated RS-232 serial ports (up to sixty simultaneous connections to a Bluetooth device at a time). We will bind and listen to Port 1server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = 1
- When the android phone is connected we get the address using this statement client_socket, address = server_socket.accept()
- After this in infinite loop we listen to the client and get the data by this statement. Here 1024 is the buffer size: data = client_socket.recv(1024)
- After this when “0” is received from phone, we turn off LED and when “1” is received we turn on LED connected on Pin 40.
Here is the working diagram:
Step 3: Testing using Android App
Here we are not going to build android app but we can use ready made app called Bluetooth Terminal. You can also use equivalent apps for connection. This was the easiest for me. Here is how you can test:
- Run above python program in Pi 3.
- Open Bluetooth Terminal App -> From the top menu click -> connect a device – Insecure
- Paired devices will appear – one of them will be raspberrypi (if you haven’t changed the name)
- Once you click raspberrypi, it will be connected to Pi 3 via Bluetooth. Now, you can send commands and click Send
- When 1 is sent, it will turn on LED and when 0 is sent it will turn off LED.
Now, we have basic stuff setup – we are going to connect the robot and create android mobile app.