Get Sensor Data From Arduino To Smartphone Via Bluetooth

Get Sensor Data From Arduino To Smartphone Via Bluetooth

73.928
Views
33 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 [email protected] My blog : riktronics.wordpress.com

view all posts by rik
Subscribe
Notify of
guest

33 Comments
Inline Feedbacks
View all comments
Eric

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…

praveen

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

email : e.praveenkumar17@gmail.com

enigmatico949

I not find the download link of source code.

mixos

Have you checked on the source website?

Alex Garcia Castro

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

Mohammad Fahad Bin Alam

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

Mallikarjun

i need a code regards sending temperature data from DHT11 sensor to a Mobile(nRF temp 2.0 for BLE app) using an arduino uno.
The code in this site doesnot work. please help me.

https://circuitdigest.com/microcontroller-projects/sending-sensor-data-to-android-phone-using-arduino-nrf24l01-over-bluetooth-ble

Dennis

Would this differ if the Raspberry Pi is running Android ?

pengo

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?

ikaroweb

Hi, the project works with Android Studio ? Thanks

RIK

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

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

ikaroweb

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

Chrisjohn Dela Cruz

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

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

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

shrey shah

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

Mel

How do I open the file on Android Studio?

imad_ed

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

Aleksander

Android studio 3.5.3
Works great !
Thank you very much !

Ola Dunk

How do you import the APP source into Android Studio ?

tejal salve

Only begin button is enabled..and all three buttons are disabled.. please help

pradeep kumara

sir, i sent an Email.i hope your kind response

Join 97,918 other subscribers

Archives