Cloud IoT Core is a fully managed service that allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices. Cloud IoT Core, in combination with other services on Google Cloud platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real time to support improved operational efficiency.

The AVR-IoT WG development board combines a powerful 8-bit ATmega4808 MCU, an ATECC608A CryptoAuthentication™ secure element IC and the fully certified ATWINC1510 Wi-Fi® network controller - providing a simple and effective way to connect your embedded application to Google's Cloud IoT core platform.

What you'll build

In this codelab, you will use the connect the AVR-IoT board from Microchip to Cloud IoT Core and set up a data pipeline for device analytics.

What you'll learn

What you'll need

In this section, we will set up the AVR-IoT board on WiFi (as described at avr-iot.com) and verify it is connected and transmitting data. You will also set up the MPLAB® X IDE in preparation for downloading new firmware to the board.

Launch AVR-IoT setup

  1. Connect the AVR-IoT board to the USB port of your development machine
  2. Open the CURIOSITY drive
  3. Open CLICK-ME.HTM in your browser

This will take you to the custom device page at https://avr-iot.com/device/<serial>

Enter WiFi credentials

Once the device page loads in your browser, follow these steps to get the board connected to WiFi:

  1. Enter your WiFi network SSID and credentials
  2. Click Download Configuration
  3. Copy the WIFI.CFG file to the CURIOSITY drive

Verify that the blue and green LEDs on the board are solid lit. The AVR-IoT browser page will automatically change to show real-time output from the light and temperature sensors on the board.

Set up MPLAB® IDE

Next, you need to install and configure MPLAB® X, the AVR compiler tools, and the Microchip Code Configurator (MCC) plugin:

  1. Download MPLAB X IDE v5.10 and run the installer
  2. Download Microchip AVR Toolchain and extract it to a known location
  3. Open MPLAB® X
  4. Go to ToolsOptionsEmbedded Build ToolsAdd
  5. Browse to ~\<toolchain-directory>\bin for Base Directory location
  1. Click Ok, then click Apply
  2. Go to ToolsPlugins Available Plugins
  3. Select MPLAB® Code Configurator
  4. Click Install
  5. Once the install is complete, restart the IDE.

In this section, you will enable the necessary APIs in the Google Cloud Platform (GCP) console and create the necessary entries for your device to communicate with the IoT Core service. By the end of this section, you will be able to verify that your device is transmitting data to Google Cloud.

  1. Sign in to the Google Cloud Console
  1. In the GCP Console, go to the Manage resources page and select or create a project.
  2. Make sure that billing is enabled for your project. Learn how to enable billing.

Create a device registry

  1. Open the Cloud IoT Core page in the GCP console.
  1. Click Create a device registry
  2. Enter AVR-IOT for the Registry ID
  3. Select the region closest to you
  4. Select MQTT for the Protocol
  5. In the Default telemetry topic dropdown list, select Create a topic
  6. Enter sensor-events as the topic name, and click Create
  7. Scroll to the bottom of the page, and click Create

You've successfully created a new device registry in Cloud IoT Core!

Create the AVR-IoT device

  1. Select Devices from the navigation pane on the IoT Core registry details page
  2. Click CREATE A DEVICE
  3. Enter the AVR-IoT serial number, prefixed with a lowercase d as the Device ID

Example Device ID: d0123CD7E9AD1DCABFE

  1. Set the AuthenticationInput method to Upload
  2. Set the AuthenticationPublic key format to ES256
  3. Browse to CURIOSITY/PUBKEY.TXT to upload the device's Public key value
  4. Scroll to the bottom of the page, and click Create

You've successfully created a new IoT device entry in Cloud IoT Core!

Update AVR-IoT firmware for your GCP project

Let's begin by creating a new project in MPLAB® for the AVR-IoT board:

  1. Open the MPLAB X IDE
  2. Select FileNew Project...
  3. Choose Microchip EmbeddedStandalone Project, and click Next
  4. Select Device: Enter ATmega4808 as the Device, and click Next
  5. Select Tool: Choose Microchip KitsAVR-IoT WG (nEDBG), and click Next
  1. Select Compiler: Choose AVR (v5.4.0), and click Next
  2. Enter avr-iot-workshop as the Project Name, and click Finish to create the project

Next, we need to generate an example project to connect this board with Cloud IoT Core:

  1. Click MCC button in the toolbar to open the MPLAB® Code Configurator
  2. Save MyConfig.mc3 file
  3. Expand Device ResourcesInternet of ThingsExamples, and double-click AVR-IoT WG Sensor Node to add it to the project
  4. Enter the GCP project, region, and registry values on the Cloud Services Google tab
  5. Enter the SSID and authentication information for your WiFi network on the WINC tab
  6. Click Generate in the top left panel to create the project source files

Add timestamp to payload

The default firmware for the AVR-IoT board uploads the light sensor and temperature sensor values as JSON. Let's add the current timestamp to each message published to Google Cloud, which will make it easier to do analytics on the data.

Select the Files tab from the top left panel, and open the main.c source file. Below are the current contents of the sendToCloud() function:

main.c (Initial)

void sendToCloud(void)
{
   static char json[70];
         
   // This part runs every CFG_SEND_INTERVAL seconds
   int rawTemperature = SENSORS_getTempValue();
   int light = SENSORS_getLightValue();
   int len = sprintf(json, "{\"Light\":%d,\"Temp\":\"%d.%02d\"}",
           light, rawTemperature/100, abs(rawTemperature)%100);

   if (len >0) {
      CLOUD_publishData((uint8_t*)json, len);
   }
   
   LED_flashYellow();
}

This code reads the sensor values and generates the JSON payload to send. Modify the contents of this function to include the current time, as below:

main.c (Updated)

void sendToCloud(void)
{
   static char json[70];
         
   // This part runs every CFG_SEND_INTERVAL seconds
   int rawTemperature = SENSORS_getTempValue();
   int light = SENSORS_getLightValue();

   // Add the timestamp to the JSON
   time_t now = time(NULL) + UNIX_OFFSET;
   int len = sprintf(json, "{\"timestamp\":%lu,\"Light\":%d,\"Temp\":\"%d.%02d\"}",
           now, light, rawTemperature/100, abs(rawTemperature)%100);

   if (len >0) {
      CLOUD_publishData((uint8_t*)json, len);
   }
   
   LED_flashYellow();
}

This code requires that you include the ctime library at the top of the file:

main.c (Updated)

#import <time.h>

Save your changes, and click Clean and Build Main Project from the toolbar. Once the build is complete, click Make and Program Device Main Project to program the device with the new firmware.

Return to the Cloud IoT Core page in the GCP console, to the device details page for your AVR-IoT device. Verify that your device has the current date shown under Latest activity, next to Telemetry event received.

(Optional) Inspect telemetry data

You can view the sensor event message contents by attaching a subscription to the Pub/Sub telemetry topic, and using the gcloud CLI tool to pull individual messages from the queue.

  1. Open the Cloud Pub/Sub page in the GCP console
  1. Select your sensor-events topic
  2. Click CREATE SUBSCRIPTION
  3. Name subscription sensor-events
  4. Set the Delivery Type to Pull, and click Create
  5. Click the Activate Cloud Shell icon in the toolbar, and then Start Cloud Shell

Enter the following command to pull the last message to inspect the event data:

$ gcloud pubsub subscriptions pull sensor-events --auto-ack

In this section we will connect the Pub/Sub data coming from the IoT devices with BigQuery, Google's cloud data warehouse, so we can perform additional analytics. By the end of this section, you will be able to graph the data coming off of your devices inside of Google Data Studio.

Set up BigQuery tables

  1. Open the BigQuery page in the GCP console
  2. Under Resources, Select your project ID
  3. Click CREATE DATASET
  4. Name dataset avriot and click Create Dataset
  5. Select your newly created dataset from Resources, and click CREATE TABLE
  6. Enter sensordata as the Table name
  7. Click Add field to create the following fields:

Name

Type

Mode

timestamp

TIMESTAMP

NULLABLE

Light

FLOAT

NULLABLE

Temp

FLOAT

NULLABLE

  1. Click Create Table

Create a temp bucket in Cloud Storage

  1. Open the Cloud Storage page in the GCP console
  2. Click Create bucket
  3. Set the bucket name to your GCP project ID
  4. Scroll to the bottom of the page and click Create

Export data with Cloud Dataflow

  1. Open the Cloud Dataflow page in the GCP console
  2. Click CREATE JOB FROM TEMPLATE
  3. Name the job avr-iot and select Cloud Pub/Sub to BigQuery from the Cloud Dataflow template dropdown.
  4. Enter projects/<project-id>/topics/sensor-events as the Cloud Pub/Sub input topic
  5. Enter <project-id>:avriot.sensordata as the BigQuery output table
  6. Enter gs://<project-id>/tmp/ as the Temporary Location
  7. Scroll to the bottom of the page and click Run job

You have now successfully created a pipeline to stream data from your IoT device into BigQuery! Open the BigQuery page in the GCP console, and enter the following into the query editor:

SELECT timestamp, Light, Temp from avriot.sensordata
ORDER BY timestamp DESC
Limit 100

Click Run to execute the query and view the results from your dataset in the table below the editor.

Display a Graph

  1. Open Google Data Studio in a new browser tab
  2. Select Explorer, and click Add
  1. Click Create New Data Source in the bottom right pane.
  2. Select BigQuery and authorize access to your account
  3. Select your GCP project ID → avriotsensordata, and click Connect
  4. Adjust the Type of timestamp to Text, then click Apply
  5. Select Line Chart form the top right panel
  6. Adjust the chart parameters in the bottom right panel:

Dimension

Metric

Sort

timestamp

Light or Temp

timestamp

Ascending

The GCP console integrates with Stackdriver Logging, a powerful framework for logging events and deriving insights from your data. In addition to the logs generated by default for various GCP services, you can also access these logs using the Stackdriver Logging Client Library.

In this section, we will use a cloud function to inspect each event coming from the IoT device, and generate a warning when the light sensor goes "out of range" (which we'll define as the sensor detecting no light and falling below a certain threshold).

Create a new function

Generate a new cloud function, triggered by any message sent to the Pub/Sub topic for our device telemetry events:

  1. Select Cloud Functions page in the GCP console
  1. Click Create Function
  2. Set name to log-sensor-errors
  3. Set Trigger to Cloud Pub/Sub
  4. Set Topic to sensor-events
  5. Set Runtime to Node.js 8

Add the source code

Using the inline editor, select the index.js tab and replace the default function with the following code:

index.js

const {Logging} = require('@google-cloud/logging');

// create the Stackdriver Logging client
const logging = new Logging({
  projectId: process.env.GCLOUD_PROJECT,
});

exports.logSensorError = async (event, context) => {
    // Check if the light sensor is out of range
    const sensorData = JSON.parse(Buffer.from(event.data, 'base64').toString());
    if (sensorData.Light < 15.0) {
        // Construct an application-specific payload to log
        const payload = {
            message: 'Light sensor is out of range',
            value: sensorData.Light
        };

        // Set the StackDriver log destination
        const log = logging.log('device-logs');
        // Set the StackDriver metadata for a Cloud IoT device
        const metadata = {
            severity: 'WARNING',
            resource: {
              type: 'cloudiot_device',
              labels: {
                project_id: event.attributes.projectId,
                device_num_id: event.attributes.deviceNumId,
                device_registry_id: event.attributes.deviceRegistryId,
                location: event.attributes.location,
              }
            },
            labels: {
              device_id: event.attributes.deviceId,
            }
          };
        
        // Write to StackDriver
        const entry = log.entry(metadata, payload);
        await log.write(entry);
    }
};

This code evaluates the light sensor value for each event, and creates a new Stackdriver log entry when the value is below 15. The entry metadata allows us to filter for just the IoT device logs when looking at Stackdriver in the GCP console.

Click the package.json tab in the inline editor, and replace the default contents with the following code:

package.json

{
  "name": "log-sensor-errors",
  "version": "0.0.1",
  "dependencies": {
    "@google-cloud/pubsub": "^0.18.0",
    "@google-cloud/logging": "^4.2.0"
  }
}

This file declares the Node.js dependencies required by the cloud function.

Deploy and test

Scroll to the bottom of the page, and set logSensorError as the Function to execute.

Finally, click Create to deploy the function. The console will show you a progress indicator for a few moments while the function is deployed. You will see a green checkmark next to the function name once deployment is complete.

Use the following steps to verify the log output:

  1. Open Stackdriver Logs in the GCP console
  2. Click the resource dropdown, and select Cloud IoT DeviceAll device_num_id
  3. Cover the light sensor on the AVR-IoT board for 3-5 seconds
  4. Click Load newer logs or refresh the page

Verify that you can see the low light events generated as warning messages in the Stackdriver log output.

Congratulations! You are now streaming data from the AVR-IoT board into Google Cloud and analyzing that data with BigQuery and Data Studio. Here are some things you can do to go deeper:

What we've covered