樹莓派是不少動手達人必備的小玩具,本節內容,讓咱們拿出樹莓派,在30分鐘內,將樹莓派鏈接到微軟雲Azure的IoT Hub,而後將溫溼度曲線可視化。html
更多內容請關注公衆號」雲計算實戰「python
本文完整的操做視頻參見:https://v.qq.com/x/page/f3025q4e75x.html
git
本節內容中,樹莓派發送的數據是模擬出來的,並無真實的鏈接到傳感器,您能夠選購不一樣的傳感器來採集真實的環境信息。
Azure IoT Hub 爲咱們提供了設備與雲雙向通信的能力,經過多種語言的SDK,咱們能輕鬆快速的將樹莓派接入到雲。本案例使用微軟官方代碼,示例代碼一共約70行,很是簡單。
github
關於IoT Hub的更多內容,請參考:app
時序看法(Azure Time Series Insights)用來存儲時間序列的值,同時提供UI,將數據可視化。
async
關於時序看法的更多內容,請參考:ide
Azure Time Series Insights-時序看法(1)
工具
時序看法和IoT Hub能夠無縫鏈接,無需寫代碼便可將上傳到IoT Hub的數據進行可視化。ui
樹莓派上傳數據的代碼:
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 = "HostName=iothubforsatest.azure-devices.cn;DeviceId=test001;SharedAccessKey=kev0eMtTv2UfUU+JD6WAQN2sSdNI9QnRbs4nv2n+1vg=" # Using the MQTT protocol. PROTOCOL = IoTHubTransportProvider.MQTT MESSAGE_TIMEOUT = 10000 # Define the JSON message to send to IoT Hub. TEMPERATURE = 100.0 HUMIDITY = 60 MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f,\"deviceid\": 'test0001'}" 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() * 15) humidity = HUMIDITY + (random.random() * 20) 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 > 30: 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(3) 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()
IoT Hub 接入文檔,請參考:
https://docs.azure.cn/zh-cn/iot-hub/quickstart-send-telemetry-python
樹莓派系統下載:
https://www.raspberrypi.org/downloads/
Micro SD卡格式化工具:
https://www.sdcard.org/downloads/index.html
樹莓派系統寫入Micro SD卡工具:
https://sourceforge.net/projects/win32diskimager/