Monitoring sensor readings and event notifications
In this scenario, you'll set up monitoring and notifications about changes in sensor readings connected to Yandex IoT Core. Sensors are emulated using the Yandex Cloud Functions service. If your sensors are connected, use them.
You do not need to create or configure any VMs to work with tutorial, as all operations are based on serverless computing in Cloud Functions. The source code used in this scenario is available on GitHub
To configure monitoring of sensor readings in the server room:
- Prepare your cloud
- Required paid resources
- Create the required Yandex IoT Core resources
- Create a device emulator based on Yandex Cloud Functions
- Create a function that emulates transmitting data from the device
- Test the data transmission emulation function
- Create a trigger to call the emulation function once per minute
- Create a function for processing received data
- Test the data processing function
- Create a trigger to call the data processing function with a signal
- Set up sensor reading monitoring
- Delete the created cloud resources
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
Required paid resources
The cost includes:
- Fee for the number of messages Yandex IoT Core (see pricing).
- Fee for the number of function calls Yandex Cloud Functions (see pricing).
- Fee for logging user metrics via the service API Yandex Monitoring.
Create the required Yandex IoT Core resources
Registry and device are the main components of the service Yandex IoT Core used for exchanging data and commands. Devices can only exchange data if they were created in the same registry.
Create a registry and configure authorization using your login and password
To create a registry:
-
In the management console
, select the folder where you are running the scenario. -
Select Yandex IoT Core.
-
Click Create registry.
-
In the Name field, enter a name for the registry, e.g.,
my-registry
. -
In the Password field, set the password to access the registry.
To create a password, you can use the password generator
.
Make sure to save your password, as you will need it later. -
(optional) In the Description field, add additional information about the registry.
-
Click Create.
You can also complete authorization using certificates. Learn more about authorization in Yandex IoT Core.
Create a device and configure authorization using your login and password
To create a device:
-
In the management console
, select the folder where you are running the scenario. -
Select Yandex IoT Core.
-
Select the registry you created in the previous step.
-
In the left part of the window, select Devices.
-
Click Add device.
-
In the Name field, enter the device name. For example,
my-device
. -
In the Password field, set the password to access the device.
To create a password, you can use the password generator
.
Make sure to save your password, as you will need it later. -
(optional) In the Description field, add additional information about the device.
-
(optional) Add an alias:
-
Click Add alias.
-
Fill out the fields by providing an alias, e.g.,
events
, and the topic type after$devices/<deviceID>
, e.g.,events
.You can use the
events
alias instead of the$devices/<deviceID>/events
topic. -
Repeat the steps for each alias you add.
-
-
Click Create.
-
Repeat the steps for each device you want to create.
You can also complete authorization using certificates. Learn more about authorization in Yandex IoT Core.
Create a device emulator in Cloud Functions
The emulator sends data from device sensors and handles data for monitoring and alerts.
You will need to:
- Create and test a function that emulates transmitting data from each device sensor.
- Create a trigger to call the emulation function once per minute.
- Create and test a function for processing received data.
- Create a trigger to call the data processing function by a signal.
Create a function that emulates transmitting data from the device
To create a function:
-
In the management console
, select the folder where you are running the scenario. -
Select Cloud Functions.
-
In the left part of the window, select Functions.
-
Click Create function.
-
In the Name field, enter the name of the function, e.g.,
my-device-emulator-function
. -
(optional) In the Description field, add additional information about the function.
-
Click Create.
-
In the Editor window that opens, select
nodejs12
from the Runtime environment list. -
Select Method: Code editor.
-
In the left part of the Code editor window, click Create file.
-
In the New file window that opens, enter the name
device-emulator.js
. -
Click Create.
-
Select the created file in the left part of the Code editor window.
-
In the right part of the Code editor window, insert the function code from GitHub
. -
Repeat steps 10-14 and, in a similar way, create a
package.json
file containing the following:{ "name": "my-app", "version": "1.0.0", "dependencies": { "yandex-cloud": "*" } }
-
In the Entry point field, enter
device-emulator.handler
. -
In the Timeout, sec field, enter
10
. -
In the Memory field, leave the value
128 MB
. -
Create a service account that the function will use to send data to Yandex IoT Core:
- Click Create an account.
- In the Create service account window that opens, enter the account name in the Name field, e.g.,
my-emulator-function-service-account
. - Add the
serverless.functions.invoker
andiot.devices.writer
roles for invoking the function and writing data to resources:- Click the
- Select a role from the list.
- Click Create.
- Click the
-
Set Environment variables for each sensor in the server room:
-
Click Add environment variable.
-
Fill in the Key and Value fields for the environment variables:
Key Description Value HUMIDITY_SENSOR_VALUE
Baseline humidity sensor reading. 80.15
TEMPERATURE_SENSOR_VALUE
Baseline temperature sensor reading. 25.25
RACK_DOOR_SENSOR_VALUE
State of the open rack door sensor. False
ROOM_DOOR_SENSOR_VALUE
State of the open server room door sensor. False
SMOKE_SENSOR_VALUE
State of the smoke sensor. False
WATER_SENSOR_VALUE
State of the water sensor. False
IOT_CORE_DEVICE_ID
ID of the device you created. See in the management console for
Yandex IoT Core.DEVICE_ID
User-defined device name. Specified by the user.
-
-
In the top-right part of the window, click Create version.
Test the emulation function
To test the function:
-
(optional) To get detailed information from the sensors, subscribe the registry to the Yandex IoT Core device topic, where
$devices/<deviceID>/events
is the device topic,<deviceID>
is the device ID in the service:CLIIf you do not have the Yandex Cloud command line interface yet, install and initialize it.
Run this command:
yc iot mqtt subscribe \ --username <registry ID> \ --password <registry password> \ --topic '$devices/<device ID>/events' \ --qos 1
Where:
--username
and--password
: Parameters for authorization using a username and password.--topic
: Device topic for sending data.--message
: Message text.--qos
: Quality of service (QoS).
Learn more about subscription to device topics in Yandex IoT Core.
-
In the management console
, select the folder where you are running the scenario. -
Select Cloud Functions.
-
In the left part of the window, select Testing.
-
In the Version tag field, select
$latest
(most recent function). -
Click Run test.
If the function is successful, the Function status field shows Done and the Function output field shows the following:
{
"statusCode" : 200
}
If you subscribe to the Yandex IoT Core device topic, you get the following JSON:
{
"":"0e3ce1d0-1504-4325-972f-55c961319814",
"TimeStamp":"2020-05-21T22:38:12Z",
"Values":[
{"Type":"Float","Name":"Humidity","Value":"25.33"},
{"Type":"Float","Name":"Temperature","Value":"80.90"},
{"Type":"Bool","Name":"Water sensor","Value":"False"},
{"Type":"Bool","Name":"Smoke sensor","Value":"False"},
{"Type":"Bool","Name":"Room door sensor","Value":"False"},
{"Type":"Bool","Name":"Rack door sensor","Value":"False"}
]
}
Learn more about MQTT topics in Yandex IoT Core.
Create a trigger that invokes the function once per minute
To create a trigger:
- In the management console
, select the folder where you are running the scenario. - Select Cloud Functions.
- Select Triggers.
- Click Create trigger.
- In the Name field, enter a name for the trigger, e.g.,
my-emulator-function-trigger
. - (optional) In the Description field, add additional information about the trigger.
- Select Type: Timer.
- In the Cron expression field, enter
* * * * ? *
(invoke once per minute). - Under Function settings, enter the parameters previously set for the function:
- Function:
my-device-emulator-function
. - Function version tag:
$latest
. - Service account:
my-emulator-function-service-account
.
- Function:
- (optional) Set parameters under Repeat request settings and Dead Letter Queue settings. They provide data security.
- Repeat request settings are useful if you want to call the function again when the current function request fails.
- Dead Letter Queue settings are required to forward messages that consumers could not process in standard queues.
You can configure a standard message queue as a DLQ. If you do not have a message queue yet, create one in Yandex Message Queue.
- Click Create trigger.
Create a function for processing received data
To create a function:
-
In the management console
, select the folder where you are running the scenario. -
Select Cloud Functions.
-
In the left part of the window, select Functions.
-
Click Create function.
-
In the Name field, enter the name of the function. For example,
my-monitoring-func
. -
(optional) In the Description field, add additional information about the function.
-
Click Create.
-
In the Editor window that opens, select
python37
in the Runtime environment list. -
Select Method: go to the Code editor tab.
-
In the left part of the Code editor window, click Create file.
-
In the New file window that opens, enter the name
monitoring.py
. -
Click Create.
-
In the left part of the Code editor window, select the created file.
-
In the right part of the window, insert the function code from GitHub
.In this function, the data to be sent to the monitoring service is in the
makeAllMetrics
method. If you want to add or remove parameters, make changes to this method. -
In the Entry point field of the Editor window, specify
monitoring.msgHandler
. -
In the Timeout, sec field, enter
10
. -
In the Memory field, leave the value
128 MB
. -
Create a service account to be used for the function to process data from the device:
- Click Create an account.
- In the Create service account window that opens, enter the account name in the Name field. For example,
my-metrics-function-service-account
. - Add the
serverless.functions.invoker
andeditor
roles for invoking functions and editing resources:- Click the
- Select a role from the list.
- Click Create.
- Click the
- Configure Environment variables:
- Click Add environment variable.
- Fill in the Key and Value fields for the environment variables:
Key Description Value VERBOSE_LOG
Enabling and disabling data logging. True
METRICS_FOLDER_ID
ID of the folder where the services are deployed and for which you will create a dashboard in Yandex Monitoring. See the management console for details.
-
In the top-right part of the window, click Create version.
Test the data processing function
To test the function:
-
In the management console
, select the folder where you are running the scenario. -
Select Cloud Functions.
-
In the left part of the window, select Testing.
-
In the Version tag field, select
$latest
(most recent function). -
In the Payload field, insert the following data:
{ "messages": [ { "event_metadata": { "event_id": "160d239876d9714800", "event_type": "yandex.cloud.events.iot.IoTMessage", "created_at": "2020-05-08T19:16:21.267616072Z", "folder_id": "b112345678910" }, "details": { "registry_id": "are1234567890", "device_id": "are0987654321", "mqtt_topic": "$devices/are0987654321/events", "payload": "eyJWYWx1ZXMiOiBbeyJUeXBlIjogIkZsb2F0IiwgIlZhbHVlIjogIjI1Ljc0IiwgIk5hbWUiOiAiSHVtaWRpdHkifSwgeyJUeXBlIjogIkZsb2F0IiwgIlZhbHVlIjogIjgwLjY1IiwgIk5hbWUiOiAiVGVtcGVyYXR1cmUifSwgeyJUeXBlIjogIkJvb2wiLCAiVmFsdWUiOiAiRmFsc2UiLCAiTmFtZSI6ICJXYXRlciBzZW5zb3IifSwgeyJUeXBlIjogIkJvb2wiLCAiVmFsdWUiOiAiRmFsc2UiLCAiTmFtZSI6ICJTbW9rZSBzZW5zb3IifSwgeyJUeXBlIjogIkJvb2wiLCAiVmFsdWUiOiAiRmFsc2UiLCAiTmFtZSI6ICJSb29tIGRvb3Igc2Vuc29yIn0sIHsiVHlwZSI6ICJCb29sIiwgIlZhbHVlIjogIkZhbHNlIiwgIk5hbWUiOiAiUmFjayBkb29yIHNlbnNvciJ9XSwgIlRpbWVTdGFtcCI6ICIyMDIwLTA1LTIxVDIzOjEwOjE2WiIsICJEZXZpY2VJZCI6ICIwZTNjZTFkMC0xNTA0LTQzMjUtOTcyZi01NWM5NjEzMTk4MTQifQ==" } } ] }
-
Click Run test.
If the function is successful, the Function status field shows Done and the Function output field shows the following:
{
"statusCode" : 200 ,
"headers" : {
"Content-Type" : "text/plain"
},
"isBase64Encoded" : false
}
Create a trigger to call the data processing function with a signal
The trigger will call the function when a message appears in the device topic.
To create a trigger:
- In the management console
, select the folder where you are running the scenario. - Select Cloud Functions.
- Select Triggers.
- Click Create trigger.
- In the Name field, enter a name for the trigger. For example,
my-monitoring-func-trigger
. - (optional) In the Description field, add additional information about the trigger.
- Select a Type: Yandex IoT Core.
- Under message settings Yandex IoT Core, enter the parameters previously set for the registry and device:
- Registry:
my-registry
. - Device:
my-device
. - MQTT topic:
$devices//events
, where<deviceID>
is the device ID in Yandex IoT Core.
- Registry:
- Under Function settings, enter the parameters previously set for the function:
- Function:
my-monitoring-func
. - Function version tag:
$latest
. - Service account:
my-metrics-function-service-account
.
- Function:
- (optional) Set parameters under Repeat request settings and Dead Letter Queue settings. They provide data security.
- Repeat request settings are useful if you want to call the function again when the current function request fails.
- Dead Letter Queue settings are required to forward messages that consumers could not process in standard queues.
You can configure a standard message queue as a DLQ. If you do not have a message queue yet, create one in Yandex Message Queue.
- Click Create trigger.
All data from the device will be automatically sent to Monitoring.
Set up sensor reading monitoring
Sensor readings are monitored using a dashboard in Yandex Monitoring. The controller sends them to the server every 60 seconds via the MQTT protocol. When the sensor readings reach the set values, Yandex Monitoring sends notifications to users.
{
"DeviceId":"e7a68b2d-464e-4222-88bd-c9e8d10a70cd",
"TimeStamp":"2020-05-21T10:16:43Z",
"Values":[
{"Type":"Float","Name":"Humidity","Value":"12.456"},
{"Type":"Float","Name":"Temperature","Value":"-23.456"},
{"Type":"Bool","Name":"Water sensor","Value":"false"},
{"Type":"Bool","Name":"Smoke sensor","Value":"false"},
{"Type":"Bool","Name":"Room door sensor","Value":"true"},
{"Type":"Bool","Name":"Rack door sensor","Value":"false"}
]
}
Set up monitoring of sensor readings by creating dashboard charts and alerts.
Create charts
To create charts on a dashboard:
- In the management console
, select the folder where you are running the scenario. - Select Monitoring.
- Go to the Dashboards tab.
- Click Create.
- Under Add widget, click New chart.
- In the list of service= services, select Custom Metrics.
- In the list of name= chart types, select Temperature.
- In the device_id= list, select the ID of the device that you want to create a chart for.
- Click Save.
- Repeat the steps for each chart in the list:
Temperature
: Temperature in the room.Humidity
: Humidity in the room.Water sensor
: If there is or is not water on the floor.Smoke sensor
: If there is or is not any smoke.Room door sensor
: If the door to the room is open or closed.Rack door sensor
: If the server rack door is open or closed.
- Click Save to save the dashboard.
- In the window that opens, enter a name for the dashboard and click Save.
The dashboard is available via a link to all users Yandex Cloud with the viewer
role. You can configure and edit it, zoom in or out, and enable or disable automatic data updates. Learn more about dashboards.
Test the charts on the dashboard
If you change the baselines in the environment variables of the emulating device function, you'll see these changes in the charts.
To test the charts:
-
In the management console
, select the folder where you are running the scenario. -
Select Cloud Functions.
-
In the left part of the window, select Functions. In the list of functions, select
my-device-emulator-function
. -
Go to the Editor tab.
-
Replace some of the original variable values with any other in the Environment variables section in the Value field at the bottom of the window.
Key Original value New value HUMIDITY_SENSOR_VALUE
80.15
40
TEMPERATURE_SENSOR_VALUE
25.25
15
RACK_DOOR_SENSOR_VALUE
False
True
ROOM_DOOR_SENSOR_VALUE
False
True
SMOKE_SENSOR_VALUE
False
True
WATER_SENSOR_VALUE
False
True
-
Click Create version at the top of the window.
-
In the management console
, select the folder where you are running the scenario. -
Select the service Monitoring and see how the chart indicators changed.
Create an alert
Create an alert based on the room temperature sensor readings and set up a list of alert recipients.
The service sends this alert to the recipients if the temperature sensor in the server room shows a certain temperature within a certain period (5 minutes
):
50 degrees
:Warning
alert.70 degrees
:Alarm
(a critical value).
To create an alert:
-
In the management console
, select the folder where you are running the scenario. -
Select Monitoring.
-
Click Create alert.
-
Enter a name for the alert in the Main section, the Name field.
-
Under Metrics, in the** Metrics** section, click
- In the list of service= services, select Custom Metrics.
- In the list of name= alert types, select Temperature.
- In the device_id= list, select the ID of the device that you want to create an alert for.
-
In the Alert settings section, select the condition to trigger the alert:
- In the Trigger condition list, select More.
- In the Alarm field, enter
70
. - In the Warning field, enter
50
.
-
Click the Show advanced settings link to expand additional alert parameters.
-
In the** Aggregation function** list, select Average.
-
In the Evaluation window list, select
5 minutes
. -
Under Notification channel, click Add channel.
-
In the window that opens, click Create channel.
-
In the Name field, enter a name for the channel. For example,
my-message-channel
. -
In the Method list, select Email.
You can also set up SMS notifications.
-
In the Recipients list, select an account.
You can select multiple recipients for notifications. You can specify the accounts of users who have access to your cloud as recipients. Learn more about how to add users to Yandex Cloud.
-
Click Create.
-
(optional) Select the notification channel from the table and set up additional notification parameters:
- To enable or disable sending notifications for a specific alert status, click the corresponding Notify about statuses column value:
Alarm
.Warning
.OK
.No data
.
- To set up sending repeated notifications, select in the Notify again list when you want to receive them:
Never
.- In
5 minutes
. - In
10 minutes
. - In
30 minutes
. - In
1 hour
.
- To edit the notification channel, click ... on the right of the row.
- To enable or disable sending notifications for a specific alert status, click the corresponding Notify about statuses column value:
-
Click Create alert at the bottom of the window.
You can create and configure alerts for any metric in Yandex Monitoring.
As a result of running this scenario:
- You can track the sensor readings on the charts.
- If the sensor readings reach the set values, you'll receive notifications.
Delete the created cloud resources
If you no longer need the cloud resources created in this scenario:
- Delete the registry in the service Yandex IoT Core.
- Delete the device in the service Yandex IoT Core.
- Delete functions in the service Yandex Cloud Functions.
- Delete function triggers in the service Yandex Cloud Functions.
- Delete charts in the service Yandex Monitoring.
- Delete dashboards in the service Yandex Monitoring.
- Delete alerts and notification channels in the service Yandex Monitoring.