Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Description:

Arduino Bluetooth controlling – This is a very detail tutorial about how to design and develop an android Application for controlling a 220v bulb using Arduino and Android Application.

For the best understanding and easy follow-up, I am going to controlling a 12v relay from the android cell phone app. In this tutorial I will add one toggle button in the app, toggle button will be used to turn on and off the bulb with a single button. Later after understanding the basics then you can add multiple toggle buttons and you can simply copy and paste the code the only difference will be in the ID, which you will see in the programming.

In this tutorial I am going to use a lot of pictures and I will try to explain everything, so this Tutorial can be a bit longer, but trust me if you follow all the steps and read this article, at the end you will be able to make your own Android app to control anything you want. Enough with the introduction, Let’s continue without any further delay!!!

The software’s used for Arduino Bluetooth controlling system are:

Android Studio

Arduino IDE


About the Android Studio:

Android studio is one of the most commonly used software used for android app development which includes designing and programming. Before you follow this tutorial you should have some basic know how to use the Android studio. Otherwise, It can be really confusing for you. My recommendation is first you should follow some basic tutorials and then you can resume from here.

Before we start the Android Toggle Button app development, first of all, I would like to explain the circuit diagram and Arduino programming. Because we can only design a good android application if we know exactly for what purpose we are making this application.

Amazon purchase links:

bluetooth module: https://amzn.to/352FLqX
Arduino Uno: https://amzn.to/39aq6ZT
Mega 2560: https://amzn.to/2SszMsI
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
330-ohm resistors pack: https://amzn.to/2Qj1Igg
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
Bread Board: https://amzn.to/2MxV5FM
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
wirecutter: https://amzn.to/2tV2lFj
PCB small portable drill machine: https://amzn.to/2MvQqnx

DISCLAIMER:

Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Arduino Bluetooth controlling  Circuit Diagram:

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

This is a very basic Bluetooth controlling  Circuit diagram designed in CadeSoft Eagle 9.1.0 version.

A bulb is connected with Pin number 13 of the Arduino through 12v relay. This is a 220v bulb and that’s why I connected this with 12v relay. It really doesn’t matter if you use a 2.5v BULB or a 5v bulb or even if you add a relay to control a 220 Vac bulb, it will have no effect on the programming.

On the left side, you can see a Bluetooth module, you can use the HC-05 or HC-06 Bluetooth Module. If you are using Bluetooth for the first time then you can watch my getting started tutorial on the Bluetooth module available in the related projects section. The Bluetooth module Tx and Rx pins are connected with the Arduino’s pin number 2 and pin number 3. Pin number and pin number 3 will be used as the Serial Port, which will be defined in the programming. While the VCC and GND are connected with the Arduino’s 5v and GND.


Arduino Bluetooth controlling Programming:

Arduino Bluetooth controlling Program Explanation:

As you know my friends in Arduino Uno we have only one Serial port which is available on pin number 0 and pin number 1. As I always say never use these pins with any Serial communication supported devices. The Arduino’s default Serial Port should only be used for debugging purposes. You can always define other Serial Ports using the SoftwareSerial library. So that’s the reason I added the SoftwareSerial.h header file.

I defined a Serial Port with the name Blue on pin number 2 and pin number 3 of the Arduino. The pin number is the Rx while Pin number 3 is the Tx.

On the third line, I defined a variable data of the type long int. This variable will be used to store the number which is sent from the Android cell phone.

the bulb is connected with pin number 13 of the Arduino.

Then I defined two variables password1 and password2 of the type long integer. The number 92 is used to turn on the bulb while 551is used to turn off the bulb. This bulb can be replaced with a relay, this way you will be able to control anything you want.

Then in the void setup function, I set the BULB as output and set it to off state using the digitalwrite function. low means off and High means ON.

To activate the serial communication I used the Serial.begin() function while 9600 is the baudrate and similarly for the Bluetooth module.  then starts the void loop function.

while(Blue.available()==0) ;

this line means that if the Bluetooth module has not received any data from the android cell phone then simply wait here.

if(Blue.available()>0)

this condition means if the Bluetooth module has received data from the android cell phone then store the received number in variable data and then using the if conditions the number stored in variable data is compared with the password1 and password2. If the number is equal to the password1 then the BULB is turned ON and if the number stored in data is equal to password2 then the bulb is turned OFF. So that’s all about the Arduino’s programming.

The best book for Android app designing and development:

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Download Android Design Patterns book: https://amzn.to/2OEgfC7

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


About Bluetooth:

The Android platform provides Bluetooth network stack support, which allows a phone to exchange data wirelessly with other Bluetooth devices. With Android Bluetooth APIs, the application framework provides access to the Bluetooth features. These APIs allow wireless connection of applications to other Bluetooth devices, allowing point-to-point and wireless multipoint features.

Using the Bluetooth APIs, the following can be achieved by an Android application:

  • Check for other phones with Bluetooth
  • For paired Bluetooth devices, ask for a local Bluetooth adapter
  • Establish channels for RFCOMM
  •  Link to other phones by finding the service
  • Data transfer from and to other phones
  • Manage multiple links

This page is dedicated to Classic Bluetooth. Classic Bluetooth is the right choice for more battery-intensive operations, including streaming and Android phone connectivity. For low-powered Bluetooth devices, Android 4.3 (API level 18) introduces Bluetooth Low Energy API support. See Bluetooth Low Energy for more information. The document defines different models for Bluetooth, including the Health System Profile. It then describes how to use the Android Bluetooth APIs to accomplish the four main tasks required for Bluetooth communication: setting up Bluetooth, identifying devices that are either paired or accessible in the local area, linking devices, and transferring data between devices.

The basic of Bluetooth

To allow Bluetooth devices to transmit data between themselves, they must first use a pairing method to form a communication channel. For incoming connection requests, one device, a discoverable device, makes itself available. Another device uses a service discovery process to find the discoverable device.

The two devices complete a bonding process where they exchange security keys after the discoverable device acknowledge the pairing request. These keys are cached for later use by the devices. The two devices exchange information after the pairing and bonding processes are complete. The device that initiated the pairing request will release the stream that connected it to the discoverable device when the session is complete. Nevertheless, the two devices remain connected, so they can automatically reconnect during a future session so long as they are within each other’s reach and the bond has not been broken by either system.

Bluetooth enabling permission

You must claim two permissions to use Bluetooth features in your software. BLUETOOTH is the first of these. To perform some Bluetooth communication, you need this permission, such as requesting a link, accepting a connection, and transferring data.

ACCESS FINE LOCATION is the other permission to be declared. Your device requires this permission because you can use a Bluetooth scan to collect information about the user’s location. This data can come from the user’s own phones, as well as from Bluetooth beacons used at places like shops and transit facilities.

You will declare the BLUETOOTH ADMIN permission in addition to the BLUETOOTH permission if you want your app to trigger network discovery or modify Bluetooth settings. Many applications only need this permission for local Bluetooth devices to be discovered. Unless the program is a “power manager” which modifies Bluetooth settings upon user request, the other features provided by this permission should not be used. Declare permission(s) for Bluetooth in your manifest application file. Don’t forget to add the Bluetooth permission in manifest.xml before you create the APK file.


Arduino Bluetooth controlling Application Designing and Development:

First off all, open the Android Studio.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Click on Start a new Android Studio project

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

While Choose your project form is open.

Select the empty activity and click on the Next button

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

After you click on the Next button, then configure your project form is opened, over here you set the project name, package name and you can select a directory where you want to store the application. Make sure the language is set to Java. So after you are done then you can click on the Finish button.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Wait for app synchronization

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

When your project is fully synchronized, as you can see the green dots in the following picture which is an indication that the synchronization is completed and we are ready for the next step.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Before starting app design there are two main classes which are PreferencesActivity.java and ActivityHelper.java

Now create a java class

Arduino Bluetooth controlling

Enter name as the ActivityHelper and click ok

Arduino Bluetooth controlling


And past below code

ActivityHelper.java code

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now create the second java class

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Enter the class name as the preferrencesActivity and click ok

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

PreferrencesActivity.java code


Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now we will make a layout for the Search and connect buttons. The search button will be used for searching the Bluetooth module and the connect button will be used to connect with the paired Bluetooth module.

For this click on the small arrow symbol given with the bluetoothlight and find the Res and click on the small arrow to expand it, under the res then click on the layout to expand it and click on the activity_main.xml this will open the design screen.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now change the layout form constraint layout to linearlayout

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

As you can see in the picture below.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now we make another linear layout within the main linearlayout

To create a button in XML we use the button attribute having some property that you can adjust as per your requirements.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

To check the design layout for this change from text mode to the design. As you can see at the end of the coding there are two button with captions Design and Text. You can click on the Design button. As you can see the mouse cursor.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

It’s a good programming practice to keep checking your desing layout as you continue to program. So now we are done with two buttons with captions search and connect. Now we will add the code for the paired device, for this, we use the listview attribute. Click on the Design button to check.Copy the below code in your activity_main.xml

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

activity_main.xml code

Now open the mainActivity.java class and paste the below code

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

MainActivity.java code



To remove  this error just click on the red bulb and select create id value resource And then click ok

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

So now you can see the list_item.xml is created

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open list_item.xml file and paste this code

Now we build the main controlling window, click on the package folder right now

Then New

Then Activity

And click on the Empty Activity

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open  activity_controlling.xml

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now change the layout form constraint layout to RelativeLayout

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open the activity controlling.xml and switch the text type to the design mode

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Before creating a toggle button first of all copy these two images in drawable folder which is giving below

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

right-click on drawable and click on the android resource file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

The resource file name is on_off and press ok and paste the below code

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now open activity_controlling.xml and paste the below code



Finally, open the Controlling.java file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

and paste the below in your Controlling.java file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

We are done with designing and programming. Now the next step is to create the APK file.

Creating Arduino Bluetooth controlling application .apk file:

Click on the Build menu then hover the mouse on build bundle/(apk) and click on the build apk

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

And then wait for around one minute depending on your laptop or computer processing speed.

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

After generating the apk file then click on locate and transfer the app-debug.apk to phone and install

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Download apk file: home automation app



Final Design of Home Automation Android Bluetooth App:

Arduino Bluetooth controlling

 

Related Project:

How to Create Android App for Arduino Sensor Monitoring over Bluetooth

Java Hello World First Program And Java Vm Error Fixing

Watch Video

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

30 Comments

  1. So, I hoped I had found a feasible app, but in the description there is a dozen of pitfalls.
    Any chance to get the Android Studio project? The APK doesn´t help, cuz I wanna adapt it.
    Thanks
    G.

          1. Thanks, Sorry for multiple comments.
            For some reason it looked like it wasn’t submitting, you can delete the other ones
            Thanks

      1. Thanks from your project dear its amazing .
        Can i have its android app source code?
        and need your help to sir g.

        Thanks

  2. Nice tutorial, thanks man. But I have a question. What’s the function of the ActivityHelper? Something about holding your phone in different positions?

  3. is this app work with only hc 05 blutooth module???? i change the UUID and try to connect with my laptop. but it’s showing as “Could not connect to device.Please turn on your Hardware” why is that???

    1. Supun first you check from mobile if it works means your connection is right then change uuid try from laptop

      1. Sir i’m still did not receive the code. Could you please send me both android and arduino codes???

          1. Got it sir. Thank you soo much. Actually do we need write both getOutputSteam method and getInputSteam method inside onCreate method?? i’ll try to obtain voice commands alternative to buttons. i’ll send a mail sir could you please check it and help me?????

Leave a Reply

%d bloggers like this: