Ingesting data to Amazon IoT SiteWise from Amazon IoT things - Amazon IoT SiteWise
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Ingesting data to Amazon IoT SiteWise from Amazon IoT things

You can ingest data to Amazon IoT SiteWise from a fleet of Amazon IoT things by using device shadows. Device shadows are JSON objects that store current state information for an Amazon IoT device. For more information, see Device shadow service in the Amazon IoT Developer Guide.

After you complete this tutorial, you can set up an operation in Amazon IoT SiteWise based on Amazon IoT things. By using Amazon IoT things, you can also easily integrate your operation with other useful features of Amazon IoT. For example, you can configure Amazon IoT features to do the following tasks:

  • Configure additional rules to stream data to Amazon IoT Events, Amazon DynamoDB, and other Amazon services. For more information, see Rules in the Amazon IoT Developer Guide.

  • Index, search, and aggregate your device data with the Amazon IoT fleet indexing service. For more information, see Fleet indexing service in the Amazon IoT Developer Guide.

  • Audit and secure your devices with Amazon IoT Device Defender. For more information, see Amazon IoT Device Defender in the Amazon IoT Developer Guide.

In this tutorial, you learn how to ingest data from Amazon IoT things' device shadows to assets in Amazon IoT SiteWise. To do so, you create one or more Amazon IoT things and run a script that updates each thing's device shadow with CPU and memory usage data. You use CPU and memory usage data in this tutorial to imitate realistic sensor data. Then, you create a rule with an Amazon IoT SiteWise action that sends this data to an asset in Amazon IoT SiteWise every time a thing's device shadow updates. For more information, see Ingesting data using Amazon IoT Core rules.

Prerequisites

To complete this tutorial, you need the following:

Step 1: Creating an Amazon IoT policy

In this procedure, you create an Amazon IoT policy that allows your Amazon IoT things to access the resources used in this tutorial.

To create an Amazon IoT policy
  1. Sign in to the Amazon Web Services Management Console.

  2. Review the Amazon Regions where Amazon IoT SiteWise is supported. Switch to one of these supported Regions, if necessary.

  3. Navigate to the Amazon IoT console. If a Connect device button appears, choose it.

  4. In the left navigation pane, choose Security and then choose Policies.

  5. Choose Create.

  6. Enter a name for the Amazon IoT policy (for example, SiteWiseTutorialDevicePolicy).

  7. Under Policy document, choose JSON to enter the following policy in JSON form. Replace region and account-id with your Region and account ID, such as us-east-1 and 123456789012.

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "arn:aws:iot:region:account-id:client/SiteWiseTutorialDevice*" }, { "Effect": "Allow", "Action": "iot:Publish", "Resource": [ "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get" ] }, { "Effect": "Allow", "Action": "iot:Receive", "Resource": [ "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/accepted", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/accepted", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get/accepted", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/rejected", "arn:aws:iot:region:account-id:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/rejected" ] }, { "Effect": "Allow", "Action": "iot:Subscribe", "Resource": [ "arn:aws:iot:region:account-id:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/accepted", "arn:aws:iot:region:account-id:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/accepted", "arn:aws:iot:region:account-id:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get/accepted", "arn:aws:iot:region:account-id:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/rejected", "arn:aws:iot:region:account-id:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/rejected" ] }, { "Effect": "Allow", "Action": [ "iot:GetThingShadow", "iot:UpdateThingShadow", "iot:DeleteThingShadow" ], "Resource": "arn:aws:iot:region:account-id:thing/SiteWiseTutorialDevice*" } ] }

    This policy allows your Amazon IoT things to connect and interact with device shadows through MQTT messages. For more information about MQTT messages, see What is MQTT?. To interact with device shadows, your Amazon IoT things publish and receive MQTT messages on topics that start with $aws/things/thing-name/shadow/. This policy uses a thing policy variable ${iot:Connection.Thing.ThingName}, which substitutes the connected thing's name in each topic. The iot:Connect statement limits which things can connect, so the thing policy variable can only substitute to names that start with SiteWiseTutorialDevice.

    For more information, see Thing policy variables in the Amazon IoT Developer Guide.

    Note

    This policy applies to things whose names start with SiteWiseTutorialDevice. To use a different name for your things, you must update the policy accordingly.

  8. Choose Create.

Step 2: Creating and configuring an Amazon IoT thing

In this procedure, you create and configure an Amazon IoT thing. You can register your development computer as an Amazon IoT thing to complete this tutorial. When you apply this tutorial's concepts to a real-world application, you can create and configure Amazon IoT things on any device that can run an Amazon IoT SDK, including Amazon IoT Greengrass and FreeRTOS. For more information, see Amazon IoT SDKs in the Amazon IoT Developer Guide.

To create and configure an Amazon IoT thing
  1. Open a command line and run the following command to create a directory for this tutorial.

    mkdir iot-sitewise-rule-tutorial cd iot-sitewise-rule-tutorial
  2. Run the following command to create a directory for your thing's certificates.

    mkdir device1

    If you're creating additional things, increment the number in the directory name accordingly to keep track of which certificates belong to which thing.

  3. Navigate to the Amazon IoT console.

  4. In the left navigation pane, choose All devices in the Manage section. Then choose Things.

  5. If a You don't have any things yet dialog box appears, choose Create a thing. Otherwise, choose Create things.

  6. On the Creating things page, choose Create a single thing and then choose Next.

  7. On the Specify thing properties page, enter a name for your Amazon IoT thing (for example, SiteWiseTutorialDevice1) and then choose Next. If you're creating additional things, increment the number in the thing name accordingly.

    Important

    The thing name must match the name used in the policy that you created in Step 1: Creating an Amazon IoT policy. Otherwise, your device can't connect to Amazon IoT.

  8. On the Configure device certificate - optional page, choose Auto-generate a new certificate (recommended) the choose Next. Certificates enable Amazon IoT to securely identify your devices.

  9. On the Attach policies to certificate - optional page, select the policy you created in Step 1: Creating an Amazon IoT policy and choose Create thing.

  10. On the Download certificates and keys dialog box, do the following:

    1. Choose the Download links to download your thing's certificate, public key, and private key. Save all three files to the directory that you created for your thing's certificates (for example, iot-sitewise-rule-tutorial/device1).

      Important

      This is the only time that you can download your thing's certificate and keys, which you need for your device to successfully connect to Amazon IoT.

    2. Choose the Download link to download a root CA certificate. Save the root CA certificate to the iot-sitewise-rule-tutorial. We recommend downloading Amazon Root CA 1.

  11. Choose Done.

You have now registered an Amazon IoT thing on your computer. You can now take one of the following next steps:

  • Continue to Step 3: Creating a device asset model without creating additional Amazon IoT things. You can complete this tutorial with only one thing.

  • Repeat the steps in this section on another computer or device to create more Amazon IoT things. For this tutorial, we recommend that you follow this option so that you can ingest unique CPU and memory usage data from multiple devices.

  • Repeat the steps in this section on the same device (your computer) to create more Amazon IoT things. Each Amazon IoT thing receives similar CPU and memory usage data from your computer, so use this approach to demonstrate ingesting non-unique data from multiple devices.

Step 3: Creating a device asset model

In this procedure, you create an asset model in Amazon IoT SiteWise to represent your devices that stream CPU and memory usage data. Asset models enforce consistent information across multiple assets of the same type, so that you can process data in assets that represent groups of devices. For more information, see Modeling industrial assets.

To create an asset model that represents a device
  1. Navigate to the Amazon IoT SiteWise console.

  2. In the left navigation pane, choose Models.

  3. Choose Create model.

  4. Under Model details, enter a name for your model. For example, SiteWise Tutorial Device Model.

  5. Under Measurement definitions, do the following:

    1. In Name, enter CPU Usage.

    2. In Unit, enter %.

    3. Leave the Data type as Double.

    Measurement properties represent a device's raw data streams. For more information, see Defining data streams from equipment (measurements).

  6. Choose Add new measurement to add a second measurement property.

  7. In the second row under Measurement definitions, do the following:

    1. In Name, enter Memory Usage.

    2. In Unit, enter %.

    3. Leave the Data type as Double.

  8. Under Metric definitions, do the following:

    1. In Name, enter Average CPU Usage.

    2. In Formula, enter avg(CPU Usage). Choose CPU Usage from the autocomplete list when it appears.

    3. In Time interval, enter 5 minutes.

    Metric properties define aggregation calculations that process all input data points over an interval and output a single data point per interval. This metric property calculates each device's average CPU usage every 5 minutes. For more information, see Aggregating data from properties and other assets (metrics).

  9. Choose Add new metric to add a second metric property.

  10. In the second row under Metric definitions, do the following:

    1. In Name, enter Average Memory Usage.

    2. In Formula, enter avg(Memory Usage). Choose Memory Usage from the autocomplete list when it appears.

    3. In Time interval, enter 5 minutes.

    This metric property calculates each device's average memory usage every 5 minutes.

  11. (Optional) Add other additional metrics that you're interested in calculating per device. Some interesting functions include min and max. For more information, see Using formula expressions. In Step 4: Creating a device fleet asset model, you create a parent asset that can calculate metrics using data from your entire fleet of devices.

  12. Choose Create model.

Step 4: Creating a device fleet asset model

In this procedure, you create an asset model in Amazon IoT SiteWise to represent your fleet of devices. In this asset model, you define a hierarchy, with which you can associate many device assets to a single fleet asset. Then, you define metrics in the fleet asset model that aggregate data from all associated device assets to gain insights about your fleet as a whole.

To create an asset model that represents a device fleet
  1. Navigate to the Amazon IoT SiteWise console.

  2. In the left navigation pane, choose Models.

  3. Choose Create model.

  4. Under Model details, enter a name for your model. For example, SiteWise Tutorial Device Fleet Model.

  5. Under Hierarchy definitions, do the following:

    1. In Hierarchy name, enter Device.

    2. In Hierarchy model, choose your device asset model (SiteWise Tutorial Device Model).

    A hierarchy defines a relationship between a parent (fleet) asset model and a child (device) asset model. Parent assets can access child assets' property data. When you create assets later, you need to associate child assets to parent assets according to a hierarchy definition in the parent asset model. For more information, see Defining asset model hierarchies.

  6. Under Metric definitions, do the following:

    1. In Name, enter Average CPU Usage.

    2. In Formula, enter avg(Device | Average CPU Usage). When the autocomplete list appears, choose Device to choose a hierarchy, then choose Average CPU Usage to choose the metric from the device asset that you created earlier.

    3. In Time interval, enter 5 minutes.

    This metric property calculates the average CPU usage of all device assets associated to a fleet asset through the Device hierarchy.

  7. Choose Add new metric to add a second metric property.

  8. In the second row under Metric definitions, do the following:

    1. In Name, enter Average Memory Usage.

    2. In Formula, enter avg(Device | Average Memory Usage). When the autocomplete list appears, choose Device to choose a hierarchy, then choose Average Memory Usage to choose the metric from the device asset that you created earlier.

    3. In Time interval, enter 5 minutes.

    This metric property calculates the average memory usage of all device assets associated to a fleet asset through the Device hierarchy.

  9. (Optional) Add other additional metrics that you're interested in calculating across your fleet of devices.

  10. Choose Create model.

Step 5: Creating and configuring a device asset

In this procedure, you create a device asset from your device asset model. Then, you define property aliases for each measurement property. A property alias is a string that uniquely identifies an asset property. You can later use these aliases, rather than asset ID and property ID, to identify a property to which to upload data. For more information, see Mapping industrial data streams to asset properties.

To create a device asset and define property aliases
  1. Navigate to the Amazon IoT SiteWise console.

  2. In the left navigation pane, choose Assets.

  3. Choose Create asset.

  4. Under Model information, choose your device asset model, SiteWise Tutorial Device Model.

  5. Under Asset information, enter a name for your asset. For example, SiteWise Tutorial Device 1.

  6. Choose Create asset.

  7. For your new device asset, choose Edit.

  8. Under CPU Usage, enter /tutorial/device/SiteWiseTutorialDevice1/cpu as the property alias. You include the Amazon IoT thing's name in the property alias, so that you can ingest data from all of your devices using a single Amazon IoT rule.

  9. Under Memory Usage, enter /tutorial/device/SiteWiseTutorialDevice1/memory as the property alias.

  10. Choose Save.

If you created multiple Amazon IoT things earlier, repeat steps 3 through 10 for each device, and increment the number in the asset name and property aliases accordingly. For example, the second device asset's name should be SiteWise Tutorial Device 2, and its property aliases should be /tutorial/device/SiteWiseTutorialDevice2/cpu, and /tutorial/device/SiteWiseTutorialDevice2/memory.

Step 6: Creating and configuring a device fleet asset

In this procedure, you create a device fleet asset from your device fleet asset model. Then, you associate your device assets to the fleet asset so that the fleet asset's metric properties can aggregate data from many devices.

To create a device fleet asset and associate device assets
  1. Navigate to the Amazon IoT SiteWise console.

  2. In the left navigation pane, choose Assets.

  3. Choose Create asset.

  4. Under Model information, choose your device fleet asset model, SiteWise Tutorial Device Fleet Model.

  5. Under Asset information, enter a name for your asset. For example, SiteWise Tutorial Device Fleet 1.

  6. Choose Create asset.

  7. For your new device fleet asset, choose Edit.

  8. Under Assets associated to this asset, choose Add associated asset and do the following:

    1. Under Hierarchy, choose Device. This hierarchy identifies the hierarchical relationship between device and device fleet assets. You defined this hierarchy in the device fleet asset model earlier in this tutorial.

    2. Under Asset, choose your device asset, SiteWise Tutorial Device 1.

  9. (Optional) If you created multiple device assets earlier, repeat steps 8 through 10 for each device asset that you created.

  10. Choose Save.

    You should now see your device assets organized as a hierarchy.

Step 7: Creating a rule in Amazon IoT Core to send data to device assets

In this procedure, you create a rule in Amazon IoT Core that parses device shadow notification messages and sends data to your device assets in Amazon IoT SiteWise. Each time your device's shadow updates, Amazon IoT sends an MQTT message. You can create a rule that takes action when device shadows change based on the MQTT message. In this case, you want to process the update message to extract the property values and send them to your device assets in Amazon IoT SiteWise.

To create a rule with an Amazon IoT SiteWise action
  1. Navigate to the Amazon IoT console.

  2. In the left navigation pane, choose Message routing and then choose Rules.

  3. Choose Create rule.

  4. Enter a name and description for your rule and the choose Next.

  5. Enter the following SQL statement and the choose Next.

    SELECT * FROM '$aws/things/+/shadow/update/accepted' WHERE startsWith(topic(3), "SiteWiseTutorialDevice")

    This rule query statement works because the device shadow service publishes shadow updates to $aws/things/thingName/shadow/update/accepted. For more information about device shadows, see Device shadow service in the Amazon IoT Developer Guide.

    In the WHERE clause, this rule query statement uses the topic(3) function to get the thing name from the third segment of the topic. Then, the statement filters out devices that have names that don't match those of the tutorial devices. For more information about Amazon IoT SQL, see Amazon IoT SQL reference in the Amazon IoT Developer Guide.

  6. Under Rule actions, choose Send message data to asset properties in Amazon IoT SiteWise and do the following:

    1. Choose By property alias.

    2. In Property alias, enter /tutorial/device/${topic(3)}/cpu.

      The ${...} syntax is a substitution template. Amazon IoT evaluates the contents within the braces. This substitution template pulls the thing name from the topic to create an alias unique to each thing. For more information, see Substitution templates in the Amazon IoT Developer Guide.

      Note

      Because an expression in a substitution template is evaluated separately from the SELECT statement, you can't use a substitution template to reference an alias created using an AS clause. You can reference only information present in the original payload, in addition to supported functions and operators.

    3. In Entry ID - optional, enter ${concat(topic(3), "-cpu-", floor(state.reported.timestamp))}.

      Entry IDs uniquely identify each value entry attempt. If an entry returns an error, you can find the entry ID in the error output to troubleshoot the issue. The substitution template in this entry ID combines the thing name and the device's reported timestamp. For example, the resulting entry ID might look like SiteWiseTutorialDevice1-cpu-1579808494.

    4. In Time in seconds, enter ${floor(state.reported.timestamp)}.

      This substitution template calculates the time in seconds from the device's reported timestamp. In this tutorial, devices report timestamp in seconds in Unix epoch time as a floating point number.

    5. In Offset in nanos - optional, enter ${floor((state.reported.timestamp % 1) * 1E9)}.

      This substitution template calculates the nanosecond offset from the time in seconds by converting the decimal portion of the device's reported timestamp.

      Note

      Amazon IoT SiteWise requires that your data has a current timestamp in Unix epoch time. If your devices don't report time accurately, you can get the current time from the Amazon IoT rules engine with timestamp(). This function reports time in milliseconds, so you must update your rule action's time parameters to the following values:

      • In Time in seconds, enter ${floor(timestamp() / 1E3)}.

      • In Offset in nanos, enter ${(timestamp() % 1E3) * 1E6}.

    6. In Data type, choose Double.

      This data type must match the data type of the asset property you defined in the asset model.

    7. In Value, enter ${state.reported.cpu}. In substitution templates, you use the . operator to retrieve a value from within a JSON structure.

    8. Choose Add entry to add a new entry for the memory usage property, and complete the following steps again for that property:

      1. Choose By property alias.

      2. In Property alias, enter /tutorial/device/${topic(3)}/memory.

      3. In Entry ID - optional, enter ${concat(topic(3), "-memory-", floor(state.reported.timestamp))}.

      4. In Time in seconds, enter ${floor(state.reported.timestamp)}.

      5. In Offset in nanos - optional, enter ${floor((state.reported.timestamp % 1) * 1E9)}.

      6. In Data type, choose Double.

      7. In Value, enter ${state.reported.memory}.

    9. Under IAM Role, choose Create new role to create an IAM role for this rule action. This role allows Amazon IoT to push data to properties in your device fleet asset and its asset hierarchy.

    10. Enter a role name and choose Create.

  7. (Optional) Configure an error action that you can use to troubleshoot your rule. For more information, see Troubleshooting a rule.

  8. Choose Next.

  9. Review the settings and choose Create to create the rule.

Step 8: Running the device client script

Because you aren't using an actual device to report data, you run a script to update your Amazon IoT thing's device shadow with CPU and memory usage to imitate real sensor data. To run the script, you must first install required Python packages. In this procedure, you install the required Python packages and then run the device client script.

To configure and run the device client script
  1. Navigate to the Amazon IoT console.

  2. At the bottom of the left navigation pane, choose Settings.

  3. Save your custom endpoint for use with the device client script. You use this endpoint to interact with your thing's shadows. This endpoint is unique to your account in the current Region.

    Your custom endpoint should look like the following example.

    identifier.iot.region.amazonaws.com
  4. Open a command line and run the following command to navigate to the tutorial directory you created earlier.

    cd iot-sitewise-rule-tutorial
  5. Run the following command to install the Amazon IoT Device SDK for Python.

    pip3 install AWSIoTPythonSDK

    For more information, see Amazon IoT Device SDK for Python in the Amazon IoT Developer Guide

  6. Run the following command to install psutil, a cross-platform process and system utilities library.

    pip3 install psutil

    For more information, see psutil in the Python Package Index.

  7. Create a file called thing_performance.py in the iot-sitewise-rule-tutorial directory and then copy the following Python code into the file.

    import AWSIoTPythonSDK.MQTTLib as AWSIoTPyMQTT import json import psutil import argparse import logging import time # Configures the argument parser for this program. def configureParser(): parser = argparse.ArgumentParser() parser.add_argument( "-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint", ) parser.add_argument( "-r", "--rootCA", action="store", required=True, dest="rootCAPath", help="Root CA file path", ) parser.add_argument( "-c", "--cert", action="store", required=True, dest="certificatePath", help="Certificate file path", ) parser.add_argument( "-k", "--key", action="store", required=True, dest="privateKeyPath", help="Private key file path", ) parser.add_argument( "-p", "--port", action="store", dest="port", type=int, default=8883, help="Port number override", ) parser.add_argument( "-n", "--thingName", action="store", required=True, dest="thingName", help="Targeted thing name", ) parser.add_argument( "-d", "--requestDelay", action="store", dest="requestDelay", type=float, default=1, help="Time between requests (in seconds)", ) parser.add_argument( "-v", "--enableLogging", action="store_true", dest="enableLogging", help="Enable logging for the AWS IoT Device SDK for Python", ) return parser # An MQTT shadow client that uploads device performance data to AWS IoT at a regular interval. class PerformanceShadowClient: def __init__( self, thingName, host, port, rootCAPath, privateKeyPath, certificatePath, requestDelay, ): self.thingName = thingName self.host = host self.port = port self.rootCAPath = rootCAPath self.privateKeyPath = privateKeyPath self.certificatePath = certificatePath self.requestDelay = requestDelay # Updates this thing's shadow with system performance data at a regular interval. def run(self): print("Connecting MQTT client for {}...".format(self.thingName)) mqttClient = self.configureMQTTClient() mqttClient.connect() print("MQTT client for {} connected".format(self.thingName)) deviceShadowHandler = mqttClient.createShadowHandlerWithName( self.thingName, True ) print("Running performance shadow client for {}...\n".format(self.thingName)) while True: performance = self.readPerformance() print("[{}]".format(self.thingName)) print("CPU:\t{}%".format(performance["cpu"])) print("Memory:\t{}%\n".format(performance["memory"])) payload = {"state": {"reported": performance}} deviceShadowHandler.shadowUpdate( json.dumps(payload), self.shadowUpdateCallback, 5 ) time.sleep(args.requestDelay) # Configures the MQTT shadow client for this thing. def configureMQTTClient(self): mqttClient = AWSIoTPyMQTT.AWSIoTMQTTShadowClient(self.thingName) mqttClient.configureEndpoint(self.host, self.port) mqttClient.configureCredentials( self.rootCAPath, self.privateKeyPath, self.certificatePath ) mqttClient.configureAutoReconnectBackoffTime(1, 32, 20) mqttClient.configureConnectDisconnectTimeout(10) mqttClient.configureMQTTOperationTimeout(5) return mqttClient # Returns the local device's CPU usage, memory usage, and timestamp. def readPerformance(self): cpu = psutil.cpu_percent() memory = psutil.virtual_memory().percent timestamp = time.time() return {"cpu": cpu, "memory": memory, "timestamp": timestamp} # Prints the result of a shadow update call. def shadowUpdateCallback(self, payload, responseStatus, token): print("[{}]".format(self.thingName)) print("Update request {} {}\n".format(token, responseStatus)) # Configures debug logging for the AWS IoT Device SDK for Python. def configureLogging(): logger = logging.getLogger("AWSIoTPythonSDK.core") logger.setLevel(logging.DEBUG) streamHandler = logging.StreamHandler() formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) streamHandler.setFormatter(formatter) logger.addHandler(streamHandler) # Runs the performance shadow client with user arguments. if __name__ == "__main__": parser = configureParser() args = parser.parse_args() if args.enableLogging: configureLogging() thingClient = PerformanceShadowClient( args.thingName, args.host, args.port, args.rootCAPath, args.privateKeyPath, args.certificatePath, args.requestDelay, ) thingClient.run()
  8. Run thing_performance.py from the command line with the following parameters:

    • -n, --thingName – Your thing name, such as SiteWiseTutorialDevice1.

    • -e, --endpoint – Your custom Amazon IoT endpoint that you saved earlier in this procedure.

    • -r, --rootCA – The path to your Amazon IoT root CA certificate.

    • -c, --cert – The path to your Amazon IoT thing certificate.

    • -k, --key – The path to your Amazon IoT thing certificate private key.

    • -d, --requestDelay – (Optional) The time in seconds to wait between each device shadow update. Defaults to 1 second.

    • -v, --enableLogging – (Optional) If this parameter is present, the script prints debug messages from the Amazon IoT Device SDK for Python.

    Your command should look similar to the following example.

    python3 thing_performance.py \ --thingName SiteWiseTutorialDevice1 \ --endpoint identifier.iot.region.amazonaws.com \ --rootCA AmazonRootCA1.pem \ --cert device1/thing-id-certificate.pem.crt \ --key device1/thing-id-private.pem.key

    If you're running the script for additional Amazon IoT things, update the thing name and certificate directory accordingly.

  9. Try opening and closing programs on your device to see how the CPU and memory usages change. The script prints each CPU and memory usage reading. If the script uploads data to the device shadow service successfully, the script's output should look like the following example.

    [SiteWiseTutorialDevice1] CPU: 24.6% Memory: 85.2% [SiteWiseTutorialDevice1] Update request e6686e44-fca0-44db-aa48-3ca81726f3e3 accepted
  10. Follow these steps to verify that the script is updating the device shadow:

    1. Navigate to the Amazon IoT console.

    2. In the left navigation pane, choose All devices and then choose Things.

    3. Choose your thing, SiteWiseTutorialDevice.

    4. Choose the Device Shadows tab, choose Classic Shadow, and verify that the Shadow state looks like the following example.

      { "reported": { "cpu": 24.6, "memory": 85.2, "timestamp": 1579567542.2835066 } }

      If your thing's shadow state is empty or doesn't look like the previous example, check that the script is running and successfully connected to Amazon IoT. If the script continues to time out when connecting to Amazon IoT, check that your thing policy is configured according to this tutorial.

  11. Follow these steps to verify that the rule action is sending data to Amazon IoT SiteWise:

    1. Navigate to the Amazon IoT SiteWise console.

    2. In the left navigation pane, choose Assets.

    3. Choose the arrow next to your device fleet asset (SiteWise Tutorial Device Fleet 1 1) to expand its asset hierarchy, and then choose your device asset (SiteWise Tutorial Device 1).

    4. Choose Measurements.

    5. Verify that the Latest value cells have values for the CPU Usage and Memory Usage properties.

      
                Amazon IoT SiteWise "SiteWise Tutorial Device 1 Asset" page screenshot with "Measurements"
                  highlighted.
    6. If the CPU Usage and Memory Usage properties don't have the latest values, refresh the page. If values don't appear after a few minutes, see Troubleshooting a rule.

  12. You have completed this tutorial. If you want to explore live visualizations of your data, you can configure a portal in Amazon IoT SiteWise Monitor. For more information, see Monitoring data with Amazon IoT SiteWise Monitor. Otherwise, you can press CTRL+C in your command prompt to stop the device client script. It's unlikely the Python program will send enough messages to incur charges, but it's a best practice to stop the program when you're done.

Step 9: Cleaning up resources after the tutorial

After you complete the tutorial, clean up your resources to avoid incurring additional charges.

To delete hierarchical assets in Amazon IoT SiteWise
  1. Navigate to the Amazon IoT SiteWise console

  2. In the left navigation pane, choose Assets.

  3. When you delete assets in Amazon IoT SiteWise, you must first disassociate them.

    Complete the following steps to disassociate your device assets from your device fleet asset:

    1. Choose your device fleet asset ( SiteWise Tutorial Device Fleet 1).

    2. Choose Edit.

    3. Under Assets associated to this asset, choose Disassociate for each device asset associated to this device fleet asset.

    4. Choose Save.

      You should now see your device assets no longer organized as a hierarchy.

  4. Choose your device asset (SiteWise Tutorial Device 1).

  5. Choose Delete.

  6. In the confirmation dialog, enter Delete and then choose Delete.

  7. Repeat steps 4 through 6 for each device asset and the device fleet asset (SiteWise Tutorial Device Fleet 1).

To delete hierarchical asset models in Amazon IoT SiteWise
  1. Navigate to the Amazon IoT SiteWise console.

  2. If you haven't already, delete your device and device fleet assets. For more information, see the previous procedure. You can't delete a model if you have assets that were created from that model.

  3. In the left navigation pane, choose Models.

  4. Choose your device fleet asset model (SiteWise Tutorial Device Fleet Model).

    When deleting hierarchical asset models, start by deleting the parent asset model first.

  5. Choose Delete.

  6. In the confirmation dialog, enter Delete and then choose Delete.

  7. Repeat steps 4 through 6 for your device asset model (SiteWise Tutorial Device Model).

To disable or delete a rule in Amazon IoT Core
  1. Navigate to the Amazon IoT console.

  2. In the left navigation pane, choose Message routing and then choose Rules.

  3. Select your rule and choose Delete.

  4. In the confirmation dialog, enter the name of the rule and then choose Delete.

Troubleshooting a rule

Follow the steps in this procedure to troubleshoot your rule if the CPU and memory usage data isn't appearing in Amazon IoT SiteWise as expected. In this procedure, you configure the republish rule action as an error action to view error messages in the MQTT test client. You can also configure logging to CloudWatch Logs to troubleshoot. For more information, see Troubleshooting an Amazon IoT SiteWise rule action.

To add a republish error action to a rule
  1. Navigate to the Amazon IoT console.

  2. In the left navigation pane, choose Message routing and then choose Rules.

  3. Choose the rule that you created earlier and choose Edit.

  4. Under Error action - optional, choose Add error action.

  5. Choose Republish a message to an Amazon IoT topic.

  6. In Topic, enter sitewise/rule/tutorial/error. Amazon IoT Core will republish error messages to this topic.

  7. Choose the role that you created earlier (for example, SiteWiseTutorialDeviceRuleRole).

  8. Choose Update.

After you set up the republish error action, you can view the error messages in the MQTT test client in Amazon IoT Core.

In the following procedure, you subscribe to the error topic in the MQTT test client.

To subscribe to the error action topic
  1. Navigate to the Amazon IoT console.

  2. In the left navigation page, choose MQTT test client to open the MQTT test client.

  3. In the Topic filter field, enter sitewise/rule/tutorial/error and choose Subscribe.

When error messages appear, view the failures array in any error message to diagnose issues. For more information about possible issues and how to resolve them, see Troubleshooting an Amazon IoT SiteWise rule action.

If errors don't appear, check that your rule is enabled and that you subscribed to the same topic that you configured in the republish error action. If errors still don't appear after you do that, check that the device script is running and updating the device's shadow successfully.

Note

You can also subscribe to your device's shadow update topic to view the payload that your Amazon IoT SiteWise action parses. To do so, subscribe to the following topic.

$aws/things/+/shadow/update/accepted