Contents:

1. Introduction

The Dragino LoRaWAN gateway can commuicate with Amazon LoRaWAN Network Server:

Below list the support products and Requirements:

  1. LoRaWAN Gateway model: LIG16LG308DLOS8 LPS8
  1. Firmware version since :lgw--build-v5.4.1614945073

What you'll learn in this tutorial

How to register a gateway with AWS

How to register a end node with AWS

How to accpet playloadDate with AWS

2. Gateway Setup Instruction

2.1 Step 1: sign up for an AWS accountand Select area

https://wiki.dragino.com/images/9/9c/AWS_account.png

sing up for an AWS account

https://wiki.dragino.com/images/thumb/0/03/Select_area.png/600px-Select_area.png

Select_area

Note:Currently,AWS IoT Core for LoRaWAN is available today in US East (N. Virginia) and Europe (Ireland) Regions. if you are not in either of these areas,there options will not affect you usage.

2.2 Step 2: Add an IAM role

To add the IAM role to allow the Configuration and Update Server (CUPS) to manage gateway credentials:

1.Open the Roles hub of the IAM console and choose Create role.

2.If you think that you might have already added the IoTWirelessGatewayCertManagerRole role, in the search bar, enter IoTWirelessGatewayCertManagerRole.

If you see an IoTWirelessGatewayCertManagerRole role in the search results, you have the necessary IAM role. You can leave the procedure now.

If the search results are empty, you don't have the necessary IAM role. Continue the procedure to add it.

3.In Select type of trusted entity, choose Another AWS account.

4.In Account ID, enter your AWS account ID, and then choose Next: Permissions.

5.In the search box, enter AWSIoTWirelessGatewayCertManager.

6.In the list of search results, select the policy named AWSIoTWirelessGatewayCertManager.

7.Choose Next: Tags, and then choose Next: Review.

8.In Role name, enter IoTWirelessGatewayCertManagerRole, and then choose Create role.

9.To edit the new role, in the confirmation message, choose IoTWirelessGatewayCertManagerRole.

10.In Summary, choose the Trust relationships tab, and then choose Edit trust relationship.

11.In Policy Document, change the Principal property to look like this example.

   "Principal": {
   "Service": "iotwireless.amazonaws.com"
   },

   After you change the Principal property, the complete policy document should look like this example.

   {
"Version": "2012-10-17",
"Statement": [
   {
     "Effect": "Allow",
     "Principal": {
       "Service": "iotwireless.amazonaws.com"
     },
     "Action": "sts:AssumeRole",
     "Condition": {}
   }
              ]
   }

12.To save your changes and exit, choose Update Trust Policy. Note:You’ve now created the IoTWirelessGatewayCertManagerRole. You won’t need to do this again.

https://wiki.dragino.com/images/thumb/7/75/Start_create_role.png/600px-Start_create_role.png

tart create role

https://wiki.dragino.com/images/thumb/3/38/Choose_Another_AWS_account.png/600px-Choose_Another_AWS_account.png

Choose Another AWS account

https://wiki.dragino.com/images/thumb/7/75/Start_create_role.png/600px-Start_create_role.png

start Create policy

https://wiki.dragino.com/images/thumb/b/b8/Choose_Next_review.png/600px-Choose_Next_review.png

Choose Next review

https://wiki.dragino.com/images/thumb/3/33/In_role_name%2Center_AWSIoTWirelessGatewayCertManagerRole%2Cand_then_choose_Create_role..png/600px-In_role_name%2Center_AWSIoTWirelessGatewayCertManagerRole%2Cand_then_choose_Create_role..png

In role name,enter AWSIoTWirelessGatewayCertManagerRole,and then choose Create role.

https://wiki.dragino.com/images/thumb/a/a6/To_edit_the_new_role.png/600px-To_edit_the_new_role.png

To edit the_new role

https://wiki.dragino.com/images/thumb/7/76/Choose_Edit_trust_relationship.png/600px-Choose_Edit_trust_relationship.png

Choose Edit trust relationship.

https://wiki.dragino.com/images/thumb/e/ea/Change_policy_Document.png/600px-Change_policy_Document.png

Change policy Document

2.3 Step 3: Add gateway

Locate IoT Core in the AWS Service Control Panel,and locate the Wireless Connection in the IoT Core Control Panel to start adding the Gateway

https://wiki.dragino.com/images/thumb/2/25/Locate_IoT_Core_in_the_AWS_Service_Control_Panel.png/600px-Locate_IoT_Core_in_the_AWS_Service_Control_Panel.png

Locate IoT Core in the AWS Service Control Panel

https://wiki.dragino.com/images/thumb/0/0e/Ocate_the_Wireless_Connection_in_the_IoT_Core_Control_Panel.png/600px-Ocate_the_Wireless_Connection_in_the_IoT_Core_Control_Panel.png

ocate the Wireless Connection in the IoT Core Control Panel

2.4 Step 4: Get a unique Gateway ID in the gateway

Every gateways has a Gateway ID,the ID can be found at Amazon AWS IoT.

https://wiki.dragino.com/images/thumb/3/37/Gateway_ID2.png/600px-Gateway_ID2.png

Gateway ID

Note:User must use the Gateway ID of the Amazon AWS IoT page as the EUI

Notice: In latest firmware. the AWS-IoT configured page is under LoRaWAN Basic Station page.

https://wiki.dragino.com/images/thumb/b/b4/Gateway_ID2_E.png/600px-Gateway_ID2_E.png

LoRaWAN Basic Station

2.5 Step 5: Configure the gateway and Creation gateway certificates

Configure the gateway and Creation gateway certificates

https://wiki.dragino.com/images/thumb/0/04/Configure_the_gateway1.png/600px-Configure_the_gateway1.png

Configure the gateway

https://wiki.dragino.com/images/thumb/f/f3/Creation_gateway_certificates.png/600px-Creation_gateway_certificates.png

Creation gateway certificates

Note:User Make sure the name of the downloaded file remains the same

2.6 Step 6: Add gateway file

user returns to your gateway,and start adding the three file they got to the gateway.

https://wiki.dragino.com/images/thumb/f/f8/Gateway_Configure_for_Amazon_AWS_IoT-LoRaWAN.png/600px-Gateway_Configure_for_Amazon_AWS_IoT-LoRaWAN.png

Configure the gateway

if user Upload successfully you will see:

https://wiki.dragino.com/images/thumb/e/e5/Uploaded_successfully.png/600px-Uploaded_successfully.png

Uploaded successfully

Notice:User must select the correct Upload on the suffix name,otherewise the upload fail.

2.7 Step 7: Add CUPS URI

User enters the CUPS endpoint copied in the previous step into the CUPS URI,and clicks "Save&Apply"

https://wiki.dragino.com/images/thumb/8/85/CUPS_URI.png/600px-CUPS_URI.png

CUPS URI

https://wiki.dragino.com/images/thumb/6/62/CUPS_URI_Setting.png/600px-CUPS_URI_Setting.png

CUPS URI Setting

Notice:After the user hits "Sace&Apply", the gateway will start Station mode on its own

2.8 Step 8:Last upLink received

After above settings,the gateway should be able to connect,the figure below shows the data of Last upLink received

https://wiki.dragino.com/images/thumb/d/d8/UpLink_received.png/600px-UpLink_received.png

UpLink received

3. Add wireless device

3.1 step 1:Add device profile

3.1.1 CREATE DEVICE PROFILE

select profile in the Control Panel and add configuration adn service files.the device's configuration paranmeters depend on the LoRaWAN specification it uses.The configuration parameters are found in the device's documentation or on the device itself.

https://wiki.dragino.com/images/thumb/e/eb/Add_device_profile.png/600px-Add_device_profile.png

Add device profile

Provide device data

Device profile name : a descriptive name of your choice for this profile, e.g. “Workshop device profile of ”
Frequency band (RFRegion)  : please select a frequency band your device and gateway will be using. For most European countries EU868 is a right choice. For USA US915 is a right choice. If in doubt, you can review a regional parameters document, section “1.2 Quick cross reference table”.
MAC version : this parameter should specify the most recent version of LoRaWAN specification supported by your device. Unless device documentation states otherwise, please select 1.0.3.
Regional parameters version: please select RP002-1.0.1, unless specified otherwise in your device documentation
MaxEIRP : please enter the default value 15
Supports class : please select class A, unless you are aware of specific requirements to use classes B and C and your device supports it

https://wiki.dragino.com/images/thumb/9/94/Device_profile.png/600px-Device_profile.png

device profile

Notice :See the documentation for more information: https://docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan-define-profiles.html

3.1.2 CREATE SERVICE PROFILE

Select “Profiles” view and click on “Add service profile”

https://wiki.dragino.com/images/thumb/a/a5/Add_service_profile.png/600px-Add_service_profile.png

Add service profile

Add a service profile

Choose a service profile name (e.g. “Default service profile”)
Ensure that AddGWMetaData parameter is enabled. By enabling this parameter you will receive additional metadata with each device payload, for example RSSI and SNR for the data transmission.
This metadata could be used for network coverage optimization.
Finally, scroll down and click on “Add service profile”.

https://wiki.dragino.com/images/thumb/9/99/Add_service_profile2.png/600px-Add_service_profile2.png

Add service profile2

3.2 Step 2:Create an IAM roles for destinations

AWS IoT Core for LoRaWAN destinations require IAM roles that give AWS IoT Core for LoRaWAN the permissions necessary to send data to the AWS IoT rule. If such a role is not already defined, you'll need to define it so that it will appear in the list of roles.

To create an IAM policy for your AWS IoT Core for LoRaWAN destination role

1.Open the Policies hub of the IAM console.

2.Choose Create policy, and choose the JSON tab.

3.In the editor, delete any content from the editor and paste this policy document.

   {
   "Version": "2012-10-17",
   "Statement": [
       {
           "Effect": "Allow",
           "Action": [
               "iot:DescribeEndpoint",
               "iot:Publish"
           ],
           "Resource": "*"
       }
   ]
   }

4.Choose Review policy, and in Name, enter a name for this policy. You'll need this name to use in the next procedure. You can also describe this policy in Description, if you want.

5.Choose Create policy.

To create an IAM role for an AWS IoT Core for LoRaWAN destination:

1.Open the Roles hub of the IAM console and choose Create role.

2.In Select type of trusted entity, choose Another AWS account.

3.In Account ID, enter your AWS account ID, and then choose Next: Permissions.

4.In the search box, enter the name of the IAM policy that you created in the previous procedure.

5.In the search results, check the IAM policy that you created in the previous procedure.

6.Choose Next: Tags, and then choose Next: Review.

7.In Role name, enter the name of this role, and then choose Create role.

8.In the confirmation message, choose the name of the role you created to edit the new role.

9.In Summary, choose the Trust relationships tab, and then choose Edit trust relationship.

10.In Policy Document, change the Principal property to look like this example.

   "Principal": {
   "Service": "iotwireless.amazonaws.com"
   },

After you change the Principal property, the complete policy document should look like this example.

   {
"Version": "2012-10-17",
"Statement": [
   {
     "Effect": "Allow",
     "Principal": {
       "Service": "iotwireless.amazonaws.com"
     },
     "Action": "sts:AssumeRole",
     "Condition": {}
   }
]
   }

11.To save your changes and exit, choose Update Trust Policy.

With this role defined, you can find it in the list of roles when you configure your AWS IoT Core for LoRaWAN destinations.

3.2.1 CREATE DESTINATION

Select Destinations view

Please click on “Destinations” menu and afterwards please click on “Add destination”.

https://wiki.dragino.com/images/thumb/1/1e/Select_destinations.png/600px-Select_destinations.png

Select destinations

Provide input

Please provide the following input:

 Please choose an IAM role that shall be used for the invocation of AWS IoT Rule.
Please select the destination name (e.g. “WorkshopDestination<Your name without spaces>”)
Please select the AWS IoT Rule name (e.g. “MyWorkshopLoRaWANRule<Your name without spaces>”)

click on “Add destination”

https://wiki.dragino.com/images/thumb/d/df/Provide_input.png/600px-Provide_input.png

provide input

3.2.2 CREATE IOT RULE

Create Rule

user navigating to AWS IoT , Act, Rules and clicking on “Create” button.

https://wiki.dragino.com/images/thumb/2/24/Rule_Create.png/600px-Rule_Create.png

Rule create

Provide a rule name'

Please input the same IoT Rule name you used in a previous step when creating a new AWS IoT Core for LoRaWAN destination (e.g. “MyWorkshopLoRaWANRule”). Please double-check that the rule is named exactly the same, considering letter case.

https://wiki.dragino.com/images/thumb/b/ba/Enter_rule_name.png/600px-Enter_rule_name.png

Enter Rule name

Provide IoT SQL query statement

Please put the following text into the rule query statement : SELECT *, timestamp() as timestamp

https://wiki.dragino.com/images/thumb/f/f2/Enter_SQL_query_statement.png/600px-Enter_SQL_query_statement.png

Enter SQL query statement

Select action “Republish to an AWS IoT Topic”

https://wiki.dragino.com/images/thumb/3/31/Seleck_topic.png/600px-Seleck_topic.png

Seleck topic

Configure action “Republish to an AWS IoT Topic”

Please provide dt/workshop_lorawanmessages as a topic name. Please click on “Create role” and select e.g. “IoTRuleRepublishRole” as a role name. Please scroll down and click on “Create action”.

https://wiki.dragino.com/images/thumb/c/cb/Enter_topic_name.png/600px-Enter_topic_name.png

Enter topic name

Create rule

https://wiki.dragino.com/images/thumb/c/c4/Create_rule.png/600px-Create_rule.png

create rule

3.3 Step 3: Create a destination for end node payloadDate

The PayloadDate of the node will be sent to this destination

a.In the destinatons,choose add destination.

b.In IMA Role,select Role you created in the last step

c.In Destination name,gie this destination a destination name.

d.In Rule name,give this rule a new name and remember it.

e.choose Add Destination.

https://wiki.dragino.com/images/thumb/2/2e/Add_destination.png/600px-Add_destination.png

Add destination

Note:The destination rule configuration sent by the end node payloadDate will be in the next step

3.4 Step 4: CREATE WIRELESS DEVICE

Users can register end nodes with AWS

3.4.1 1.Open Add wireless device

a.In Device,choose Add wireless device.
b.In Wireless device specification,select OTTA 1.0.x
c.In OTTA 1.0.x
d.In Profiles,select profilees,and choose Next

https://wiki.dragino.com/images/thumb/d/d4/Add_device.png/600px-Add_device.png

Add device

https://wiki.dragino.com/images/thumb/a/ab/Add_device2.png/600px-Add_device2.png

Enter device keys

Select device and service profile

Scroll down and endure that “Associate a thing with your wireless device” option is enabled. AWS will create a thing in AWS IoT for you and associate it with this device. Things in AWS IoT can make it easier to search for and manage your devices.

https://wiki.dragino.com/images/thumb/f/f2/Seleck_porfile.png/600px-Seleck_porfile.png

Seleck profile

3.4.2 2.Select a destination

Plese select a destination you have created in one of the previous steps and choose add device

https://wiki.dragino.com/images/thumb/a/a8/Add_device_choose_destination.png/600px-Add_device_choose_destination.png

Add device_choose destition

REVIEW THE CREATED DEVICER

https://wiki.dragino.com/images/thumb/4/4a/Uplink_Received.png/600px-Uplink_Received.png

Uplink Received

VERIFY DEVICE CONNECTIVITY

https://wiki.dragino.com/images/thumb/b/b2/Verify_device_connectivity.png/600px-Verify_device_connectivity.png

Verify device connectivity

3.4.3 3.Verify date ingestion

Open MQTT Test Client

https://wiki.dragino.com/images/thumb/f/f5/Open_MQTT_Client.png/600px-Open_MQTT_Client.png

Open MQTT Client

Subscribe to topic

Please input the topic name workshop_lorawanmessages in “Subscription topic” field and click on “Subscribe to topic”.

https://wiki.dragino.com/images/thumb/7/7b/Subscription_topic.png/600px-Subscription_topic.png

Subscription Topic

Trigger your device to send telemetry

User can start the registered device

Review the incoming message from uesr's device

Please find below an explanation for some of the attributes:

  PayloadData   : Base64-encoded payload from the device
 WirelessMetadata.FPort           : FPort used by the device
 WirelessMetadata.LoRaWAN.DevEui  : EUI of the device sending the data
  WirelessMetadata.LoRaWAN.Gateways: Information on RSSI and SNR per gateway

https://wiki.dragino.com/images/thumb/0/0e/Review_message.png/600px-Review_message.png

Review message

 {
"WirelessDeviceId": "3ff07678-0c3d-4ed7-a01a-4e2cc927e315",
"PayloadData": "AAAAAAAAAAAPo2I=",
"WirelessMetadata": {
   "LoRaWAN": {
     "DataRate": "5",
     "DevEui": "a840410801818749",
     "FCnt": 579,
     "FPort": 2,
     "Frequency": "867500000",
     "Gateways": [
       {
         "GatewayEui": "a84041ffff1ec39f",
         "Rssi": -79,
         "Snr": 7.25
       },
       {
         "GatewayEui": "a84041ffff1ee2a0",
         "Rssi": -129,
         "Snr": -4
       }
     ],
     "Timestamp": "2021-04-10T08:16:06Z"
   }
},
"timestamp": 1618042566345
}

How to decode Base64 data into hexadecimal representation

You can run the following command in your shell to decode Base64 payload data into their hexadecimal representation.

MacOS

$  echo -n "<Value of PayloadData>" | base64 -D | hexdump -v -e '/1 "%02x"'

Linux

$  echo -n "<Value of PayloadData>" | base64 -d | hexdump -v -e '/1 "%02x"'

Example

$  echo -n "7WkoOEfwfTTioxG6CatHBw==" | base64 -D | hexdump -v -e '/1 "%02x"'
   ed69283847f07d34e2a311ba09ab4707

4. Format a notification by using an AWS Lambda function

4.1 Introduction

This tutorial demonstrates how to send MQTT message data to an AWS Lambda action for formatting and sending to another AWS service. In this tutorial, the AWS Lambda action uses the AWS SDK to send the formatted message to the Amazon SNS topic

4.1.1 What you'll learn in this tutorial

How to create and test an AWS Lambda function

How to use the AWS SDK in an AWS Lambda function to publish an Amazon SNS notification

How to use simple SQL queries and functions in a rule query statement

How to use the MQTT client to test an AWS IoT rule

4.1.2 In this tutorial, you'll:

Create an AWS Lambda function that sends a text message

Create an AWS IoT rule with an AWS Lambda rule action

Test the AWS IoT rule and AWS Lambda rule action

4.2 Approach A: DECODING BINARY PAYLOADS

4.2.1 Step 1 :Start deployment of a severless application with AWS Lambda function and AWS ioT Rule

1:Open AWS Lambda console by clicking on link

https://wiki.dragino.com/images/thumb/7/7b/Deployment_Lambda.png/600px-Deployment_Lambda.png

Deployment Lambda

2:Select a decoder

Please scroll down to the bottom of the page. Please provide the parameter decoder name based on the following table.

Device Decoder name

LHT65 dragino_lht65

LBT1 dragino_lbt1

LSE01 dragino_lse01

LGT92 dragino_lgt92

LDS01 dragino_lds01

After that please check the box “I acknowledge that this app creates resource policies.” and click on “Deploy”.

https://wiki.dragino.com/images/thumb/c/cd/Seleck_a_decoder_name.png/600px-Seleck_a_decoder_name.png

Seleck a decoder name

3:Review deployment

Please wait few seconds for a successful deployment. After that please click on the name of the Lambda function “TransformLoRaWANBinaryPayloadFunction”.

https://wiki.dragino.com/images/thumb/f/fc/Review_deployment.png/600px-Review_deployment.png

Review deployment

4. Create the test event

Please click on “Configure the test event”

https://wiki.dragino.com/images/thumb/2/2f/Create_the_test_event.png/600px-Create_the_test_event.png

Create the test event

In the window that opens, please provide the event name e.g. IoTCoreForLoRaWANEvent. After that please paste the following JSON content:

   {
      "PayloadData": "<Sample PayloadData>",
      "WirelessDeviceId": "57728ff8-5d1d-4130-9de2-f004d8722bc2",
      "WirelessMetadata": {
        "LoRaWAN": {
          "DataRate": 0,
          "DevEui": "a84041d55182720b",
          "FPort": 2,
          "Frequency": 867900000,
          "Gateways": [
            {
              "GatewayEui": "dca632fffe45b3c0",
              "Rssi": -76,
              "Snr": 9.75
            }
          ],
          "Timestamp": "2020-12-07T14:41:48Z"
        }
      }
  }

https://wiki.dragino.com/images/thumb/0/07/Create_a_new_event.png/600px-Create_a_new_event.png

Create a new event

5. Provide PayloadData sample

Please replace the string <Sample PayloadData> in the JSON document with a sample payload for the device you selected in step 2 according to this table. After that please click on “Create”.

       Device name          Sample “PayloadData”

LHT65 -- y6QHxgG4AQhmf/8=

       LSE01         --     AuHtlACmawQPVGM=
       LGT92         --     DSEAAAEVCMUGpAA=
       LBT1          --     DxwAAAIDQUJCQ0NEREVFRkYwMjcxMjFGNkFDMy0wNTk=

[[File:replace <Sample PayloadDate>.png|600px|none|thumb|replace <Sample PayloadDate>]]

6. Run a test

Please click on “Test”.

https://wiki.dragino.com/images/thumb/3/3f/TEST.png/600px-TEST.png

TEST
7. Note the AWS Lamdbda function ARN

As a preparation for the next step, please note the ARN of the deployed AWS Lambda function. We will need this ARN later to review the created AWS IoT Rule.

https://wiki.dragino.com/images/thumb/3/3f/ARN.png/600px-ARN.png

ARN

8. Optional: review the source code of the binary decoder Though not required for the purpose of this workshop, feel invited to switch to the “Code” section of the AWS Lambda function and inspect the Python source code. You will find the decoders for the individual devices in the directory decoders. The AWS Lambda function handler is in file app.py

https://wiki.dragino.com/images/thumb/8/8c/Review_the_source_code.png/600px-Review_the_source_code.png

review the source code

4.2.2 Step 2:Update the Destination rule and get device's playload

In this step, you update the IoT rule that forwards the device payload to your application. This rule is associated with the destination created earlier in Set up a Destination for device traffic.

· Find the IoT Rule · Create a Destination with IoT Rule (MyWorkshopLoRaWANRuleWithDecoder_dragino_lht65) · Update the destination to the device · Check the playload · Approach A with MQTT · Approach B with Lamdbda

4.2.2.1 1: Find the IoT Rule MyWorkshopLoRaWANRuleWithDecoder_…

Please put the IoT Rule name prefix MyWorkshopLoRaWANRuleWithDecoder_ into the search field and click on the search symbol. The rule named MyWorkshopLoRaWANRuleWithDecoder_<Decoder name> should appear:

https://wiki.dragino.com/images/thumb/3/39/Find_the_IoT_rule.png/600px-Find_the_IoT_rule.png

Find the IoT rule

4.2.2.2 2: Create a Destination with IoT Rule

(MyWorkshopLoRaWANRuleWithDecoder_dragino_lht65)

https://wiki.dragino.com/images/thumb/0/0a/Create_a_Destnation.png/600px-Create_a_Destnation.png

Create a Destnation

4.2.2.3 3 Update the destination to the device

https://wiki.dragino.com/images/thumb/7/79/Update_the_destanation.png/600px-Update_the_destanation.png

Update the destination to the device

4.2.2.4 4 Check the playload

Approach A with MQTT

click the rule

https://wiki.dragino.com/images/thumb/3/39/Find_the_IoT_rule.png/600px-Find_the_IoT_rule.png

click the rule

Copy the Topic name

https://wiki.dragino.com/images/thumb/b/b6/Copy_the_rule_name1.png/600px-Copy_the_rule_name1.png

copy the rule name

Open MQTT client and subscrlibe Topic

https://wiki.dragino.com/images/thumb/8/80/Open_MQTT_Client1.png/600px-Open_MQTT_Client1.png

Open MQTT client and subscrlibe Topic

Get the payload

https://wiki.dragino.com/images/thumb/b/bd/Get_the_payload1.png/600px-Get_the_payload1.png

Get the payload

Approach B with Lamdbda

open lamdbda console and click the application

https://wiki.dragino.com/images/thumb/3/31/Open_lamdbda_console1.png/600px-Open_lamdbda_console1.png

open lamdbda console and click the application

https://wiki.dragino.com/images/thumb/f/f1/Open_lamdbda_console2.png/600px-Open_lamdbda_console2.png

open lamdbda console and click the application

View logs in CloudWatch

https://wiki.dragino.com/images/thumb/0/01/View1.png/600px-View1.png

View logs in CloudWatch

https://wiki.dragino.com/images/thumb/8/8d/View2.png/600px-View2.png

View logs in CloudWatch

4.3 Approach B Create an AWS Lambda function that sends a text message

The AWS Lambda function in this tutorial receives the result of the rule query statement, inserts the elements into a text string, and sends the resulting string to Amazon SNS as the message in a notification.

4.3.1 Step.1: To create an AWS Lambda function that sends a text message

4.3.1.1 1.Create a new AWS Lambda function.

a.In the AWS Lambda console, choose Create function.

https://wiki.dragino.com/images/thumb/5/59/Creat_function.png/600px-Creat_function.png

Create function

b.In Create function, select Use a blueprint. Search for and select the hello-world-python blueprint, and then choose Configure.

https://wiki.dragino.com/images/thumb/b/b5/Creat_function2.png/600px-Creat_function2.png

blueprint

c.In Basic information:

       1).In Function name, enter the name of this function, format-high-temp-notification.
       2).In Execution role, choose Create a new role from AWS policy templates.
       3).In Role name, enter the name of the new role, format-high-temp-notification-role.
       4).In Policy templates - optional, search for and select Amazon SNS publish policy.
       5).Choose Create function.

https://wiki.dragino.com/images/thumb/4/44/Basic_information.png/600px-Basic_information.png

blueprint

4.3.1.2 2.Modify the blueprint code to format and send an Amazon SNS notification.

a.After you created your function, you should see the format-high-temp-notification details page. If you don't, open it from the Lambda Functions page.

b.In the format-high-temp-notification details page, choose the Configuration tab and scroll to the Function code panel.

c.In the Function code window, in the Environment pane, choose the Python file, lambda_function.py.

d.In the Function code window, delete all of the original program code from the blueprint and replace it with this code.

 from __future__ import print_function
import json
import boto3
print('Loading function')
def lambda_handler(event, context):
   # Parse the JSON message
   eventText = json.dumps(event)
   # Print the parsed JSON message to the console. You can view this text in the Monitoring tab in the AWS Lambda console or in the Amazon CloudWatch Logs console.
   print('Received event: ', eventText)
   # Create an SNS client
   sns = boto3.client('sns')
   # Publish a message to the specified topic
   response = sns.publish (
     TopicArn = 'arn:aws:iam::123456789012:My_IoT_SNS_Topic',
     Message = eventText
   )
   print(response)

e.Choose Deploy.

Note:Replace the value of TopicArn with the ARN of the Amazon SNS topic that you created SNS topic in which SNS

Note:If you don't have SNS topic it does't affect the use of Lambda.About how to Create SNS topic will be show next chapter

4.3.1.3 3.In a new window, look up the Amazon Resource Name (ARN) of your Amazon SNS topic from the tutorial about how to Send an Amazon SNS notification.

a.In a new window, open the Topics page of the Amazon SNS console

b.In the Topics page, find the high_temp_notice notification topic in the list of Amazon SNS topics.

c.Find the ARN of the high_temp_notice notification topic to use in the next step.

4.3.1.4 4.Create a test case for your Lambda function.

a.In the Lambda Functionspage of the console,from Select a test eventto select Configure test events

https://wiki.dragino.com/images/thumb/0/08/Configure_test_events.png/600px-Configure_test_events.png

Configure test events

b.On Configure test event, enter a name for your test event and replace the message JSON with the following:

 {
"message" : "Hello, world"
}

c.choose Create

https://wiki.dragino.com/images/thumb/b/b2/Event_create.png/600px-Event_create.png

event create

d.In the upper right of the Lambda function details page, choose Test to test your Lambda function with the message you specified in the test event.

https://wiki.dragino.com/images/thumb/8/85/Event_test.png/600px-Event_test.png

event test

e.Under your Lambda function code, on the Execution result tab, you see the output from the Lambda function.

https://wiki.dragino.com/images/thumb/0/08/Event_output.png/600px-Event_output.png

event output

Now The Lambda function table is created

4.3.2 Step.2: Create Rule with Lambda function

1.Browse to the AWS IoT console, and in the navigation pane, select Act

File:Console Act.png

console_Act

2.Choose Create to create an AWS IoT rule.

https://wiki.dragino.com/images/thumb/7/73/Create_Rule.png/600px-Create_Rule.png

create Rule

3.On the Create a rule page, enter a name for your rule.

https://wiki.dragino.com/images/thumb/1/14/Enter_a_Rule_name.png/600px-Enter_a_Rule_name.png

enter a Rule name

4.In Rule query statement, enter the following query:

SELECT * FROM "my/lambda/topic"

https://wiki.dragino.com/images/thumb/d/dd/Enter_the_following_query.png/600px-Enter_the_following_query.png

enter the following query

5.In Set one or more actions, select Add action.

https://wiki.dragino.com/images/thumb/d/d8/Add_action.png/600px-Add_action.png

Add action

6.Under the select operation, select Send a Message to a Lambda Function, and then select the configuration operation.

https://wiki.dragino.com/images/thumb/4/49/Select_operation.png/600px-Select_operation.png

select operation

5. For End Node

Decode LHT65

6. Reference

Tags:
Created by Xiaoling on 2022/05/16 09:44
    
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0