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, e.g.:
{
"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.
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.
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-terraform
folder. It will store the Terraform configuration files. -
Download
an archive with the files you need for this scenario and unpack it to theiot-terraform
directory.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.tf
file 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-terraform
folder and check the configuration using the following command:terraform validate
Result:
Success! The configuration is valid.
-
Format the configuration files in the current folder and subfolders:
terraform fmt
Result:
main.tf variables.tf
-
After checking the configuration, run the command:
terraform plan
The terminal will display a list of resources with parameters. This is a test step; no resources will be created. If the configuration contains any errors, Terraform will point them out.
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
yes
in 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:
- Go to your working folder.
- In the list of services, select IoT Core.
- Select the registry.
- Go to the Devices tab.
- To the right of the device you created, click
and select Delete. - Click Delete.
- Delete the function:
- Go 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:
- Go to your working folder.
- In the list of services, select Cloud Functions.
- Go to the Triggers tab.
- To the right of the trigger you created, click
and select Delete. - Click Delete.
- Delete the service account:
- Go 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.