Background
This article describes how to use REST to extract data from an Automatic® OBD-II bluetooth adapter and its accompanying app, and subsequently send it to the Artik Cloud.
Prerequisites
You must have basic knowledge about the following:
Java programming language
Automatic® mobile app
Artik Cloud (Samsung) Account
GitHub repository
Android Emulator or actual Android Device
We won’t go through a detailed step-by-step tutorial for setting up Artik Cloud, but we’ll give you some nice pointers. If you are not familiar with Artik Cloud, please review this article with video tutorial for instructions.
Setting up an Account and Manifest in the Automatic® Cloud
The first step is creating device type (manifest) for Automatic® at developer dashboard. For our example, we took several values from a few Automatic® API Objects: “Trip”, “VehicleMILHistory”, and “Vehicle”. We used Aritk’s “Simple Manifest” GUI to create the manifest JSON file.
You can choose any name for device type, but note it for next step when device will be created. We should create manifest with two field for each value. After finishing all the steps, you should have device type as shown on next image :
For now we will not create groups since we want to test to make sure the connection is successful first.
Create a “Device” in Artik Cloud
Next, you will create an Artik Cloud “device” using your newly created device type. Log into your dashboard with your devices. Create new device for the device type you created in first step. After the device has been created, click on the settings gear icon and generate a device token. From this dialog box, you will eventually need to copy-and-paste the “Device ID” and the “Device Token” into your GitHub code, so write these down for later implementation.
Connecting Your Artik Cloud “Device”
Login to the Artik Cloud website. Under the main Artik Cloud Page (NOT under the “DEVELOPERS” tab) click on the “My Artik Cloud” dropdown.
Then, click on “Connect Another Device”. Choose the device you created in the prior “Artik Cloud” step, and set it up.
Give it a unique name and connect the device. Next, click on your new device’s gear-icon to view its info.
Then, generate a device token by clicking on the link, “Generate Device Token” and save the changes. Once set up, Take note of the “Device ID” and “Device Token”.
Now, navigate back to the main Artik Cloud Webpage and click on the “DEVELOPERS” tab. On the succeeding page, click on the “Dashboard” drop-down.
Click on “Applications”, and then click on “New Application” to create a new Artik application. Label it accordingly, and click the “Save Application” button.
Next, back on the “Applications” page, click on your newly created application to edit it. Under “Permissions”, add the Artik-Cloud-Developer-Device that you created earlier.
Give the device “read and write" permissions using the DTID you generated earlier from your Artik-Cloud-Developer-Device.
Click “Save” and move on to adjusting the “App Info”.
Give the App a description, categories, tags, and Authentication Redirect URL of your choice. Make sure the selected authorization method includes the “implicit” radio button.
Code Explanation
For acquiring data from the Automatic® and its accompanying smartphone-app, the Artik Cloud HTTPS REST API is used. So you will need to download the files from the referenced GitHub repository, and modify their code within a program like Android Studio.
For more information about Artik Cloud REST API, you can read more, here at https://developer.artik.cloud/documentation/api-reference/ .
Downloading Needed Files from GitHub
You’ll need to download and build some files from a specific GitHub repository to create an application that extracts the data from Automatic®. We used the Automatic-Android-SDK repository, that can be found at https://github.com/Automatic/Automatic-Android-SDK . From the link, you can create an app which allows you to do the following:
• Access to your users' trips to analyze driving habits
• Query your users' cars to provide up-to-date resale values estimates
• Populate your users' profiles without a lengthy signup form
• Receive "pushed" events such as Ignition On, Ignition Off, etc.
For our example however we decided to just use the Hello Cloud example from the Samsung Artik first application. Details on that application and the Android Studio setup can be found at https://developer.artik.cloud/documentation/tutorials/your-first-android-app.html. Once you have setup the application in Android Studio, follow please continue.
Code Modification
Once you have downloaded the repository into Android Studio, you will have to modify the code according to the repository instructions. Follow steps 1, 2, 3, and 8. Once you have completed those steps, do the following:
In the below MessageActivity.java snippet, replace the below asterisks with the “Device ID” and the dashes with the “Device Token”.
private static final String DEVICE_ID = “*************”;
public static final String KEY_ACCESS_TOKEN = “——————————“;
Also, in MessageActivity.java, replace the postMsg function block with the code snippet below. Revise the contents of this snippet to fit your Manifest’s fields, accordingly.
private void postMsg() {
final String tag = TAG + " sendMessageActionAsync";
HashMap<String, Object> data = new HashMap<String, Object>();
/*data.put("stepCount", 4393);
data.put("heartRate", 110);
data.put("description", "Run");
data.put("activity", 2);*/
data.put("average_from_EPA_kmpl", 975.9);
data.put("average_kmpl", 194.853);
data.put("city_fraction", 514.676);
data.put("distance_m", 465.096);
data.put("duration_over_70_s", 939);
data.put("duration_over_75_s", 4393);
data.put("duration_over_80_s", 646);
data.put("duration_s", "1m");
data.put("hard_accels", 674);
data.put("hard_brakes", 78);
data.put("highway_fraction", 788.032);
data.put("id", "7P8s");
data.put("idling_time", 955.638);
data.put("fuel_grade", "LOGGC4");
data.put("make", "Honda");
data.put("model", "Accord");
data.put("submodel", "SE");
data.put("year", 2000);
data.put("code", "EIlbK");
data.put("description", "Test");
MessageAction msg = new MessageAction();
msg.setSdid(DEVICE_ID);
msg.setData(data);
Log.i(TAG, "Sent data from device "+DEVICE_ID+ " with the data "+data);
try {
mMessagesApi.sendMessageActionAsync(msg, new ApiCallback<MessageIDEnvelope>() {
@Override
public void onFailure(ApiException exc, int i, Map<String, List<String>> stringListMap) {
processFailure(tag, exc);
}
@Override
public void onSuccess(MessageIDEnvelope result, int i, Map<String, List<String>> stringListMap) {
Log.v(tag, " onSuccess response to sending message = " + result.getData().toString());
updateSendResponseOnUIThread(result.getData().toString());
}
@Override
public void onUploadProgress(long bytes, long contentLen, boolean done) {
}
@Override
public void onDownloadProgress(long bytes, long contentLen, boolean done) {
}
});
} catch (ApiException exc) {
processFailure(tag, exc);
}
}
Next, revise the following code in MainActivity.java, replacing text with your customized values attained from previous steps.
private static final String ARTIK_CLOUD_AUTH_BASE_URL = “https://accounts.artik.cloud";
private static final String CLIENT_ID = “*****”;
// AKA application id
private static final String REDIRECT_URL = “<AuthRedirectURL>”;
Next we will run the application to verify the setup.
Running The Application
Now that you’ve adjusted the code, you should be able to build and run it from Android Studio. Press the “Run App” icon to run the code. The icon looks like a green, sideways triangle. It also looks like a “Press Play” icon. The code will execute and a GUI should appear on your android device or “Android Emulator”.
Next, click on the "Login" button to login to the Samsung Artik cloud. You will need to enter in your login and password for the Artik Cloud to continue. Once you are logged in you will see a screen with two buttons. Click the "send a message" button to pass the data information we setup in our PostMsg() function earlier. You should now see something similar to this screen in your emulator or mobile device:
You have now verified the data is transmitted and received by the Artik Cloud. Now continue with a connection to the Automatic SDK for passing the real data from the device.
Comments