Gauges for Arduino on Android

indicator

Sensor reading from Arduino can be read on your Android device with Bluetooth module. You can display this sensor data on the beautiful gauges. We need some software and hardware carpentry to pull this through. Here is the screenshot of the temperature reading measured using the Arduino UNO on the Samsung 10.2 tablet. You can have multiple sensors and display using gauges on Android device.

 

 

 

Things You Need

A JY-MCU bluetooth module

JY

A temperature sensor (TMP 35)

sensor

Arduino UNO

ArduinoUno_r2_front450px

Connections on Breadboard

sketch_bb2

 

Arduino Sketch

Open the Arduino and copy the following code into it.
Create a new sketch and copy the following code.

const int temperaturePin = A0; 

void setup()
{ 
Serial.begin(9600); 
} 

void loop() 
{ 
float voltage, degreesC ; 
char message; 

voltage = getVoltage(temperaturePin); 
degreesC = (voltage - 0.5) * 100.0; 

while (Serial.available() == 0); 
message = Serial.read(); 
if(message=='T'){ 
Serial.print(degreesC); 
} 
Serial.flush(); 
} 

float getVoltage(int pin) 
{  
return (analogRead(pin) * 0.004882814); 
}

Explanation of the above code is like this.

For JY-MCU Bluetooth module, we need to setup the baud rate of 9600. Remainder of the code is setting up the temperature sensor on A0 pin and reading voltage. Voltage is further converted to the temperature in degree Celsius. When message is received from the Android device, android sends the temperature value over the serial.

When a message to read temperature is received from the Android device, Arduino sends the temperature value over the serial port to Android. Connect Arduino to your machine and upload the sketch to it.

Android Setup
We will be using Eclipse and Windows platform to create the app for your Android device. Before getting started become familiar with “Hello World” tutorial of the Android. This tutorial will also help to install Eclipse, Android device driver and Android installation on your Windows machine.

For Bluetooth communication between Android devices Arduino we will use tBlue library from Arduino Bots and Gadgets book from Make. TBlue library is available from here.

Connecting to Arduino from Android

With tBlue library connecting from android is very easy with command,

tBlue=new TBlue("XX:XX:XX:XX:XX:XX");

where Bluetooth address of the Bluetooth module is provided in the quotes. Easiest way to get MAC address of your Bluetooth module is to power up Arduino, enable Bluetooth mode from Android and connect to it with default password provided by manufacturer. Install the BluetoothScanner app from Google play store on Android to know the MAC address of the paired Bluetooth module.

Once Bluetooth module on Arduino is connected to the Android device, reading the data with tBlue is very easy with this command.

String s = tBlue.read();

Sensor data received in the string is further converted to a number and displayed in the gauge with the GaugeView library. Sending data to Arduino over Bluetooth can be done with command

 tBlue.write();

 

GaugeView Library for Android
Download the GaugeView Library with demo project from Github. In next step we will customize this project.

Customizing GaugeView Demo Project in Eclipse
Open Eclipse and go to File > Import > Android > Existing Android Code into Workspace option. Click Next and select GaugeView Demo project as root directory.

Open the src folder in GaugeView Library, copy GaugeView.java file and paste into src.org.codeandmagic.android.gauge.demo folder of the demo project.

Correct the package name in the library as,

package org.codeandmagic.android.gauge.demo;

Create a new class TBlue.java and paste code from tBlue library from here. Expand res folder and copy the images from drawable folder of the GaugeView library to drawable folder of demo project. Similarly copy the arrays.xml and attrs.xml files containing color ranges to values folder from the GaugeView Library.

 

Modify the Default GUI in GaugeView Library
Open the activity_main.xml file in layout folder and add a button tag in layout file to measure the temperature form Arduino. Paste the following lines inside the button.

<Button
android:id=”@+id/button1″
style=”android:attr/buttonStyleSmall
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:onClick=”onButtonMeasure”
android:text=”Measure” />

Also write to create a text box to indicate the message when Bluetooth module is connected to Android.

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Small Text”
android:textAppearance=”?android:attr/textAppearanceSmall” />

Also change the unit in layout file to the desired one by using a string variable.

gauge:textUnit="@string/tempUnit" 

Here we used degree Celsius for temperature. To do this add a string variable in string.xml file values folder.

<string name=”tempUnit”><sup><small>o</small></sup>C</string>

Modifying the GauageView Demo Project
Open the MainActivity.java and import the additional packages for textView and View

import android.widget.TextView;
import android.view.View;

Also add the following variables

TextView messagesTv;  
TBlue tBlue;  

In the oncreate method, provide the id of the gauge1, gauge2 and text message viewer same as your layout file. Also provide the id of the Bluetooth module and exception handing code which prints messages in LogCat window of Eclipse.

mGaugeView1 = (GaugeView) findViewById(R.id.gauge_view1);
mGaugeView2 = (GaugeView) findViewById(R.id.gauge_view2);
messagesTv =  (TextView)findViewById(R.id.textView1);  

tBlue = new TBlue("xx:xx:xx:xx:xx:xx");  
		if (tBlue.streaming()) {  
            messagesTv.setText("Sensor Connected successfully! ");  
        } else {  
            messagesTv.setText("Error: Sensor Failed to connect. ");  
        } 
		tBlue.write("T");  

Also remove the timer code

mTimer.start();

Permissions for Bluetooth Communication
androidmanifes

Open the AndroidManifest.xml file and add the following two lines to allow Bluetooth communication as highlighted in the screenshot.

<uses-permissionandroid:name="android.permission.BLUETOOTH"/>
"android.permission.BLUETOOTH_ADMIN"/>

 

Running the Android App

Follow these steps to install and run

  • Power up the Arduino with Bluetooth module and also enable Bluetooth on your Android device
  • Connect to Bluetooth module from Android by entering the password provided by the Bluetooth module manufacturer
  • Connect your Android device to Windows machine
  • Go to Run > Run configuration > Target tab to run the app on connection device instead of Dalvik Virtual Machine
  • Run the project to install and launch the application on the Android
  • Click on measure button to display the temperature in the room on gauge

Get completed project from here.  

Starter Kit for Arduino

Which kit to start with Arduino ?

When searching for Arduino, I noticed that market is flooded with starter kits and it takes quite some time to find the good starter kit.

Ultimate Starter Kit from Vilros

I finally settled for the Ultimate Starter Kit from Vilros and sold on Amazon. This kit is a good value and comes with a very good tutorial book from the Sparkfun. In less than a day, I was able to do all tutorials covering LEDs, speakers, photo-diode and music. Except motors everything worked fine in this kit. Still it is a flimsy kit and Arduino UNO board does fit correctly in the plastic fixture provided with it.

Fritzing Creator Kit
This is an ultimate kit which I suggested to a Friend who bought it and used it for his kids. This kit comes with storage box and it is a great kit to start with Arduino. It does comes with book and contains even more items such as paper models of dinosaurs and robot. It is more fun and systematic kit especially for kids. Get it from Seedstudio website. Here is a video which shows the kit.