A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.aws.amazon.com/iot/latest/developerguide/iot-sns-rule.html below:

Tutorial: Sending an Amazon SNS notification

Tutorial: Sending an Amazon SNS notification

This tutorial demonstrates how to create an AWS IoT rule that sends MQTT message data to an Amazon SNS topic so that it can be sent as an SMS text message.

In this tutorial, you create a rule that sends message data from a weather sensor to all subscribers of an Amazon SNS topic, whenever the temperature exceeds the value set in the rule. The rule detects when the reported temperature exceeds the value set by the rule, and then creates a new message payload that includes only the device ID, the reported temperature, and the temperature limit that was exceeded. The rule sends the new message payload as a JSON document to an SNS topic, which notifies all subscribers to the SNS topic.

What you'll learn in this tutorial:

This tutorial takes about 30 minutes to complete.

Step 1: Create an Amazon SNS topic that sends a SMS text message

This procedure exaplains how to create the Amazon SNS topic your weather sensor can send message data to. The Amazon SNS topic will then notify all of its subscribers via a SMS text message of the temperature limit that was exceeded.

To create an Amazon SNS topic that sends an SMS text message
  1. Create an Amazon SNS topic.

    1. Sign in to the Amazon SNS console.

    2. In the left navigation pane, choose Topics.

    3. On the Topics page, choose Create topic.

    4. In Details, choose the Standard type. By default, the console creates a FIFO topic.

    5. In Name, enter the SNS topic name. For this tutorial, enter high_temp_notice.

    6. Scroll to the end of the page and choose Create topic.

      The console opens the new topic's Details page.

  2. Create an Amazon SNS subscription.

    Note

    The phone number that you use in this subscription might incur text messaging charges from the messages you will send in this tutorial.

    1. In the high_temp_notice topic's details page, choose Create subscription.

    2. In Create subscription, in the Details section, in the Protocol list, choose SMS.

    3. In Endpoint, enter the number of a phone that can receive text messages. Be sure to enter it such that it starts with a +, includes the country and area code, and doesn't include any other punctuation characters.

    4. Choose Create subscription.

  3. Test the Amazon SNS notification.

    1. In the Amazon SNS console, in the left navigation pane, choose Topics.

    2. To open the topic's details page, in Topics, in the list of topics, choose high_temp_notice.

    3. To open the Publish message to topic page, in the high_temp_notice details page, choose Publish message.

    4. In Publish message to topic, in the Message body section, in Message body to send to the endpoint, enter a short message.

    5. Scroll down to the bottom of the page and choose Publish message.

    6. On the phone with the number you used earlier when creating the subscription, confirm that the message was received.

    If you did not receive the test message, double check the phone number and your phone's settings.

    Make sure you can publish test messages from the Amazon SNS console before you continue the tutorial.

Step 2: Create an AWS IoT rule to send the text message

The AWS IoT rule that you'll create in this tutorial subscribes to the device/device_id/data MQTT topics where device_id is the ID of the device that sent the message. These topics are described in a topic filter as device/+/data, where the + is a wildcard character that matches any string between the two forward slash characters. This rule also tests the value of the temperature field in the message payload.

When the rule receives a message from a matching topic, it takes the device_id from the topic name, the temperature value from the message payload, and adds a constant value for the limit it's testing, and sends these values as a JSON document to an Amazon SNS notification topic.

For example, an MQTT message from weather sensor device number 32 uses the device/32/data topic and has a message payload that looks like this:

{
  "temperature": 38,
  "humidity": 80,
  "barometer": 1013,
  "wind": {
    "velocity": 22,
    "bearing": 255
  }
}

The rule's rule query statement takes the temperature value from the message payload, the device_id from the topic name, and adds the constant max_temperature value to send a message payload that looks like this to the Amazon SNS topic:

{
  "device_id": "32",
  "reported_temperature": 38,
  "max_temperature": 30
}
To create an AWS IoT rule to detect an over-limit temperature value and create the data to send to the Amazon SNS topic
  1. Open the Rules hub of the AWS IoT console.

  2. If this is your first rule, choose Create, or Create a rule.

  3. In Create a rule:

    1. In Name, enter temp_limit_notify.

      Remember that a rule name must be unique within your AWS account and Region, and it can't have any spaces. We've used an underscore character in this name to separate the words in the rule's name.

    2. In Description, describe the rule.

      A meaningful description makes it easier to remember what this rule does and why you created it. The description can be as long as needed, so be as detailed as possible.

  4. In Rule query statement of Create a rule:

    1. In Using SQL version, select 2016-03-23.

    2. In the Rule query statement edit box, enter the statement:

      SELECT topic(2) as device_id, 
          temperature as reported_temperature, 
          30 as max_temperature 
        FROM 'device/+/data' 
        WHERE temperature > 30

      This statement:

      • Listens for MQTT messages with a topic that matches the device/+/data topic filter and that have a temperature value greater than 30.

      • Selects the second element from the topic string and assigns it to the device_id field.

      • Selects the value temperature field from the message payload and assigns it to the reported_temperature field.

      • Creates a constant value 30 to represent the limit value and assigns it to the max_temperature field.

  5. To open up the list of rule actions for this rule, in Set one or more actions, choose Add action.

  6. In Select an action, choose Send a message as an SNS push notification.

  7. To open the selected action's configuration page, at the bottom of the action list, choose Configure action.

  8. In Configure action:

    1. In SNS target, choose Select, find your SNS topic named high_temp_notice, and choose Select.

    2. In Message format, choose RAW.

    3. In Choose or create a role to grant AWS IoT access to perform this action, choose Create Role.

    4. In Create a new role, in Name, enter a unique name for the new role. For this tutorial, use sns_rule_role.

    5. Choose Create role.

    If you're repeating this tutorial or reusing an existing role, choose Update role before continuing. This updates the role's policy document to work with the SNS target.

  9. Choose Add action and return to the Create a rule page.

    In the new action's tile, below Send a message as an SNS push notification, you can see the SNS topic that your rule will call.

    This is the only rule action you'll add to this rule.

  10. To create the rule and complete this step, in Create a rule, scroll down to the bottom and choose Create rule.

Step 3: Test the AWS IoT rule and Amazon SNS notification

To test your new rule, you'll use the MQTT client to publish and subscribe to the MQTT messages used by this rule.

Open the MQTT client in the AWS IoT console in a new window. This will let you edit the rule without losing the configuration of your MQTT client. If you leave the MQTT client to go to another page in the console, it won't retain any subscriptions or message logs.

To use the MQTT client to test your rule
  1. In the MQTT client in the AWS IoT console, subscribe to the input topics, in this case, device/+/data.

    1. In the MQTT client, under Subscriptions, choose Subscribe to a topic.

    2. In Subscription topic, enter the topic of the input topic filter, device/+/data.

    3. Keep the rest of the fields at their default settings.

    4. Choose Subscribe to topic.

      In the Subscriptions column, under Publish to a topic, device/+/data appears.

  2. Publish a message to the input topic with a specific device ID, device/32/data. You can't publish to MQTT topics that contain wildcard characters.

    1. In the MQTT client, under Subscriptions, choose Publish to topic.

    2. In the Publish field, enter the input topic name, device/32/data.

    3. Copy the sample data shown here and, in the edit box below the topic name, paste the sample data.

      {
        "temperature": 38,
        "humidity": 80,
        "barometer": 1013,
        "wind": {
          "velocity": 22,
          "bearing": 255
        }
      }
    4. Choose Publish to topic to publish your MQTT message.

  3. Confirm that the text message was sent.

    1. In the MQTT client, under Subscriptions, there is a green dot next to the topic to which you subscribed earlier.

      The green dot indicates that one or more new messages have been received since the last time you looked at them.

    2. Under Subscriptions, choose device/+/data to check that the message payload matches what you just published and looks like this:

      {
        "temperature": 38,
        "humidity": 80,
        "barometer": 1013,
        "wind": {
          "velocity": 22,
          "bearing": 255
        }
      }
    3. Check the phone that you used to subscribe to the SNS topic and confirm the contents of the message payload look like this:

      {"device_id":"32","reported_temperature":38,"max_temperature":30}

      Notice that the device_id value is a quoted string and the temperature value is numeric. This is because the topic() function extracted the string from the input message's topic name while the temperature value uses the numeric value from the input message's payload.

      If you want to make the device_id value a numeric value, replace topic(2) in the rule query statement with:

      cast(topic(2) AS DECIMAL)

      Note that casting the topic(2) value to a numeric, DECIMAL value will only work if that part of the topic contains only numeric characters.

  4. Try sending an MQTT message in which the temperature does not exceed the limit.

    1. In the MQTT client, under Subscriptions, choose Publish to topic.

    2. In the Publish field, enter the input topic name, device/33/data.

    3. Copy the sample data shown here and, in the edit box below the topic name, paste the sample data.

      {
        "temperature": 28,
        "humidity": 80,
        "barometer": 1013,
        "wind": {
          "velocity": 22,
          "bearing": 255
        }
      }
    4. To send your MQTT message, choose Publish to topic.

    You should see the message that you sent in the device/+/data subscription. However, because the temperature value is below the max temperature in the rule query statement, you shouldn't receive a text message.

    If you don't see the correct behavior, check the troubleshooting tips.

Troubleshooting your SNS message rule

Here are some things to check, in case you're not seeing the results you expect.

Step 4: Review the results and next steps In this tutorial: Next steps

After you send a few text messages with this rule, try experimenting with it to see how changing some aspects of the tutorial affect the message and when it's sent. Here are some ideas to get you started.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4