确保您在Greengrass Core设备上安装了最新版本的aws-iot-device-sdk-js-v2(v1.x以前)。
在您的Greengrass组件代码中,确保您已经按照aws-iot-device-sdk-v2文档中的指示将SDK连接到AWS IoT Core。示例代码如下所示:
const iotClient = new IoTClient({
region: ,
credentials: {
accessKeyId: ,
secretAccessKey:
}
});
const mqttClient = new MQTTClient({
broker: ,
clientId: ,
tlsConfig: {
caPath: path.resolve(),
certPath: path.resolve(),
keyPath: path.resolve()
}
});
mqttClient.on('connect', () => {
// Subscribe to topic
mqttClient.subscribe();
});
mqttClient.on('message', (topic, message) => {
// Handle message
});
iotClient.attachPolicy({
policyName: ,
target:
}).then(() => {
// Start MQTT client
mqttClient.connect();
});
const shadowClient = iotClient.createDeviceShadowClient({ thingName });
shadowClient.on('connect', () => {
shadowClient.updateThingShadow({
payload: JSON.stringify({ state: { reported: } })
});
});