您可以使用HTTP 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. }

样例脚本

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

  1. device-provision-example.py

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

  1. python3 device-provision-example.py

脚本源代码:

  1. from requests import post
  2. from json import 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. HTTP 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 (https://thingsboard.cloud): ")
  9. config["host"] = host if host else "https://thingsboard.cloud"
  10. port = input("Please write your ThingsBoard \033[93mHTTP port\033[0m or leave it blank to use default (80): ")
  11. config["port"] = int(port) if port else 80
  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. response = post("%s:%i/api/v1/provision" % (THINGSBOARD_HOST, THINGSBOARD_PORT), json=PROVISION_REQUEST)
  41. decoded_response = response.json()
  42. print("Received response: ")
  43. print(decoded_response)
  44. received_token = decoded_response.get("credentialsValue")
  45. if received_token is not None:
  46. response = post('%s:%i/api/v1/%s/telemetry' % (THINGSBOARD_HOST, THINGSBOARD_PORT, received_token,), dumps(to_publish))
  47. print("[THINGSBOARD CLIENT] Response code from Thingsboard.")
  48. print(response.status_code)
  49. else:
  50. print("Failed to get access token from response.")
  51. 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. }

样例脚本

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

  1. device-provision-example.py

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

  1. python3 device-provision-example.py

脚本源代码:

  1. from requests import post
  2. from json import 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. HTTP API\033[0m", sep="")
  7. print("="*80, "\n\n", sep="")
  8. host = input("Please write your ThingsBoard \033[93murl\033[0m or leave it blank to use default (https://thingsboard.cloud): ")
  9. config["host"] = host if host else "https://thingsboard.cloud"
  10. port = input("Please write your ThingsBoard \033[93mHTTP port\033[0m or leave it blank to use default (80): ")
  11. config["port"] = int(port) if port else 80
  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. response = post("%s:%i/api/v1/provision" % (THINGSBOARD_HOST, THINGSBOARD_PORT), json=PROVISION_REQUEST)
  44. decoded_response = response.json()
  45. print("Received response: ")
  46. print(decoded_response)
  47. received_token = decoded_response.get("credentialsValue")
  48. if received_token is not None:
  49. response = post('%s:%i/api/v1/%s/telemetry' % (THINGSBOARD_HOST, THINGSBOARD_PORT, received_token,), dumps(to_publish))
  50. print("[THINGSBOARD CLIENT] Response code from Thingsboard.")
  51. print(response.status_code)
  52. else:
  53. print("Failed to get access token from response.")
  54. print(decoded_response.get("errorMsg"))