Unit 3: Connecting the Devices to EnOS


This unit shows how to use EnOS Device SDK for Java to develop a program that simulates the temperature sensor and air-con 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.

  1. Install JDK, which can be downloaded at https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.

  2. Install Maven, which can be downloaded at http://maven.apache.org/download.cgi.

  3. 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 EnOS Management Console lists all EnOS SDKs with links to GitHub and Maven repositories. Take the following steps to install EnOS Device SDK for MQTT for Java.

  1. Open the Maven repository of the SDK at https://mvnrepository.com/artifact/com.envisioniot/enos-mqtt.

  2. Copy the maven dependency information of the SDK.

  3. 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 EnOS Device SDK from GitHub and install it in your development environment.

Programming for Device Connection

After EnOS Device SDK for MQTT for Java is installed, follow the steps below to connect the temperature sensor and air-con device to EnOS Cloud.


  1. Import the following.

    import com.envisioniot.enos.iot_mqtt_sdk.core.MqttClient;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    


  2. 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 sensor_productKey = "product_key";
    public static final String sensor_deviceKey = "customized_key";
    public static final String sensor_deviceSecret = "device_secret";
    
    public static final String ac_productKey = "product_key";
    public static final String ac_deviceKey = "customized_key";
    public static final String ac_deviceSecret = "device_secret";
    
    private static MqttClient sensorClient;
    private static MqttClient acClient;
    


    • The address and port 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 EnOS Management Console and click Help > Environment Information.

    • The productKey, deviceKey, and deviceSecret are the device properties generated when you registered the device in Unit 1.


  3. Declare the main function initWithCallback() for initializing the device connection.


    public static void main(String[] args) throws Exception {
        initWithCallback();
    }
    


  4. Use the initWithCallback function to connect both devices to EnOS Cloud.


    public static void initWithCallback() {
       System.out.println("start connect with callback ... ");
       ExecutorService executor = Executors.newFixedThreadPool(1);
       executor.submit(new Runnable() {
          @Override
          public void run() {
             // construct an MQTT client by static device credential
             // uri is the URL of EnOS MQTT Broker for Devices, which can be obtained in Environment Information page in EnOS Console
             // ProductKey, DeviceKey and DeviceSecrect can be obtained in Device Details page in EnOS Console
             acClient = new MqttClient(uri, ac_productKey, ac_deviceKey, ac_deviceSecret);
    
             acClient.getProfile().setConnectionTimeout(60).setAutoReconnect(true).setKeepAlive(600);
    
             // connect to EnOS Cloud and register callbacks. onConnectSuccess method will be called
             try {
                acClient.connect();
             } catch (Throwable t) {
                t.printStackTrace();
             }
    
             while (true) {
                System.out.println("acClient connected: " + acClient.isConnected());
    
                try{
                   Thread.sleep(5_000L);
                   System.out.println("acClient is connected");
                } catch (Throwable t) {
    
                }
             }
          }
       });
    
       // construct an MQTT client by static device credential
       // uri is the URL of EnOS MQTT Broker for Devices, which can be obtained in Environment Information page in EnOS Console
       // ProductKey, DeviceKey and DeviceSecrect can be obtained in Device Details page in EnOS Console
       sensorClient = new MqttClient(uri, sensor_productKey, sensor_deviceKey, sensor_deviceSecret);
    
       // enable auto-reconnect feature
       sensorClient.getProfile().setConnectionTimeout(60).setAutoReconnect(true).setKeepAlive(600);
    
       // connect to EnOS Cloud and register callbacks. onConnectSuccess method will be called
       try {
          sensorClient.connect();
        } catch (Throwable t) {
          t.printStackTrace();
        }
    
       System.out.println("sensorClient connected :" + sensorClient.isConnected());
    }
    


  5. Check the running result of the program. The program will return the following result if the device connection is successful.

    start connect with callback ...
    acClient connected: true
    sensorClient connected :true
    


  6. Check the status change of the devices in the Device List on EnOS Management Console. The status of the device will change from Inactive to Online.


    ../../_images/s1_device_state.png

Next Unit

Simulating the Measurement Point Data and Results