Unit 2: Connecting the Device to EnOS™ Cloud¶
This tutorial shows how to use the EnOS Device SDK for Java to develop a program that simulates the battery device to connect to EnOS.
Setting up the Development Environment¶
EnOS Device SDK for MQTT for Java requires Java SE 8 and Maven 3. To set up your development environment, follow the steps below.
Install JDK, which can be downloaded at https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
Install Maven, which can be downloaded at http://maven.apache.org/download.cgi.
Install a development environment, such as IntelliJ IDEA, which can be downloaded at https://www.jetbrains.com/idea/download/.
In this tutorial, we will use IntelliJ IDEA as the development environment.
Installing EnOS Device SDK for MQTT for Java¶
The SDK Center on the EnOS Management Console lists all the EnOS SDKs with links to GitHub and Maven repositories. Take the following steps to install EnOS Device SDK for MQTT for Java.
Open the Maven repository of the SDK at https://mvnrepository.com/artifact/com.envisioniot/enos-mqtt.
Copy the maven dependency information of the SDK.
Open IntelliJ IDEA, and include the maven dependency by adding the following code snippet in
pom.xml
. See the following example.<dependency> <groupId>com.envisioniot</groupId> <artifactId>enos-mqtt</artifactId> <version>2.2.16</version> </dependency>
Alternatively, you can download the source code of the EnOS Device SDK from GitHub and install it in your development environment.
Programming for Device Connection¶
After the EnOS Device SDK for MQTT for Java is installed, take the following steps to connect the battery device to EnOS Cloud (you can also refer to the code sample available at https://github.com/EnvisionIot/enos-device-sdk-java/blob/master/enos-sdk-sample/src/main/java/mqtt/SimpleSendReceive.java).
Declare the public variables that will be used in the program. For example:
public static final String uri = "tcp://{address}:{port}"; public static final String productKey = "product_key"; public static final String deviceKey = "customized_key"; public static final String deviceSecret = "device_secret"; private static MqttClient client;
The
address
andport
of the server varies according to the cloud region and instance. For private cloud instances, you can find the MQTT Broker address and port information by going to the EnOS Console Management and click Help > Environment Information.The
productKey
,deviceKey
, anddeviceSecret
are the device properties generated when you registered the device in Unit 1.
Declare the main function
initWithCallback()
for initializing the device connection.public static void main(String[] args) throws Exception { initWithCallback(); }
Use the
initWithCallback
function to connect the battery device to EnOS Cloud.public static void initWithCallback() { System.out.println("start connect with callback ... "); try { client = new MqttClient(uri, productKey, deviceKey, deviceSecret); client.getProfile().setConnectionTimeout(60).setAutoReconnect(false); client.connect(new ConnCallback() { public void connectComplete(boolean reconnect) { System.out.println("connect success"); } public void connectLost(Throwable cause) { System.out.println("onConnectLost"); } public void connectFailed(Throwable cause) { System.out.println("onConnectFailed : " + cause); } }); } catch (Throwable var1) { } System.out.println("connect result :" + client.isConnected()); }
Check the running result of the program. The program will return the following result if the device connection is successful.
start connect with callback ... connect result :true
Check the status change of the battery device in the Device List on the EnOS Management Console. The status of the device will change from
Inactive
toOnline
.