溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

30分鐘內(nèi)將樹莓派連接到微軟云Azure IoT Hub并將

發(fā)布時(shí)間:2020-02-24 16:33:54 來源:網(wǎng)絡(luò) 閱讀:451 作者:13188888544 欄目:云計(jì)算

樹莓派是很多動(dòng)手達(dá)人必備的小玩具,本節(jié)內(nèi)容,讓我們拿出樹莓派,在30分鐘內(nèi),將樹莓派連接到微軟云Azure的IoT Hub,然后將溫濕度曲線可視化。


更多內(nèi)容請(qǐng)關(guān)注公眾號(hào)”云計(jì)算實(shí)戰(zhàn)“


本文完整的操作視頻參見:https://v.qq.com/x/page/f3025q4e75x.html

30分鐘內(nèi)將樹莓派連接到微軟云Azure IoT Hub并將


本節(jié)內(nèi)容中,樹莓派發(fā)送的數(shù)據(jù)是模擬出來的,并沒有真實(shí)的連接到傳感器,您可以選購不同的傳感器來采集真實(shí)的環(huán)境信息。

Azure IoT Hub 為我們提供了設(shè)備與云雙向通訊的能力,通過多種語言的SDK,我們能輕松快速的將樹莓派接入到云。本案例使用微軟官方代碼,示例代碼一共約70行,非常簡單。

30分鐘內(nèi)將樹莓派連接到微軟云Azure IoT Hub并將


關(guān)于IoT Hub的更多內(nèi)容,請(qǐng)參考:


Azure 上的物聯(lián)網(wǎng)服務(wù)介紹


時(shí)序見解(Azure Time Series Insights)用來存儲(chǔ)時(shí)間序列的值,同時(shí)提供UI,將數(shù)據(jù)可視化。

30分鐘內(nèi)將樹莓派連接到微軟云Azure IoT Hub并將


關(guān)于時(shí)序見解的更多內(nèi)容,請(qǐng)參考:


Azure Time Series Insights-時(shí)序見解(1)


時(shí)序見解和IoT Hub可以無縫連接,無需寫代碼即可將上傳到IoT Hub的數(shù)據(jù)進(jìn)行可視化。



樹莓派上傳數(shù)據(jù)的代碼:

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 接入文檔,請(qǐng)參考:


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


樹莓派系統(tǒng)下載:


https://www.raspberrypi.org/downloads/


Micro SD卡格式化工具:


https://www.sdcard.org/downloads/index.html


樹莓派系統(tǒng)寫入Micro SD卡工具:


https://sourceforge.net/projects/win32diskimager/


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI