In this post I will discuss the Home automation system based on the Raspberry Pi using the Bluetooth protocol and I will use the Python IDLE to write the code for this Wireless Home Automation System. This post will discuss all the requirements needed to make the Bluetooth Home Automation System based on the Raspberry Pi. As described in the earlier posts Raspberry Pi is popular among IoT applications and we can use its built in Bluetooth to perform various IoT based tasks.
Home Automation Using Raspberry Pi via Bluetooth
After reading this post you will be able to install all the Packages needed to be able to talk to the Raspberry Pi via Bluetooth technology using the Python language. You will also understand the circuit used to control the AC home appliances.
- Raspberry Pi.
- Relay module.
- Smart phone with app (described later in the post).
- A bulb.
Raspberry Pi Bluetooth:
The Raspberry Pi has a built in Bluetooth which makes it very handy and easy to use in our wireless applications. But like many other peripherals on the Raspberry Pi Bluetooth also is disabled by default. So we need to install some packages to be able to manipulate the Bluetooth in the Raspberry Pi. The Python library for Bluetooth wireless communication also needs some libraries to install in the Raspberry Pi, so you can write the code for Bluetooth in the Python IDLE. If you have all the required files for the Bluetooth wireless communication between the Raspberry Pi and other Bluetooth devices you need to follow some instructions to be able to pair up with the Bluetooth devices. The method of installation of all the Bluetooth files for the Raspberry Pi will be discussed in detail in the sections to follow.
Raspberry Pi Python GPIO library:
Along with the required files for the Bluetooth we will also need the GPIO python library to be able to manipulate the General Purpose Input / Output pins of the Raspberry Pi as we will need to actuate the relay to control the AC appliances of the home automation system. Thus to be able the write the code for Home Automation System we will need to install the Python GPIO library for the Raspberry Pi. Fortunately latest Raspbian versions have already installed GPIO python library. If you do not have the library this post will guide to install the Python GPIO library for the Raspberry Pi.
Installing the Requisite Files for Home Automation System based on Raspberry Pi for Bluetooth and GPIO:
As noted earlier we will need two important types of requisites to build the Home Automation System.
Installing the Bluetooth files:
Let us now know what are the important files required for Bluetooth wireless communication and how to install them.
First of all open the terminal window of the Raspberry Pi.
Now type the following command in the terminal window:
sudo apt-get install Bluetooth blueman bluez
Here we have installed “blueman” and “bluez” by typing the above command. Bluez is the Linux Bluetooth protocol stack which supports all the core Bluetooth protocols and Blueman provides the Graphical Interfaces for interacting and managing the available Bluetooth devices.
These are the files for the Bluetooth wireless communication protocol now let us install the Python library for Bluetooth wireless communication.
Type the following command in the terminal window of the Raspberry Pi:
sudo apt-get install python-bluetooth
This will install the required files for supporting code writing in the Python IDLE.
Now after Bluetooth install the Python GPIO library.
sudo apt-get install python-rpi.gpio
Now we have installed all the required files for the Bluetooth based Home Automation System.
Raspberry Pi Bluetooth Devices Pairing:
Now you have installed all the libraries and packages it is now time to connect to your smart phone with Raspberry Pi via Bluetooth. This step is also quite simple.
Type the following command in the terminal window:
Now type the following command:
After the execution of this statement you will see the MAC addresses of all the available devices near you Raspberry Pi.
Type the following command in the terminal with the MAC address of your Mobile phone in the parenthesis like below:
pair <address of your Bluetooth device come here>
After the execution of this statement you will be asked for the passcode, write your passcode and then write the same passcode in your mobile and you will have pairing between the Raspberry Pi and your Smart Phone via Bluetooth communication.
Now let us discuss the circuit configuration for the Home Automation System:
Let us discuss the circuit configuration needed for the Home Automation based on the Raspberry Pi. The AC Home appliance to be controlled remotely is connected to the relay and the relay with a proper circuitry will be connected to the Raspberry Pi proper in sense that as we know the Raspberry Pi is unable to give as much current as required to drive the relay so we need a circuit for this power to on off the relay which in turn on and off the home appliances. The whole circuit will be discussed in detail in the following discussion. The Raspberry Pi GPIO pin is connected to the Relay Board which is connected to the Home appliance. We will make the GPIO high and LOW remotely with smart phone over Bluetooth to control the switch. The block diagram of the project is shown below:
The detailed circuit diagram is as shown in the following figure.
Notice that the GPIO pins number 21 is connected to the Relay module. The state of this pin will control state of the AC home appliance that is connected.
Raspberry Pi Python Bluetooth Code:
Now let us begin writing the code Raspberry Pi based home automation system based on Bluetooth technology.
Open the Python IDLE in the Raspberry Pi.
You can copy and paste the following code in the Python IDLE.
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
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"):
Notice the Python libraries we have imported in order to write this. So the installation of these libraries is now clear. The GPIO pin number 21 is used in this code as can be seen in the code above.
Raspberry Pi Home Automation Mobile App:
This is the most important part in the whole project. You need to download and install that mobile application which supports Bluetooth wireless communication using the RFCOMM protocol. There are a number of mobile applications available online. Here I am using the BlueTerm mobile application.
That is all for now I hope this post would be helpful for you. In the next post I will come up with more interesting applications of the Raspberry Pi. till then stay connected, keep reading and enjoy learning.