Emulating multiple IoT devices
In this scenario, you will learn to emulate multiple devices that send messages to MQTT topics. The example shows the emulation of air sensors that measure the following parameters:
- Temperature
- Humidity
- Pressure
- CO2 level
Each sensor sends its readings in JSON format. Here is an example:
{
"DeviceId":"0e3ce1d0-1504-4325-972f-55c961319814",
"TimeStamp":"2020-05-21T22:53:16Z",
"Values":[{
"Type":"Float",
"Name":"Humidity",
"Value":"25.281837"
},
{
"Type":"Float",
"Name":"CarbonDioxide",
"Value":"67.96608"
},
{
"Type":"Float",
"Name":"Pressure",
"Value":"110.7021"
},
{
"Type":"Float",
"Name":"Temperature",
"Value":"127.708824"
}]
}
To emulate the operation of multiple devices:
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:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Install Terraform
Using Terraform in Yandex Cloud, you can create cloud resources of any type, such as VMs, disks, or images. For more information about the resources you can create with Terraform, see the provider documentation.
From a mirror
Alert
The mirror may be unavailable.
You can download a Terraform distribution for your platform from a mirrorPATH variable:
export PATH=$PATH:/path/to/terraform
From the HashiCorp website
Use one of the following methods:
-
Download the Terraform distribution
and follow this guide to install it. -
Install Terraform using the Chocolatey
package manager and the command below:choco install terraform
Download the Terraform distribution
Use one of the following methods:
-
Download the Terraform distribution
and follow this guide to install it. -
Install Terraform using the Homebrew
package manager and the command below:brew install terraform
Describe the infrastructure
-
Create the
iot-terraformfolder. It will store the Terraform configuration files. -
Download
an archive with the files you need for this scenario and unpack it to theiot-terraformdirectory.The archive contains:
common.tf: Terraform provider settings.files.tf: Parameters for publishing code files from the local directory.function.tf: Parameters of the function used to write emulated messages to devices.iot_core.tf: Parameters of the registry where the devices are located.output.tf.tf: Output variables.publish: Files required to create a function.service_account.tf: Parameters of the service account being created in this scenario.trigger.tf: Parameters of the trigger to invoke a function with a specified timeout.variables.tf: Applicable variables and their values.
-
Edit the
variables.tffile specifying the following emulation parameters:-
token: OAuth token for Yandex Cloud access. -
cloud_id: Cloud ID. -
folder_id: Folder ID. -
zone: Availability zone. -
device_count: Number of emulated devices.Note
To emulate the operation of over 1000 devices, you need to increase quotas by making a request to technical support.
-
subtopic_for_publish: Subtopic in$devices/<device_ID>/events/<subtopic>format. -
publish_execution_timeout: Timeout for sending messages in seconds. -
publish_cron_expression: Schedule for sending messages to an MQTT topic as a cron expression. By default, messages are sent every minute.
You can leave the remaining files unchanged.
-
Deploy cloud resources
-
Go to the
iot-terraformfolder and check the configuration using the following command:terraform validateResult:
Success! The configuration is valid. -
Format the configuration files in the current folder and subfolders:
terraform fmtResult:
main.tf variables.tf -
After checking the configuration, run the command:
terraform planYou will see a list of resources and their properties. This is a test step; no resources will be created. If the configuration contains any errors, Terraform will show them.
Alert
You will be charged for all the resources created with Terraform. Check the pricing plan carefully.
-
To create resources, run the command:
terraform apply -
Confirm creating the resources: type
yesin the terminal and press Enter.Result:
Outputs: function = "d4erep.......aq085f0" iot_core = "are.......ht10enkb3u" service_account = "ajestqfepa.......0l6" trigger = "a1sva8sse.......7kf6"Terraform will create all the required resources, and the terminal will display the IDs of the resources created. You can check the new resources and their configuration using the management console
.
Delete the resources you created
- Delete the registry:
- Go to your working folder.
- In the list of services, select IoT Core.
- To the right of the registry you created, click
and select Delete. - Click Delete.
- Delete the devices:
- Navigate to your working folder.
- In the list of services, select IoT Core.
- Select the registry.
- Navigate to the Devices tab.
- To the right of the device you created, click
and select Delete. - Click Delete.
- Delete the function:
- Navigate to your working folder.
- In the list of services, select Cloud Functions.
- To the right of the function you created, click
and select Delete. - Click Delete.
- Delete the trigger:
- Navigate to your working folder.
- In the list of services, select Cloud Functions.
- Navigate to the Triggers tab.
- To the right of the trigger you created, click
and select Delete. - Click Delete.
- Delete the service account:
- Navigate to your working folder.
- In the list of services, select Identity and Access Management.
- In the left-hand panel, select
Service accounts. - In the row with the name of the service account you created, click
and select Delete. - Click Delete.