本案例适用于开发者入门理解Azure Functions/ IoT Hub / Service Bus / Power BI等几款产品。

主要实战的内容为:

  1. 将设备遥测数据上传到物联网中心,

  2. 将遥测数据路由到消息中间件的Topic中,

  3. 使用Azure Function解析消息中间件Topic中的消息并推送到大屏 。

本文主要是本案例的准备工作,即(第1条和第2条的内容):

1.创建IoT Hub:

https://v.qq.com/x/page/h3031pnaxi8.html

2.创建Service Bus:

https://v.qq.com/x/page/b3031hdv9yk.html

3. 创建IoT Hub 消息路由,将遥测消息路由到Service Bus Topic

https://v.qq.com/x/page/i3031hkec4q.html

本示例中的Python Device 代码来自于微软官网,请参照:

https://docs.azure.cn/zh-cn/iot-hub/quickstart-send-telemetry-python

import random
import time
import sys # Using the Python Device SDK for IoT Hub:
# https://github.com/Azure/azure-iot-sdk-python
# The sample connects to a device-specific MQTT endpoint on your IoT Hub.
import iothub_client
# pylint: disable=E0611
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue # The device connection string to authenticate the device with your IoT hub.
# Using the Azure CLI:
# az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
CONNECTION_STRING = "your device conn string" # Using the MQTT protocol.
PROTOCOL = IoTHubTransportProvider.MQTT
MESSAGE_TIMEOUT = # Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY =
MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}" def send_confirmation_callback(message, result, user_context):
print ( "IoT Hub responded to message with status: %s" % (result) ) def iothub_client_init():
# Create an IoT Hub client
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
return client def iothub_client_telemetry_sample_run(): try:
client = iothub_client_init()
print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" ) while True:
# Build the message with simulated telemetry values.
temperature = TEMPERATURE + (random.random() * )
humidity = HUMIDITY + (random.random() * )
msg_txt_formatted = MSG_TXT % (temperature, humidity)
message = IoTHubMessage(msg_txt_formatted) # Add a custom application property to the message.
# An IoT hub can filter on these properties without access to the message body.
prop_map = message.properties()
if temperature > :
prop_map.add("temperatureAlert", "true")
else:
prop_map.add("temperatureAlert", "false") # Send the message.
print( "Sending message: %s" % message.get_string() )
client.send_event_async(message, send_confirmation_callback, None)
time.sleep() except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" ) if __name__ == '__main__':
print ( "IoT Hub Quickstart #1 - Simulated device" )
print ( "Press Ctrl-C to exit" )
iothub_client_telemetry_sample_run()

最新文章

  1. Java学习笔记1
  2. ORB-SLAM(二)性能
  3. 代理模式及jdk动态代理原理
  4. [Android] android .keystore文件转x509pem工具
  5. [DB那些事]数据库加密
  6. 25条提高iOS app性能的方法和技巧
  7. php中cookie+mysql实现的购物车代码
  8. 40个超酷的jQuery动画效果教程
  9. JobScheduler
  10. ECSHOP 数据库结构说明 (适用版本v2.7.3)
  11. python+django+wusgi+nginx安装部署
  12. HDU 5224 Tom and paper(最小周长)
  13. Api 和 Spi
  14. MySQL千万级多表关联SQL语句调优
  15. git和github新手安装使用教程(三步入门)
  16. JdbcTemplate的使用
  17. Bootstrap3 表单-基本表单
  18. SQL Server的JOIN是支持使用小括号修改执行顺序的
  19. WPF仿网易云音乐系列(序)
  20. CSS中的偏僻知识点

热门文章

  1. 2019-11-3:渗透测试,基础学习,bypass类型笔记
  2. pyspark报错Exception: Java gateway process exited before sending its port number解决方法
  3. at、crontab、anacron的基本使用
  4. SpringMVC 请求参数绑定
  5. C语言I博客作业11
  6. 【决战西二旗】|理解Sort算法
  7. Java package 包的命名规范。
  8. 使用IDEA2017.3.5搭建SSM框架
  9. PTA 1140 1141 1142 1143
  10. CoderForces999B- Reversing Encryption