【问题标题】:Send a command to multiple devices in Google Cloud IoT Core with MQTT使用 MQTT 向 Google Cloud IoT Core 中的多个设备发送命令
【发布时间】:2019-03-20 13:51:41
【问题描述】:

如何使用 MQTT 和 Google Cloud IoT Core 向注册表中的所有设备发送命令?

到目前为止我看到的所有例子都只是send the command to a single device。我是否必须在我的设备上循环并向每个设备发送消息?

提前致谢。

【问题讨论】:

  • 您是否尝试过在 device-id 字段中使用 MQTT 通配符(我认为是哈希“#”)?

标签: python iot google-cloud-iot


【解决方案1】:

您需要list devices in a registry,然后按照您的建议在循环中调用sendCommandToDevice 方法。

作为参考,它看起来像这样(在 Python 中):

command = '{ "state": "off" }'
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
    project_id, cloud_region, registry_id)

client = get_client(service_account_json)
devices = client.projects().locations().registries().devices(
    ).list(parent=registry_path).execute().get('devices', [])

for device in devices:
  device_path = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(
      project_id, cloud_region, registry_id, device.get('id'))

  config_body = {
    'binaryData': base64.urlsafe_b64encode(
      command.encode('utf-8')).decode('ascii')
  }

  client.projects().locations().registries().devices().sendCommandToDevice(
      name=device_path, body=config_body).execute()

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2018-07-17
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多