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 levels.
Each sensor sends its result in JSON format. For 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 these 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.
Install Terraform
Using Terraform in Yandex Cloud, you can create cloud resources of any type, such as VMs, disks, and images. For more information about 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
directory. It will store the Terraform configuration files. -
Download
the archive with the files required to run the script and unpack it toiot-terraform
.The archive contains:
common.tf
: Terraform provider settings.files.tf
: Code file parameters to publish from the local folder.function.tf
: Parameters of a function 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 created by the script.trigger.tf
: Parameters of the trigger to invoke a function with a specified timeout.variables.tf
: Variables used 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
: ID of the folder. -
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 the following format:$devices/<device ID>/events/<subtopic>
. -
publish_execution_timeout
: Message send timeout in seconds. -
publish_cron_expression
: Schedule for sending messages to an MQTT topic as a cron expression. By default, messages send 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 are 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 plan carefully.
-
To create resources, run the command:
terraform apply
-
Confirm the resource creation: 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 whether the resources are there, as well as verify their settings, using the management console
.
Delete the resources you created
- Delete the registry:
- Go to your working folder.
- In the list of services, select Yandex IoT Core.
- To the right of the registry created, click
and select Delete. - Click Delete.
- Delete devices:
- Go to your working folder.
- In the list of services, select Yandex IoT Core.
- Select the registry.
- Go to the Devices tab.
- To the right of the name of the device 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 name, 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 name of the trigger created, click
and select Delete. - Click Delete.
- Delete the service account:
- Go to your working folder.
- In the left pane, select Service accounts.
- To the right of the service account name, click
and select Delete. - Click Delete.