Sensor reading monitoring and event notifications
In this tutorial, you will set up monitoring and notifications for changes in readings of the sensors connected to Yandex IoT Core. To emulate sensors, we will use Yandex Cloud Functions. If you have any connected sensors, use them.
You do not need to create or configure VMs, as all steps in this tutorial are entirely based on serverless computing in Cloud Functions. The source code used in this tutorial is available on GitHub
To configure monitoring of sensor readings in the server room:
- Get your cloud ready.
- Create the required Yandex IoT Core resources.
- Create a device emulator in Cloud Functions.
- Set up sensor reading monitoring.
If you no longer need the resources you created, delete them.
Get your cloud ready
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 Yandex Cloud 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.
Required paid resources
The support cost includes:
- Fee for the number of Yandex IoT Core messages (see Yandex IoT Core pricing).
- Fee for the number of function invocations in Cloud Functions (see Yandex Cloud Functions pricing).
- Fee for logging user metrics via the Monitoring API (see Yandex Monitoring pricing).
Create service accounts
- Create a service account named
my-emulator-function-service-account
for data sending. Assign thefunctions.functionInvoker
andiot.devices.writer
roles to it. - Create a service account named
my-metrics-function-service-account
for data processing. Assign thefunctions.functionInvoker
andeditor
roles to it.
Create the required Yandex IoT Core resources
Registry and device are the main Yandex IoT Core components for exchanging data and commands. Devices can only exchange data if created in the same registry.
Create a registry and set up username and password authentication
-
In the management console
, select the folder you are using to complete this tutorial. -
From the list of services, select IoT Core.
-
Click Create registry.
-
In the Name field, enter a name for the registry, e.g.,
my-registry
. -
In the Password field, set a password for registry access. To create a password, you can use this password generator
.Note
Save the password as you will need it for authentication.
-
Click Create.
Create a device and set up username and password authentication
-
In the management console
, select the folder you are using to complete this tutorial. -
From the list of services, select IoT Core.
-
Select the registry you created in the previous step.
-
Select Devices in the left-hand menu.
-
Click Add device.
-
In the Name field, enter a device name, e.g.,
my-device
. -
In the Password field, set a password for device access. To create a password, you can use this password generator
.Note
Save the password as you will need it for authentication.
-
Optionally, add an alias:
-
Click Add alias.
-
Fill in the fields: enter an alias, e.g.,
events
, and the topic type after$devices/{id}
, e.g.,events
.You can now use the
events
alias instead of the$devices/{id}/events
topic. -
Repeat these steps for each alias you add.
-
-
Click Create.
-
Repeat these steps for each device you create.
Create a device emulator in Cloud Functions
The emulator sends data from device sensors and processes data for monitoring and alerts.
Create a function to emulate sending data from the device
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Cloud Functions.
- Create a function:
- Click Create function.
- In the Name field, enter a name for the function, e.g.,
my-device-emulator-function
. - Click Create.
- Create a function version:
-
In the Editor window that opens, select
Node.js 18
. -
Disable Add files with code examples.
-
Click Continue.
-
In the Method field, select
Code editor
. -
Click Create file:
- File name:
device-emulator.js
. - File contents: Function code from GitHub
.
- File name:
-
Similarly, create a file named
package.json
with the following content:{ "name": "my-app", "version": "1.0.0", "dependencies": { "yandex-cloud": "*" } }
-
In the Entry point field, specify
device-emulator.handler
. -
Under Parameters, specify:
- Timeout:
10
- Memory:
128 MB
- Service account:
my-emulator-function-service-account
- 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
Rack door sensor reading False
ROOM_DOOR_SENSOR_VALUE
Server room door sensor reading False
SMOKE_SENSOR_VALUE
Smoke sensor reading False
WATER_SENSOR_VALUE
Water sensor reading False
IOT_CORE_DEVICE_ID
ID of the device you created. See the management console ,
Yandex IoT CoreDEVICE_ID
Custom devicename It is specified by the user.
- Timeout:
-
Click Save changes.
-
Test the emulation function
Optionally, to get detailed information from the sensors, subscribe the registry to the Yandex IoT Core device topic.
If you do not have the Yandex Cloud CLI 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
: Credentials for authentication with a username and password.--topic
: Device topic for sending data or topic alias.--qos
: Quality of service (QoS).
The command should keep running until the test is complete.
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Cloud Functions.
-
Select the function named
my-device-emulator-function
. -
In the left-hand menu, select Testing.
-
In the Version tag list, select
$latest
, which is the most recently created function. -
Click Run test.
If the function runs successfully, the Done field will show Function status and the Function output field, the following result:
{ "statusCode" : 200 }
If you subscribed to the Yandex IoT Core device topic, you will get the following JSON strings in your terminal:
{ "DeviceId":"my-device-id", "TimeStamp":"2024-06-14T15:29:59Z", "Values":[ {"Type":"Float","Name":"Humidity","Value":"80.84"}, {"Type":"Float","Name":"Temperature","Value":"25.46"}, {"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"} ] }
Create a trigger to invoke the function once per minute
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Cloud Functions.
- In the left-hand panel, select Triggers.
- Click Create trigger.
- In the Name field, enter a name for the trigger, e.g.,
my-emulator-function-trigger
. - In the Type field, select
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:
- Optionally, configure the Repeat request settings and Dead Letter Queue settings sections to protect data against loss.
-
Repeat request settings enable automatic retries if the current function invocation fails.
-
Dead Letter Queue settings enable redirecting messages that consumers failed to process in regular 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 to process incoming data
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Cloud Functions.
- In the left-hand panel, select Functions.
- Create a function:
- Click Create function.
- In the Name field, enter a name for the function, e.g.,
my-monitoring-func
. - Click Create.
- Create a function version:
-
In the Editor window that opens, select
Python 3.12
. -
Disable Add files with code examples.
-
Click Continue.
-
In the Method field, select
Code editor
. -
Click Create file:
-
File name:
monitoring.py
. -
File contents: Function code from GitHub
.Within this function, the
makeAllMetrics
method handles the processing of data to send to the monitoring service. If you want to add or remove parameters, make changes to this method.
-
-
In the Entry point field, specify
monitoring.msgHandler
. -
Under Parameters, specify:
-
Timeout:
10
-
Memory:
128 MB
-
Service account:
my-metrics-function-service-account
-
Environment variables:
Key Description Value VERBOSE_LOG
Enabling or disabling data logging True
METRICS_FOLDER_ID
ID of the folder where you deployed the services and for which you will create a dashboard in Monitoring. See the management console .
-
-
Click Save changes.
-
Test the data processing function
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Cloud Functions.
-
Select the
my-monitoring-func
function. -
In the left-hand menu, select Testing.
-
In the Version tag list, select
$latest
, which is the most recently created function. -
In the Payload field, paste 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 runs successfully, the Done field will show Function status and the Function output field, the following result:
{ "statusCode" : 200 , "headers" : { "Content-Type" : "text/plain" }, "isBase64Encoded" : false }
Create a trigger to invoke the data processing function on a signal
The trigger will invoke the function when a message appears in the device topic.
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Cloud Functions.
- In the left-hand panel, select Triggers.
- Click Create trigger.
- In the Name field, enter a name for the trigger, e.g.,
my-monitoring-func-trigger
. - In the Type field, select
IoT Core (device)
. - Under IoT Core message settings, enter the parameters previously set for the registry and device:
- Registry:
my-registry
. - Device:
my-device
. - MQTT topic:
$devices/<device_ID>/events
, where<device_ID>
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:
- Optionally, configure the Repeat request settings and Dead Letter Queue settings sections to protect data against loss.
-
Repeat request settings enable automatic retries if the current function invocation fails.
-
Dead Letter Queue settings enable redirecting messages that consumers failed to process in regular 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
Yandex Monitoring dashboards are used to monitor sensor readings. The controller sends readings to the server every 60 seconds using MQTT. When they reach the specified limits, Monitoring sends notifications to users.
Data transmission format
{
"DeviceId":"e7a68b2d-464e-4222-88bd-c9e8********",
"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 sensor reading monitoring: create charts on the dashboard, notification channel, and alert.
Create a dashboard
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Monitoring.
- Navigate to the Dashboards tab.
- Click Create.
- At the top right, click Save.
- In the window that opens, enter a name for the dashboard and click Save.
Create charts
Create a temperature variation chart:
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Monitoring.
-
Navigate to the Dashboards tab.
-
Next to the dashboard name, click
→ Edit. -
In the bottom section, click Graph.
-
Create a query for the chart:
- In the query editor, click
. - In the list of services (service=), select
Custom Metrics
. - In the list of chart types (name=), select
Temperature
, which is room temperature. - In the device_id= list, select the ID of the device for which you want to create a chart.
- In the query editor, click
-
At the top right, click Save.
Create other charts in a similar way:
Humidity
: Room air humidity.Water sensor
: Water on the floor (yes/no).Smoke sensor
: Smoke in the room (yes/no).Room door sensor
: Room door state (open/closed).Rack door sensor
: Server rack door state (open/closed).
The dashboard is available to all Yandex Cloud users with the viewer
role via the link. You can customize it, zoom in or out, and enable or disable auto-refresh.
Test the charts on the dashboard
If you change the baselines in the environment variables of the emulation function, you will see these changes in the charts.
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Cloud Functions.
-
Select the function named
my-device-emulator-function
. -
Click the Editor tab.
-
Change some original variable values as you like under Environment variables 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 Save changes and wait for the operation to complete.
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Monitoring and check how the chart metrics have changed.
Create a notification channel
Customize a list of recipients and select a notification method.
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Monitoring.
-
Navigate to the Notification channels tab.
-
Click Create channel.
-
In the Name field, enter a name for the channel, e.g.,
my-message-channel
. -
In the Method list, select
Email
.You can also set up notifications through text messages, push, or Telegram.
-
In the Recipients list, select an account.
You can select multiple recipients for notifications. You can specify the accounts of users having access to your cloud as recipients. Learn more about how to add users to Yandex Cloud here.
-
Click Create.
Create an alert
Create an alert based on the room temperature sensor readings and connect the created notification channel to it.
Monitoring will send 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
alert (critical value).
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Monitoring.
- Click Create alert.
- In the Name field, enter a name for the alert.
- Under Alert configuration, click
and fill out the fields:- In the list of services (service=), select
Custom Metrics
. - In the list of alert types (name=), select
Temperature
. - In the device_id= list, select the ID of the device for which you want to create an alert.
- In the list of services (service=), select
- Under Alert conditions, set the alert trigger conditions:
- In the Aggregation function list, select
Average
. - In the Comparison function list, select
Greater than
. - In the Warning field, enter
50
. - In the Alarm field, enter
70
. - In the Evaluation window list, select
5 minutes
.
- In the Aggregation function list, select
- Under Notifications, click Edit, and then Add.
- In the window that opens, select
my-message-channel
in the Notification method field and click Add. - At the bottom of the window, click Create.
You can create and configure alerts for any metric in Monitoring.
Once you complete this tutorial:
- You will be able to monitor sensor readings on the charts.
- If sensor readings reach the specified limits, you will receive notifications.
How to delete the resources you created
To stop paying for the resources you created: