我尝试将日期和时间输入Azure IoT hub,以便使用Azure DX作为时间序列进行分析。我可以获得温度和湿度(此时的湿度只是一个随机数)。如果我使用此代码,则一切正常,JSON格式良好,并流入IoT hub和Azure DX:
代码的基础取自此处的Microsoft示例-https://github.com/Azure-Samples/azure-iot-samples-python/blob/master/iot-hub/Quickstarts/simulated-device/SimulatedDeviceSync.py
import asyncio
import random
from azure.iot.device import Message
from azure.iot.device.aio import IoTHubDeviceClient
import time
from datetime import datetime
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
import json
CONNECTION_STRING = "xxxxx"
HUMIDITY = 60
MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'
async def main():
try:
# Create instance of the device client
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
print("Simulated device started. Press Ctrl-C to exit")
while True:
humidity = round(HUMIDITY + (random.random() * 20), 2)
temperature = sensor.get_temperature()
msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
message = Message(msg_txt_formatted)
# Send a message to the IoT hub
print(f"Sending message: {message}")
await client.send_message(message)
await asyncio.sleep(1)
except KeyboardInterrupt:
print("Simulated device stopped")
if __name__ == '__main__':
asyncio.run(main())
JSON格式是有效的,并且运行良好-{ "temperature": 7, "humidity": 66.09 }
如果我尝试添加这样的日期/时间字段:
import asyncio
import random
from azure.iot.device import Message
from azure.iot.device.aio import IoTHubDeviceClient
import time
from datetime import datetime
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
import json
CONNECTION_STRING = "xxxxx"
HUMIDITY = 60
x = datetime.now()
timesent = str(x)
MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity},"timesent": {timesent}}}'
async def main():
try:
# Create instance of the device client
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
print("Simulated device started. Press Ctrl-C to exit")
while True:
humidity = round(HUMIDITY + (random.random() * 20), 2)
temperature = sensor.get_temperature()
msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity, timesent=timesent)
message = Message(msg_txt_formatted)
# Send a message to the IoT hub
print(f"Sending message: {message}")
await client.send_message(message)
await asyncio.sleep(1)
except KeyboardInterrupt:
print("Simulated device stopped")
if __name__ == '__main__':
asyncio.run(main())
JSON的输出不再有效,Azure DX将不会Map。我获得的无效JSON为:"{\"temperature\": 7,\"humidity\": 72.88, \"timesent\": 2022-11-08 14:21:04.021812}"
我怀疑这与日期/时间被格式化为字符串有关,但我完全迷路了。
有人知道我怎么发送这些数据吗?
1条答案
按热度按时间bvjxkvbb1#
@JoeHo,感谢您提供帮助您解决此问题的来源。我在此发布解决方案,以便遇到类似问题的其他社区成员可以从中受益。对代码进行以下修改帮助我解决了此问题。
我在Azure数据资源管理器上的表定义了以下字段定义。
我的数据Map查询如下
然后,我使用以下资源中概述的步骤将Azure Data Explorer表连接到IoT Hub
当我执行程序时,我可以看到Azure IoT Hub遥测数据流绑定到Azure数据资源管理器表,没有任何问题。