示例代码: // 初始化 AWS IoT SDK const awsIot = require('aws-iot-device-sdk');
// 连接到 AWS IoT 服务 const device = awsIot.device({ keyPath: 'your-private-key.pem', certPath: 'your-certificate.pem.crt', caPath: 'root-CA.crt', clientId: 'your-unique-client-identifier', host: 'your-aws-iot-endpoint' });
// 连接成功回调函数 device.on('connect', function() { console.log('Connected to AWS IoT');
// 订阅数据主题
device.subscribe('data-topic');
// 发布控制指令
device.publish('control-topic', JSON.stringify({ command: 'set-level', level: 50 }));
});
// 接收到订阅消息回调函数 device.on('message', function(topic, payload) { console.log('Received message:', topic, payload.toString()); });
下一篇:AWSIOT重连问题