Connecting an Intel® NUC to the Google Cloud Platform Service

0
42

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

Introduction

This paper will show you how to use an Intel® NUC to connect sensors on an Arduino 101* (branded Genuino 101* outside the U.S.) to the Google Cloud Platform*. You’ll see how to read real-time sensor data from the Arduino 101, view it locally on the Intel® NUC, and send it to the Google Cloud Platform where the data can be stored, processed and visualized in the cloud. We’ll use Node-RED* on the Intel® NUC to create processing flows to perform input, processing and output functions that drive our application.

Setup and Prerequisites

  • Intel® NUC connected to the Internet
  • Arduino 101 connected to Intel® NUC through USB
  • Grove* Base Shield attached to Arduino 101 and switched to 3V3 VCC
  • Grove sensors connected to base shield: Light on A1, Rotary Encoder on A2, Button on D4, Green LED on D5, Buzzer on D6, Relay on D7
  • packagegroup-cloud-google package installed on the Intel® NUC
  • An active Google Cloud Platform account
  • Python 2.7.x and pip on your developer workstation

Read Sensors and Display Data on Intel® IoT Gateway Developer Hub

Log into the Intel® IoT Gateway Developer Hub by web browsing to the Intel® NUC IP address and using gwuser as the default username and password. You’ll see basic information about the Intel® NUC including model number, version, Ethernet address, and network connectivity status.

Click the Sensors icon and then click the Program Sensors button. This will open the Node-RED canvas where you’ll see Sheet 1 with a default flow for a RH-USB sensor. We won’t be using the RH-USB sensor so you can use your mouse to drag a box around the entire flow and delete it by pressing the Delete key on your keyboard. You’ll be left with a blank canvas.

Along the left side of the Node-RED screen you’ll see a series of nodes. These are the building blocks for creating a Node-RED application on the Intel® NUC. We’ll use several nodes in this application:

Read button presses

On/off LED indicator

Measure light level

Format chart display on Intel® NUC

Measure rotary position

Send data to Intel® NUC MQTT chart listener

Relay open/closed

Processing function

Send data to Google Cloud Platform    

Drag and drop nodes onto the canvas and arrange them as shown below. For some of the nodes we’ll need multiple copies. Use your mouse to connect wires between the nodes as shown. We’ll use the function and pubsub nodes later so don’t include them for now.

When nodes are first placed on the canvas they are in a default state and need to be configured before they’ll work. Nodes are configured by double-clicking them and setting parameters in their configuration panels.

Double-click each node on the canvas and set its parameters as shown in the table below. In some cases the Name field is left blank to use the default name of the node. Pin numbers correspond to the Grove Base Shield jack where the sensor or actuator is connected.

Node

Parameters

Grove Button

Platform: Firmata, Pin: D4, Interval (ms): 1000

Grove Light

Platform: Firmata, Pin: A1, Unit: Raw Value, Interval (ms): 1000

Grove Rotary

Platform: Firmata, Pin: A2, Unit: Absolute Raw, Interval (ms): 1000

Grove LED

Platform: Firmata, Pin: D5, Mode: Output

Grove Relay (upper)

Platform: Firmata, Pin: D7, Interval (ms): 1000

Grove Relay (lower)

Name: Grove Buzzer, Platform: Firmata, Pin: D6 (we’ll use this node to control the buzzer), Interval (ms): 1000

chart tag connected to Grove Button

Title: Button, Type: Status Text

chart tag connected to Grove Light

Title: Light, Type: Gauge, Units: RAW

chart tag connected to Grove Rotary

Title: Rotary, Type: Gauge, Units: RAW

mqtt

Server: localhost:1883, Topic: /sensors, Name: Charts

Verify your settings and wiring connections, then click the Deploy button to deploy your changes and make them active on the Intel® NUC. After deploying the flow, you should see a data display towards the top of the Intel® IoT Gateway Developer Hub screen with live values for Rotary, Light and Button. Turning the rotary knob and covering the light sensor should make the numbers change and pressing the button should turn on the LED, sound the buzzer, and energize the relay.

Create Google Cloud Platform* Pub/Sub API Instance

Before we can send sensor data to Google Cloud Platform we need to create a Google Cloud Platform Pub/Sub API instance in your Google Cloud Platform account. Log into Google Cloud Platform and create a new Project named IoT Pub Sub with billing enabled. A unique project ID will be automatically generated that is globally unique. It will consist of letters and/or numbers that are different for each project. Our unique project ID is small-dynamo-392814. Enable the Pub/Sub API for the IoT Pub Sub project and then navigate to the Topics screen. Create a new pub/sub topic named intelnuc in lowercase. A full topic name will be automatically generated from the project name and the resulting topic string should look similar to projects/small-dynamo-392814/topics/intelnuc.

Next, navigate to API Manager > Credentials and create a new set of credentials of type Service account key. Select Compute Engine default service account, select JSON key type, then click Create. A service account key JSON file will automatically download to your developer workstation and you’ll need its contents during the next steps.

Publish Sensor Data to Google Cloud Platform* Pub/Sub

Drag a pubsub output node onto the canvas and double-click to edit its parameters. Click the pencil icon to the right of Add new google-cloud-credentials… which will open a settings box. Set Name to Pub/Sub Credential and for the Key field, copy and paste the entire contents of the service account key JSON file downloaded from Google Cloud Platform in the previous step. Click Add to save the credential information and return to the pubsub node configuration box. Set Topic to intelnuc and set Name to Google Cloud Publish. Click Done to save your changes.

Now drag four copies of the function node onto the canvas and set their properties as follows:

Function 1, Name = Format Button


Function 2, Name = Format Light

Function 3, Name = Format Rotary


Function 4, Name = Generate Payload

Now draw wires between the new nodes so that the finished flow looks like this:

Click Deploy to deploy your updated flow to the Intel® NUC. At this point data from the Arduino 101 sensors is being read and published to Google Cloud Pub/Sub once a second. The logic in Generate Payload waits until new data values are available from all three sensors before generating a combined payload message that’s sent to Google Cloud Pub/Sub.

To confirm that data messages are being received in Google Cloud Pub/Sub, in the Google Cloud Platform console navigate to API Manager > Google Cloud Pub/Sub API and view the Traffic graph, which should look something like this:

View Live Sensor Data

Now that we have data flowing into Google Cloud Pub/Sub we can view it in real time by subscribing to the intelnuc topic. There are several ways this can be done and here we’ll use an open-source Python* program from Google that can subscribe to the intelnuc topic and print received messages. The source code runs on your developer workstation with Python 2.7 and uses pip to install Python support packages.

Download the source code .zip file to your developer workstation from https://github.com/GoogleCloudPlatform/cloud-pubsub-samples-python/archive/master.zip

Unzip the archive file which will create a directory named cloud-pubsub-samples-python-master.

On your developer workstation, issue the following shell commands (these are for MacOS* or Linux*; Windows* commands are similar) to configure and run the Python program. You’ll need to know the full path to the service account key JSONfile you downloaded previously from Google Cloud Pub/Sub and our full path and filename is “/Users/nuc/key-53716c71ceb6.json“.

Run these commands to install Python support packages used by the program and set an environment variable for the key file:

$ cd cloud-pubsub-samples-python-master/cmdline-pull
$ pip install -r requirements.txt
$ export GOOGLE_APPLICATION_CREDENTIALS=/Users/nuc/key-53716c71ceb6.json

Run this command to create a new Google Pub/Sub subscription named mysub for the intelnuc topic. This will be used to retrieve messages from the topic. Use your actual Google Pub/Sub project name in place of small-dynamo-392814.

$ python pubsub_sample.py small-dynamo-392814 create_subscription mysub intelnuc

Run this command to use the mysub subscription to read and display messages:

$ python pubsub_sample.py small-dynamo-392814 pull_messages mysub

You should see a new message print once a second. These are the sensor messages published by the Intel® NUC containing the button, rotary and light sensor readings along with a timestamp.

{"button":0,"rotary":344,"light":566,"time":1490368653518}
{"button":0,"rotary":344,"light":563,"time":1490368654540}
{"button":0,"rotary":344,"light":561,"time":1490368655551}
{"button":0,"rotary":344,"light":565,"time":1490368656558}
{"button":0,"rotary":344,"light":568,"time":1490368657566}

Press Control-C to stop the program.

When you’re done retrieving messages, run this command to delete the mysub subscription:

$ python pubsub_sample.py small-dynamo-392814 delete_subscription mysub

When you’re done testing this application be sure to stop your Node-RED flow (e.g. by turning off the Intel® NUC or removing the wire between Generate Payload and Google Cloud Publish and re-deploying the flow) in order to preserve the remaining messages in your free allotment of Google Cloud Pub/Sub messages. Otherwise they’ll be consumed by the Node-RED app continuing to run.

Where to Go From Here

This application provides the basic foundation for connecting your Arduino 101 and Intel® NUC to the Google Cloud Platform. From here you could wire up other sensors and send their data to Google Cloud Pub/Sub, then build more complex applications that subscribe to the pub/sub topic and store, process and/or visualize the incoming sensor data.

LEAVE A REPLY