1
我正在使用aiocoap庫向嵌入式設備發出CoAP請求。通信和基本功能似乎工作正常,但文檔沒有說明如何啓用觀察。觀察功能確實似乎出現在源代碼中,包括服務器端和客戶端 - 我需要客戶端。如何使用aiocoap庫觀察CoAP資源?
這是我到目前爲止有:一GET
請求在服務器上創建的觀察狀態:
import asyncio, aiocoap
@asyncio.coroutine
def coap_get_with_observe():
protocol = yield from aiocoap.Context.create_client_context()
request = aiocoap.Message(code = aiocoap.GET)
request.set_request_uri('coap://[aaaa::212:4b00:a49:e903]/sensors/temp')
# set observe bit from None to 0
request.opt.observe = 0
try:
response = yield from protocol.request(request).response
except Exception as e:
print("request failed: %s" % str(e))
else:
print("request ok: %r" % response.payload)
event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)
event_loop.create_task(coap_get_with_observe())
asyncio.get_event_loop().run_forever()
輸出:
request ok: b'{"HDC_TEMP":2426}'
這版畫只是客戶端收到的第一個值;我也想打印後續的值。