Union Shield IoT Platform
使用 Python aiocoap 库连接 CoAP 服务器:
import asyncio
from aiocoap import *
import json
async def connect():
context = await Context.create_client_context()
# 1. 设备认证
auth_req = Message(
code=POST,
uri='coap://coap.union-shield.com:5683/auth',
payload=json.dumps({
'deviceName': 'YOUR_DEVICE_NAME',
'authKey': 'YOUR_AUTH_KEY'
}).encode()
)
auth_resp = await context.request(auth_req).response
token = json.loads(auth_resp.payload)['token']
# 2. 发送数据
send_req = Message(
code=POST,
uri=f'coap://coap.union-shield.com:5683/storage/ts?token={token}',
payload=json.dumps({
'temperature': 25.5,
'humidity': 60
}).encode()
)
await context.request(send_req).response
asyncio.run(connect())