Raspberry Pi based Smart Phone Controlled Home Automation

Published  September 7, 2017   9
J Jayant
Raspberry Pi based Smart Phone Controlled Home Automation

Raspberry Pi is very popular for IoT projects because of its seamless ability of wireless communication over internet. Raspberry Pi 3 has inbuilt Wi-Fi and Bluetooth, and Bluetooth is a very popular wireless communication Protocol. So today we are going to Control Home Appliances through Smart Phone using Raspberry Pi 3 and Bluetooth.

Here we are using Raspberry Pi 3 which have inbuilt Bluetooth, so we don't need to use any external USB Bluetooth dongle. Apart from that we only need Relay Modules for this Wireless Home Automation Project. Here we are using RFCOMM Bluetooth protocol for wireless communication.

Programming for Bluetooth in Python follows the socket programming model and communications between the Bluetooth devices is done through RFCOMM socket. RFCOMM (Radio Frequency Communication) is a Bluetooth Protocol which provided emulated RS-232 serial ports and also called as Serial Port Emulation. Bluetooth serial port profile is based on this protocol. RFCOMM is very popular in Bluetooth applications because of its wide support and publically available API. It is bound to L2CAP protocol.

If you have Raspberry Pi 2, then you either need to use external Bluetooth dongle or Bluetooth module HC-06. Check our previous projects for using these external Bluetooth devices: Controlling Raspberry Pi GPIO using Android App over Bluetooth and Voice controlled LEDs using Raspberry Pi. Also check our previous Raspberry Pi Projects along with some good IoT Projects.


Installing Required Packages for Bluetooth Communication:

Before start, we need to install some softwares for setting up Bluetooth communication in Raspberry Pi. You should have a Raspbian Jessie installed memory card ready with Raspberry Pi. Check this article to install the Raspbian OS and getting started with Raspberry Pi. So now we first need to update the Raspbian using below commands:

sudo apt-get update
sudo apt-get upgrade


Then we need to install few Bluetooth related packages:

sudo apt-get install bluetooth blueman bluez

Then reboot the Raspberry Pi:

sudo reboot

BlueZ is a open source project and official Linux Bluetooth protocol stack. It supports all the core Bluetooth protocols and now become part of official Linux Kernel.

Blueman provides the Desktop interface to manage and control the Bluetooth devices.


Finally we need python Library for Bluetooth communication so that we can send and receive data through RFCOMM using Python language:

sudo apt-get install python-bluetooth


Also install the GPIO support libraries for Raspberry Pi:

sudo apt-get install python-rpi.gpio


Now we are done with installing required packages for Bluetooth communication in Raspberry Pi.


Pairing Devices with Raspberry Pi over Bluetooth:

Pairing Bluetooth Devices, like mobile phone, with Raspberry Pi is very easy. Here we have paired our Android Smart phone with Raspberry Pi. We have previously installed BlueZ in Pi, which provides a command line utility called “bluetoothctl” to manage our Bluetooth devices.


Now open the bluetoothctl utility by below command:

sudo bluetoothctl

You can check all the commands of bluetoothctl utility by typing ‘help’. For now we need to enter below commands in given order:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# scan on

After the last command “scan on”, you will see your Bluetooth device (Mobile phone) in the list. Make sure that your mobile has Bluetooth turned on and visible by nearby devices. Then copy the MAC address of you device and pair it by using given command:

pair <address of your phone>

Then you will be prompted for Passcode or Pin in your Terminal console then type passcode there and press enter. Then type the same passcode in your mobile phone when prompted and you are now successfully paired with Raspberry Pi. We have also explained this whole process in the Video given in previous GPIO controlling Tutorial. Here is the direct YouTube link.



As told earlier, you can also use Desktop interface to pair the Mobile phone. After installing Blueman, you will see a Bluetooth icon in right side of your Raspberry Pi desktop as shown below, using which you can easily do the pairing.



Circuit Diagram:

Circuit diagram for this Raspberry Pi based Bluetooth Controlled Home Automation is very simple, we just connected Relay Module's input signal Pin to PIN 40 (GPIO 21) of Raspberry Pi and other two Pin (Vcc and GND of relay module) to Pin 2 and 6 of Raspberry Pi 3. Then we have connected a AC CFL bulb to the Relay as shown in the circuit diagram:

raspberry pi based smart phone controlled home automation circuit

If you are new to Relay and want to learn more about Relay and its connections with AC appliance, check this Article.

6v Relay Module


Controlling AC Appliance with Android App BlueTerm:

Now after paring the Mobile Phone, we need to install a Android App for communicating with Raspberry Pi using a Bluetooth Serial Adapter, so that we can control the GPIO pins of Raspberry Pi. As told earlier RFCOMM/SPP protocol emulates serial communication over Bluetooth, so we installed here BlueTerm App which supports this protocol.


You can also use any other Bluetooth Terminal App which supports communication via RFCOMM socket.

Now after downloading and installing the BlueTerm App, run the below given Python Program from the terminal and connect the paired raspberrypi device from the BlueTerm App at the same time.


After successful connection you will see connected:raspberrypi at the top right corner of the App as shown below:

controlling AC bulb with raspberry-pi GPIO pin using bluetooth BlueTerm-android-app-for-controlling-raspberry-pi-GPIO                

Now you can just enter ‘1’ or ‘0’ from the BlueTerm app to make the GPIO pin HIGH and LOW respectively, which in turns triggers the Relay module connected to this pin, which further controls the AC bulb connected to Relay. Press ‘q’ to exit the program. You can use Google Voice Typing Keyboard to control the GPIO using your Voice. Check the complete demo in the Video given at the end.

raspberry pi based bluetooth controlled home automation circuit

So this is how you can wirelessly control the AC Appliance using an Android App over Bluetooth. Also check How to use Bluetooth with Arduino.


Programming Explanation:

Python Program for Controlling Raspberry Pi GPIO with Android App is very simple and self-explanatory. Only we need to learn little bit about the code related to Bluetooth RFCOMM communication. First we need to import the Bluetooth socket library which enables us to control Bluetooth with Python language; we have installed the library for the same in the previous section.

import Bluetooth


Below is the code responsible for Bluetooth communication:

server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
client_socket,address = server_socket.accept()
print "Accepted connection from ",address
while 1:
 data = client_socket.recv(1024)

Here we can understand them line by line:

server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM ): Creating socket for Bluetooth RFCOMM communication.

server_socket.bind(("", port):- Server binds the script on host '' to port.

server_socket.listen(1): Server listens to accept one connection at a time.

client_socket, address = server_socket.accept(): Server accepts client’s connection request and assign the mac address to the variable address, client_socket is the client’s socket

data = client_socket.recv(1024):  Receive data through the client socket client_socket and assign it to the variable data. Maximum 1024 characters can be received at a time.


Finally after all the programming, close the client and server connection using below code:



All the other code is easy and self-explanatory. Check the full code below. Try to modify this project and you can use it to control many other things wirelessly, Robot car through android phone or can use your voice to control the lights.

Also check our many types of Home Automations Projects using different technologies and Microcontrollers like:


import bluetooth
import RPi.GPIO as GPIO   #calling for header file which helps in using GPIOs of PI
GPIO.setmode(GPIO.BCM)    #programming the GPIO by BCM pin numbers. (like PIN40 as GPIO21)
GPIO.setup(BULB,GPIO.OUT)  #initialize GPIO21 (Relay connected at this pin) as an output Pin
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
client_socket,address = server_socket.accept()
print "Accepted connection from ",address
while 1:
 data = client_socket.recv(1024)
 print "Received: %s" % data
 if (data == "0"):  #if '0' is sent from the Android App, turn OFF the CFL bulb
  print ("AC light OFF")
 if (data == "1"):  #if '1' is sent from the Android App, turn OFF the CFL bulb
  print ("AC light ON")
 if (data == "q"):
  print ("Quit")


Have any question realated to this Article?

Ask Our Community Members


Submitted by sarathchandra on Thu, 02/01/2018 - 11:49


we are getting the output like this and the if loop is not executing
Accepted connection from ('D8:3C:69:4C:A8:B9', 1)

Submitted by Antonio Alvarado on Wed, 03/28/2018 - 00:19


How does one install BlueTerm on a iPhone 5 (non-Android device). If it cannot be done - then how can I connect my iPhone 5 to the above project?

Submitted by Ami on Wed, 05/23/2018 - 14:07


i use a 5v relay, and it doesn't work. But the code is working. What should i do sir?
May you answer my question bcs i use this for experiment in my class

Dear sir I have installed bluetooth blueman bluez, and also run the commonds  "sudo apt-get install python-bluetooth" and "sudo apt-get install python-rpi.gpio". After this I paired my phone with RPi. Now running the given python code but the error is occuring that "no module name 'bluetooth' ". Please guide me why its and how to resolve it.


hello everybody i'm using Raspberry pi 3B in built bluetooth & wifi with raspbian stretc i use this script but not work error show.

>>> %Run 222.py
Traceback (most recent call last):
  File "/home/pi/Desktop/222.py", line 1, in <module>
    import bluetooth
  File "/usr/lib/python3/dist-packages/thonny/backend.py", line 290, in _custom_import
    module = self._original_import(*args, **kw)
ImportError: No module named 'bluetooth'

python version: Python 2.7.13 (default, Sep 26 2018, 18:42:22)  [GCC 6.3.0 20170516] on linux2

please help me what's happen whats's problem 

thanks in advace