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.

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

Connect the AVR-IoT board to the USB port of your development machine

Open the CURIOSITY drive

Open CLICK-ME.HTM in your browser

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

Note: Make a note of your device serial number, you will need it later!

Enter WiFi credentials

Important: To ensure a successful connection, make sure you are connecting to a 2.4GHz network

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

Enter your WiFi network SSID and credentials

Click Download Configuration

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.

Having Trouble?

You can also connect to the board using a serial console:

Open serial terminal at 9600,8,N,1

Enter help to see the list of available commands

Use one of the following commands, based on your network type, to send the WiFi credentials:

Open WiFi networks: wifi <SSID>,,1

WPA secured networks: wifi <SSID>,<password>,2

You can also use the device command to print out the device serial number

Set up MPLAB® IDE

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

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.

Enter the SSID and authentication information for your WiFi network on the WINC tab

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:

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.

Note: If you see a message saying the API is disabled, click Enable API

Select your sensor-events topic

Click CREATE SUBSCRIPTION

Name subscription sensor-events

Set the Delivery Type to Pull, and click Create

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.

Display a Graph

Note: If you see a setup wizard pop up, select Get Started and accept the terms of service for Data Studio before continuing.

Click Create New Data Source in the bottom right pane.

Select BigQuery and authorize access to your account

Select your GCP project ID → avriot → sensordata, and click Connect

Adjust the Type of timestamp to Text, then click Apply

Select Line Chart form the top right panel

Adjust the chart parameters in the bottom right panel:

Dimension

Metric

Sort

timestamp

Light or Temp

timestamp

Ascending

Why the adjustment?

Google Data Studio is a tool designed to process large amounts of data for analytics purposes by aggregating individual data points into buckets. This enables you to easily create interactive reports with actionable metrics as the number of devices and events in the system scales up.

For this codelab, we only have a handful of data points generated over a short period of time from a single device. Adjusting the timestamp data type disables the aggregation of the data with respect to time so we can graph each data point individually.

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:

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

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.