Monitoring sensor readings and event notifications
- Prepare your cloud
- Create the required Yandex IoT Core resources
- Create a device emulator in Cloud Functions
- Create a function that emulates transmitting data from the device
- Test the 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
- How to delete the resources you created
In this tutorial, you will set up monitoring and notifications about changes in the 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 to work with the tutorial, as all operations are 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:
- Prepare your cloud.
- 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, delete them.
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 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 Cloud Functions function calls (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
to send data. Assign thefunctions.functionInvoker
andiot.devices.writer
roles to it. - Create a service account named
my-metrics-function-service-account
to process data. Assign thefunctions.functionInvoker
andeditor
roles to it.
Create the required Yandex IoT Core resources
Registry and device are the main components of the Yandex IoT Core service used for exchanging data and commands. Devices can only exchange data if created in the same registry.
Create a registry and configure authorization with your login and password
-
In the management console
, select the folder you are using to complete this tutorial. -
In 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 to access the registry. To create a password, you can use the password generator
.Note
Save the password as you will need it for authentication.
-
Click Create.
Create a device and configure authorization using your login and password
-
In the management console
, select the folder you are using to complete this tutorial. -
In 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 the device name, e.g.,
my-device
. -
In the Password field, set a password to access the device. To create a password, you can use the 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 topic type after$devices/{id}
, e.g.,events
.You can use the
events
alias instead of the$devices/{id}/events
topic. -
Repeat the steps for each alias you add.
-
-
Click Create.
-
Repeat the steps for each device you want to create.
Create a device emulator in Cloud Functions
The emulator sends data from device sensors and handles data for monitoring and alerts.
Create a function that emulates transmitting 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 the Add files with code examples option.
-
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:
-
In a similar way, create a
package.json
file with the following contents:{ "name": "my-app", "version": "1.0.0", "dependencies": { "yandex-cloud": "*" } }
-
In the Entry point field, specify
device-emulator.handler
. -
Under Parameters, specify:
- Timeout, sec:
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
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 the Yandex IoT Core
management console .DEVICE_ID
User-defined device name. Specified by the user.
- Timeout, sec:
-
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 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 authentication with a username and password.--topic
: Device topic for sending data or topic alias.--qos
: Quality of service (QoS).
The command should continue 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
my-device-emulator-function
. -
In the left-hand menu, select Testing.
-
In the Version tag list, select
$latest
, which is the latest created function. -
Click Run test.
If the function is successful, the Done status will be displayed in the Function status field and the result will appear in the Function output field:
{ "statusCode" : 200 }
If you subscribed to the Yandex IoT Core device topic, you will get the following JSON strings in the 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 call the emulation 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 section parameters. They provide data security.
-
Repeat request settings allow you to repeatedly call the function if the current function call fails.
-
Dead Letter Queue settings allow you to redirect messages that consumers could not process in common 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
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Cloud Functions.
- In the panel on the left, select the Functions section.
- 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 the Add files with code examples option.
-
Click Continue.
-
In the Method field, select
Code editor
. -
Click Create file:
-
File name:
monitoring.py
. -
File contents: Function code from GitHub
.In this function, the data to send 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, specify
monitoring.msgHandler
. -
Under Parameters, specify:
-
Timeout, sec:
10
-
Memory:
128 MB
-
Service account:
my-metrics-function-service-account
. -
Environment variables:
Key Description Value VERBOSE_LOG
Turning data logging on and off True
METRICS_FOLDER_ID
ID of the folder where the services are deployed 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 latest 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 is successful, you will see the Done status in the Function status field and the Function output field will display this result:
{ "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.
- 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 section parameters. They provide data security.
-
Repeat request settings allow you to repeatedly call the function if the current function call fails.
-
Dead Letter Queue settings allow you to redirect messages that consumers could not process in common 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, Monitoring sends notifications to users.
Data transfer 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 monitoring of the sensor readings: 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.
- Go 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 chart of the temperature change:
-
In the management console
, select the folder you are using to complete this tutorial. -
In the list of services, select Monitoring.
-
Go to the Dashboards tab.
-
Next to the dashboard name you need, 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
: Temperature in the room. - In the device_id= list, select the ID of the device that you want to create a chart for.
- In the query editor, click
-
At the top right, click Save.
Create other charts in a similar way:
Humidity
: Humidity in the room.Water sensor
: Water on the floor (there is water/no water).Smoke sensor
: Smoke in the room (there is smoke/no smoke).Room door sensor
: Room door status (door open/door closed).Rack door sensor
: Server rack door status (door open/door closed).
The dashboard is available via a link to all Yandex Cloud users with the viewer
role. You can customize it, zoom in or out, and enable or disable automatic data updates.
Test the charts on the dashboard
If you change the baselines in the environment variables of the emulating device 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
my-device-emulator-function
. -
Click the Editor tab.
-
Replace some original variable values with any other 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 until the operation is complete.
- In the management console
, select the folder you are using to complete this tutorial. - In the list of services, select Monitoring and see 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.
-
Go to the Notification channels tab.
-
Click Create channel.
-
In the Name field, specify the channel name, e.g.,
my-message-channel
. -
In the Method list, select
Email
.You can also set up notifications through text messages, push notifications, or Telegram.
-
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.
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
(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 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 list of services (service=), select
- Under Alert conditions, set the conditions to trigger the alert:
- 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 Notification method field of the window that opens, select
my-message-channel
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 track the sensor readings on the charts.
- If the sensor readings reach the specified values, you will receive notifications.
How to delete the resources you created
To stop paying for the resources you created: