Tuesday, August 9, 2022
HomeIoTMonitor AWS IoT connections in near-real time utilizing MQTT LWT

Monitor AWS IoT connections in near-real time utilizing MQTT LWT

In a related gadget, it’s possible you’ll want to observe gadgets in near-real time to detect error and mitigate actions, Final Will and Testomony (LWT) technique for MQTT addresses this problem. LWT is a typical technique of MQTT protocol specification that permits to detect abrupt disconnects of gadgets and to inform different shoppers about this abrupt disconnections.

IoT gadgets are sometimes utilized in environments with unreliable community connectivity and/or gadgets may disconnect attributable to lack of energy provide, low battery, lack of connection, or another motive. This can trigger abrupt disconnections from the dealer with out figuring out if the disruption was pressured by the shopper or really abrupt, That is the place LWT let’s a shopper present a testomony together with its credentials when connecting to the AWS IoT Core. If the shopper disconnects abruptly sooner or later later (i.e. energy loss), it could actually let AWS IoT Core ship a message to different shoppers and inform them of this abrupt disconnect and ship LWT message.

MQTT Model 3.1.1 supplies an LWT characteristic as a part of the MQTT message and is supported by AWS IoT Core, so any shopper which disconnects abruptly can specify its LWT message together with the MQTT matter when it connects to the dealer. When the shopper disconnects abruptly, the dealer (AWS IoT Core) will then publish the LWT message supplied by that shopper at connection time to all of the gadgets which subscribed to this LWT matter.

The MQTT LWT characteristic lets you monitor AWS IoT connections in near-real time that can assist you to take corrective actions. You’ll be able to react to abrupt disconnection occasions by verifying standing, restoring connections, and finishing up both edge-based (gadget facet) actions or cloud-based actions to analyze and mitigate this abrupt disconnect of the gadget.

On this weblog we are going to undergo following steps:

  1. A simulated ‘lwtThing’ gadget connects to AWS IoT Core by giving Hold-alive time
  2. The ‘lwtThing’ gadget, on the connection to AWS IoT Core, supplies the next:
    1. Subject for LWT (i.e. /final/will/matter)
    2. LWT message
    3. QoS sort both 0 or 1
  3. ‘lwtThing’ gadget disconnects abruptly from AWS IoT Core
  4. AWS IoT Core detects this and publishes the LWT message to all of the subscribers of the subject (i.e. /final/will/matter)
  5. Guidelines for AWS IoT (rule engine) picks up the set off on the subject and invokes Amazon Easy Notifications Service (SNS)
  6. Amazon SNS sends a notification electronic mail

We are going to setup a digital atmosphere utilizing a CloudFormation template (by utilizing AWS IoT workshop setup directions) and launch a digital IoT factor (naming ‘lwtThing’) to create an actual life simulation of the bodily gadget.


We are going to simulate the sting gadget utilizing a script supplied under and ship the LWT message, displaying abrupt disconnects and triggering AWS IoT guidelines and subsequently invoking Amazon SNS to ship emails.


We are going to use the next workshop setup to get rapidly bootstrapped and take a look at LWT. You need to use the next hyperlink to setup AWS Cloud9 atmosphere (decide any area closest to your location).

As soon as we have now the atmosphere setup utilizing the workshop AWS CloudFormation pre-provided template, lets start testing the ungraceful disconnects with AWS IoT Core (AWS MQTT dealer on the cloud).

Now open the Cloud9 terminal (see right here) and let’s setup Python SDK for us to make use of.

Create a folder for us to make use of to attach our IoT factor utilizing the Cloud9 terminal window.

mkdir -p /house/ubuntu/atmosphere/lwt/certs
cd /house/ubuntu/atmosphere/lwt/

Setup Python IoT SDK utilizing full directions right here.

Fast directions:

git clone https://github.com/aws/aws-iot-device-sdk-python.git
cd aws-iot-device-sdk-python
python setup.py set up

Now, to setup your AWS IoT Factor comply with steps outlined right here.

As soon as we have now created the factor, let’s add these certificates in our Cloud9 occasion for us to attach from there.

Add the newly created certificates and RootCA into following folder (created earlier)


LWT factor messages

Let’s copy the Python code to Cloud9 and execute because the simulated AWS IoT factor.

Copy the next instructions:

contact lwtTest.py

Open the file and duplicate the next code into it.

 * # Copyright Amazon.com, Inc. or its associates. All Rights Reserved.
 * # SPDX-License-Identifier: MIT-0

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import logging
import time
import argparse
import json

AllowedActions = ['both', 'publish', 'subscribe']

# Customized MQTT message callback
def customCallback(shopper, userdata, message):
    print("Obtained a brand new message: ")
    print("from matter: ")

# LWT JSON payload
payload ={
  "state": {
    "reported": {
      "last_will": "sure",
      "trigger_action": "on",
      "client_id": "lwtThing"
# conversion to JSON performed by dumps() operate
jsonPayload = json.dumps(payload)
# printing the output

# Learn in command-line parameters
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--endpoint", motion="retailer", required=True, dest="host", assist="Your AWS IoT customized endpoint")
parser.add_argument("-r", "--rootCA", motion="retailer", required=True, dest="rootCAPath", assist="Root CA file path")
parser.add_argument("-c", "--cert", motion="retailer", dest="certificatePath", assist="Certificates file path")
parser.add_argument("-k", "--key", motion="retailer", dest="privateKeyPath", assist="Personal key file path")
parser.add_argument("-p", "--port", motion="retailer", dest="port", sort=int, assist="Port quantity override")
parser.add_argument("-w", "--websocket", motion="store_true", dest="useWebsocket", default=False,
                    assist="Use MQTT over WebSocket")
parser.add_argument("-id", "--clientId", motion="retailer", dest="clientId", default="basicPubSub",
                    assist="Focused shopper id")
parser.add_argument("-t", "--topic", motion="retailer", dest="matter", default="sdk/take a look at/Python", assist="Focused matter")
parser.add_argument("-m", "--mode", motion="retailer", dest="mode", default="each",
                    assist="Operation modes: %s"%str(AllowedActions))
parser.add_argument("-M", "--message", motion="retailer", dest="message", default="AWS IoT Factor related message to IoT Core",
                    assist="Message to publish")

args = parser.parse_args()
host = args.host
rootCAPath = args.rootCAPath
certificatePath = args.certificatePath
privateKeyPath = args.privateKeyPath
port = args.port
useWebsocket = args.useWebsocket
clientId = args.clientId
matter = args.matter

if args.mode not in AllowedActions:
    parser.error("Unknown --mode choice %s. Have to be one among %s" % (args.mode, str(AllowedActions)))

if args.useWebsocket and args.certificatePath and args.privateKeyPath:
    parser.error("X.509 cert authentication and WebSocket are mutual unique. Please decide one.")

if not args.useWebsocket and (not args.certificatePath or not args.privateKeyPath):
    parser.error("Lacking credentials for authentication.")

# Port defaults
if args.useWebsocket and never args.port:  # When no port override for WebSocket, default to 443
    port = 443
if not args.useWebsocket and never args.port:  # When no port override for non-WebSocket, default to 8883
    port = 8883

# Configure logging - we are going to see messages on STDOUT
logger = logging.getLogger("AWSIoTPythonSDK.core")
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(identify)s - %(levelname)s - %(message)s')

# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
if useWebsocket:
    myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId, useWebsocket=True)
    myAWSIoTMQTTClient.configureEndpoint(host, port)
    myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
    myAWSIoTMQTTClient.configureEndpoint(host, port)
    myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

# Will Subject
# Enter parameters are: Subject, Final will message and at last QoS
myAWSIoTMQTTClient.configureLastWill('/final/will/matter', jsonPayload, 0)

# Join and subscribe to AWS IoT
# keep-alive join parameter - setting 30s
loopCount = 1
whereas loopCount < 2:
    if args.mode == 'each' or args.mode == 'publish':
        message = {}
        message['message'] = args.message
        messageJson = json.dumps(message)
        myAWSIoTMQTTClient.publish(matter, messageJson, 1)
        if args.mode == 'publish':
            print('Revealed matter %s: %sn' % (matter, messageJson))
            loopCount +=1
#lets put the gadget to sleep so it creates disconnect after 60s
print("--- Placing gadget to sleep now, so IoT core keep-alive time expires. ---")
print("--- We are going to abruptly disconnect the gadget after 60seconds. ---")

Let’s take a look at the next line which is doing all of the work on setting the LWT Subject, JSON payload, and what degree of QoS we’re utilizing.

myAWSIoTMQTTClient.configureLastWill('/final/will/matter', jsonPayload, 0)
  • Subject used is : /final/will/matter
  • QoS (High quality of Service) is: 0
  • JSON Payload variable accommodates following payload:
  "state": {
    "reported": {
      "last_will": "sure",
      "trigger_action": "on",
      "client_id": "lwtThing"

The above setup defines the LWT matter in addition to what matter to submit this message to, which will probably be understood and executed by AWS IoT guidelines as soon as the gadget disconnects abruptly (The “Final Will” is
revealed by the server when its connection to the shopper is unexpectedly misplaced.) An AWS IoT rule will set off the motion on Amazon SNS to ship an electronic mail upon its execution. You’ll be able to learn extra on the opposite choices within the SDK doc.

We’re setting keep-alive to 30seconds at connection to AWS IoT core so it retains the session alive for the given time. As soon as the time runs out, the session is expired.

On the expiration of the session, we set the gadget to sleep for 60 seconds, As soon as 60 seconds finishes we abruptly disconnects the gadgets which in flip generates Final Will Testomony (LWT) set off from AWS IoT Core and message will get revealed to all matter subscribers who’re listening to this LWT matter.

Setup Amazon SNS

Let’s setup Amazon SNS and configure it to ship electronic mail as its notification, From the Amazon SNS console do the next:

  • Choose Matters
    • Choose Create matter
      • Choose Customary
      • Choose Title (i.e. lwtSNSTopic)
      • Choose Show identify (i.e. lwtSNSTopic)
      • Choose Create matter
    • As soon as matter is created
      • Choose Create subscription
      • Choose E mail from Protocol dropdown
      • For Endpoint give the e-mail handle you wish to use
      • Choose Create subscription

You need to obtain an electronic mail. Please verify the subscription. In case you have not confirmed the subscription, you won’t be able to obtain any emails.

Setup Guidelines for AWS IoT Core

From the AWS IoT Core console do the next:

  • Choose Act
    • Choose Guidelines
    • Choose Create
    • Give a identify (i.e. lastWillRule) and description (My first LWT rule)
    • In Rule question assertion enter following:
      • SELECT * FROM '/final/will/matter' the place state.reported.last_will="sure" and state.reported.trigger_action = 'on'
    • In Actions part
      • Choose Add Motion
      • Choose Ship a message to an SNS push notification
      • Choose Configure motion
      • In SNS goal Choose the SNS matter you created earlier (i.e. lwtSNSTopic)
      • In Message format, Choose JSON
      • Choose Create Function
      • Give it a reputation (i.e. lwtRuleRole)
      • Choose Add motion

Let’s add one other motion right here, we are going to republish the incoming LWT message to a different matter to confirm its incoming.

    • In Actions part
      • Choose Add Motion
      • Choose Republish a message to an AWS IoT matter
      • Choose Configure motion
      • Below Subject
        • Choose /lwt/executed
        • we will go away the High quality of Service default
        • For ‘Select or create a task to grant AWS IoT entry to carry out this motion
          • Choose lwtRuleRole
          • Choose Replace function
        • Choose Add motion

This concludes our guidelines setup part, let’s proceed and setup sending LWT messages and execute our setup.

Sending LWT messages

Earlier than we execute the simulated gadget (utilizing python code) let’s subscribe to the subject within the AWS IoT Core console.

Determine 2

Now that we have now all the pieces in place, let’s execute the IoT Factor (simulated utilizing Python code). You need to use the pattern execution command which can differ for you as your thingID may be completely different or your certificates path may be in a distinct location.

Pattern command (substitute xxxx with related values in your setup):

python lwtTest.py -e xxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com -r /house/ubuntu/atmosphere/lwt/certs/AmazonRootCA1.pem -c /house/ubuntu/atmosphere/lwt/certs/xxxxxxxxxxxxxxxxxxxxxxxxxxxx-certificate.pem.crt -k /house/ubuntu/atmosphere/lwt/certs/xxxxxxxxxxxxxxxxxxxxxxxxxxxx-private.pem.key -id lwtThing -t /lwt/related/matter -m publish

What we’re passing as enter parameters to the code is as follows:

  • -e is referring to the top level of AWS IoT Core
  • -r is the total file path the place our Amazon Root CA is positioned
  • -c is the total file path for our certificates location
  • -k is the total file path for our non-public key
  • -id is the ClientID we’re utilizing to ship to AWS IoT Core (you need to match this to what you will have created the Factor in IoT Core as)
  • -t is the subject we’re offering to publish on when it first connects to AWS IoT Core
  • -m is the mode we have now outlined within the code and we are going to use publish for this take a look at. (obtainable modes are: publish, subscribe or each)

Let’s take a look at the execution of the command, we must always see that LWT is getting configured and what message we revealed to AWS IoT Core. Additionally, you will see abrupt disconnect after 60 seconds.

Determine 3

Switching over to the AWS IoT Core console to see incoming messages, subscribe to following subjects:

  • Subject used for republishing of the message when the rule is executed (utilizing as debug): /lwt/executed
  • Subject used for when LWT message is revealed upon ungraceful disconnect of a shopper: /final/will/matter
  • Subject /lwt/related/matter you’ll be able to see messages posted by the factor. This happens when the shopper is related to AWS IoT Core and sends the message to tell the dealer I’m right here and related.

Determine 4

Below matter /final/will/matter we will see the message executed by AWS IoT Core as soon as the gadget ungracefully disconnects.

Determine 5

When AWS IoT rule is executed for LWT we will see inside matter /lwt/executed payload is revealed to this matter too, we configured this matter earlier to repost to when AWS IoT rule is executed upon gadget abrupt disconnection.

Determine 6

Upon profitable execution of the AWS IoT rule we additionally triggered Amazon SNS electronic mail notification and when you’ve got configured this accurately earlier you will note comparable electronic mail in your inbox.

Determine 7


On this weblog we checked out how you should utilize AWS IoT Core to detect errors and failures of a tool and abrupt disconnections, and upon abrupt disconnection triggering Amazon SNS electronic mail notification to help workforce who can rapidly examine and mitigate failure and resolve points at giant. If the factor closes connection correctly or in a advisable method, then AWS IoT Core will disregard the LWT which we set on the time of connection. By utilizing LWT, we will implement many error dealing with eventualities the place the connection of the shopper drops and the place there’s a dependency of different shoppers counting on this connection chain. For instance, when an industrial gateway liable for gathering sensor knowledge throughout the manufacturing unit flooring experiences an abrupt disconnection from AWS IoT Core, then you’ll be able to monitor these disconnections and take corrective measures to cut back second diploma affect downstream. You’ll be able to learn extra right here about MQTT and SNS.

Concerning the writer

Syed Rehan is a Sr. International specialist Options Architect at Amazon Net Companies (AWS) and relies out of London. He’s overlaying international span of shoppers and supporting them as lead IoT Answer Architect. Syed has in-depth data of IoT and cloud and works on this function with international prospects starting from start-up to enterprises to allow them to construct IoT options with the AWS eco system.



Please enter your comment!
Please enter your name here

Most Popular