Let's build a Java console application that communicates to ARTIK Cloud device channel WebSocket (/websocket) endpoint using ARTIK Cloud Java SDK. This app emulates a smart light, which turns on or off based on Actions from ARTIK Cloud and sends the latest state (on or off) back to ARTIK Cloud.
After completing this sample, you will learn how to:
- establish WebSocket device channel
- receive Actions
- send messages to report the latest state.
- Java version >= 1.8
- Apache Maven >= 3.3.9
-
At My ARTIK Cloud, connect a device of the type "Example Simple Smart Light" (unique type name
cloud.artik.example.simple_smartlight). You can use the one that you already own. -
Get the device ID and token. You will need them when running the console app (smart light emulator).
-
Clone this repository if you haven't already done so.
-
At the root directory and run the command:
mvn clean package
The executable
websocket-dc-smartlight-x.x.jaris generated under the target directory. -
Run the command at the target directory to learn the usage:
java -jar websocket-dc-smartlight-x.x.jar
-
Start and run the following command in the target directory:
java -jar websocket-dc-smartlight-x.x.jar -d YOUR_DEVICE_ID -t YOUR_DEVICE_TOKENAfter the channel is successfully estabilished, the app should receive registration acknowledgement as following:
Connecting to: wss://api.artik.cloud/v1.1/websocket Status: CONNECTING DeviceChannelWebSocket::onOpen: registering device 81788 with token 830 DeviceChannelWebSocket::onPing: 1514922438372 DeviceChannelWebSocket::onAck: class Acknowledgement { mid: null cid: myRegisterMessage message: OK code: 200 } -
Send Actions to the emulator using the web tool at My ARTIK Cloud.
-
The emulator terminal should show that Actions are received and updated states (on or off) are sent back to ARTIK Cloud. The following is the example:
DeviceChannelWebSocket::onAction: class ActionOut { data: class ActionDetailsArray { actions: [class ActionDetails { parameters: {} name: setOn }] } type: action mid: aab80ef5656a4e3687ebfd7f40586ad4 .... } DeviceChannelWebSocket sendMessage: class MessageIn { data: {state=true} sdid: 81788 ts: 1514920563273 type: message } DeviceChannelWebSocket::onAck: class Acknowledgement { mid: aea1fa60d89d4c2c9fae71bf7184577c }
If you are not familiar with ARTIK Cloud, we have extensive documentation at https://developer.artik.cloud/documentation
The full ARTIK Cloud API specification can be found at https://developer.artik.cloud/documentation/api-reference/
Peek into advanced sample applications at https://developer.artik.cloud/documentation/tutorials/code-samples
To create and manage your services and devices on ARTIK Cloud, visit the Developer Dashboard at https://developer.artik.cloud
Licensed under the Apache License. See LICENSE.
Copyright (c) 2018 Samsung Electronics Co., Ltd.