您可以使用CoAP 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"
}
样例脚本
为了与ThingsBoard通信,我们将使用CoAPthon3模块,因此我们应该安装它:
pip3 install coapthon3 --user
脚本源代码如下。您可以将其复制粘贴到文件中,例如:
device-provision-example.py
现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:
python3 device-provision-example.py
脚本源代码:
from coapthon.client.helperclient import HelperClient
from json import loads, dumps
def collect_required_data():
config = {}
print("\n\n", "="*80, sep="")
print(" "*10, "\033[1m\033[94mThingsBoard device provisioning without authorization example script. CoAP 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 (thingsboard.cloud): ")
config["host"] = host if host else "thingsboard.cloud"
port = input("Please write your ThingsBoard \033[93mCoAP port\033[0m or leave it blank to use default (5683): ")
config["port"] = int(port) if port else 5683
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"]
client = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
response = client.post('/api/v1/provision', dumps(PROVISION_REQUEST))
client.stop()
decoded_response = loads(response.payload)
print("Received response: ")
print(decoded_response)
received_token = decoded_response.get("credentialsValue")
if received_token is not None:
thingsboardClient = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
response = thingsboardClient.post('/api/v1/%s/telemetry' % (received_token,), dumps(to_publish))
print("[THINGSBOARD CLIENT] Response from Thingsboard.")
print(response)
thingsboardClient.stop()
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"
}
样例脚本
为了与ThingsBoard通信,我们将使用CoAPthon3模块,因此我们应该安装它:
pip3 install coapthon3 --user
脚本源代码如下。您可以将其复制粘贴到文件中,例如:
device-provision-example.py
现在,您应该运行脚本并按照其中的步骤进行操作。
您可以使用python 3启动脚本:
python3 device-provision-example.py
脚本源代码:
from coapthon.client.helperclient import HelperClient
from json import loads, 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. CoAP 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 (thingsboard.cloud): ")
config["host"] = host if host else "thingsboard.cloud"
port = input("Please write your ThingsBoard \033[93mCoAP port\033[0m or leave it blank to use default (5683): ")
config["port"] = int(port) if port else 5683
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"]
client = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
response = client.post('/api/v1/provision', dumps(PROVISION_REQUEST))
client.stop()
decoded_response = loads(response.payload)
print("Received response: ")
print(decoded_response)
received_token = decoded_response.get("credentialsValue")
if received_token is not None:
thingsboardClient = HelperClient(server=(THINGSBOARD_HOST, THINGSBOARD_PORT))
response = thingsboardClient.post('/api/v1/%s/telemetry' % (received_token,), dumps(to_publish))
print("[THINGSBOARD CLIENT] Response from Thingsboard.")
print(response)
thingsboardClient.stop()
else:
print("Failed to get access token from response.")
print(decoded_response.get("errorMsg"))