Help Center/ IoT Device Access/ API Reference/ API Reference on the Application Side/ Examples/ Example 3: Creating a Device in a Specific Resource Space
Updated on 2023-02-16 GMT+08:00

Example 3: Creating a Device in a Specific Resource Space

Scenarios

This topic describes how to create a device in a specific resource space using APIs. For details about how to call APIs, see Calling APIs.

By default, the platform creates a default resource space for you. Unless otherwise specified, a device created is allocated to the default resource space. To manage devices by resource space, you can specify a resource space when creating a device.

Involved APIs

Procedure

  1. Create a resource space.

    1. Create a resource space.
      • API

        URL: POST /v5/iot/{project_id}/apps

        For details, see Create a Resource Space.

      • Example request
        POST https://{Endpoint}/v5/iot/{project_id}/apps
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "app_name" : "testApp"
        }
      • Example response

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "applications" : [ {
            "app_id" : "0ab87ceecbfc49acbcc8d5acdef3c68c",
            "app_name" : "testApp",
            "create_time" : "20151212T121212Z",
            "default_app" : true
          } ]
        }
    2. Record the value of app_id in the response.

  2. Create a product in the resource space created in 1.

    1. Create a product by carrying the app_id recorded in 1.b.
      • API

        URL: POST /v5/iot/{project_id}/products

        For details, see Create a Product.

      • Example request
        POST https://{Endpoint}/v5/iot/{project_id}/products
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "product_id" : "5ba24f5ebbe8f56f5a14f605",
          "name" : "Thermometer",
          "device_type" : "Thermometer",
          "protocol_type" : "CoAP",
          "data_format" : "binary",
          "manufacturer_name" : "ABC",
          "industry" : "smartCity",
          "description" : "this is a thermometer produced by Huawei",
          "service_capabilities" : [ {
            "service_type" : "temperature",
            "service_id" : "temperature",
            "description" : "temperature",
            "properties" : [ {
              "unit" : "centigrade",
              "min" : "1",
              "method" : "R",
              "max" : "100",
              "data_type" : "decimal",
              "description" : "force",
              "step" : 0.1,
              "default_value" : {
                "color" : "red",
                "size" : 1
              },
              "enum_list" : [ "string" ],
              "required" : true,
              "property_name" : "temperature",
              "max_length" : 100
            } ],
            "commands" : [ {
              "command_name" : "reboot",
              "responses" : [ {
                "response_name" : "ACK",
                "paras" : [ {
                  "unit" : "km/h",
                  "min" : "1",
                  "max" : "100",
                  "para_name" : "force",
                  "data_type" : "string",
                  "description" : "force",
                  "step" : 0.1,
                  "enum_list" : [ "string" ],
                  "required" : false,
                  "max_length" : 100
                } ]
              } ],
              "paras" : [ {
                "unit" : "km/h",
                "min" : "1",
                "max" : "100",
                "para_name" : "force",
                "data_type" : "string",
                "description" : "force",
                "step" : 0.1,
                "enum_list" : [ "string" ],
                "required" : false,
                "max_length" : 100
              } ]
            } ],
            "events" : [ {
              "event_type" : "reboot",
              "paras" : [ {
                "unit" : "km/h",
                "min" : "1",
                "max" : "100",
                "para_name" : "force",
                "data_type" : "string",
                "description" : "force",
                "step" : 0.1,
                "enum_list" : [ "string" ],
                "required" : false,
                "max_length" : 100
              } ]
            } ],
            "option" : "Mandatory"
          } ],
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka"
        }
      • Example response

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "app_name" : "testAPP01",
          "product_id" : "5ba24f5ebbe8f56f5a14f605",
          "name" : "Thermometer",
          "device_type" : "Thermometer",
          "protocol_type" : "CoAP",
          "data_format" : "binary",
          "manufacturer_name" : "ABC",
          "industry" : "smartCity",
          "description" : "this is a thermometer produced by Huawei",
          "service_capabilities" : [ {
            "service_id" : "temperature",
            "service_type" : "temperature",
            "properties" : [ {
              "property_name" : "temperature",
              "required" : true,
              "data_type" : "decimal",
              "min" : 1,
              "max" : 100,
              "max_length" : 100,
              "step" : 0.1,
              "unit" : "centigrade",
              "method" : "R",
              "description" : "force",
              "default_value" : {
                "color" : "red",
                "size" : 1
              }
            } ],
            "commands" : [ {
              "command_name" : "reboot",
              "paras" : [ {
                "para_name" : "force",
                "required" : false,
                "data_type" : "string",
                "min" : 1,
                "max" : 100,
                "max_length" : 100,
                "step" : 0.1,
                "unit" : "km/h",
                "description" : "force"
              } ],
              "responses" : [ {
                "paras" : [ {
                  "para_name" : "force",
                  "required" : false,
                  "data_type" : "string",
                  "min" : 1,
                  "max" : 100,
                  "max_length" : 100,
                  "step" : 0.1,
                  "unit" : "km/h",
                  "description" : "force"
                } ],
                "response_name" : "ACK"
              } ]
            } ],
            "events" : [ {
              "event_type" : "reboot",
              "paras" : [ {
                "para_name" : "force",
                "required" : false,
                "data_type" : "string",
                "min" : 1,
                "max" : 100,
                "max_length" : 100,
                "step" : 0.1,
                "unit" : "km/h",
                "description" : "force"
              } ]
            } ],
            "description" : "temperature",
            "option" : "Mandatory"
          } ],
          "create_time" : "20190303T081011Z"
        }
    2. Record the value of product_id in the response.

  3. Create a device in the resource space created in 1.a.

    1. Create a product by carrying the app_id and product_id recorded in 1.b and 2.b, respectively.
      • API

        URL: POST /v5/iot/{project_id}/devices

        For details, see Create a Device.

      • Example request
        POST https://{Endpoint}/v5/iot/{project_id}/devices
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "node_id" : "ABC123456789",
          "device_name" : "dianadevice",
          "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
          "auth_info" : {
            "auth_type" : "SECRET",
            "secure_access" : true,
            "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
            "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
            "timeout" : 300
          },
          "description" : "watermeter device",
          "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "extension_info" : {
            "aaa" : "xxx",
            "bbb" : 0
          },
          "shadow" : [ {
            "desired" : {
              "temperature" : "60"
            },
            "service_id" : "WaterMeter"
          } ]
        }
      • Example response

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "app_name" : "testAPP01",
          "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "node_id" : "ABC123456789",
          "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "device_name" : "dianadevice",
          "node_type" : "ENDPOINT",
          "description" : "watermeter device",
          "fw_version" : "1.1.0",
          "sw_version" : "1.1.0",
          "auth_info" : {
            "auth_type" : "SECRET",
            "secure_access" : true,
            "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
            "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
            "timeout" : 300
          },
          "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
          "product_name" : "Thermometer",
          "status" : "INACTIVE",
          "create_time" : "20190303T081011Z",
          "tags" : [ {
            "tag_value" : "testTagValue",
            "tag_key" : "testTagName"
          } ],
          "extension_info" : {
            "aaa" : "xxx",
            "bbb" : 0
          }
        }
    2. Confirm the device creation result based on the response.