Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Connect the raspberry pie to Microsoft Cloud Azure IoT Hub in 30 minutes and set the

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Raspberry Pi is a must-have toy for many hands-on people. In this section, let's take out Raspberry Pi, connect Raspberry Pi to Microsoft Azure IoT Hub in 30 minutes, and then visualize the temperature and humidity curve.

For more content, please pay attention to the public number "Cloud Computing Actual Combat"

For the full video of this article, see v.qq.com/x/page/f3025q4e75x.html

In this section, the data sent by Raspberry Pi is simulated, and there is no real connection to the sensor. You can choose different sensors to collect real environmental information.

Azure IoT Hub gives us the ability to communicate two-way between devices and the cloud, and through the SDK in multiple languages, we can easily and quickly connect the Raspberry Pi to the cloud. This case uses Microsoft official code, the sample code is about 70 lines, very simple.

For more on IoT Hub, please refer to:

Introduction to IoT Services on Azure

Azure Time Series Insights is used to store time series values and provide UI to visualize data.

For more on timing insights, see:

Azure Time Series Insights (1)

Temporal insights and IoT hubs connect seamlessly, visualizing data uploaded to IoT hubs without writing code.

Raspberry Pi upload code:

import randomimport timeimport 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=E0611from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResultfrom 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 tableCONNECTION_STRING = "HostName=iothubforsatest.azure-devices.cn;DeviceId=test001;SharedAccessKey=kev0eMtTv2UfUU+JD6WAQN2sSdNI9QnRbs4nv2n+1vg="# Using the MQTT protocol.PROTOCOL = IoTHubTransportProvider.MQTTMESSAGE_TIMEOUT = 10000# Define the JSON message to send to IoT Hub.TEMPERATURE = 100.0HUMIDITY = 60MSG_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 clientdef 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 access documentation, please refer to:

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

Raspberry Pi system download:

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

Micro SD card formatting tools:

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

Raspberry Pi System Write Micro SD Card Tool:

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report