M2M(即Machine-to-Machine)是一種端對端通訊技術。本章節以Node JS SDK爲例,使用基於Topic消息路由的M2M設備間通訊,主要介紹如何基於物聯網平臺構建一個M2M設備間通訊架構。
第一部分:配置相關html
一、產品、設備、Topic的建立參考連接java
消息路由創建node
本部分目前不支持門戶直接配置,須要基於管理API: CreateTopicRouteTable 來創建消息路由關係。
測試能夠直接使用OpenAPI來快速實現相關功能,本地集成相關功能直接基於SDK便可。git
二、JAVA SDK Demogithub
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()); } } }
注意:SDK版本差別按照實際版本調整便可。
三、查詢路由關係小程序
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()); } } }
運行結果:微信小程序
{"requestId":"9404FD71-7461-478E-B064-0AEB15C91111","success":true,"dstTopics":["/a12OcQ4****/device2/user/RouteData"]}
第二部分:客戶端代碼相關api
四、Node JS SDK安裝參考連接瀏覽器
五、設備端業務代碼微信
device1
// 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: '3yWqKtWxN7VPuWEEDEn4eKWN********' // 支付寶小程序和微信小程序額外須要配置協議參數 // "protocol": 'alis://', "protocol": 'wxs://', }); device.on('connect', () => { console.log('connect successfully!'); // 發送消息到指定的Topic,等待規則引擎轉發到另外的一個設備的Topic device.publish('/a12OcQ4****/device1/user/RouteData', '{"ke1":"value1 test"}'); device.publish('/a12OcQ4****/device1/user/RouteData', 'This is my test job.'); });
device2
// 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: 'X9fzX9u0aIOORNghPyfYKq22********' // 支付寶小程序和微信小程序額外須要配置協議參數 // "protocol": 'alis://', "protocol": 'wxs://', }); // 定於規則引擎轉發過來的消息 device.subscribe('/a12OcQ4****/device2/user/RouteData'); device.on('connect', () => { console.log('connect successfully!'); }); device.on('message', (topic, payload) => { console.log(topic, payload.toString()); });
八、測試運行
本文爲雲棲社區原創內容,未經容許不得轉載。