从CircuitPython访问Microsoft Azure IoT
项目描述
Adafruit_CircuitPython_AzureIoT
这是一个CircuitPython设备库,用于从CircuitPython设备访问Microsoft Azure IoT服务。此库仅支持基于密钥的认证,目前不支持X.509证书。
从PyPI安装
在支持的GNU/Linux系统(如Raspberry Pi)上,您可以从PyPI本地安装驱动程序。为当前用户安装
pip3 install adafruit-circuitpython-azureiot
为系统范围安装(在某些情况下可能需要)
sudo pip3 install adafruit-circuitpython-azureiot
在当前项目的虚拟环境中安装
mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-azureiot
依赖项
此驱动程序依赖于
请确保所有依赖项都在CircuitPython文件系统中可用。这可以通过下载Adafruit库和驱动程序包轻松实现。
板兼容性: 必须提供以下内置模块:gc, json, time
使用示例
此库支持Azure IoT Hub和Azure IoT Central。
要创建 Azure IoT Hub 实例或 Azure IoT Central 应用程序,您需要一个 Azure 订阅。如果您没有 Azure 订阅,可以免费注册。
如果您是 18 岁或以上的学生,请前往 aka.ms/FreeStudentAzure 并注册,使用您的学生电子邮件地址进行验证。这将为您提供 100 美元的 Azure 信用额度以及大量服务的免费层,每年您都是学生即可续期。您不需要信用卡。
如果您不是学生,请前往 aka.ms/FreeAz 并注册,以获得 30 天内 200 美元的信用额度,以及大量服务的免费层。您只需要信用卡进行验证,您的卡不会被扣费。
ESP32 AirLift 网络通信
要使用此库,您需要创建一个连接到 WiFi 的 ESP32_SPI WifiManager。您还需要设置当前时间,因为这将用于生成基于时间的认证密钥。一种方法是使用以下代码
# get_time will raise ValueError if the time isn't available yet so loop until
# it works.
now_utc = None
while now_utc is None:
try:
now_utc = time.localtime(esp.get_time()[0])
except ValueError:
pass
rtc.RTC().datetime = now_utc
原生网络通信
要使用此库,对于具有原生网络支持的板子,您需要连接到网络。您还需要设置当前时间,因为这将用于生成基于时间的认证密钥。一种方法是使用以下代码的 Adafruit NTP 库
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
# NOTE: This changes the system time so make sure you aren't assuming that time
# doesn't jump.
rtc.RTC().datetime = ntp.datetime
Azure IoT Hub
要与 Azure IoT Hub 交互,您需要创建一个中心,并在其中注册一个设备。有一个免费层可用,这个免费层每天允许最多 8,000 条消息,所以如果您使用这个层,请不要频繁发送消息。
打开 Azure Portal。
按照 Microsoft Docs 中的说明创建 Azure IoT Hub 并注册设备。
复制设备的 Primary 或 secondary 连接字符串,并将其添加到您的 secrets.py 文件中。
您可以在 Azure Portal 中选择 IoT Hub,选择 Explorer -> IoT 设备,然后选择您的设备来找到设备连接字符串。
在 IoT 中心标签页中定位设备
然后使用旁边的复制按钮复制 primary 或 secondary 连接字符串。
复制 primary 连接字符串
将您的设备连接到 Azure IoT Hub
from adafruit_azureiot import IoTHubDevice
device = IoTHubDevice(wifi, secrets["device_connection_string"])
device.connect()
一旦设备连接,您将需要定期运行一个 loop 来轮询来自云的消息。
while True:
device.loop()
time.sleep(1)
向云发送设备消息
message = {"Temperature": temp}
device.send_device_to_cloud_message(json.dumps(message))
接收来自云的设备消息
def cloud_to_device_message_received(body: str, properties: dict):
print("Received message with body", body, "and properties", json.dumps(properties))
# Subscribe to cloud to device messages
device.on_cloud_to_device_message_received = cloud_to_device_message_received
接收直接方法
def direct_method_invoked(method_name: str, payload) -> IoTResponse:
print("Received direct method", method_name, "with data", str(payload))
# return a status code and message to indicate if the direct method was handled correctly
return IoTResponse(200, "OK")
# Subscribe to direct methods
device.on_direct_method_invoked = direct_method_invoked
在设备孪生上更新报告属性
此功能在基本层 IoT 中心上不受支持,仅在免费和标准层上支持。
patch = {"Temperature": temp}
device.update_twin(patch)
订阅设备孪生上期望的属性更改
此功能在基本层 IoT 中心上不受支持,仅在免费和标准层上支持。
def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int):
print("Property", desired_property_name, "updated to", str(desired_property_value), "version", desired_version)
# Subscribe to desired property changes
device.on_device_twin_desired_updated = device_twin_desired_updated
Azure IoT Central
要使用 Azure IoT Central,您需要创建一个 Azure IoT Central 应用程序,创建一个设备模板,并将设备注册到该模板。
按照 Microsoft Docs 中的说明创建应用程序。每个层最多支持 2 个设备的免费使用。
按照 Microsoft Docs 中的说明创建设备模板。
根据模板创建一个设备,并选择 连接 以获取设备连接详情。将 ID Scope、设备 ID 以及 primary 或 secondary 设备 SAS 密钥存储在您的 secrets.py 文件中。
连接按钮
连接详情对话框
secrets = {
# WiFi settings
"ssid": "",
"password": "",
# Azure IoT Central settings
"id_scope": "",
"device_id": "",
"device_sas_key": ""
}
将您的设备连接到 Azure IoT Central 应用程序
from adafruit_azureiot import IoTCentralDevice
device = IoTCentralDevice(wifi, secrets["id_scope"], secrets["device_id"], secrets["device_sas_key"])
device.connect()
一旦设备连接,您将需要定期运行一个 loop 来轮询来自云的消息。
while True:
device.loop()
time.sleep(1)
发送遥测数据
message = {"Temperature": temp}
device.send_telemetry(json.dumps(message))
监听命令
def command_executed(command_name: str, payload) -> IoTResponse:
print("Command", command_name, "executed with payload", str(payload))
# return a status code and message to indicate if the command was handled correctly
return IoTResponse(200, "OK")
# Subscribe to commands
device.on_command_executed = command_executed
更新属性
device.send_property("Desired_Temperature", temp)
监听属性更新
def property_changed(property_name, property_value, version):
print("Property", property_name, "updated to", str(property_value), "version", str(version))
# Subscribe to property updates
device.on_property_changed = property_changed
了解更多关于Azure IoT服务的信息
如果您想了解更多关于设置或使用Azure IoT服务的信息,请查看以下资源
Microsoft Learn上的IoT学习路径和模块 - 免费在线,自我指导的Azure IoT服务实践学习
文档
该库的API文档可以在Read the Docs上找到。
有关构建库文档的信息,请参阅本指南。
贡献
欢迎贡献!在为该项目做出贡献之前,请阅读我们的行为准则,以帮助保持项目的友好。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
构建分发
adafruit_circuitpython_azureiot-2.6.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 83e4c5ede86a2e9db6230c6d76104ae493a82dd759bdb0f43d687527cd299227 |
|
MD5 | 18bc01451842808e7f4fd5ea7841aa15 |
|
BLAKE2b-256 | 9e4619a2b50838f79fac5fc85b95dbe2be7dc76c9bd1f06366e2227131eaeba0 |
adafruit_circuitpython_azureiot-2.6.0-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | a8e7b4d7e81100c1c4814aa47ee789c2861426925c57124c18a3bd4188daa0d8 |
|
MD5 | 5d54cbd37b74d3cde7e2fe7d70d613c0 |
|
BLAKE2b-256 | 4fd84b1ee4f1c338b78ed2789314faaa38eec1625dc3e01d8ccf205428f5bd73 |