您可以使用HTTP API参考来开发将执行配置请求的设备固件。
如前所述,设备可以在注册过程中请求服务器生成凭据或提供其自己的凭据。请参阅以下每个选项的请求/响应和代码示例:
由ThingsBoard服务器生成的凭证 |
设备提供 访问令牌 |
---|---|
参数 | 示例值 | 描述 |
---|---|---|
设备名称 | 设备名称 | ThingsBoard中的设备名称。 |
ProvisionDeviceKey | PUT_PROVISION_KEY_HERE | 供应设备密钥,您应该从已配置的设备配置文件中获取它。 |
ProvisionDeviceSecret | PUT_PROVISION_SECRET_HERE | 设置设备密码,您应该从已配置的设备配置文件中获取它。 |
请求数据示例:
{
"deviceName": "DEVICE_NAME",
"provisionDeviceKey": "PUT_PROVISION_KEY_HERE",
"provisionDeviceSecret": "PUT_PROVISION_SECRET_HERE"
}
响应示例:
{
"status":"SUCCESS",
"credentialsType":"ACCESS_TOKEN",
"credentialsValue":"sLzc0gDAZPkGMzFVTyUY"
}
样例脚本
脚本源代码如下。您可以将其复制粘贴到文件中,例如:
device-provision-example.py
现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:
python3 device-provision-example.py
脚本源代码:
from requests import post
from json import dumps
def collect_required_data():
config = {}
print("\n\n", "="*80, sep="")
print(" "*10, "\033[1m\033[94mThingsBoard device provisioning without authorization example script. HTTP API\033[0m", sep="")
print("="*80, "\n\n", sep="")
host = input("Please write your ThingsBoard \033[93mhost\033[0m or leave it blank to use default (https://thingsboard.cloud): ")
config["host"] = host if host else "https://thingsboard.cloud"
port = input("Please write your ThingsBoard \033[93mHTTP port\033[0m or leave it blank to use default (80): ")
config["port"] = int(port) if port else 80
config["provision_device_key"] = input("Please write \033[93mprovision device key\033[0m: ")
config["provision_device_secret"] = input("Please write \033[93mprovision device secret\033[0m: ")
device_name = input("Please write \033[93mdevice name\033[0m or leave it blank to generate: ")
if device_name:
config["device_name"] = device_name
print("\n", "="*80, "\n", sep="")
return config
# Example for message to ThingsBoard
to_publish = {
"stringKey": "value1",
"booleanKey": True,
"doubleKey": 42.0,
"longKey": 73,
"jsonKey": {
"someNumber": 42,
"someArray": [1, 2, 3],
"someNestedObject": {"key": "value"}
}
}
if __name__ == '__main__':
config = collect_required_data()
THINGSBOARD_HOST = config["host"] # ThingsBoard instance host
THINGSBOARD_PORT = config["port"] # ThingsBoard instance MQTT port
PROVISION_REQUEST = {"provisionDeviceKey": config["provision_device_key"], # Provision device key, replace this value with your value from device profile.
"provisionDeviceSecret": config["provision_device_secret"], # Provision device secret, replace this value with your value from device profile.
}
if config.get("device_name") is not None:
PROVISION_REQUEST["deviceName"] = config["device_name"]
response = post("%s:%i/api/v1/provision" % (THINGSBOARD_HOST, THINGSBOARD_PORT), json=PROVISION_REQUEST)
decoded_response = response.json()
print("Received response: ")
print(decoded_response)
received_token = decoded_response.get("credentialsValue")
if received_token is not None:
response = post('%s:%i/api/v1/%s/telemetry' % (THINGSBOARD_HOST, THINGSBOARD_PORT, received_token,), dumps(to_publish))
print("[THINGSBOARD CLIENT] Response code from Thingsboard.")
print(response.status_code)
else:
print("Failed to get access token from response.")
print(decoded_response.get("errorMsg"))
参数 | 示例值 | 描述 |
---|---|---|
设备名称 | 设备名称 | ThingsBoard中的设备名称。 |
ProvisionDeviceKey | PUT_PROVISION_KEY_HERE | 供应设备密钥,您应该从已配置的设备配置文件中获取它。 |
ProvisionDeviceSecret | PUT_PROVISION_SECRET_HERE | 设置设备密码,您应该从已配置的设备配置文件中获取它。 |
凭据类型 | ACCESS_TOKEN | 凭证类型参数。 |
代币 | DEVICE_ACCESS_TOKEN | 在ThingsBoard中访问设备的令牌。 |
请求数据示例:
{
"deviceName": "DEVICE_NAME",
"provisionDeviceKey": "PUT_PROVISION_KEY_HERE",
"provisionDeviceSecret": "PUT_PROVISION_SECRET_HERE",
"credentialsType": "ACCESS_TOKEN",
"token": "DEVICE_ACCESS_TOKEN"
}
响应示例:
{
"credentialsType":"ACCESS_TOKEN",
"credentialsValue":"DEVICE_ACCESS_TOKEN",
"status":"SUCCESS"
}
样例脚本
脚本源代码如下。您可以将其复制粘贴到文件中,例如:
device-provision-example.py
现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:
python3 device-provision-example.py
脚本源代码:
from requests import post
from json import dumps
def collect_required_data():
config = {}
print("\n\n", "="*80, sep="")
print(" "*10, "\033[1m\033[94mThingsBoard device provisioning with access token authorization example script. HTTP API\033[0m", sep="")
print("="*80, "\n\n", sep="")
host = input("Please write your ThingsBoard \033[93murl\033[0m or leave it blank to use default (https://thingsboard.cloud): ")
config["host"] = host if host else "https://thingsboard.cloud"
port = input("Please write your ThingsBoard \033[93mHTTP port\033[0m or leave it blank to use default (80): ")
config["port"] = int(port) if port else 80
config["provision_device_key"] = input("Please write \033[93mprovision device key\033[0m: ")
config["provision_device_secret"] = input("Please write \033[93mprovision device secret\033[0m: ")
config["token"] = input("Please write \033[93mdevice access token\033[0m: ")
device_name = input("Please write \033[93mdevice name\033[0m or leave it blank to generate: ")
if device_name:
config["device_name"] = device_name
print("\n", "="*80, "\n", sep="")
return config
# Example for message to ThingsBoard
to_publish = {
"stringKey": "value1",
"booleanKey": True,
"doubleKey": 42.0,
"longKey": 73,
"jsonKey": {
"someNumber": 42,
"someArray": [1, 2, 3],
"someNestedObject": {"key": "value"}
}
}
if __name__ == '__main__':
config = collect_required_data()
THINGSBOARD_HOST = config["host"] # ThingsBoard instance host
THINGSBOARD_PORT = config["port"] # ThingsBoard instance MQTT port
PROVISION_REQUEST = {"provisionDeviceKey": config["provision_device_key"], # Provision device key, replace this value with your value from device profile.
"provisionDeviceSecret": config["provision_device_secret"], # Provision device secret, replace this value with your value from device profile.
"credentialsType": "ACCESS_TOKEN",
"token": config["token"],
}
if config.get("device_name") is not None:
PROVISION_REQUEST["deviceName"] = config["device_name"]
response = post("%s:%i/api/v1/provision" % (THINGSBOARD_HOST, THINGSBOARD_PORT), json=PROVISION_REQUEST)
decoded_response = response.json()
print("Received response: ")
print(decoded_response)
received_token = decoded_response.get("credentialsValue")
if received_token is not None:
response = post('%s:%i/api/v1/%s/telemetry' % (THINGSBOARD_HOST, THINGSBOARD_PORT, received_token,), dumps(to_publish))
print("[THINGSBOARD CLIENT] Response code from Thingsboard.")
print(response.status_code)
else:
print("Failed to get access token from response.")
print(decoded_response.get("errorMsg"))