您可以使用CoAP API参考来开发将执行配置请求的设备固件。
如前所述,设备可以在注册过程中请求服务器生成凭据或提供其自己的凭据。请参阅以下每个选项的请求/响应和代码示例:[ |
由ThingsBoard服务器生成的凭证
|
设备提供
访问令牌
| | —- | —- |

]()

参数 示例值 描述
设备名称 设备名称 ThingsBoard中的设备名称。
ProvisionDeviceKey PUT_PROVISION_KEY_HERE 供应设备密钥,您应该从已配置的设备配置文件中获取它。
ProvisionDeviceSecret PUT_PROVISION_SECRET_HERE 设置设备密码,您应该从已配置的设备配置文件中获取它。

供应请求数据示例:

  1. {
  2. "deviceName": "DEVICE_NAME",
  3. "provisionDeviceKey": "PUT_PROVISION_KEY_HERE",
  4. "provisionDeviceSecret": "PUT_PROVISION_SECRET_HERE"
  5. }

供应响应示例:

  1. {
  2. "status":"SUCCESS",
  3. "credentialsType":"ACCESS_TOKEN",
  4. "credentialsValue":"sLzc0gDAZPkGMzFVTyUY"
  5. }

样例脚本

为了与ThingsBoard通信,我们将使用CoAPthon3模块,因此我们应该安装它:

  1. pip3 install coapthon3 --user

脚本源代码如下。您可以将其复制粘贴到文件中,例如:

  1. device-provision-example.py

现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:

  1. python3 device-provision-example.py

脚本源代码:

  1. from coapthon.client.helperclient import HelperClient
  2. from json import loads, dumps
  3. def collect_required_data():
  4. config = {}
  5. print("\n\n", "="*80, sep="")
  6. print(" "*10, "\033[1m\033[94mThingsBoard device provisioning without authorization example script. CoAP API\033[0m", sep="")
  7. print("="*80, "\n\n", sep="")
  8. host = input("Please write your ThingsBoard \033[93mhost\033[0m or leave it blank to use default (thingsboard.cloud): ")
  9. config["host"] = host if host else "thingsboard.cloud"
  10. port = input("Please write your ThingsBoard \033[93mCoAP port\033[0m or leave it blank to use default (5683): ")
  11. config["port"] = int(port) if port else 5683
  12. config["provision_device_key"] = input("Please write \033[93mprovision device key\033[0m: ")
  13. config["provision_device_secret"] = input("Please write \033[93mprovision device secret\033[0m: ")
  14. device_name = input("Please write \033[93mdevice name\033[0m or leave it blank to generate: ")
  15. if device_name:
  16. config["device_name"] = device_name
  17. print("\n", "="*80, "\n", sep="")
  18. return config
  19. # Example for message to ThingsBoard
  20. to_publish = {
  21. "stringKey": "value1",
  22. "booleanKey": True,
  23. "doubleKey": 42.0,
  24. "longKey": 73,
  25. "jsonKey": {
  26. "someNumber": 42,
  27. "someArray": [1, 2, 3],
  28. "someNestedObject": {"key": "value"}
  29. }
  30. }
  31. if __name__ == '__main__':
  32. config = collect_required_data()
  33. THINGSBOARD_HOST = config["host"] # ThingsBoard instance host
  34. THINGSBOARD_PORT = config["port"] # ThingsBoard instance MQTT port
  35. PROVISION_REQUEST = {"provisionDeviceKey": config["provision_device_key"], # Provision device key, replace this value with your value from device profile.
  36. "provisionDeviceSecret": config["provision_device_secret"], # Provision device secret, replace this value with your value from device profile.
  37. }
  38. if config.get("device_name") is not None:
  39. PROVISION_REQUEST["deviceName"] = config["device_name"]
  40. client = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
  41. response = client.post('/api/v1/provision', dumps(PROVISION_REQUEST))
  42. client.stop()
  43. decoded_response = loads(response.payload)
  44. print("Received response: ")
  45. print(decoded_response)
  46. received_token = decoded_response.get("credentialsValue")
  47. if received_token is not None:
  48. thingsboardClient = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
  49. response = thingsboardClient.post('/api/v1/%s/telemetry' % (received_token,), dumps(to_publish))
  50. print("[THINGSBOARD CLIENT] Response from Thingsboard.")
  51. print(response)
  52. thingsboardClient.stop()
  53. else:
  54. print("Failed to get access token from response.")
  55. print(decoded_response.get("errorMsg"))
参数 示例值 描述
设备名称 设备名称 ThingsBoard中的设备名称。
ProvisionDeviceKey PUT_PROVISION_KEY_HERE 供应设备密钥,您应该从已配置的设备配置文件中获取它。
ProvisionDeviceSecret PUT_PROVISION_SECRET_HERE 设置设备密码,您应该从已配置的设备配置文件中获取它。
凭据类型 ACCESS_TOKEN 凭证类型参数。
代币 DEVICE_ACCESS_TOKEN 在ThingsBoard中访问设备的令牌。

供应请求数据示例:

  1. {
  2. "deviceName": "DEVICE_NAME",
  3. "provisionDeviceKey": "PUT_PROVISION_KEY_HERE",
  4. "provisionDeviceSecret": "PUT_PROVISION_SECRET_HERE",
  5. "credentialsType": "ACCESS_TOKEN",
  6. "token": "DEVICE_ACCESS_TOKEN"
  7. }

供应响应示例:

  1. {
  2. "credentialsType":"ACCESS_TOKEN",
  3. "credentialsValue":"DEVICE_ACCESS_TOKEN",
  4. "status":"SUCCESS"
  5. }

样例脚本

为了与ThingsBoard通信,我们将使用CoAPthon3模块,因此我们应该安装它:

  1. pip3 install coapthon3 --user

脚本源代码如下。您可以将其复制粘贴到文件中,例如:

  1. device-provision-example.py

现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:

  1. python3 device-provision-example.py

脚本源代码:

  1. from coapthon.client.helperclient import HelperClient
  2. from json import loads, dumps
  3. def collect_required_data():
  4. config = {}
  5. print("\n\n", "="*80, sep="")
  6. print(" "*10, "\033[1m\033[94mThingsBoard device provisioning with access token authorization example script. CoAP API\033[0m", sep="")
  7. print("="*80, "\n\n", sep="")
  8. host = input("Please write your ThingsBoard \033[93mhost\033[0m or leave it blank to use default (thingsboard.cloud): ")
  9. config["host"] = host if host else "thingsboard.cloud"
  10. port = input("Please write your ThingsBoard \033[93mCoAP port\033[0m or leave it blank to use default (5683): ")
  11. config["port"] = int(port) if port else 5683
  12. config["provision_device_key"] = input("Please write \033[93mprovision device key\033[0m: ")
  13. config["provision_device_secret"] = input("Please write \033[93mprovision device secret\033[0m: ")
  14. config["token"] = input("Please write \033[93mdevice access token\033[0m: ")
  15. device_name = input("Please write \033[93mdevice name\033[0m or leave it blank to generate: ")
  16. if device_name:
  17. config["device_name"] = device_name
  18. print("\n", "="*80, "\n", sep="")
  19. return config
  20. # Example for message to ThingsBoard
  21. to_publish = {
  22. "stringKey": "value1",
  23. "booleanKey": True,
  24. "doubleKey": 42.0,
  25. "longKey": 73,
  26. "jsonKey": {
  27. "someNumber": 42,
  28. "someArray": [1, 2, 3],
  29. "someNestedObject": {"key": "value"}
  30. }
  31. }
  32. if __name__ == '__main__':
  33. config = collect_required_data()
  34. THINGSBOARD_HOST = config["host"] # ThingsBoard instance host
  35. THINGSBOARD_PORT = config["port"] # ThingsBoard instance MQTT port
  36. PROVISION_REQUEST = {"provisionDeviceKey": config["provision_device_key"], # Provision device key, replace this value with your value from device profile.
  37. "provisionDeviceSecret": config["provision_device_secret"], # Provision device secret, replace this value with your value from device profile.
  38. "credentialsType": "ACCESS_TOKEN",
  39. "token": config["token"],
  40. }
  41. if config.get("device_name") is not None:
  42. PROVISION_REQUEST["deviceName"] = config["device_name"]
  43. client = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
  44. response = client.post('/api/v1/provision', dumps(PROVISION_REQUEST))
  45. client.stop()
  46. decoded_response = loads(response.payload)
  47. print("Received response: ")
  48. print(decoded_response)
  49. received_token = decoded_response.get("credentialsValue")
  50. if received_token is not None:
  51. thingsboardClient = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
  52. response = thingsboardClient.post('/api/v1/%s/telemetry' % (received_token,), dumps(to_publish))
  53. print("[THINGSBOARD CLIENT] Response from Thingsboard.")
  54. print(response)
  55. thingsboardClient.stop()
  56. else:
  57. print("Failed to get access token from response.")
  58. print(decoded_response.get("errorMsg"))