Example of communication Node JS SDK between M2M devices based on rule engine-Alibaba Cloud Developer Community

Author: Qiao Ba

overview

M2M (Machine-to-Machine) is an end-to-end communication technology. This topic uses Node JS SDK as an example to implement M2M inter-device communication by using the rule engine data transfer. This topic describes how to build an M2M inter-device communication architecture based on IoT platform.

Procedure

part I: configuration related 1. Create a product

2. Add device 1 and device 2 to the product

3. Configuration of regulation engine

4. Start the rules engine

part II: client code-related 5. Install Node JS device SDK

npm install alibabacloud-iot-device-sdk --save

6. Device business code

equipment 1

// node引入包名
const iot = require('alibabacloud-iot-device-sdk');
// 浏览器、微信小程序,支付宝小程序引入./dist编译的js文件
// const iot = require('./dist/alibabacloud-iot-device-sdk.js');
// js版本下载地址:
//    https://github.com/aliyun/alibabacloud-iot-device-sdk/tree/master/dist  或
//    alibabacloud-iot-device-sdk.js 下载地址 https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.js  或
//    alibabacloud-iot-device-sdk.min.js 下载地址 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: '3yWqKtWxN7VPuWEEDEn4eKWNNtUv****'
  // 支付宝小程序和微信小程序额外需要配置协议参数
  // "protocol": 'alis://', "protocol": 'wxs://',
});
device.on('connect', () => {
  console.log('connect successfully!');
  // 发送消息到指定的Topic,等待规则引擎转发到另外的一个设备的Topic
  device.publish('/a12OcQ4****/device1/user/m2mdata', '{"ke1":"value1 test"}');
});

equipment 2

// node引入包名
const iot = require('alibabacloud-iot-device-sdk');
// 浏览器、微信小程序,支付宝小程序引入./dist编译的js文件
// const iot = require('./dist/alibabacloud-iot-device-sdk.js');
// js版本下载地址:
//    https://github.com/aliyun/alibabacloud-iot-device-sdk/tree/master/dist  或
//    alibabacloud-iot-device-sdk.js 下载地址 https://unpkg.com/alibabacloud-iot-device-sdk@1.2.4/dist/alibabacloud-iot-device-sdk.js  或
//    alibabacloud-iot-device-sdk.min.js 下载地址 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: 'X9fzX9u0aIOORNghPyfYKq22IL7Q****'
  // 支付宝小程序和微信小程序额外需要配置协议参数
  // "protocol": 'alis://', "protocol": 'wxs://',
});
// 定于规则引擎转发过来的消息
device.subscribe('/a12OcQ4****/device2/user/m2mdata');
device.on('connect', () => {
  console.log('connect successfully!');
});
device.on('message', (topic, payload) => {
  console.log(topic, payload.toString());
});

7. Test run

part 3: Troubleshooting: If device 2 fails to listen to messages during use, you can use the operation and maintenance monitoring log service to troubleshoot the problem.

Reference

requirements and configurations of communication Node JS SDK between M2M devices based on rule engine

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now