In the Raspberry Pi Bluetooth tutorial, we are going to control the GPIO pins of Raspberry Pi through the Android App over the bluetooth. I am going to use Raspberry Pi 3 because it have inbuilt bluetooth and you won’t need an external bluetooth device.
We are going to use the RFCOMM Bluetooth protocol for the communication between the Raspberry Pi and the Smart Phone. The RFCOMM (Radio Frequency Communication) bluetooth protocol is made on top of the L2CAP (Logical link control and adaption protocol) protocol, Provides emulated RS-232 serial ports and is also known as the serial port emulation. This protocol is widely used because it is simple to use and also because it uses the serial port to communicate with other devices.
Before going further, install the BlueTerm app from the play store in your smart phone.
Installing Required Packages
First you need to update your Raspberry Pi. To update, type the below commands
sudo apt-get update sudo apt-get upgrade
Then you need to install the packages related to Bluetooth. Blueman provides you the desktop interface to manage and control Bluetooth devices. Bluez is the official Bluetooth protocol stack and it supports all the core Bluetooth protocols.
sudo apt-get install bluetooth blueman bluez
Then install the python library for Bluetooth communication.
sudo apt-get install python-bluetooth
In the end, reboot your Raspberry Pi.
Pairing the Device
- From the menu, go to preference and click on Bluetooth Manager. A new window will open up.
- Then from the Bluetooth options on your mobile, make your device visible.
- In the Bluetooth devices window, click on search. It will show all the devices in the range.
- Then right click on the device you want to connect and click on pair. It will ask for the permission on both the devices (Raspberry Pi and Smart Phone). Select yes on both sides.
Now your devices are paired and it’s time to make them communicate with each other.
Connect the LED with the raspberry pi between the GPIO 21 and ground through the 220 ohm resistor. The positive of LED (longer leg of LED) should be connected to the GPIO 21 and connect the negative of led to the 220 ohm resistor and then connect the other end of resistor to the ground.
The python code is as follows
# Importing the Bluetooth Socket library import bluetooth # Importing the GPIO library to use the GPIO pins of Raspberry pi import RPi.GPIO as GPIO led_pin = 21 # Initializing pin 40 for led GPIO.setmode(GPIO.BCM) # Using BCM numbering GPIO.setup(led_pin, GPIO.OUT) # Declaring the pin 40 as output pin host = "" port = 1 # Raspberry Pi uses port 1 for Bluetooth Communication # Creaitng Socket Bluetooth RFCOMM communication server = bluetooth.BluetoothSocket(bluetooth.RFCOMM) print('Bluetooth Socket Created') try: server.bind((host, port)) print("Bluetooth Binding Completed") except: print("Bluetooth Binding Failed") server.listen(1) # One connection at a time # Server accepts the clients request and assigns a mac address. client, address = server.accept() print("Connected To", address) print("Client:", client) try: while True: # Receivng the data. data = client.recv(1024) # 1024 is the buffer size. print(data) if data == "1": GPIO.output(led_pin, True) send_data = "Light On " elif data == "0": GPIO.output(led_pin, False) send_data = "Light Off " else: send_data = "Type 1 or 0 " # Sending the data. client.send(send_data) except: # Making all the output pins LOW GPIO.cleanup() # Closing the client and server connection client.close() server.close()
After running this code in the Raspberry Pi, open the BlueTerm App, it will show you that no device is connected.
Now touch the option button and connect with the Raspberry Pi.
After connecting, if you will type ‘1’ or ‘0’ from the smart phone then the led on the Raspberry Pi will light up or light down.
If you have any questions, feel free to ask in the comment section.