Let's build a Java console application that communicates to ARTIK Cloud MQTT endpoint using ARTIK Cloud's Java/Android 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 an MQTT session with ARTIK Cloud.
- How to subscribe to receive Actions.
- How to publish messages.
- 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
mqtt-smartlight-x.x.jar
is generated under the target directory. -
Run the command at the target directory to learn the usage:
java -jar mqtt-smartlight-x.x.jar
- Start the smart light emulator. Run the command at the target directory:
java -jar mqtt-smartlight-x.x.jar -d YOUR_DEVICE_ID -t YOUR_DEVICE_TOKEN
Remember that you got the device ID and token of your smart light in the ARTIK Cloud setup phase.
-
Send Actions to the emulator using the web tool at My ARTIK Cloud.
-
The emulator terminal should show that Actions are received from the subscribed topic and updated states (on or off) are published back to ARTIK Cloud. The following is the example:
Start mqtt session.... Connecting to broker: ssl://api.artik.cloud:8883...... Succeeded: CONNECT subscribe to: /v1.1/errors/7d469 subscribe to: /v1.1/actions/7d469 Succeeded: SUBSCRIBE_ERRORS Succeeded: SUBSCRIBE_ACTIONS Received message. Payload: {"actions":[{"name":"setOn"}]}; Topic:/v1.1/actions/7d469; Qos:0 Handle Action: setOn Set State to true Publishing to topic: /v1.1/messages/7d469; message payload: {"state":true}; QoS:2..... Succeeded: PUBLISH Published to topic: /v1.1/messages/7d469 Received message. Payload: {"actions":[{"name":"setOff"}]}; Topic:/v1.1/actions/7d469; Qos:0 Handle Action: setOff Set State to false Publishing to topic: /v1.1/messages/7d469; message payload: {"state":false}; QoS:2..... Succeeded: PUBLISH Published to topic: /v1.1/messages/7d469
-
Enter Ctrl+C to terminate the console application as following:
^C Prepare shutting down. Disconnecting... Please wait... Succeeded: DISCONNECT
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-spec
Check out sample applications at https://developer.artik.cloud/documentation/tutorials/
To create and manage your services and devices on ARTIK Cloud, create an account at https://developer.artik.cloud
Also see the ARTIK Cloud blog for tutorials, updates, and more: http://artik.io/blog/cloud
Licensed under the Apache License. See LICENSE.
Copyright (c) 2017 Samsung Electronics Co., Ltd.