Arduino

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Description:

Arduino Bluetooth controlling – This is a very detailed 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.


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 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 Arduino’s programming.


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.

for android app development visit my next article Arduino Bluetooth controlling application development using android studio

Related Project:

https://www.electroniclinic.com/how-to-create-android-app-for-arduino-sensor-monitoring-over-bluetooth/

Sqlite Database in Android Studio Creating CRUD Application

Watch Video

 

Fawad

Shahzada Fawad

Related Articles

59 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?????

  4. Hello, Fawad. Could you, please, send the android studio project to me. meleknaz1136gmail.com. Thank you so much!

  5. Assalam u alaikum sir,
    I have run your code in android studio and after removing all the errors and other issues it work completely fine with HC06 module…Thanks for that…!!!
    I have tried this app on esp32 also but it does not send alphabetical strings like a, b, c, etc. But it does send numbered strings and only the first number like if I have a string “72” it will only show 7 in esp32…And also if I have a string “7” it will not show in esp32…Can you help in this Issue???
    And also I want to do something with the disconnect button, When I press disconnect button it will disconnect the esp32 and if I want to connect again, it will connect without any hardware restart…
    I have only these two issues left for my App…
    Can you help me in these Issues…
    Regards

Leave a Reply

Back to top button
%d bloggers like this: