烟感器设备接入AWS IOT的一种方法

      最近和同事一起做了一个烟感器接入 AWS IOT 的Demo ,遇到一些问题,想记录下来,以备日后查阅。

需求:

1. 烟感器(WIFI方案)接入AWS IOT core 平台,APP端也接入AWS IOT 端。

2. 烟感器收发消息数据(jason格式)通过AWS IOT 与APP互动。

3.APP端可以控制烟感器端状态,并获取烟感器端的history Data.

环境:

1.device 端是使用的Free RTOS,下载AWS IOT 嵌入式C sdk

2.APP端 是IOS ,用的objective-C 开发。

3.注册AWS 账号(注意是Global Account),选择地域。

架构示意图:

一些要点:

1. 基于MQTT 3.1.1协议 通过Subscribe/Publish 进行消息订阅和发布。

2.以 $aws/*  开头的是AWS 预留主题$aws/things/SmokeSensor_Demo/shadow/update

3. 创建Things +Policy +Crt  注意policy 的权限设置。

一些实验:

1. https://aws.amazon.com/cn/iot-core/getting-started/#kits     模拟Device接入AWS IOT

步骤:

1. 在AWS IOT 端创建Things +Policy +Crt ,并将root-CA,  Things.pem.crt ,Things.private.pem 下方给Device 端。

 2. Device 端配置接入。可以在此验证接入状态:

3. APP 端 通过AWS Cognito 的Identity pool 进行身份认证,然后调用AWS 的Device shadow RestfulAPI 进行subscribed/Publish .

4.  在Rule Engine 中进行操作,数据流:Device-->AWS IOT--->Rule --->Lambda---->SNS(Topic)--->APP(SMS/SES/APNS)

5. Lambda 配置示例:

console.log('Loading function');
    // Load the AWS SDK
    var AWS = require("aws-sdk");
    
    // Set up the code to call when the Lambda function is invoked
    exports.handler = (event, context, callback) => {
        // Load the message passed into the Lambda function into a JSON object
        var eventText = JSON.stringify(event, null, 4);
        
        // Log a message to the console, you can view this text in the Monitoring tab in the Lambda console or in the CloudWatch Logs console
        console.log("Received event:", eventText);
        
    
        
        // Create a string extracting the click type and serial number from the message sent by the AWS IoT button
        var messageText = " CODensity is: " + event.state.desired.CODensity + "   BatteryCapacity is: " + event.state.desired.BatteryCapacity ;
        //CODensity is: undefined   BatteryCapacity is: undefined

        
        // Write the string to the console
        console.log("Message to send: " + messageText);
        
        // Create an SNS object
        var sns = new AWS.SNS();
        
        // Populate the parameters for the publish operation
        // - Message : the text of the message to send
        // - TopicArn : the ARN of the Amazon SNS topic to which you want to publish
        var params = {
            Message: messageText,
            TopicArn: "arn:aws:sns:us-west-2:71******1465:lambda-x-account"
         };
         sns.publish(params, context.done);
    };

6. Topic  示例:

7. device 端update 到"Reported": 字段 ,APP端从Reported 字段获取信息,并通过修改Desired 字段的参数值 来控制device 端的状态 。 device 端通过Subscribed  $aws/***/update/delta 来获取app端修改的值,然后在update 新的值 给Reported 字段。并重置Desired:null 

猜你喜欢

转载自blog.csdn.net/madson_min/article/details/82659538