An Example of Node JS SDK for Communication between M2M Devices Based on Topic Message Routing

Summary

M2M (Machine-to-Machine) is an end-to-end communication technology. This chapter takes Node JS SDK as an example, uses Topic message routing based M2M device-to-device communication, mainly introduces how to build a M2M device-to-device communication architecture based on the Internet of Things platform.

Experimental steps

Part I: Configuration-related

1. Reference for the Creation of Products, Equipment and Topic link

Message Routing Establishment

This section currently does not support direct portal configuration and needs to be based on management API: CreateTopicRouteTable To establish a message routing relationship.

Tests can be used directly OpenAPI To quickly implement related functions, local integration related functions can be directly based on SDK.

2,JAVA SDK Demo

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20170420.CreateTopicRouteTableRequest;
import com.aliyuncs.iot.model.v20170420.CreateTopicRouteTableResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;

public class CreateTopicRouteTable {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAIOZZg********", "v7CjUJCMk7j9aKduMAQLjy********");
        IAcsClient client = new DefaultAcsClient(profile);

        CreateTopicRouteTableRequest request = new CreateTopicRouteTableRequest();
        request.setRegionId("cn-shanghai");

        List<String> dstTopicList = new ArrayList<String>();
        dstTopicList.add("/a12OcQ4****/device2/user/RouteData");
        request.setDstTopics(dstTopicList);
        request.setSrcTopic("/a12OcQ4****/device1/user/RouteData");

        try {
            CreateTopicRouteTableResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

Note: SDK version differences can be adjusted according to the actual version.

3. Query Routing Relations

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20170420.QueryTopicRouteTableRequest;
import com.aliyuncs.iot.model.v20170420.QueryTopicRouteTableResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class QueryTopicRouteTable {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAIOZZgY********", "v7CjUJCMk7j9aKduMAQLjy********");
        IAcsClient client = new DefaultAcsClient(profile);

        QueryTopicRouteTableRequest request = new QueryTopicRouteTableRequest();
        request.setRegionId("cn-shanghai");
        request.setTopic("/a12OcQ4****/device1/user/RouteData");

        try {
            QueryTopicRouteTableResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

Operation result

{"requestId":"9404FD71-7461-478E-B064-0AEB15C91111","success":true,"dstTopics":["/a12OcQ4****/device2/user/RouteData"]}
Part 2: Client Code Relevance

4. Node JS SDK Installation Reference link

5. Equipment-side business code

device1


// node introduces package names
const iot = require('alibabacloud-iot-device-sdk');
// Browsers, small programs, and small programs introduce js files compiled by./dist.
// const iot = require('./dist/alibabacloud-iot-device-sdk.js');
// js version download address:
//    https://github.com/aliyun/alibabacloud-iot-device-sdk/tree/master/dist or
//    Alibabacloud-iot-device-sdk.js download address https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.js or
//    Alibabacloud-iot-device-sdk.min.js Download Address https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.min.js  
//  

const device = iot.device({
  productKey: 'a12OcQ4****',
  deviceName: 'device1',
  deviceSecret: '3yWqKtWxN7VPuWEEDEn4eKWN********'
  // Alipay small program and WeChat applet need additional configuration protocol parameters.
  // "protocol": 'alis://', "protocol": 'wxs://',
});
device.on('connect', () => {
  console.log('connect successfully!');
  // Send a message to the specified Topic, waiting for the rule engine to forward to the Topic of another device
  device.publish('/a12OcQ4****/device1/user/RouteData', '{"ke1":"value1 test"}');
  device.publish('/a12OcQ4****/device1/user/RouteData', 'This is my test job.');
});

device2

// node introduces package names
const iot = require('alibabacloud-iot-device-sdk');
// Browsers, small programs, and small programs introduce js files compiled by./dist.
// const iot = require('./dist/alibabacloud-iot-device-sdk.js');
// js version download address:
//    https://github.com/aliyun/alibabacloud-iot-device-sdk/tree/master/dist or
//    Alibabacloud-iot-device-sdk.js download address https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.js or
//    Alibabacloud-iot-device-sdk.min.js Download Address https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.min.js  
//  

const device = iot.device({
  productKey: 'a12OcQ4****',
  deviceName: 'device2',
  deviceSecret: 'X9fzX9u0aIOORNghPyfYKq22********'
  // Small programs and small programs need additional configuration protocol parameters.
  // "protocol": 'alis://', "protocol": 'wxs://',
});
// Messages scheduled to be forwarded by the rule engine
device.subscribe('/a12OcQ4****/device2/user/RouteData');
device.on('connect', () => {
  console.log('connect successfully!');
});
device.on('message', (topic, payload) => {
  console.log(topic, payload.toString());
});

8. Test Running

Reference link

Communication between M2M devices based on Topic message routing

Keywords: Java SDK Google github

Added by lettie on Sat, 05 Oct 2019 17:56:27 +0300