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/2.1.2.
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.1.2</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 SimpleSendReceive.java
code sample available from <SDK_Path>\src\main\java\com\envisioniot\enos\iot_mqtt_sdk\sample
).
Declare the public variables that will be used in the program. For example:
public static final String uri = "tcp://{host}:{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
host
andport
of the server varies according to the cloud region and instance. For private cloud instances, contact your Envision project manager or support representative to get the host and port 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 IConnectCallback() { public void onConnectSuccess() { SimpleSendReceive.subDeviceLogin(); System.out.println("connect success"); } public void onConnectLost() { System.out.println("onConnectLost"); } public void onConnectFailed(int reasonCode) { System.out.println("onConnectFailed : " + reasonCode); } }); } catch (EnvisionException 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
.