Python 使用REST api向设备发送云消息Azure IoT Hub,端点
我正在尝试使用Azure IoT Hub和REST api(不使用Azure IoT Hub python SDK)从云向我的设备发送消息 我可以使用uriPython 使用REST api向设备发送云消息Azure IoT Hub,端点,python,azure,rest,cloud,azure-iot-hub,Python,Azure,Rest,Cloud,Azure Iot Hub,我正在尝试使用Azure IoT Hub和REST api(不使用Azure IoT Hub python SDK)从云向我的设备发送消息 我可以使用uri0从设备向集线器成功发送消息(POST请求)https://.azure-devices.net/devices//messages/events?api-版本=2018-06-30。在它的文档中说,/messages/devicebound上有一个服务端端点。但是,它们并没有显示一个完整的示例,因此我不能完全确定我应该使用的完整端点是什么,
0从设备向集线器成功发送消息(POST请求)https://.azure-devices.net/devices//messages/events?api-版本=2018-06-30
。在它的文档中说,/messages/devicebound
上有一个服务端端点。但是,它们并没有显示一个完整的示例,因此我不能完全确定我应该使用的完整端点是什么,以及如何/在哪里指定要发送到哪个设备
无论如何,我尝试了以下方法:
curl -v POST \
https://<myhub>.azure-devices.net/messages/devicebound?api-version=2018-06-30 \
-H 'Authorization: SharedAccessSignature <sas>' \
-H 'Content-Type: application/json' \
-d '{
"payload": {
"key": "value"
}
}'
在那里我切断了末端。所以我尝试添加一个“To”头,不管我放什么,它都会返回相同的错误消息
我也尝试了这里的建议,即通过端点发送,但没有运气 要使用IoT Hub API从设备端接收云到设备消息,您必须执行以下请求-
curl -X GET \
'https://{your hub}.azure-devices.net/devices/{your device id}/messages/deviceBound?api-version=2018-06-30' \
-H 'Authorization: {your sas token}'
以下是如何做到这一点:
pip安装azure iothub服务客户端
python SendCloudToDeviceMessage.py
参考:我不是在问如何接收消息,我是在问如何将它们从云端发送到设备。嘿,我正在尝试实现同样的目标,你找到了一种方法吗???我已经跟踪了你收到的错误消息,并将
-H'iothub添加到:'\
标题,但我得到了错误{“消息”:“ErrorCode:ArgumentInvalid;请求必须在IoTHub custom'To'标头中包含设备Id”,“ExceptionMessage”
,尽管我已在标头中放置了正确的设备Id。
curl -X GET \
'https://{your hub}.azure-devices.net/devices/{your device id}/messages/deviceBound?api-version=2018-06-30' \
-H 'Authorization: {your sas token}'
import random
import sys
import iothub_service_client
from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
OPEN_CONTEXT = 0
FEEDBACK_CONTEXT = 1
MESSAGE_COUNT = 1
AVG_WIND_SPEED = 10.0
MSG_TXT = "{\"service client sent a message\": %.2f}"
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
def open_complete_callback(context):
print ( 'open_complete_callback called with context: {0}'.format(context) )
def send_complete_callback(context, messaging_result):
context = 0
print ( 'send_complete_callback called with context : {0}'.format(context) )
print ( 'messagingResult : {0}'.format(messaging_result) )
def iothub_messaging_sample_run():
try:
iothub_messaging = IoTHubMessaging(CONNECTION_STRING)
iothub_messaging.open(open_complete_callback, OPEN_CONTEXT)
for i in range(0, MESSAGE_COUNT):
print ( 'Sending message: {0}'.format(i) )
msg_txt_formatted = MSG_TXT % (AVG_WIND_SPEED + (random.random() * 4 + 2))
message = IoTHubMessage(bytearray(msg_txt_formatted, 'utf8'))
# optional: assign ids
message.message_id = "message_%d" % i
message.correlation_id = "correlation_%d" % i
# optional: assign properties
prop_map = message.properties()
prop_text = "PropMsg_%d" % i
prop_map.add("Property", prop_text)
iothub_messaging.send_async(DEVICE_ID, message, send_complete_callback, i)
try:
# Try Python 2.xx first
raw_input("Press Enter to continue...\n")
except:
pass
# Use Python 3.xx in the case of exception
input("Press Enter to continue...\n")
iothub_messaging.close()
except IoTHubError as iothub_error:
print ( "Unexpected error {0}" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubMessaging sample stopped" )
if __name__ == '__main__':
print ( "Starting the IoT Hub Service Client Messaging Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_messaging_sample_run()