In this article, we will discuss how we can implement dynamic registration of Internet of Things (IoT) devices the serverless way using Alibaba Cloud Function Compute.
The figure above only demonstrates the core process of dynamically registering devices through the API, but the production environment must add security verification logic based on the enterprise situation to ensure the identity security of the devices.
This involves the use of the RegisterDevice and QueryDeviceDetail APIs.
RegisterDevice https://www.alibabacloud.com/help/doc-detail/69470.htm
QueryDeviceDetail https://www.alibabacloud.com/help/doc-detail/69594.htm
https://help.aliyun.com/document_detail/69594.html
Node.js
const getRawBody = require('raw-body');
const co = require('co');
const RPCClient = require('@alicloud/pop-core'). RPCClient;
const options = {
accessKey: "AK of the cloud account",
accessKeySecret: "AK Secret of the cloud account",
};
//1. Create a client
const iotClient = new RPCClient({
accessKeyId: options.accessKey,
secretAccessKey: options.accessKeySecret,
endpoint: options.endpoint || 'https://iot.cn-shanghai.aliyuncs.com',
apiVersion: options.apiVersion || '2018-01-20'
});
const productKey = 'productKey of the product';
module.exports.handler = function(req, resp, context) {
getRawBody(req, function(err, body) {
body = JSON.parse(decodeURIComponent(body.toString()));
const deviceId = body.deviceId;
co(function* () {
try {
//Create a device
const registerResponse = yield iotClient.request('RegisterDevice', {
productKey: productKey,
deviceName: deviceId
});
resp.send(JSON.stringify({ success: true, data: registerResponse.Data }));
} catch (err) {
//The device already exists; check the device details
const queryResponse = yield iotClient.request('QueryDeviceDetail', {
productKey: productKey,
deviceName: deviceId
});
const returnJson = { success: true }
returnJson.data = {
DeviceName: queryResponse.Data.DeviceName,
ProductKey: queryResponse.Data.ProductKey,
DeviceSecret: queryResponse.Data.DeviceSecret,
IotId: queryResponse.Data.IotId
}
resp.send(JSON.stringify(returnJson));
}
});
});
};
The device obtains the trituples by triggering Function Compute through HTTP POST Json
To learn more about Alibaba Cloud's Internet of Things (IoT) Platform, visit https://www.alibabacloud.com/product/iot
Developing Apps on Alibaba Cloud IoT Platform with Raspberry Pi and Node.js
Clouders - January 10, 2022
Alibaba Clouder - March 14, 2019
Alibaba Clouder - June 16, 2020
Alibaba_Wei - June 5, 2018
Alibaba Cloud Native Community - July 20, 2021
Alibaba Cloud Native Community - June 18, 2024
Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.
Learn MoreAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreA cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms
Learn MoreMigrate your Internet Data Center’s (IDC) Internet gateway to the cloud securely through Alibaba Cloud’s high-quality Internet bandwidth and premium Mainland China route.
Learn MoreMore Posts by GXIC
Raja_KT March 15, 2019 at 11:59 am
It reminds me of LoraWAN devices set up.