CONTENTS
Introduction
AWS IoT provides secure, bi-directional communication between Internet-connected devices such as sensors, actuators, embedded micro-controllers, or smart appliances and the AWS cloud. This article will describe how to connect UC300/UC3x cellular controller to AWS IoT platform via MQTT.
Requirement
- Milesight Controller: UC300 (cellular version), UC3x
- SIM card
- An AWS account
- Toolbox software
Configuration
AWS Configuration
1. Log in to your AWS account.
2. Go to Manage > All device > Things and click Create things button to create a thing.
3. Click Create a single thing and click Next.
4. Name the thing and click Next.
5. Select Auto-generate a new certificate and click Next, then you can create a policy or skip this step which we will detail on next chapter and continue to click Create thing.
6. Download below certificates.
Note:
CA certificates have an expiration date after which they cannot be used to validate a server's certificate. CA certificates are needed to be replaced after expiration date in order to ensure ongoing connectivity and to keep up to date with security best practices.
7. Go to Manage > Security > Policies and click Create policy to create a policy.
8. Name the policyand add statements as shown below, then click Create to save this policy.
9. In the list of Certificates, locate the certificate you want to attach a policy, open the certificate’s option menu by choosing the ellipsis icon and choose Attach Policy.
10. Choose and attach the policy we created for device.
Note: Click certificates to check if correct policy and thing are attached.
UC300/UC3x Configuration
1. Insert the SIM card to device and ensure it registers to Internet network.
2. Log in Toolbox, go to Application page, select the application mode as AWS to configure the server settings, then import the ca.pem to CA File, certificate.pem.crt to Client Certificate and private.pem.key to Client Key. AWS server address can be found on AWS Settings page.
Uplink Test
On the AWS GUI, go to Test > MQTT test client page to subscribe the topics to get uplinks. About the uplink topics and payload description please refer to corresponding communication protocol document: https://www.milesight.com/iot/resources/download-center/
Uplink Decode
1. On the AWS GUI, go to Manage > Message Routing > Rules page to create rule.
2. Name the rule and click Next.
3. Set the SQL statement as this example and go to Next:
SELECT * FROM 'uc/6454C48610260046/ucp/14/status'
(replace the content after FROM as real uplink topic)
4. Choose action as Lambda, and click Create a Lambda function.
5. On the new page, name a function and set the information as below, then click Create function.
6. Go back to Rule page, fresh the lambda function and choose the function created in last step, then click Next and Create.
7. Click the rule you created, click the lambda service and go to the lambda page.
8. Go to Code view, paste below codes first, then paste decoder from github after then.
var AWS = require('aws-sdk'); console.log('Loading function'); exports.handler = (event, context, callback) => { console.log('Received event:', event.base64OriginalPayload); var buffer = Buffer.from(event.base64OriginalPayload, 'base64'); var data = buffer.toString('hex'); console.log(decode_UART_F4(data)) };
After pasting, click Deploy.
9. Go to Monitor view, click View CloudWatch logs to check uplink decoded results.
Downlink Test
You can download another MQTT client software to connect to AWS and send downlinks. About the uplink topics and payload description please refer to corresponding communication protocol document.
Take MQTTX software as example,
1. Generate and download new certificates on AWS Security > Certificates, then attach the certificate to UC300_Policy
2. Configure the MQTTX to connect to AWS, then you can subscribe topics to get uplinks or publish downlink commands.
---END---