简体   繁体   中英

How to publish message to Google Cloud MQTT topic?

Using Google Cloud Functions with Python, I want to publish a message to MQTT topic.

I use pubsub_v1 PublisherClient, and after invoking cloud function and looking into logs it looks like an "google.api_core.exceptions.InvalidArgument: 400 Invalid resource name given". I understand that happens because my topic_name contains "/", because I want to use more levels/subtopics, something like this: "devices/raspberrypi/events/msg/device/3/status". I have "msg" topic created, but can't create or publish to any subtopics.

I didn't find any information about subtopics in official documentation or anywhere else. Thanks in advance.

The Cloud Pub/Sub client library is only for interacting with Google Cloud Pub/Sub directly. If you are using MQTT topics, it sounds like you are trying to use the Cloud IoT Core MQTT bridge. In that case, you need to use an MQTT client that is configured to authenticate the device with Cloud IoT Core and use it to publish the telemetry events. See the Cloud IoT guide to publishing .

The subtopics for events should have their own topics associated with them, but they are not going to be accessible using the subtopic name via the Cloud Pub/Sub library. To use the Cloud Pub/Sub library, you will have to use the name of the Cloud Pub/Sub topic. The topic would be the one you associated with the MQTT subtopic in the Cloud Console in the "Additional topic" section:

子文件夹主题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM