Get Sensor Data From Arduino To Smartphone Via Bluetooth

Get Sensor Data From Arduino To Smartphone Via Bluetooth

38751
Views
29 Comments

Hariharan Mathavan at allaboutcircuits.com designed a project on using Bluetooth to communicate with an Arduino. Bluetooth is one of the most popular wireless communication technologies because of its low power consumption, low cost and a light stack but provides a good range. In this project, data from a DHT-11 sensor is collected by an Arduino and then transmitted to a smartphone via Bluetooth.

Required Parts

  • An Arduino. Any model can be used, but all code and schematics in this article will be for the Uno.
  • An Android Smartphone that has Bluetooth.
  • HC-05 Bluetooth Module
  • Android Studio (To develop the required Android app)
  • USB cable for programming and powering the Arduino
  • DHT-11 temperature and humidity sensor

Connecting The Bluetooth Module

To use the HC-05 Bluetooth module, simply connect the VCC to the 5V output on the Arduino, GND to Ground, RX to TX pin of the Arduino, and TX to RX pin of the Arduino. If the module is being used for the first time, you’ll want to change the name, passcode etc. To do this the module should be set to command mode. Connect the Key pin to any pin on the Arduino and set it to high to allow the module to be programmed.

Circuit to connect HC-05 with Arduino

To program the module, a set of commands known as AT commands are used. Here are some of them:

AT Check connection status.
AT+NAME =”ModuleName” Set a name for the device
AT+ADDR Check MAC Address
AT+UART Check Baudrate
AT+UART=”9600″ Sets Baudrate to 9600
AT+PSWD Check Default Passcode
AT+PSWD=”1234″ Sets Passcode to 1234

The Arduino code to send data using Bluetooth module:

//If youre not using a BTBee connect set the pin connected to the KEY pin high
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4,5); 
void setup() {
 String setName = String("AT+NAME=MyBTBee\r\n"); //Setting name as 'MyBTBee'
 Serial.begin(9600);
 BTSerial.begin(38400);
 BTSerial.print("AT\r\n"); //Check Status
 delay(500);
 while (BTSerial.available()) {
 Serial.write(BTSerial.read());
 }
 BTSerial.print(setName); //Send Command to change the name
 delay(500);
 while (BTSerial.available()) {
 Serial.write(BTSerial.read());
 }}
void loop() {}

Connecting The DHT-11 Sensor

To use the DHT-11, the DHT library by Adafruit is used. Go here to download the library. When the letter “t” is received, the temperature, humidity, and heat index will be transmitted back via Bluetooth.

circuit to connect DHT-11 with Arduino
circuit to connect DHT-11 with Arduino

The code used to read data from the DHT sensor, process it and send it via Bluetooth:

#include "DHT.h"
#define DHTPIN 2 
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
void setup() {
 Serial.begin(9600);
 dht.begin();}

void loop()
{ char c; 
if(Serial.available()) 
 { 
 c = Serial.read(); 
 if(c=='t')
 readSensor();
 }}
void readSensor() {
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 if (isnan(h) || isnan(t)) {
 Serial.println("Failed to read from DHT sensor!");
 return;
 }
 float hic = dht.computeHeatIndex(t, h, false);
 Serial.print("Humidity: ");
 Serial.print(h);
 Serial.print(" %\t");
 Serial.print("Temperature: ");
 Serial.print(t);
 Serial.print(" *C ");
 Serial.print("Heat index: ");
 Serial.print(hic);
 Serial.print(" *C ");
}

Developing The Android App

The flow diagram of the Android app is illustrated below,

Flow diagram of the Android app
Flow diagram of the Android app

As this app will be using the onboard Bluetooth adapter, it will have to be mentioned in the Manifest.

uses-permission android:name="android.permission.BLUETOOTH"

Use the following code to test if Bluetooth adapter is present or not,

BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}

The following part of the code deals with reading the data,

int byteCount = inputStream.available();
 if(byteCount > 0)
 {
 byte[] rawBytes = new byte[byteCount];
 inputStream.read(rawBytes);
 final String string=new String(rawBytes,"UTF-8");
 handler.post(new Runnable() {
 public void run()
 {
 textView.append(string);
 }
 });
 }

To send data, pass the String to the OutputStream.

outputStream.write(string.getBytes());

The complete source code of the Android application is attached here:  Arduino Bluetooth(Source)

Testing

Power up the Arduino and turn on the Bluetooth from your mobile. Pair with the HC-05 module by providing the correct passcode – 0000 is the default one. Now, when “t” is sent to the Arduino, it replies with the Temperature, Humidity, and Heat Index.

the application screen
the application screen

Myself Rik and I am founder of Riktronics. I study Electronics and Communication Engineering in IIE. My hobby is playing with electronics and making various projects, mainly about embedded systems. Love to do coding, and making tutorials about electronics/programming. Contact me in any need at abhra0897@gmail.com My blog : riktronics.wordpress.com

view all posts by rik

29 Responses

  • Eric
    4 February, 2018 at 5:09 pm

    Had to join to get the android code but after joining, no code was available – error 404.
    Good way to get people to enter their information but not a good way to make friends…

    • Rik
      4 February, 2018 at 5:18 pm

      Hi Eric,
      I am sorry for the problem you have faced. After reading your comment I have checked the link and yes, it’s not there anymore.
      That’s something we can’t do anything about as the tutorial was taken (and simplified) from allaboutcircuits.com.
      If they delete/move the content from their server, we can do nothing.
      I’ll try my best to find a working alternative code and send you through email.

      Thanks for your interest in electronics-lab.com.

    • Rik
      7 March, 2018 at 11:15 am

      Hey there. Here is the working source code along with the apk: https://www.electronics-lab.com/wp-content/uploads/2017/10/ArduinoBluetoothSource.zip

  • praveen
    26 February, 2018 at 1:15 pm

    Hey, i cant find the code ! Could you please mail the code

    email : e.praveenkumar17@gmail.com

  • 7 March, 2018 at 5:38 am

    I not find the download link of source code.

  • 7 May, 2018 at 2:15 pm

    Hi, in the android APP I press the Begin button but nothing happens…

  • Mohammad Fahad Bin Alam
    8 June, 2018 at 12:42 pm

    Hey, How can I do the same thing with Raspberry Pi 3 with built-in bluetooth?

  • pengo
    23 August, 2018 at 10:05 am

    Getting an error while compiling: “Arduino\libraries\DHT-sensor-library-master\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory”, Any idea’s to solve this?

    • Rik
      26 October, 2018 at 4:41 pm

      Download Adafruit Sensor library from library manager of Arduino IDE. Also, you can go to https://github.com/adafruit/Adafruit_Sensor and download the files and add them in the libraries folder under the Arduino IDE installation directory.

  • ikaroweb
    18 October, 2018 at 7:42 pm

    Hi, the project works with Android Studio ? Thanks

    • RIK
      26 October, 2018 at 4:35 pm

      You can design the android application on Android studio if you want. The code for the Arduino should be compiled with Arduino IDE or any other alternative like platformio.

      • ikaroweb
        26 October, 2018 at 4:38 pm

        Yes i know’ but i was talking about the android app 🙂

        • Rik
          26 October, 2018 at 4:52 pm

          Yes, you can go for Android Studio without any problem. 🙂

          • ikaroweb
            29 October, 2018 at 1:04 pm

            Hi,
            I’ve opened the project on Android Studio but i’ve this error: Plugin with id ‘com.android.application’ not found.
            Thanks

  • 29 December, 2018 at 6:09 pm

    when i change textView.append to textView.setText the output value is giving 2 lines and the only showing message to the mobile is line 2

  • gaurav singh gusain
    6 February, 2019 at 3:54 pm

    Hey i am controlling microcontroller via android bluetooth i want to know to the message structure of controlling the microcontroller.i want to control dc and servo motor. thankw

  • Haris
    24 May, 2019 at 11:30 pm

    Hi sir, App is not work . Its Send and stop button disable kindly help me plzz

  • shrey shah
    8 June, 2019 at 2:27 pm

    hi, I’m getting data perfectly , but i m not able to send the data from one activity to another !! What are the ways by which we can do??
    thank you

    • Rik
      11 June, 2019 at 5:54 pm

      Hi Shrey, can you please explain what do you mean by sending data from one activity to another? Exactly what are you trying to accomplish?

  • Mel
    12 June, 2019 at 6:29 am

    How do I open the file on Android Studio?

  • imad_ed
    19 June, 2019 at 3:34 pm

    Hi, Please i had a problem .. when i sent “t” i didn’t get any information (temperature &humidity)
    despite of that the andriod app no error have noticed … and the Arduino code is working very well it send to the serial monitor ..
    please i need your help in the recieving fonction of the andriod App

    thank you so much

Leave a Reply

Archives