This challenge project involves a Remote Controller Android Application, which connects at least two devices (FlicButton, LifxLight) from some Most Wanted list, to Samsung Iot Artik Cloud, based on dynamic rules. The service will be managed through an android native app.
For this challenge, I have been first inspired after attending recent webinar about "Build your first Android App using the ARTIK Cloud SDKs"
Then I decide to adapt it to some RemoteController special needs :
- Rules managed thru the API console, will include action parameters and on dynamic color value from a third device Strobe.
- Access Token will generated using thru API console, and used thru the Android App.
- Android app will exchange data with Artik Cloud using websockets.
After creating a Samsung developer account, using instructions on Samsung Artik Cloud developer dashboard pages links, special steps about Artik cloud
device type / application settings are detailed below in chapters 1 and 2
Then, using Artik cloud account, following the user links,
More details about Device(s) connection, , Rules will be decribed respectively below in chapters 3, 4.
Then, we will present the Android Native App in chapter 5, followed by Access Token (chapter 6) and Data generation thru Device simulator in Chapter 7, (in case there is no real Device yet).
1 Configuration of Devices Type"Device Types" is accessible from the "Dashboard" menu. Click the "+ New Device Type" button to create a new one. The following screenshot describes some used device types : FlicButton and LifxLight
Here are details for LifxLight Display Name and Unique Name (can be considered as package identity) and optional description (see following screenshot)
Now, you can get details (Fields/Actions) about Manifest
In Artik Cloud Application Setting (see screenshot below), since we will use an android native companion app,
- please click on 'show client & secret' button on top right to get client id, which will be used in android app source code.
- please select 'client credentials, auth code, implicit' radio button, due to usage of android native app.
- and set 'android-app://redirect' as auth redirect url value
Then in permissions, you just need to select 'Read' for application Permission profile, and Read/Write for all Device Types (FlicButton, Lifx, Strobe). The system will prompt you automatically the possible Device types, associated to the developer account.
Please find below screenshot describing the three devices to be used in the application.
The device setting button enables to get more details about any special device.
Please note the device id and token in next screenshot, which will be used, later on, for data generation using simulator, in particular.
The following screenshot list some preliminary rules, clicking on the button 'new rule' enables to create a new rule.
The following screenshot provides example of usage of rule action parameters, about a destination (target) device (MyLifxLight) for the first rule above.
The development of Native App will be performed using Android Studio.
It is recommended to upgrade the dependency with last version of artik java library code as follows :
compile 'cloud.artik:artikcloud-java:2.0.5'
The following screenshots describe some android native app screens :
The android app and associated rules will be completed and enhanced before end of challenge ;)
6 Access TokenThe following samsung authentication document explains different ways to get access token. Here we will use only application and device token.
6.1 android application token :
It is received by the application in addition to redirect url argument and then can be logged. For instance, ADB (Android Debug Bridge) tool enables to track the application token,
6.2 Device Token :
The device token indicated in second screenshot in chapter 3 will be used for device register type request.
7 Data Generation thru Device SimulatorThe following samsung document describes how to install and use Device Simulator
You can run the simulator using the command :
java -jar device-simulator.jar -token=<token>
Type ? to get available simulator commands, here are some :
- ld (or list devices) enables to get list of devices
- cs (create scenario) enables to initialize a configuration file (second argument), depending to device argument specified as first parameter. You may need to create manually the device id subdirectory if needed on your working directory.
- rs (run scenario) enables to run a scenario for some device (1rst argument) and config file (2nd argument).
$ rs <source_deviceid> myFB
Loading scenario from <working_dir>\<source_deviceid>\myFB.json
Reading file: <working_dir>\<source_deviceid>\myFB.json
$ Using this token to send the messages: <application_token>
Send #0 {"isDoubleClick":false,"DeviceId":"nrmolwmzt","State":true,"isSingleClick":true,"isHold":true,"timestamp":5693}
Got MID: 56fdf21d193646be87745dd982ff9c8f
Send #1 {"isDoubleClick":false,"DeviceId":"wscivy","State":false,"isSingleClick":false,"isHold":true,"timestamp":1846}
Got MID: 96bbbdb146b547f08ff1a26eb58eaa0a
- s : stop the current simulator session
- lw : enables to listen to actions received by some destination (target) device. Remember that rules enable to trigger automatically actions, if some conditions are met.
$ lw <destination_device_id>
Using this token to connect: <application_token>
WS -> Connected to wss://api.artik.cloud/v1.1/websocket?ack=true
Register {"Authorization":"bearer <application_token>","sdid":"<device_id>","type":"register","cid":<client_id>}
$ WS -> {"data":{"code":"200","message":"OK","cid":"<client_id>"}}
WS -> {"type":"ping", "ts":1472448554412}
WS -> {"type":"ping", "ts":1472448584412}
WS -> {"type":"ping", "ts":1472448614412}
WS -> {"type":"ping", "ts":1472448644411}
WS -> {"type":"action","cts":1472448665064,"ts":1472448665064,"mid":"f5eae599fadc4ccc92befc06b2885cb0","ddid":"<destination_device_id>","data":{"actions":[{"name":"setOn","parameters":{"BRIGHTNESS":0.5,"COLOR":"RED","DURATION":2}}]},"sdid":"<source_device_id>","pmid":"56fdf21d193646be87745dd982ff9c8f","ddtid":"dtbd92a735a9de404b993bd551a0593cb0","uid":"<user_id>","mv":1}
WS -> {"type":"action","cts":1472448671164,"ts":1472448671164,"mid":"1c7a86816dec4bb488622013b353fb9e","ddid":"<destination_device_id>","data":{"actions":[{"name":"setOff","parameters":{}}]},"sdid":"<service_device_id>","pmid":"96bbbdb146b547f08ff1a26eb58eaa0a","ddtid":"dtbd92a735a9de404b993bd551a0593cb0","uid":"<user_id>","mv":1}8 Demo
After launching the android app (see short live demo about initial version of android native app below) and starting the device simulator using rs command (see details above), here are extracts of Android Debugger Bridge traces showing that each rules has been triggered and its respective action has been called :
8.1. setOff action for lifx :
09-01 19:36:09.780 6064-6343/cloud.artik.example.rules D/ArtikCloudSession: FirehoseWebSocket: onMessage(class MessageOut {
mid: 54045aa113ed46c1aadc50b95ea27575
uid: <user_id>
sdtid: dt0ed2dade0bf84dbbaade8656fc1eba2f
cts: 1472751358233
mv: 11
})
09-01 19:36:09.810 6064-6355/cloud.artik.example.rules I/ArtikCloudSession: got action>class ActionOut {
mid: 025e9e5a20f346599d78bf626977e847
uid: <user_id>
sdtid: null
cts: 1472751358502
mv: 1
}
ddid:<destination_device_id>
data:class ActionDetailsArray {
actions: [class ActionDetails {
parameters: {}
name: setOff
}]
}
8.2. setOn action for lifx :
09-01 19:36:09.780 6064-6343/cloud.artik.example.rules D/ArtikCloudSession: FirehoseWebSocket: onMessage(class MessageOut {
mid: 68310efb6d304aa791f0ee30acbc675e
uid: <user_id>
sdtid: dt0ed2dade0bf84dbbaade8656fc1eba2f
cts: 1472751368978
mv: 11
})
09-01 19:36:09.810 6064-6355/cloud.artik.example.rules I/ArtikCloudSession: got action>class ActionOut {
mid: eaf535ba54824ea382bef48451ec7d0d
uid: <user_id>
sdtid: null
cts: 1472751369211
mv: 1
}
ddid:<destination_device_id>
data:class ActionDetailsArray {
actions: [class ActionDetails {
parameters: {DURATION=2.0, COLOR=RED, BRIGHTNESS=0.5}
name: setOn
}]
}9 Data Logs
Following some run session, Artik cloud enables also to analyze Data generated (for instance from Device simulator) as incoming "messages" requests and also actions trigged by rules based on received incoming data (messages), using the "Data Logs" feature from "My Artik Cloud" menu option. More details on following screenshots
This challenge has enabled to experiment Samsung Iot Artik Cloud which has provided facility and flexibility for developers to interconnect cross devices/services. It proposes also several helpful tools like "Device Simulator", "Data Logs" Analysis, Charts enabling data visualization / Exports,...
Comments