Fast Access
This section uses the smoke detector demo to create a product model, configure and start the demo, and use basic SDK functions. For details about the demo source code, see SmokeDetector.cs. A smoke detector product model is provided to help you understand the product model. This smoke detector can report the smoke density, temperature, humidity, and smoke alarms, and execute the ring alarm command. The following uses the smoke detector as an example to introduce the procedures of message reporting and property reporting.
Creating a Product
A smoke detector product model is provided to help you understand the product model. This smoke detector can report the smoke density, temperature, humidity, and smoke alarms, and execute the ring alarm command. The following uses the smoke detector as an example to introduce the procedures of message reporting and property reporting.
- Access the IoTDA service page and click Access Console. Click the target instance card.
- Choose Products in the navigation pane and click .
Figure 1 Creating a product
- Set the parameters as prompted and click OK.
Basic Info
Resource Space
The platform automatically allocates the created product to the default resource space. You can also select an existing resource space from the drop-down list or create one.
Product Name
Customize the product name. The name can contain letters, numbers, underscores (_), and hyphens (-).
Protocol
Select MQTT.
Data Type
Select JSON.
Device Type Selection
Select Custom.
Device Type
Select smokeDetector.
Advanced Settings
Product ID
Leave this parameter blank.
Description
Set this parameter based on service requirements.
Figure 2 Creating a product - MQTT
Uploading a Product Model
- Download the smokeDetector product model file.
- Click the name of the created product to access its details page.
- On the Basic Information tab page, click Import from Local to upload the product model file obtained in 1.
Figure 3 Product - Uploading a product model
Registering a Device
- In the navigation pane, choose Devices > All Devices, and click Register Device.
- Set the parameters as prompted and click OK.
Parameter
Description
Resource Space
Ensure that the device and the created product belong to the same resource space.
Product
Select the created product.
Node ID
Customize a unique physical identifier for the device. The value consists of letters and digits.
Device Name
Customize the device name.
Authentication Type
Select Secret.
Secret
Customize the device secret. If this parameter is not set, the platform automatically generates a secret.
After the device is registered, save the node ID, device ID, and secret.
Accessing the Device
- Obtain the access address by choosing Overview > Access Details on the console.
Figure 4 Access information - MQTT access address on the device side
- Open the iot-device-demo/config/DemoConfigDefault.json file, and enter the obtained access address, device ID, and device secret in the corresponding positions. Alternatively, enter the configuration in the following template and overwrite the iot-device-demo/config/DemoConfigDefault.json file.
{ "DemoName": "SmokeDetector", "AuthConfig": { "AuthUseCert": false, "ServerAddress": "Access address", "ServerPort": 8883, "DeviceId": "Device ID", "DeviceSecret": "Device secret" } }DemoName specifies the demo to be run. The demo named SmokeDetector showcases the programming demo for the smoke detector product model. Go to the SDK directory and run the following commands to check the list of all demo names:
cd iot-device-demo dotnet run --project .\iot-device-demo.csproj
Figure 5 Checking the demo name list
- Write and run the code.
- Go to the SDK directory and run the following commands to run the demo. .\config\DemoConfigDefault.json is the configuration file path.
cd iot-device-demo dotnet run --project .\iot-device-demo.csproj .\config\DemoConfigDefault.json
If you use the SDK of an earlier version (5.0+) and cannot upgrade it to .NET 8.0, you can modify the TargetFramework in the corresponding file.Figure 6 .NET versions
- Run the commands below to compile the project. If connect success is displayed, the connection is successful.
dotnet clean dotnet build
Figure 7 Connection succeeded
- Go to the SDK directory and run the following commands to run the demo. .\config\DemoConfigDefault.json is the configuration file path.
- Check the device running status. On the device details page of the console, check whether the device is online and the reported product model data.
Figure 8 Viewing reported data - smokeDetector
The SDK provides automatic reconnection by default. After the device is initialized, if the connection fails due to an unstable or unreachable network, or proactive disconnection by the platform, the SDK automatically initiates reconnection in the backend. To disable this function, see Disconnection and Connection.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot