Table of Contents

AWS IoT Exploration – Part III – Alert Creation and Data Monitoring

 

In this post, we will see how to send notifications via email or text message by creating an AWS IoT rule. This rule sends MQTT message data to an Amazon SNS topic. The topic will then filter the data and send a notification as per the set rule.

Assume you are working with a temperature sensor and wish to receive a notification via email and SMS if the temperature crosses a set limit. The rule will detect whenever the reported temperature crosses the maximum capacity and sends out a notification that includes the device ID, reported temperature, and the exceeded temperature in JSON format. You must set the maximum limit for your temperature sensor device in the rule section.

 Create an Amazon SNS topic

  • In the Amazon SNS console choose Topics from left side menu
  • Choose Create topic and then the Standard type In Details By default, the console creates topics in FIFO order.
  • Enter the SNS topic name in Name (e.g. high_temp_notice)
  • At the end of the page choose Create topic
  • The new topic’s Details page will now display in the console

Create a subscription for Amazon SNS

  • The topic’s details page has a name high_temp_notice.
  • Choose Create subscription
  • Within Create subscription page, under the Details section, we can see the Protocol list
  • Choose SMS or Email (you can add both by creating two subscriptions)
  • In the Endpoint field, enter the mobile number where the text message must be sent. Ensure it starts with a + symbol including the country code. In the same way, we add an email by creating another subscription.
  • Choose Create subscription.

Test the Amazon SNS notification

  • Choose Topics from the Amazon SNS console
  • Here we’ll see a list of topics. Choose the topic name i.e high_temp_notice.
  • In the high_temp_notice details page, choose Publish message
  • In the Publish message to topic section, we’ll see a Message body section, and then in the Message body to send to the endpoint field, enter a short message
  • Choose Publish message at the bottom
  • Confirm that the text message was received on the mobile number which we configured earlier

Sending a text message based on specific criteria with an AWS IoT rule

The AWS IoT rule that we’ll create here will subscribe to the MQTT topics device/device_id/data where device_id is the ID of the IoT device (ex: temperature sensor) that will send the message. We’ve described these topics in a topic filter as device/+/data. The plus symbol is a wildcard character using which we can pass the identity for any device (such as device_id). With this rule, we can also test the value of the temperature field in the MQTT message payload.

If the rule receives a message from a matching topic, it will take the temperature value and the device_id from the topic name, add a constant value for the maximum limit for it’s testing, and send these values as a JSON document to an Amazon SNS notification topic.

For example, an MQTT message from the weather sensor device #32 uses the device/32/data topic and has the following message payload,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"temperature" : 22 ,
"humidity" : 84 ,
"barometer" : 1233 ,
"wind" :
{
"velocity" : 32 ,
"bearing" : 245
}
}
{ "temperature": 22, "humidity": 84, "barometer": 1233, "wind": { "velocity": 32, "bearing": 245 } }
 
{
    "temperature": 22,
    "humidity": 84,
    "barometer": 1233,
    "wind":
    {
        "velocity": 32,
        "bearing": 245
    }
}

The rule query statement will collect the ‘temperature’ value from the message payload and ‘device_id’ from the topic. It will add a constant value such as ‘max_temperature’ and send the message payload to an Amazon SNS topic that looks like this,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"device_id" : "32" ,
"reported_temperature" : 38 ,
"max_temperature" : 30
}
{ "device_id": "32", "reported_temperature": 38, "max_temperature": 30 }
 
{
    "device_id": "32",
    "reported_temperature": 38,
    "max_temperature": 30
}

Creating rules that verify an over-limit temperature value and create the data for an Amazon SNS topic

  • Select Rule from left Navigation in the AWS IoT console
  • Choose Create.
  • Under Create a rule page
    • Enter any name in the Name field – temp_limit_notify. This name must be unique in your AWS region and IoT rule, and should not contain any spaces (underscores are allowed).
    • Add a meaningful and easy to understand description in the Description
  • In the Rule query statement section of Create a rule page:
    • Select 2016-03-23 under the Using SQL version
    • Enter the following statement in the Rule query statement field:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT topic ( 2 ) as device_id,
temperature as reported_temperature,
30 as max_temperature
FROM 'device/+/data'
WHERE temperature > 30
SELECT topic(2) as device_id, temperature as reported_temperature, 30 as max_temperature FROM 'device/+/data' WHERE temperature > 30
 
SELECT topic(2) as device_id, 
temperature as reported_temperature, 
30 as max_temperature 
FROM 'device/+/data' 
WHERE temperature > 30
    • This query will: Match the rule query with the device/+/data topic filter where the and if the temperature value is greater than 30, it will listen for the MQTT message. It then Selects the second element from the topic string (device/+/data) and assigns it to the device_id It selects the value temperature and assigns it to the reported_temperature field from the message payload. It then creates a constant value 30 which represent the maximum temperature limit value and assigns it to the max_temperature field.
  • Choose Add action, in Set one or more actions section to open the list of rule actions.
  • In Select an action, choose Send a message as an SNS push notification
  • if you wish to open the selected action’s configuration page, choose Configure action, at the bottom of the action list
  • Under Configure action:
    • SNS target -> Select -> search for the SNS topic with the name high_temp_notice and then Select.
    • Choose RAW, In Message format
    • Choose Create Role in Choose or create a role to grant AWS IoT access to perform this action
    • Under Create a new role, enter a unique name for the new role. (For e.g. sns_rule_role)
    • Now choose Create role.
  • If you are reusing an existing role, choose Update role. This will update the role’s policy document to work with the SNS target.
  • Choose Add action and return to the Create a rule You can now see the SNS topic that the rule will call in the new action’s tile, right below Send a message as an SNS push notification
  • On the Create a rule page, scroll down and choose Create rule, to complete the step.

Test the AWS IoT rule and Amazon SNS notification:

To test the rule that we just created, we must use the MQTT client to subscribe and to publish the message using this rule.

Open the MQTT client in the AWS IoT console in a new window. We can edit our existing rules without losing the previous configuration of the MQTT client. The client will not retain any subscriptions or message logs if we open the same window or if move away from the client to visit another page.

  • In AWS IoT Console, Subscribe to the topic device/+/data.
    • In the left navigation, choose Test, and then Subscribe to a topic.
    • Enter the topic in the input topic filter – device/+/data
    • In the Subscription topic, leave the other fields as they are.
    • In the Subscriptions column, choose Subscribe to topic, and select device/+/data from Publish to a topic
  • Publish a message to the given input topic by providing a specific device ID – device/32/data. You must list the complete device ID. We cannot publish to MQTT topics that contain wildcard characters.
  • Under the Subscriptions section, choose Publish to topic in the MQTT client
    • Enter the input topic name e., device/32/data in the Publish field
    • Enter the following data in the message payload section
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"temperature" : 22 ,
"humidity" : 84 ,
"barometer" : 1233 ,
"wind" :
{
"velocity" : 32 ,
"bearing" : 245
}
}
{ "temperature": 22, "humidity": 84, "barometer": 1233, "wind": { "velocity": 32, "bearing": 245 } }
 
{
    "temperature": 22,
    "humidity": 84,
    "barometer": 1233,
    "wind":
    {
        "velocity": 32,
        "bearing": 245
    }
}
    • Choose Publish to topic to publish your MQTT message.
  • Confirm that the text message was sent successfully as per the AWS IoT rule.
    • A green dot next to the topic under Subscriptions indicates new messages in the topic.
    • Under the Subscriptions section, choose the topic (i.e., device/+/data) to see the message payload that passed through MQTT matches and should look like this:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"temperature" : 22 ,
"humidity" : 84 ,
"barometer" : 1233 ,
"wind" :
{
"velocity" : 22 ,
"bearing" : 255
}
}
{ "temperature": 22, "humidity": 84, "barometer": 1233, "wind": { "velocity": 22, "bearing": 255 } }
 
{
    "temperature": 22,
    "humidity": 84,
    "barometer": 1233,
    "wind":
    {
        "velocity": 22,
        "bearing": 255
    }
}
  • On the phone, a message like below would show,
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"device_id" : "32" ,
"reported_temperature" : 38 ,
"max_temperature" : 30
}
{ "device_id": "32", "reported_temperature": 38, "max_temperature": 30 }
 
{
    "device_id": "32",
    "reported_temperature": 38,
    "max_temperature": 30
}

The device_id is a string data type and temperature values are numeric since the topic() function extracts device_id from the subscribed topic as a string. However, the temperature value from the message payload is numeric. To make the device_id a numeric value, you must typecast it in the rule query statement – cast(topic(2) AS DECIMAL. If you have passed any numeric value in the topic (i.e. in place of a wildcard character) then casting topic(2) value to a numeric is possible.

  • If the recorded temperature is below the maximum limit then you will not receive any notification.
    • Under the Subscriptions section, choose Publish to topic in the MQTT client
    • Enter your input topic name e. device/33/data, in the Publish field
    • You must pass the data message payload section shown below,
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"temperature" : 22 ,
"humidity" : 84 ,
"barometer" : 1233 ,
"wind" :
{
"velocity" : 32 ,
"bearing" : 245
}
}
{ "temperature": 22, "humidity": 84, "barometer": 1233, "wind": { "velocity": 32, "bearing": 245 } }
 
{
    "temperature": 22,
    "humidity": 84,
    "barometer": 1233,
    "wind":
    {
        "velocity": 32,
        "bearing": 245
    }
}
    • Choose Publish to topic to publish your MQTT message

Since the temperature value is below the maximum value, no email or phone notification will be sent.

 

Alert creation for high temperature.

 

Rule creation for alert message based on range.

 

Alert message in the inbox.

 

Monitoring data using AWS IoT.

 

New call-to-action

Learn More about Encora

We are the software development company fiercely committed and uniquely equipped to enable companies to do what they can’t do now.

Learn More

Global Delivery

READ MORE

Careers

READ MORE

Industries

READ MORE

Related Insights

Online Travel Agencies: Some Solutions to changes in booking and commission attributions

Discover how we can simplify travel changes for both travelers and OTAs using blockchain and ...

Read More

The AI-Powered Journey: How AI is Changing the Face of Travel

As travel elevates itself into an experience where every journey is as unique as the travelers ...

Read More

Enhancing Operational Excellence with AI: A Game-Changer for the Hospitality Industry

By AI, the hospitality industry can offer the best of both worlds: the efficiency and ...

Read More
Previous Previous
Next

Accelerate Your Path
to Market Leadership 

Encora logo

Santa Clara, CA

+1 669-236-2674

letstalk@encora.com

Innovation Acceleration

Speak With an Expert

Encora logo

Santa Clara, CA

+1 (480) 991 3635

letstalk@encora.com

Innovation Acceleration