Use Google Sheets API from Pico LTE

Use the Google Sheets API from the Pico LTE over the cellular connection: create a spreadsheet, add data and rows, delete and read data.

These examples use the Google Sheets API from the Pico LTE over the cellular connection: create a spreadsheet, add data and rows, delete data and read it. They need a Google Cloud project with the Sheets API, OAuth credentials, an API key and a refresh token.

Before you begin

Required

Hardware

  • Sixfab Pico LTE or Pico LTE 2Registered and active in Sixfab Connect, connected to the computer with the micro USB cable.

Software

  • Thonny with MicroPython and the Pico LTE SDKSet up as in the quickstart.
  • config.jsonIn the root of the board: Prepare the configuration file.
  • Google accountFor the Google Cloud Console and Google Sheets.

Set up Google Sheets API

  1. Create project.

    Sign in to the Google Cloud Console and create a project with a name.

  2. Enable Google Sheets API.

    Select the project, open APIs & Services, click ENABLE APIS AND SERVICES, search for Google Sheets API and enable it.

  3. Configure consent screen.

    Click Create Credentials, select Google Sheets API and User data, and continue.

    Fill in the app information, with your Gmail address in both email fields, and click SAVE AND CONTINUE.

  4. Add scopes.

    Click ADD OR REMOVE SCOPES, search for spreadsheets, tick the scope and click UPDATE. Do the same for drive, then click SAVE AND CONTINUE.

  5. Create OAuth client.

    Select Web application, name the client and add http://localhost and https://developers.google.com/oauthplayground to Authorized redirect URIs, then create it.

    Download the client information: the file holds the client ID and the client secret.

  6. Publish app.

    In Enabled APIs & services, click PUBLISH APP, then CONFIRM.

  7. Create API key.

    Under Credentials, create an API key and keep it.

Get refresh token

  1. Open OAuth 2.0 Playground.

    Open developers.google.com/oauthplayground, open the settings and enter your client ID and client secret. The client secret is also in Credentials → OAuth 2.0 Client IDs: click the client name, then see Client secrets under Additional information.

  2. Authorize Sheets scope.

    Select https://spreadsheets.google.com/feeds/ in the list on the left and click Authorize APIs.

    Choose your account, then click Advanced and Go to MyApp (unsafe), then Continue.

  3. Exchange code for tokens.

    Click Exchange authorization code for tokens: a refresh token appears without it too, but the exchange gives a new one. Keep the refresh token.

Prepare spreadsheet

Every example except Create Sheet works on an existing spreadsheet. Create one in Google Sheets, or with Create Sheet below. The spreadsheet ID is in its URL, https://docs.google.com/spreadsheets/d/[SPREADSHEET_ID]; add or rename a sheet with the buttons at the bottom: the examples name the sheet.

Run examples

The scripts are in the examples/google_sheets folder of the SDK repository. In config.json, set the values you saved:

{
    "google_sheets": {
        "api_key": "[API_KEY]",
        "spreadsheetId": "[SPREADSHEET_ID]",
        "client_id": "[CLIENT_ID]",
        "client_secret": "[CLIENT_SECRET]",
        "refresh_token": "[REFRESH_TOKEN]"
    }
}

Open the script in Thonny, set the parameters of its function and click Run.

If sheet is not given, the functions read it from a sheet key in the google_sheets section of config.json.