adafruit_gc_iot_core

CircuitPython Google Cloud IoT Module

  • Author(s): Brent Rubell, Google Inc.

Implementation Notes

Software and Dependencies:

class adafruit_gc_iot_core.Cloud_Core(esp: ESP_SPIcontrol | None = None, secrets: Dict[str, Any] | None = None, log: bool = False)

CircuitPython Google Cloud IoT Core module.

Parameters:
  • esp (ESP_SPIcontrol) – ESP32SPI object.

  • secrets (dict) – Secrets.py file.

  • log (bool) – Enable Cloud_Core logging, defaults to False.

property client_id: str

Returns a Google Cloud IOT Core Client ID.

generate_jwt(ttl: int = 43200, algo: str = 'RS256') str

Generates a JSON Web Token (https://jwt.io/) using network time.

Parameters:
  • jwt_ttl (int) – When the JWT token expires, defaults to 43200 minutes (or 12 hours).

  • algo (str) – Algorithm used to create a JSON Web Token.

Example usage of generating and setting a JSON-Web-Token:

jwt = CloudCore.generate_jwt()
print("Generated JWT: ", jwt)
class adafruit_gc_iot_core.MQTT_API(mqtt_client: MQTT)

Client for interacting with Google’s Cloud Core MQTT API.

Parameters:

mqtt_client (MQTT) – MiniMQTT Client object

connect() None

Connects to the Google MQTT Broker.

disconnect() None

Disconnects from the Google MQTT Broker.

property is_connected: bool

Returns if client is connected to Google’s MQTT broker.

loop() None

Maintains a connection with Google Cloud IoT Core’s MQTT broker. You will need to manually call this method within a loop to retain connection.

Example of “pumping” a Google Core IoT loop.

while True:
    google_iot.loop()
publish(payload: int | str, topic: str = 'events', subfolder: str | None = None, qos: int = 0) None

Publishes a payload from the device to its Google Cloud IoT device topic, defaults to “events” topic. To send state, use the publish_state method.

Parameters:
  • payload (float) – Data to publish to Google Cloud IoT

  • payload – Data to publish to Google Cloud IoT

  • payload – Data to publish to Google Cloud IoT

  • topic (str) – Required MQTT topic. Defaults to events.

  • subfolder (str) – Optional MQTT topic subfolder. Defaults to None.

  • qos (int) – Quality of Service level for the message.

publish_state(payload: int | str) None

Publishes a device state message to the Cloud IoT MQTT API. Data sent by this method should be information about the device itself (such as number of crashes, battery level, or device health). This method is unidirectional, it communicates Device-to-Cloud only.

reconnect() None

Reconnects to the Google MQTT Broker.

subscribe(topic: str, subfolder: str | None = None, qos: int = 1) None

Subscribes to a Google Cloud IoT device topic.

Parameters:
  • topic (str) – Required MQTT topic. Defaults to events.

  • subfolder (str) – Optional MQTT topic subfolder. Defaults to None.

  • qos (int) – Quality of Service level for the message.

subscribe_to_all_commands(qos: int = 1) None

Subscribes to a device’s “commands/#” topic.

Parameters:

qos (int) – Quality of Service level for the message.

subscribe_to_config(qos: int = 1) None

Subscribes to a Google Cloud IoT device’s configuration topic.

Parameters:

qos (int) – Quality of Service level for the message.

subscribe_to_subfolder(topic: str, subfolder: str | None = None, qos: int = 1) None

Subscribes to a Google Cloud IoT device’s topic subfolder

Parameters:
  • topic (str) – Required MQTT topic.

  • subfolder (str) – Optional MQTT topic subfolder. Defaults to None.

  • qos (int) – Quality of Service level for the message.

unsubscribe(topic: str, subfolder: str | None = None) None

Unsubscribes from a Google Cloud IoT device topic.

Parameters:
  • topic (str) – Required MQTT topic. Defaults to events.

  • subfolder (str) – Optional MQTT topic subfolder. Defaults to None.

unsubscribe_from_all_commands() None

Unsubscribes from a device’s “commands/#” topic.

Parameters:

qos (int) – Quality of Service level for the message.

exception adafruit_gc_iot_core.MQTT_API_ERROR

Exception raised on MQTT API return-code errors.