Connecting Android with Cloud Service Ubidots

Let’s face it, an old Android phone is hard to sale.

So, let’s make something useful from it. Let’s make a Bike Tracking System from an “old Android Phone” (Android 4 or higher).

Even older phones, 3-4 years old, normally have GPS and are by default connected to the Internet via SIM card. This will be enough for the basic project, the “Bike Tracking System”.

This is the goal

Getting information about the battery status and the position of the bike.

Ubidots Dashboard

UbidotDashboard

Parts are needed for the project:

  • Android phone with Android version 4 or higher
    IMG_20160618_094808
  • SIM card with a data plan
  • Free Ubidots account, a cloud service for things
  • Power bank is optional
  • A box that will hold the phone and eventually the power bank.
    IMG_20160618_100537

Costs for the project:

I pay about $4 for the data plan with 0.5 GB per month.

Ubidots Account

The first thing to do is, getting an Ubidots Account.

Ubidots is a Cloud Service for Internet of Thing. It supports a wide range of hardware like Arduino, Raspberry Pi by providing API’s. And against many other Cloud services for things, Ubidots also has an API for Android that let your Android App connect to the service and send and get information.

Go to http://ubidots.com/ and get yourself an Account.

If you want to read about other Cloud Services for Things, you may like the following post: http://emaker-studio.goprogramming.space/se/connect-your-iot-to-the-internet/

Downloading the Ubidots Tracking App or Source

On Ubidots Android Page you find a link to the Google Play Store where you can download the ready to use Tracking app. If you want to customize the App, you may want to download the Source Code from Github:

http://ubidots.com/docs/devices/android.html#android-sdk

For this example, we will use 2 Apps, theApp from Google Play for the Tracking part and the App for the Battery Level which is explained here (based on Ubidots docs).

Add Source

Create the Datasource by clicking the on the orange button located in the upper-right corner of the screen.

AddSource

Add Variable

Click on the created Datasource and then on “Add new variable”. As we are going to send our battery level, we will create a variable named batteryLevel.

AddVariable

Customize the icon for your Datasource if you want.

VariableBatteryLevel

API Credits

From the API Credits, you will need the API Key later in Android Studio

APICredit

Take note of the variable ID as we are going to need it later for our application to work. You will need it later in Android Studio.

BatteryLevel-ID

Dashboard Add Source

To your Dashboard add the Datasource for the Battery Level and the location you get from the Tracking App.

DashboardAddSource

Android App for the Battery Level

Open Android Studio and create a New Project with a Blank Activity.

NewAndroidStudioProject

Now, there are four steps to do:

  • Editing the AndroidManifest.xml
  • Editing the build.gradle (Module app)
  • Editing the MainActivity or whatever you name it.
  • Editing the activity_main.xml

Editing the AndroidManifest.xml

To allow Internet connections, add the following line to your AndroidManifest.xml:

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>

Editing the build.gradle (Module app)

Add to the dependencies section the 2 following lines:

compile ‘com.google.code.gson:gson:2.2.4’
compile ‘com.ubidots:ubidots-java:1.6.4’

Click on Sync Project with Gradle file to include the libraries.

Editing the MainActivity or whatever you named it.

Replace all the code from the MainActivity but not the first line, which contains your package information:

        import android.app.Activity;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.widget.TextView;

        import com.ubidots.ApiClient;
        import com.ubidots.Variable;

public class MainActivity extends Activity {
    private static final String BATTERY_LEVEL = "level";
    private TextView mBatteryLevel;
    private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int level = intent.getIntExtra(BATTERY_LEVEL, 0);

            mBatteryLevel.setText(Integer.toString(level) + "%");
            new ApiUbidots().execute(level);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBatteryLevel = (TextView) findViewById(R.id.batteryLevel);
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    protected void onStop() {
        unregisterReceiver(mBatteryReceiver);
        super.onStop();
    }

    public class ApiUbidots extends AsyncTask<Integer, Void, Void> {
        private final String API_KEY = "your API KEY";
        private final String VARIABLE_ID = "your battery level ID";

        @Override
        protected Void doInBackground(Integer... params) {
            ApiClient apiClient = new ApiClient(API_KEY);
            Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);

            batteryLevel.saveValue(params[0]);
            return null;
        }
    }
}

Copy your API Key in here:
private final String API_KEY = "your API KEY";

And copy the ID for your Battery Level Variable in here:
 private final String VARIABLE_ID = "your battery level ID";

Editing the activity_main.xml

Replace alle the code from the activity_main.xml with the following:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”.MyActivity”>
<TextView
android:id=”@+id/batteryLevel”
android:text=”XX%”
android:textSize=”50sp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>

Thats it!
Now you can execute your code and launch the app on your phone.

To see the Battery Level and the Location, we need to run both apps at the same time. If you like the general idea, you can go from here and make your customized version and put everything in One App.

For me it's time to pack everything together and take a bike tour for testing.

IMG_20160618_101023

 

IMG_20160618_101308

IMG_20160618_101324

Good Luck!

 

 

Tags: