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, secrets, log=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.
client_id

Returns a Google Cloud IOT Core Client ID.

generate_jwt(ttl=43200, algo='RS256')

Generates a JSON Web Token (https://jwt.io/) using network time. :param int jwt_ttl: When the JWT token expires, defaults to 43200 minutes (or 12 hours). :param str algo: Algorithm used to create a JSON Web Token.

Example usage of generating and setting a JSON-Web-Token: ..code-block:: python

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

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

Parameters:mqtt_client (MiniMQTT) – MiniMQTT Client object.
connect()

Connects to the Google MQTT Broker.

disconnect()

Disconnects from the Google MQTT Broker.

is_connected

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

loop()

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. ..code-block:: python

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

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)

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()

Reconnects to the Google MQTT Broker.

subscribe(topic, subfolder=None, qos=1)

Subscribes to a Google Cloud IoT device topic. :param str topic: Required MQTT topic. Defaults to events. :param str subfolder: Optional MQTT topic subfolder. Defaults to None. :param int qos: Quality of Service level for the message.

subscribe_to_all_commands(qos=1)

Subscribes to a device’s “commands/#” topic. :param int qos: Quality of Service level for the message.

subscribe_to_config(qos=1)

Subscribes to a Google Cloud IoT device’s configuration topic. :param int qos: Quality of Service level for the message.

subscribe_to_subfolder(topic, subfolder, qos=1)

Subscribes to a Google Cloud IoT device’s topic subfolder :param str topic: Required MQTT topic. :param str subfolder: Optional MQTT topic subfolder. Defaults to None. :param int qos: Quality of Service level for the message.

unsubscribe(topic, subfolder=None)

Unsubscribes from a Google Cloud IoT device topic. :param str topic: Required MQTT topic. Defaults to events. :param str subfolder: Optional MQTT topic subfolder. Defaults to None.

unsubscribe_from_all_commands()

Unsubscribes from a device’s “commands/#” topic. :param int qos: Quality of Service level for the message.

exception adafruit_gc_iot_core.MQTT_API_ERROR

Exception raised on MQTT API return-code errors.