Show last authors
1 **~ Table of Contents:**
2
3 {{toc/}}
4
5
6
7
8
9 = **1. Introduction** =
10
11
12 //The Dragino LoRaWAN gateway can communicate with [[Amazon>>url:https://portal.aws.amazon.com/]] LoRaWAN Network Server~://
13
14
15 (% style="color:blue" %)**//Below list of the support products and Requirements~://**
16
17 1. //LoRaWAN Gateway model: [[LIG16>>url:http://www.dragino.com/products/lora-lorawan-gateway/item/171-lig16.html]], [[LG308>>url:http://www.dragino.com/products/lora-lorawan-gateway/item/140-lg308.html]], [[DLOS8>>url:http://www.dragino.com/products/lora-lorawan-gateway/item/160-dlos8.html]] [[LPS8>>url:http://www.dragino.com/products/lora-lorawan-gateway/item/148-lps8.html]]//
18 1. //Firmware version since:[[lgw~~-~~-build-v5.4.1614945073>>url:http://www.dragino.com/downloads/index.php?dir=LoRa_Gateway/LIG16/Firmware/Release/lgw--build-v5.4.1614945073-20210305-1953/]]//
19
20 (% style="color:blue" %)**//What you'll learn in this tutorial//**
21
22 //1. How to register a gateway with AWS//
23
24 //2. How to register an end node with AWS//
25
26 //3. How to accept payload date with AWS//
27
28
29
30 = **2. Gateway Setup Instruction** =
31
32
33 == **2.1 Step** **1:  Sign up for [[an AWS >>url:https://portal.aws.amazon.com/]]account and select the area** ==
34
35
36 //Sign up for an AWS account//
37
38 [[image:image-20220524141920-1.png]]
39
40
41
42 //Select area//
43
44 [[image:image-20220524142008-2.png]]
45
46
47 (% style="color:red" %)**//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, their options will not affect your usage.//**
48
49
50
51 == **2.2 Step 2:  [[Add an IAM role>>url:https://console.aws.amazon.com/iam/home#/roles]]** ==
52
53
54 (% style="color:blue" %)**//To add the IAM role to allow the Configuration and Update Server (CUPS) to manage gateway credentials:、//**
55
56 (% style="color:red" %)//**1.**//(%%)// Open the Roles hub of the IAM console and choose Create role.//
57
58 (% style="color:red" %)//**2.**//(%%)// If you think that you might have already added the IoTWirelessGatewayCertManagerRole role, in the search bar, enter IoTWirelessGatewayCertManagerRole.//
59
60 //If you see an IoTWirelessGatewayCertManagerRole role in the search results, you have the necessary IAM role. You can leave the procedure now.//
61
62 //If the search results are empty, you don't have the necessary IAM role. Continue the procedure to add it.//
63
64 (% style="color:red" %)//**3. **//(%%)//In Select type of trusted entity, choose Another AWS account.//
65
66 (% style="color:red" %)//**4.**//(%%)// In Account ID, enter your AWS account ID and then choose Next: Permissions.//
67
68 (% style="color:red" %)//**5.**//(%%)// In the search box, enter AWSIoTWirelessGatewayCertManager.//
69
70 (% style="color:red" %)//**6.**//(%%)// In the list of search results, select the policy named AWSIoTWirelessGatewayCertManager.//
71
72 (% style="color:red" %)//**7.**//(%%)// Choose Next: Tags, and then choose Next: Review.//
73
74 (% style="color:red" %)//**8.** //(%%)//In Role name, enter IoTWirelessGatewayCertManagerRole, and then choose to Create role.//
75
76 (% style="color:red" %)//**9.**//(%%)// To edit the new role, in the confirmation message, choose IoTWirelessGatewayCertManagerRole.//
77
78 (% style="color:red" %)//**10.**//(%%)// In Summary, choose the Trust relationships tab and then choose Edit trust relationship.//
79
80 (% style="color:red" %)//**11.**//(%%)// In the Policy Document, change the Principal property to look like this example.//
81
82
83 (% class="box" %)
84 (((
85 "Principal": {
86 "Service": "iotwireless.amazonaws.com"
87 },
88 )))
89
90 (% class="box" %)
91 (((
92 After you change the Principal property, the complete policy document should look like this example.
93 )))
94
95 (% class="box" %)
96 (((
97 {
98 "Version": "2012-10-17",
99 "Statement": [
100 {
101 "Effect": "Allow",
102 "Principal": {
103 "Service": "iotwireless.amazonaws.com"
104 },
105 "Action": "sts:AssumeRole",
106 "Condition": {}
107 }
108 ]
109 }
110
111
112 )))
113
114
115 (% style="color:red" %)**12.** (%%)To save your changes and exit, choose Update Trust Policy. (% style="color:red" %)Note: You've now created the IoTWirelessGatewayCertManagerRole. You won't need to do this again.
116
117 [[image:image-20220530171729-40.png]]
118
119 tart create role
120
121
122
123 [[image:image-20220530172709-43.png]]
124
125 Choose Another AWS account
126
127
128
129 [[image:image-20220530171729-40.png]]
130
131 start Create policy
132
133
134
135 [[image:image-20220530172904-44.png]]
136
137 Choose Next review
138
139
140
141 [[image:image-20220530173653-48.png]]
142
143
144 In role name,enter AWSIoTWirelessGatewayCertManagerRole,and then choose Create role.
145
146
147 [[image:image-20220530180203-53.png]]
148
149
150 To edit the_new role
151
152
153 [[image:image-20220530180326-54.png]]
154
155 Choose Edit trust relationship.
156
157
158
159 [[image:image-20220530180604-56.png]]
160
161
162 Change policy Document
163
164
165
166 == **2.3 Step 3: Add gateway** ==
167
168
169 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
170
171
172 [[image:image-20220530162125-11.png]]
173
174 Locate IoT Core in the AWS Service Control Panel
175
176
177
178 [[image:image-20220530162223-12.png]]
179
180
181 locate the Wireless Connection in the IoT Core Control Panel
182
183
184
185 == **2.4 Step 4: Get a unique Gateway ID in the gateway** ==
186
187
188 Every gateway has a Gateway ID, which can be found on LoRaWAN ~-~- Basic Station page.
189
190
191 [[image:image-20220909142759-1.png]]
192
193 Gateway ID
194
195
196
197 (% style="color:red" %)**Note**:(%%) User must use the Gateway ID of the LoRaWAN ~-~- Basic Station page as the EUI
198
199 (% style="color:red" %)**Notice**:(%%) In the latest firmware. the AWS-IoT configured page is under the LoRaWAN Basic Station page.
200
201
202 [[image:image-20220909143444-2.png]]
203
204
205 LoRaWAN Basic Station
206
207
208
209 == 2.5 Step 5: Configure the gateway and Creation gateway certificates ==
210
211
212 Configure the gateway and Creation gateway certificates
213
214
215 [[image:image-20220530162446-14.png]]
216
217 Configure the gateway
218
219
220
221 [[image:image-20220530162523-15.png]]
222
223 Creation gateway certificates
224
225
226 (% style="color:red" %)**Note: **(%%)User Make sure the name of the downloaded file remains the same.
227
228
229
230 == 2.6 Step 6: Add the configuration to the Gateway Web UI. ==
231
232
233 user returns to your gateway, and start adding the three file they got to the gateway.
234
235
236 [[image:image-20220909143523-3.png]]
237
238 Configure the gateway
239
240
241
242 if the user Upload successfully you will see:
243
244 [[image:image-20220909143551-4.png||height="417" width="1057"]]
245
246 Uploaded successfully
247
248
249 (% style="color:red" %)**Notice: **(% style="color:black" %)User must select the correct Upload on the suffix name, otherwise the upload fails.
250
251 == ==
252
253 (% style="color:red" %)**Notice:**(%%) After the user hits "(% style="color:blue" %)**Save&Apply**"(%%), the gateway will start Station mode on its own.
254
255
256
257 == 2.8 Step 8: Check the last upLink received ==
258
259
260 After the above settings, the gateway should be able to connect, the figure below shows the data of the Last upLink received
261
262
263 **On the gateway Web UI**
264
265 [[image:image-20220909172235-8.png||height="657" width="1313"]]
266
267
268 **On the AWS console**
269
270 [[image:image-20220909171935-6.png||height="458" width="1310"]]
271
272 [[image:image-20220909171754-5.png||height="790" width="1307"]]
273
274 UpLink received
275
276
277
278 == 2.9 Trouble Shooting ==
279
280
281 In normal cases, the logs of the gateway station are as follows:
282
283 [[image:image-20220905143445-12.png]]
284
285
286 [[image:image-20220905143031-9.png]]
287
288
289
290 === **1. WS connect failed: NET - Failed** ===
291
292
293 [[image:image-20220923091933-2.png]]
294
295
296 The log output indicates that the gateway was unable to resolve to an IP address from the domain name and then connect to the AWS lns server.
297
298 Here is the advice:
299
300 **//1). Whether the gateway is registered cycle to AWS multiple times?//**
301
302 **//2). Check if the  gateway's EUI is the same as the AWS's//**
303
304 **//3). Change the gateway EUI and use the new EUI to register and connect the AWS.//**
305
306
307 === 1.  gateway station log displays "Recv failed: SSL - The peer notified us that The connection is going to be closed". ===
308
309
310 In this case, the user needs to check whether the Gateway ID matches the GatewayEUI on AWS IoT Core.
311
312 If the configuration is incorrect, user can first click **"Restore_Configuration"** and then reconfigure it.
313
314 [[image:image-20220905110033-2.png]]
315
316
317
318 === 2.  gateway station log displays "Failed to retrieve TCURI from CUPS: (400) Bad Request". ===
319
320
321 In this case, the user needs to check whether the CUPS URI matches the CUPS URI on AWS IoT Core.
322
323 It is recommended to re-check the configuration by following **[[Step 5>>http://wiki.dragino.com/xwiki/bin/view/Main/AWS%20IoT%20Core%20for%20LoRaWAN/#H2.5Step5:ConfigurethegatewayandCreationgatewaycertificates]]** in the wiki.
324
325 [[image:image-20220905141143-8.png]]
326
327
328
329 === 3.  gateway station log displays "[4] Send failed: NET - Connection was reset by peer" ===
330
331
332 If the following information is displayed in the gateway station log, it indicates that the station version is too early and the configuration file is missing,
333
334 Users need to upgrade the firmware of the gateway. users can download the latest firmware from this link : **[[lgw~~-~~-build-v5.4.1661158991-20220822-1704>>https://www.dragino.com/downloads/index.php?dir=LoRa_Gateway/LPS8/Firmware/Release/lgw--build-v5.4.1661158991-20220822-1704/]]**
335
336
337 [[image:image-20220905140823-7.png]]
338
339
340 = 3. Add wireless device =
341
342 == 3.1 step 1 : Add device profile ==
343
344 === 3.1.1 CREATE DEVICE PROFILE ===
345
346
347 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.
348
349
350 [[image:image-20220530163030-21.png]]
351
352 Add device profile
353
354
355 **Provide device data**
356
357 (% class="box" %)
358 (((
359 (% style="color:#037691" %)**Device profile name** (%%): a descriptive name of your choice for this profile, e.g. “Workshop device profile of ”
360 (% style="color:#037691" %)**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”.
361 (% style="color:#037691" %)**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.
362 (% style="color:#037691" %)**Regional parameters version**(%%): please select RP002-1.0.1, unless specified otherwise in your device documentation
363 (% style="color:#037691" %)**MaxEIRP**(%%) : please enter the default value 15
364 (% style="color:#037691" %)**Supports class **(%%): please select class A, unless you are aware of specific requirements to use classes B and C and your device supports it
365 )))
366
367 [[image:image-20220530163105-22.png]]
368
369 device profile
370
371
372 (% style="color:red" %)**Notice** **: See the documentation for more information: [[https:~~/~~/docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan-define-profiles.html>>url:https://docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan-define-profiles.html]]**
373
374
375
376 === 3.1.2 CREATE SERVICE PROFILE ===
377
378
379 Select (% style="color:blue" %)**“Profiles”**(%%) view and click on (% style="color:blue" %)**“Add service profile”.**
380
381 [[image:image-20220530163201-23.png]]
382
383 Add a service profile
384
385
386 (% class="box" %)
387 (((
388 Choose a service profile name (e.g. “Default service profile”)
389 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.
390 This metadata could be used for network coverage optimization.
391 Finally, scroll down and click on “Add service profile”.
392 )))
393
394
395 [[image:image-20220530163352-25.png]]
396
397 Add service profile2
398
399
400
401 == 3.2 Step 2 : Create an IAM roles for destinations ==
402
403
404 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.
405
406
407 (% style="color:blue" %)**To create an IAM policy for your AWS IoT Core for LoRaWAN destination role:**
408
409 (% style="color:red" %)**1.**(%%) Open the Policies hub of the IAM console.
410
411 (% style="color:red" %)**2. **(%%)Choose Create policy, and choose the JSON tab.
412
413 (% style="color:red" %)**3. **(%%)In the editor, delete any content from the editor and paste this policy document.
414
415 (% class="box" %)
416 (((
417 {
418 "Version": "2012-10-17",
419 "Statement": [
420 {
421 "Effect": "Allow",
422 "Action": [
423 "iot:DescribeEndpoint",
424 "iot:Publish"
425 ],
426 "Resource": "*"
427 }
428 ]
429 }
430 )))
431
432 (% style="color:red" %)**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.
433
434 (% style="color:red" %)**5.**(%%) Choose Create policy.
435
436
437 (% style="color:blue" %)**To create an IAM role for an AWS IoT Core for LoRaWAN destination:**
438
439 (% style="color:red" %)**1.** (%%)Open the Roles hub of the IAM console and choose Create role.
440
441 (% style="color:red" %)**2.**(%%) In Select type of trusted entity, choose Another AWS account.
442
443 (% style="color:red" %)**3. **(%%)In Account ID, enter your AWS account ID, and then choose Next: Permissions.
444
445 (% style="color:red" %)**4.**(%%) In the search box, enter the name of the IAM policy that you created in the previous procedure.
446
447 (% style="color:red" %)**5.**(%%) In the search results, check the IAM policy that you created in the previous procedure.
448
449 (% style="color:red" %)**6.**(%%) Choose Next: Tags, and then choose Next: Review.
450
451 (% style="color:red" %)**7. **(%%)In Role name, enter the name of this role, and then choose Create role.
452
453 (% style="color:red" %)**8.**(%%) In the confirmation message, choose the name of the role you created to edit the new role.
454
455 (% style="color:red" %)**9. **(%%)In Summary, choose the Trust relationships tab, and then choose Edit trust relationship.
456
457 (% style="color:red" %)**10.**(%%) In Policy Document, change the Principal property to look like this example.
458
459 (% class="box" %)
460 (((
461 "Principal": {
462 "Service": "iotwireless.amazonaws.com"
463 },
464 )))
465
466
467 After you change the Principal property, the complete policy document should look like this example.
468
469 (% class="box" %)
470 (((
471 {
472 "Version": "2012-10-17",
473 "Statement": [
474 {
475 "Effect": "Allow",
476 "Principal": {
477 "Service": "iotwireless.amazonaws.com"
478 },
479 "Action": "sts:AssumeRole",
480 "Condition": {}
481 }
482 ]
483 }
484 )))
485
486 (% style="color:red" %)**11. **(%%)To save your changes and exit, choose Update Trust Policy.
487
488 With this role defined, you can find it in the list of roles when you configure your AWS IoT Core for LoRaWAN destinations.
489
490
491
492 === 3.2.1 CREATE DESTINATION ===
493
494
495 (% style="color:blue" %)**Select Destinations view**
496
497
498 Please click on **“(% style="color:red" %)Destinations” (%%)**menu and afterwards please click on **“(% style="color:red" %)Add destination”(%%)**(% style="color:red" %).
499
500 [[image:image-20220530163507-26.png]]
501
502 Select destinations
503
504
505
506 (% style="color:blue" %)**Provide input**
507
508 Please provide the following input:
509
510 (% class="box" %)
511 (((
512 Please choose an IAM role that shall be used for the invocation of AWS IoT Rule.
513 Please select the destination name (e.g. “WorkshopDestination<Your name without spaces>”)
514 Please select the AWS IoT Rule name (e.g. “MyWorkshopLoRaWANRule<Your name without spaces>”)
515 )))
516
517
518 click on **“(% style="color:red" %)Add destination”(%%)**
519
520 [[image:image-20220530163600-27.png]]
521
522 provide input
523
524
525
526 === 3.2.2 CREATE IOT RULE ===
527
528
529 (% style="color:blue" %)**Create Rule**
530
531 user navigating to AWS IoT , Act, Rules and clicking on (% style="color:red" %)**“Create” **(%%)button.
532
533
534 [[image:image-20220525193329-45.png]]
535
536 Rule create
537
538
539
540 **//Provide a rule name'//**
541
542 (((
543 Please input the same IoT Rule name you used in a previous step when creating a new AWS IoT Core for the LoRaWAN destination (e.g. “test_example”).
544 )))
545
546 [[image:image-20220601100315-2.png]]
547
548 Enter Rule name
549
550
551
552 (% style="color:blue" %)**Provide IoT SQL query statement**
553
554 Please put the following text into the rule query statement: **SELECT *, timestamp() as timestamp**
555
556 [[image:image-20220601100401-3.png]]
557
558 Enter SQL query statement
559
560
561 **Select action “(% style="color:red" %)Republish to an AWS IoT Topic”(%%)**
562
563 [[image:image-20220601100505-4.png]]
564
565
566
567 == 3.3 Step 3 : Create a destination for end node payloadDate ==
568
569
570 (% style="color:blue" %)**The PayloadDate of the node will be sent to this destination**
571
572 (% style="color:red" %)**a.**(%%) In the [[destinatons>>url:https://console.aws.amazon.com/iot/home?region=us-east-1#/wireless/destinations]],choose add destination.
573
574 (% style="color:red" %)**b.**(%%) In IMA Role, select the Role you created in the last step
575
576 (% style="color:red" %)**c.** (%%)In Destination name, give this destination a destination name.
577
578 (% style="color:red" %)**d.**(%%) In Rule name, give this rule a new name and remember it.
579
580 (% style="color:red" %)**e.**(%%) choose Add Destination.
581
582 [[image:image-20220524204934-9.png||height="663" width="627"]]
583
584 Add destination
585
586
587 (% style="color:red" %)**Note: **(%%)The destination rule configuration sent by the end node payloadDate will be in the next step.
588
589
590
591 == 3.4 Step 4 : CREATE WIRELESS DEVICE ==
592
593
594 Users can register end nodes with AWS
595
596
597
598 === 3.4.1 Open Add wireless device ===
599
600
601 (% class="box" %)
602 (((
603 (((
604 (% style="color:red" %)**a.**(%%) In Device,choose Add wireless device.
605 (% style="color:red" %)**b.**(%%) In Wireless device specification,select OTTA 1.0.x
606 (% style="color:red" %)**c. **(%%)In OTTA 1.0.x
607 (% style="color:red" %)**d.**(%%) In Profiles,select profilees,and choose Next
608 )))
609 )))
610
611 [[image:image-20220530164137-28.png]]
612
613 Add device
614
615
616
617 [[image:image-20220530164246-29.png]]
618
619 Enter device keys
620
621
622
623 (% style="color:blue" %)**Select device and service profile**
624
625 (((
626 Scroll down and endure that (% style="color:red" %)**“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.
627
628
629 )))
630
631 [[image:image-20220530164324-30.png]]
632
633 Seleck profile
634
635
636
637 === 3.4.2 Select a destination ===
638
639
640 **Plese select a destination you have created in one of the previous steps and choose add device**
641
642 [[image:image-20220530164504-32.png]]
643
644 Add device_choose destition
645
646
647 **REVIEW THE CREATED DEVICER**
648
649 [[image:image-20220530164440-31.png]]
650
651 Uplink Received
652
653
654 **VERIFY DEVICE CONNECTIVITY**
655
656 [[image:image-20220530164545-33.png]]
657
658 Verify device connectivity
659
660
661
662 === 3.4.3 Verify date ingestion ===
663
664
665 (% style="color:blue" %)**Open MQTT Test Client**
666
667 [[image:image-20220530164819-36.png]]
668
669
670 Open MQTT Client
671
672
673 (% style="color:blue" %)**Subscribe to topic**
674
675 (((
676 Please input the topic name (% style="color:red" %)**workshop_lorawanmessages**(%%) in **“(% style="color:red" %)Subscription topic”(%%)**(% style="color:red" %) (%%)field and click on **“(% style="color:red" %)Subscribe to topic”(%%)**(% style="color:red" %).
677
678 Users can choose to subscribe to "**#**" ,"#" indicates to subscribe to all.
679
680 [[image:image-20220530170719-38.png]]
681 )))
682
683
684 Subscription Topic
685
686
687
688 (% style="color:blue" %)**Trigger your device to send telemetry**
689
690 User can start the registered device
691
692
693 (% style="color:blue" %)**Review the incoming message from uesr's device**
694
695 Please find below an explanation for some of the attributes:
696
697 (% class="box" %)
698 (((
699 (% style="color:#037691" %)**PayloadData** (%%) :  Base64-encoded payload from the device
700 (% style="color:#037691" %)**WirelessMetadata.FPort**(%%) :  FPort used by the device
701 (% style="color:#037691" %)**WirelessMetadata.LoRaWAN.DevEui**(%%)  :  EUI of the device sending the data
702 (% style="color:#037691" %)**WirelessMetadata.LoRaWAN.Gateways **(%%): Information on RSSI and SNR per gateway
703 )))
704
705 [[image:image-20220530170810-39.png]]
706
707 Review message
708
709
710 (% class="box" %)
711 (((
712 {
713 "WirelessDeviceId": "3ff07678-0c3d-4ed7-a01a-4e2cc927e315",
714 "PayloadData": "AAAAAAAAAAAPo2I=",
715 "WirelessMetadata": {
716 "LoRaWAN": {
717 "DataRate": "5",
718 "DevEui": "a840410801818749",
719 "FCnt": 579,
720 "FPort": 2,
721 "Frequency": "867500000",
722 "Gateways": [
723 {
724 "GatewayEui": "a84041ffff1ec39f",
725 "Rssi": -79,
726 "Snr": 7.25
727 },
728 {
729 "GatewayEui": "a84041ffff1ee2a0",
730 "Rssi": -129,
731 "Snr": -4
732 }
733 ],
734 "Timestamp": "2021-04-10T08:16:06Z"
735 }
736 },
737 "timestamp": 1618042566345
738 }
739 )))
740
741
742 (% style="color:blue" %)**How to decode Base64 data into hexadecimal representation**
743
744 You can run the following command in your shell to decode Base64 payload data into their hexadecimal representation.
745
746
747 **MacOS**
748
749 (% class="box" %)
750 (((
751 **$  echo -n "<Value of PayloadData>" | base64 -D | hexdump -v -e '/1 "%02x"'**
752 )))
753
754
755 **Linux**
756
757 (% class="box" %)
758 (((
759 **$  echo -n "<Value of PayloadData>" | base64 -d | hexdump -v -e '/1 "%02x"'**
760 )))
761
762
763 **Example**
764
765 (% class="box" %)
766 (((
767 **$  echo -n "7WkoOEfwfTTioxG6CatHBw==" | base64 -D | hexdump -v -e '/1 "%02x"'
768 ed69283847f07d34e2a311ba09ab4707**
769 )))
770
771
772
773 = 4. Format a notification by using an AWS Lambda function =
774
775 == 4.1 Introduction ==
776
777
778 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
779
780
781
782 === 4.1.1 What you'll learn in this tutorial ===
783
784
785 How to create and test an AWS Lambda function
786
787 How to use the AWS SDK in an AWS Lambda function to publish an Amazon SNS notification
788
789 How to use simple SQL queries and functions in a rule query statement
790
791 How to use the MQTT client to test an AWS IoT rule
792
793
794
795 === 4.1.2 In this tutorial, you'll: ===
796
797
798 (((
799 Create an AWS Lambda function that sends a text message
800 )))
801
802 (((
803 Create an AWS IoT rule with an AWS Lambda rule action
804 )))
805
806 (((
807 Test the AWS IoT rule and AWS Lambda rule action
808
809
810
811 )))
812
813 == 4.2 Approach A: DECODING BINARY PAYLOADS ==
814
815
816 === 4.2.1 Step 1 : Start deployment of a severless application with AWS Lambda function and AWS ioT Rule ===
817
818
819 (% style="color:blue" %)**1. Open AWS Lambda console by clicking on [[link>>url:https://console.aws.amazon.com/lambda/home?region=us-east-1#/create/app?applicationId=arn:aws:serverlessrepo:us-east-1:614797420359:applications/aws-iot-corelorawan-sampledecoder]]**
820
821 [[image:image-20220525193652-47.png]]
822
823 Deployment Lambda
824
825
826
827 (% style="color:blue" %)**2. Select a decoder**
828
829 (((
830 Please scroll down to the bottom of the page. Please provide the parameter decoder name based on the following table.
831 )))
832
833 Device Decoder name
834
835 LHT65 dragino_lht65
836
837 LBT1 dragino_lbt1
838
839 LSE01 dragino_lse01
840
841 LGT92 dragino_lgt92
842
843 LDS01 dragino_lds01
844
845 After that please check the box **“(% style="color:red" %)I acknowledge that this app creates resource policies.”(%%)** and click on **“(% style="color:red" %)Deploy”(%%)**(% style="color:red" %).
846
847 [[image:image-20220530155623-1.png]]
848
849 Seleck a decoder name
850
851
852
853 (% style="color:blue" %)**3. Review deployment**
854
855 (((
856 Please wait few seconds for a successful deployment. After that please click on the name of the Lambda function **“(% style="color:red" %)TransformLoRaWANBinaryPayloadFunction”(%%)**(% style="color:red" %).
857 )))
858
859 [[image:image-20220530155838-2.png]]
860
861 Review deployment
862
863
864
865 (% style="color:blue" %)**4. Create the test event**
866
867 Please click on **“(% style="color:red" %)Configure the test event”(%%)**
868
869 [[image:image-20220530155925-3.png]]
870
871 Create the test event
872
873
874 (((
875 In the window that opens, please provide the event name e.g. IoTCoreForLoRaWANEvent. After that please paste the following JSON content:
876 )))
877
878 (% class="box" %)
879 (((
880 {
881 "PayloadData": "<Sample PayloadData>",
882 "WirelessDeviceId": "57728ff8-5d1d-4130-9de2-f004d8722bc2",
883 "WirelessMetadata": {
884 "LoRaWAN": {
885 "DataRate": 0,
886 "DevEui": "a84041d55182720b",
887 "FPort": 2,
888 "Frequency": 867900000,
889 "Gateways": [
890 {
891 "GatewayEui": "dca632fffe45b3c0",
892 "Rssi": -76,
893 "Snr": 9.75
894 }
895 ],
896 "Timestamp": "2020-12-07T14:41:48Z"
897 }
898 }
899 }
900 )))
901
902
903 [[image:image-20220530160030-4.png]]
904
905 Create a new event
906
907
908
909 (% style="color:blue" %)**5. Provide PayloadData sample**
910
911 (((
912 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 (% style="color:red" %)**“Create”**.
913 )))
914
915 (% class="box" %)
916 (((
917 (((
918 Device name          Sample “PayloadData”
919 )))
920
921 (((
922 LHT65  ~-~-  y6QHxgG4AQhmf/8=
923 )))
924
925 (((
926 LSE01         ~-~-     AuHtlACmawQPVGM=
927 LGT92         ~-~-     DSEAAAEVCMUGpAA=
928 LBT1  ~-~-     DxwAAAIDQUJCQ0NEREVFRkYwMjcxMjFGNkFDMy0wNTk=
929 )))
930 )))
931
932 ~[~[File:replace <Sample PayloadDate>.png|600px|none|thumb|replace <Sample PayloadDate>]]
933
934
935
936 (% style="color:blue" %)**6. Edit project Files**
937
938 The project file needs to be modified when the user needs to test or add a new decoder, 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**
939
940 Users can find the required decoder in this **[[link>>url:https://github.com/aws-samples/aws-iot-core-lorawan/tree/main/transform_binary_payload]], **then added to the project.
941
942 How to modify see the figure below:
943
944 (% class="box" %)
945 (((
946 1) Add a decoder in app.py  ~-~-->  For example, add code: import dragino_lht52
947
948 2) Add the decoder name to "VALID_PAYLOAD_DECODER_NAMES"  ~-~-->  add dragino_lht52
949
950 3) Add the statement  ~-~-->  DECODER_NAME = "dragino_lht52"
951
952 **(The user can input the decoding project that needs to be tested)**
953
954 4)Comment out "DECODER_NAME = os.environ.get("DECODER_NAME")"
955
956 **(Comment out it in order to use other project files)**
957 )))
958
959 [[image:image-20220601100037-1.png]]
960
961 After the user finishes editing, Click on the **"(% style="color:red" %)Deploy"(%%)**
962
963
964
965 (% style="color:blue" %)**7. Run a test**
966
967 Please click on **“(% style="color:red" %)Test”(%%)**(% style="color:red" %).
968
969 [[image:image-20220530160314-5.png]]
970
971
972
973 (% style="color:blue" %)**8. Check whether the decoding succeeds **
974
975 The following example is the successful decoding of LHT52,
976
977 [[image:image-20220525100532-4.png||height="488" width="612"]]
978
979
980
981 (% style="color:blue" %)**9. Note the AWS Lamdbda function ARN**
982
983 (((
984 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.
985 )))
986
987 [[image:image-20220530160518-6.png]]
988
989
990
991 === 4.2.2 Step 2 : Update the Destination rule and get the device's payload ===
992
993
994 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.
995
996 (((
997 ~1. Find the IoT Rule
998
999 2. Create a Destination with IoT Rule (MyWorkshopLoRaWANRuleWithDecoder_dragino_lht65)
1000
1001 3. Update the destination to the device
1002
1003 4. Check the payload
1004
1005 5. Approach A with MQTT
1006
1007 6. Approach B with Lambda
1008
1009
1010
1011 )))
1012
1013 ==== (% style="color:blue" %)**1. Find the IoT Rule MyWorkshopLoRaWANRuleWithDecoder_…**(%%) ====
1014
1015
1016 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:
1017
1018 [[image:image-20220530160632-7.png]]
1019
1020 Find the IoT rule
1021
1022
1023
1024 ==== (% style="color:blue" %)**2. Create a Destination with IoT Rule**(%%) ====
1025
1026
1027 (MyWorkshopLoRaWANRuleWithDecoder_dragino_lht65)
1028
1029 [[image:image-20220530160712-8.png]]
1030
1031 Create a Destnation
1032
1033
1034
1035 ==== (% style="color:blue" %)**3. Update the destination to the device**(%%) ====
1036
1037
1038 [[image:image-20220530160803-9.png]]
1039
1040 Update the destination to the device
1041
1042
1043
1044 ==== (% style="color:blue" %)**4.  Check the payload**(%%) ====
1045
1046
1047 (% style="color:blue" %)**Approach A with MQTT**
1048
1049 click the rule
1050
1051 [[image:image-20220525184325-18.png]]
1052
1053 click the rule
1054
1055
1056
1057 **Copy the Topic name**
1058
1059 [[image:image-20220525185444-24.png]]
1060
1061 copy the rule name
1062
1063
1064 Open MQTT client and subscrlibe Topic
1065
1066 [[image:image-20220525185554-25.png]]
1067
1068 Open MQTT client and subscrlibe Topic
1069
1070
1071
1072 Get the payload
1073
1074 [[image:image-20220525190308-27.png]]
1075
1076 Get the payload
1077
1078
1079
1080 (% style="color:blue" %)**Approach B with Lamdbda**
1081
1082 open lamdbda console and click the application
1083
1084 [[image:image-20220525190617-28.png]]
1085
1086 open lamdbda console and click the application
1087
1088
1089
1090 [[image:image-20220525190749-30.png]]
1091
1092 open lamdbda console and click the application
1093
1094
1095
1096 View logs in CloudWatch
1097
1098 [[image:image-20220525183907-15.png]]
1099
1100 View logs in CloudWatch
1101
1102
1103
1104 [[image:image-20220525190854-31.png]]
1105
1106 View logs in CloudWatch
1107
1108
1109
1110 == 4.3 Approach B Create an AWS Lambda function that sends a text message ==
1111
1112
1113 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.
1114
1115
1116
1117 === 4.3.1 Step 1 : To create an AWS Lambda function that sends a text message ===
1118
1119
1120
1121 ==== (% style="color:blue" %)**1.  Create a new AWS Lambda function.**(%%) ====
1122
1123
1124 (% style="color:red" %)**a.**(%%) In the [[AWS Lambda console>>url:https://console.aws.amazon.com/lambda/home]], choose Create function.
1125
1126 [[image:image-20220525191138-32.png]]
1127
1128 Create function
1129
1130
1131
1132 (((
1133 (% style="color:red" %)**b.**(%%) In Create function, select Use a blueprint. Search for and select the hello-world-python blueprint, and then choose (% style="color:red" %)**Configure**.
1134 )))
1135
1136 [[image:image-20220525191640-33.png]]
1137
1138 blueprint
1139
1140
1141
1142 (% style="color:red" %)**c. **(%%)In Basic information:
1143
1144 (% class="box" %)
1145 (((
1146 1). In Function name, enter the name of this function, format-high-temp-notification.
1147 2). In Execution role, choose Create a new role from AWS policy templates.
1148 3). In Role name, enter the name of the new role, format-high-temp-notification-role.
1149 4). In Policy templates - optional, search for and select Amazon SNS publish policy.
1150 5). Choose Create function.
1151 )))
1152
1153 [[image:image-20220525191717-34.png]]
1154
1155 blueprint
1156
1157
1158
1159 ==== (% style="color:blue" %)**2.  Modify the blueprint code to format and send an Amazon SNS notification.**(%%) ====
1160
1161
1162 (((
1163 (% style="color:red" %)**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.
1164 )))
1165
1166 (((
1167 (% style="color:red" %)**b.**(%%) In the format-high-temp-notification details page, choose the Configuration tab and scroll to the Function code panel.
1168 )))
1169
1170 (((
1171 (% style="color:red" %)**c. **(%%)In the Function code window, in the Environment pane, choose the Python file, lambda_function.py.
1172 )))
1173
1174 (((
1175 (% style="color:red" %)**d.** (%%)In the Function code window, delete all of the original program code from the blueprint and replace it with this code.
1176
1177
1178 )))
1179
1180 (% class="box" %)
1181 (((
1182 from ~_~_future~_~_ import print_function
1183 import json
1184 import boto3
1185 print('Loading function')
1186 def lambda_handler(event, context):
1187 # Parse the JSON message
1188 eventText = json.dumps(event)
1189 # 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.
1190 print('Received event: ', eventText)
1191 # Create an SNS client
1192 sns = boto3.client('sns')
1193 # Publish a message to the specified topic
1194 response = sns.publish (
1195 TopicArn = 'arn:aws:iam::123456789012:My_IoT_SNS_Topic',
1196 Message = eventText
1197 )
1198 print(response)
1199 )))
1200
1201 (% style="color:red" %)**e.**(%%) Choose Deploy.
1202
1203
1204 (% style="color:red" %)**Note: **(%%)Replace the value of **TopicArn** with the ARN of the Amazon SNS topic that you created SNS topic in which [[SNS>>url:https://console.aws.amazon.com/sns/home?region=us-east-1]]
1205
1206 (% style="color:red" %)**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
1207
1208
1209
1210 ==== (% style="color:blue" %)**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.**(%%) ====
1211
1212
1213 (((
1214 (% style="color:red" %)**a. **(%%)In a new window, open the Topics page of the Amazon SNS console
1215 )))
1216
1217 (((
1218 (% style="color:red" %)**b.**(%%) In the Topics page, find the high_temp_notice notification topic in the list of Amazon SNS topics.
1219 )))
1220
1221 (((
1222 (% style="color:red" %)**c.** (%%)Find the ARN of the high_temp_notice notification topic to use in the next step.
1223
1224
1225
1226 )))
1227
1228 ==== (% style="color:blue" %)**4.  Create a test case for your Lambda function.**(%%) ====
1229
1230
1231 (% style="color:red" %)**a.**(%%) In the Lambda Functionspage of the console,from (% style="color:red" %)**Select a test event**(%%)to select Configure test events
1232
1233 [[image:image-20220525191819-35.png]]
1234
1235 Configure test events
1236
1237
1238
1239 (((
1240 (% style="color:red" %)**b. **(%%)On Configure test event, enter a name for your test event and replace the message JSON with the following:
1241 )))
1242
1243 (% class="box" %)
1244 (((
1245 {
1246 "message" : "Hello, world"
1247 }
1248 )))
1249
1250
1251
1252 (% style="color:red" %)**c.** (%%)choose Create
1253
1254 [[image:image-20220525191853-36.png]]
1255
1256 event create
1257
1258
1259
1260 (((
1261 (% style="color:red" %)**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.
1262 )))
1263
1264 [[image:image-20220525191938-37.png]]
1265
1266 event test
1267
1268
1269
1270 (% style="color:red" %)**e.**(%%) Under your Lambda function code, on the Execution result tab, you see the output from the Lambda function.
1271
1272 [[image:image-20220525192325-38.png]]
1273
1274 event output
1275
1276
1277 Now The Lambda function table is created
1278
1279
1280
1281 === 4.3.2 Step 2 : Create Rule with Lambda function ===
1282
1283
1284 (% style="color:red" %)**1. **(%%)Browse to the AWS IoT console, and in the navigation pane, select (% style="color:red" %)**Act**
1285
1286 File:Console Act.png
1287
1288 (% class="box" %)
1289 (((
1290 **~ console_Act**
1291 )))
1292
1293
1294
1295 (% style="color:red" %)**2. **(%%)Choose Create to create an AWS IoT rule.
1296
1297 [[image:image-20220525192403-39.png]]
1298
1299 create Rule
1300
1301
1302
1303 (% style="color:red" %)**3.** (%%)On the Create a rule page, enter a name for your rule.
1304
1305 [[image:image-20220525192424-40.png]]
1306
1307 enter a Rule name
1308
1309
1310
1311 (% style="color:red" %)**4.**(%%) In Rule query statement, enter the following query:
1312
1313 (% class="box" %)
1314 (((
1315 SELECT * FROM "my/lambda/topic"
1316 )))
1317
1318 [[image:image-20220525192453-41.png]]
1319
1320 enter the following query
1321
1322
1323
1324 (% style="color:red" %)**5.**(%%) In Set one or more actions, select Add action.
1325
1326 [[image:image-20220525192524-42.png]]
1327
1328 Add action
1329
1330
1331
1332 (% style="color:red" %)**6.**(%%) Under the select operation, select Send a Message to a Lambda Function and then select the configuration operation.
1333
1334 [[image:image-20220525192619-43.png]]
1335
1336 select operation
1337
1338
1339
1340 = 5. For End Node =
1341
1342 == 5.1 AWS Downlink Note ==
1343
1344
1345 Prerequisites: The node must be online in real-time before downlink.
1346
1347
1348 [[image:image-20220525113243-2.png||height="346" width="623"]]
1349
1350 Convert the data to Base64
1351
1352 [[image:image-20220525113612-3.png||height="377" width="622"]]
1353
1354
1355 When the serial port logs of downlink are received, the Receive date is displayed
1356
1357 [[image:image-20220525115046-4.png]]
1358
1359 In addition to the above methods, there is also a downlink,This method requires a certain amount of manipulation,
1360
1361 Users can view this [[Downlink method 2>>url:https://github.com/aws-samples/aws-iot-core-lorawan/tree/main/send_downlink_payload]]
1362
1363
1364
1365 == 5.2 Node decoder ==
1366
1367
1368 [[Decode LHT65>>url:https://github.com/aws-samples/aws-iot-core-lorawan/tree/main/transform_binary_payload]]
1369
1370
1371 = 6. Reference =
1372
1373
1374 * [[Video Introduction for how to use LoRaWAN AWS-IoT Core>>url:https://www.youtube.com/watch?v=eKK0M8iC6l4]]
1375 * [[AWS LoRaWAN Workshop>>url:https://iotwireless.workshop.aws/]]
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0