Quick Start: Create a Digital Twin and Send Data
This scenario shows how to create a digital twin instance and how to test your configuration by sending sample data using basic authentication.
Prerequisites
Allow group <group-name> to manage iot-family in compartment <your-compartment-name>When you use the command for Step 3: Create a digital twin instance if you use the basic authentication, then you will need to create a secret, to do that you need the following policy.
Allow any-user to {SECRET_BUNDLE_READ, SECRET_READ} in compartment <compartment-name> where ALL {request.principal.type = 'iotdomain', target.vault.id = '<vault-OCID>'}Step 1: Create an IoT Domain Group
If you have an existing IoT domain group or IoT domain you want to work with, skip this step and use those OCIDs to create the associated digital twin instance. Or complete these steps to create an IoT domain group and IoT domain. For a complete list of options, see Creating an IoT Domain Group.
Use the oci iot domain-group create command and the required --compartment-idparameter to create an IoT domain group in a specific compartment. Replace the<compartment-OCID>value with your compartment OCID:oci iot domain-group create --compartment-id <compartment-OCID>Type option:- If you do not specify a
--typethe default domain group type islightweight, for development and testing a lightweight domain group that costs less and uses fewer resources. When you want to create a production domain group, then use the standard option. - For a production domain group, use the
--typeparameter with thestandardvalue to create a standard domain group that scales with high availability and automatic failover.
For a complete list of parameters and values for CLI commands, see CLI Command Reference.
- If you do not specify a
Run the CreateIotDomainGroup operation to create an IoT domain group in a compartment.
Step 2: Create an IoT Domain
Run CreateIotDomain operation to create an IoT domain.
Step 3: Create a Digital Twin Instance
In this example, the digital twin instance is not associated to a digital twin model or to a digital twin adapter. You can use this method if you are just testing your digital twin setup or if you are setting up a digital twin instance to connect to a device that sends unstructured data. For a complete list of options, see Creating a Digital Twin Instance.
- External Key Options: The external key is the device user name when you connect to a device to send data in step 5. If you do not include an
--external-keyparameter, the external key is generated in the response. If you specify the external key, it's recommended not to use quotes with the external key value. - Authentication Options: When you create a digital twin instance, authentication is optional and only required if you send data to or from a device. Add authentication to the digital twin instance using the
--auth-idparameter.
- Use the oci iot digital-twin-instance create command and these parameters to create a digital twin instance.
- Replace the
<iot-domain-OCID>with the IoT domain you want to associate to this IoT digital twin instance. Replace the<secret-or-certificate-OCID>with the vault secret OCID:oci iot digital-twin-instance create --auth-id <secret-OCID> --iot-domain-id <iot-domain-OCID>
Run the CreateDigitalTwinInstance operation to create a digital twin instance.
Step 4: Get the IoT Domain Details
If you have the device host endpoint for the IoT domain you want to use, skip this step.
The IoT domain details contains the device host, including the domain short id, for more Getting an IoT Domain's Details,
Use the oci iot domain get command and the required parameter to get an IoT domain's details, including the device host with the domain short id:
oci iot domain get --iot-domain-id <iot-domain-OCID>For a complete list of parameters and values for CLI commands, see CLI Command Reference.
Run the GetIotDomain operation to get a domain's details.
Step 5: Send Data
- Device user name: Use the
external keyfrom the digital twin instance as the device user name. - Device password: Is associated to the authentication ID for the digital twin instance. If the digital twin instance uses a vault secret OCID for authentication, then use the plain text secret contents as the device password, see Creating a Secret and Getting a Secret's Contents. Using a vault secret as a device password is recommended only for testing, not for production.
Using Curl
POSIX-style shells: Use thiscurlcommand when using bash, zsh, macOS Terminal, Linux, or Git Bash on Windows.curl -u '<digital-twin-instance-external-key>:<device-password>' \ -H 'Content-Type: text/plain' \ -d 'sample data 1' \ 'https://<iot-domain-short-id>.device.iot.<region>.oci.oraclecloud.com/sampletopic'Windows Powershell: Use this
curlcommand in this example use-Gto make aGETrequest and append any data supplied with--data-urlencodeto the URL as query parameters. Or you can use the environmental variables.curl.exe -u "<digital-twin-instance-external-key>:<device-password>" ` -H "Content-Type: text/plain" ` -d "sample data 1" ` "https://<iot-domain-short-id>.device.iot.<region>.oci.oraclecloud.com/sampletopic"When you complete Step 3: Create a Digital Twin Instance, if you defined the external key parameter value with quotes, you must include the quotes when you send data:
"external-key". For quoting best practices, see Troubleshooting.- Use any MQTT client, this example uses MQTTX with the following settings.
- Download and set up MQTTX follow these instructions, see Getting Started with MQTTX. Open MQTTX.
- Select + New Connection, to create a new connection.
- Enter the external key
<unique-id>value as the Username. You can find the external key in theoci iot digital-twin-instance createresponse, from the previous Step 3: Create a Digital Twin Instance:"external-key": "<unique-id>" - Enter the device password. If you testing use vault secret, this must be the plain text secret contents. Or for a secure production environment, use a mTLS certificate.
- Enter the Host. Select the
mqtts://protocol from the host drop-down list and enter the device host:<domain-short-id>.device.iot.<region>.oci.oraclecloud.comfrom the IoT domain. - Enter the port, for example:
8883Note
Currently, MQTT Secure (MQTTS) is only supported using port8883. - Turn on the toggle SSL/TLS.
- Turn on the toggle SSL Secure.
- For the Certificate, select the CA signed server certificate option.
- When you configure the MQTTX connection make sure to connect using a
clean sessionand set theLast-Will-Retainoption tofalseto allow client subscriptions to be retained if the device briefly disconnects. - Set the Last Will QoS to 1.
- Select Connect.
After you finish these steps, you have a digital twin instance in the IoT platform that can receive data from a device.
What to do Next
- Review the IoT sample files to set up test and production environments.
- Configure APEX to view you IoT domain data in APEX, and then install a sample APEX IoT Explorer application to visually monitor your IoT devices and messages.
- If you don't plan to keep the resources, delete the IoT domains associated to the IoT domain group, and delete the IoT domain group.