# Arm-based IoT Kit for Cloud IoT Core - Getting Started

## Arm-based IoT Kit for Cloud IoT Core

![](https://cdn-learn.adafruit.com/assets/assets/000/046/840/medium800/temperature_3594.jpg?1506961247)

## [Example projects and code on GitHub](https://github.com/ARM-software/Cloud-IoT-Core-Kit-Examples) are supplied to support the [Arm-based IoT Kit for Cloud IoT Core](https://www.adafruit.com/product/3609).

This [getting started guide](https://github.com/ARM-software/Cloud-IoT-Core-Kit-Examples/blob/master/readme.md) assumes you have enabled [Google Cloud IoT Core](https://cloud.google.com/iot-core/) in your GCP concole. It will step you through setting up the [gcloud SDK tools](https://cloud.google.com/sdk/docs/), getting your Raspberry Pi 3 connected to the internet and securely registered as a device with a Google Cloud project.
---

##Parts included in kit:

• [1x Pi3](https://www.adafruit.com/product/3055) 
• [1x Power Supply](https://www.adafruit.com/product/1995 )
• [1x I2C Temp/Pressure/Humidity Sensor](https://www.adafruit.com/product/2652)
• [1x 8-Channel 10-bit ADC with SPI](https://www.adafruit.com/product/856) 
• [1x 8Gb SD with latest Raspian pre-loaded](https://www.adafruit.com/product/2767)
• [1x Breadboard](https://www.adafruit.com/product/239)
• [1x Servo](https://www.adafruit.com/product/169)
• [1x Joystick (pluggable into breadboard)](https://www.adafruit.com/product/245) 
• [1 x Small pixel screen](https://www.adafruit.com/product/1633)
• [1x Photo cell- CdS photoresistor](https://www.adafruit.com/product/161)
• 1x Premium Male/Male Jumper Wires - 20 x 6" [150mm] 
• [1x Assembled Pi Cobbler Plus - Breakout Cable - for Pi B+/A+/Pi 2/Pi 3](https://www.adafruit.com/product/2029) 
• [1x 8 Channel ADC](https://www.adafruit.com/product/856)
• 5x 10K 5% 1/4W Resistor
• 5x 560 ohm 5% 1/4W Resistor
• 1x Diffused 10mm Blue LED
• 1x Electrolytic Capacitor - 1.0uF
• 2x Diffused 10mm Red LED
• 2x Diffused 10mm Green LED
• 2x Diffused 10mm Blue LED
• 2x Breadboard Trim Potentiometer 
• 3x 12mm Tactile Switches



## [Example projects and code](https://github.com/ARM-software/Cloud-IoT-Core-Kit-Examples) are supplied to support the [Arm-based IoT Kit for Cloud IoT Core](https://www.adafruit.com/product/3609) [(kit including RasPi3)](https://www.adafruit.com/product/3594) from [Adafruit](https://www.adafruit.com)
---
# Getting Started

If you purchased the kit that includes the Raspberry Pi 3 Model B, this comes with a pre-formatted NOOBS microSD card. Simply inserting the card into the Pi and powering up the Pi with the included 5v micro USB power supply will boot the Pi and with no interaction, it will default to installing the Raspbian Linux distribution. This is what we want. There are many ways to get a Raspbian and the Pi set up for Google Cloud IoT Core functionality but this guide will focus on getting Raspbian on your WiFi network and headless with secure shell running, gcloud tools installed and IoT Core dependencies for Python installed. These steps will require an HDMI monitor, USB keyboard and mouse.

## Network and firmware updates

1.	Hook up an HDMI monitor, USB keyboard and mouse (plug in an Ethernet cable if you do not intend to use WiFi) then power up your Pi. Once booted, use the WiFi menu in the upper right hand corner of the screen (it should appear with two red 'x's on boot) to connect to the SSID of the wireless network you wish to use. This assumes your network has a DHCP service running on it. If your network has corporate security features, please use another guide appropriate to the type of security required [most require creative use of the wpa_supplicant command and configuration in /etc]. 

2.&nbsp;Use the Raspberry menu to access Preferences-\>Raspberry Pi Configuration. Under the system tab you can change the hostname to whatever you like and set Boot to CLI (not Desktop); this is optional. Under the Interfaces tab enable "ssh" if you intend to use the Pi without a keyboard and monitor going forward. Enable SPI and I2C while you're there. Under the Localisation tab, set up your Locale, Time Zone and Keyboard preferences. A reboot is required after this. All of these options are also available with the raspi-config command in a terminal shell.

![temperature_Screen_Shot_2017-10-12_at_10.55.53_AM.png](https://cdn-learn.adafruit.com/assets/assets/000/047/175/medium640/temperature_Screen_Shot_2017-10-12_at_10.55.53_AM.png?1507831261)

![temperature_Screen_Shot_2017-10-12_at_10.55.00_AM.png](https://cdn-learn.adafruit.com/assets/assets/000/047/176/medium640/temperature_Screen_Shot_2017-10-12_at_10.55.00_AM.png?1507831294)

![temperature_Screen_Shot_2017-10-12_at_11.04.18_AM.png](https://cdn-learn.adafruit.com/assets/assets/000/047/180/medium640/temperature_Screen_Shot_2017-10-12_at_11.04.18_AM.png?1507831536)

![temperature_Screen_Shot_2017-10-12_at_11.07.34_AM.png](https://cdn-learn.adafruit.com/assets/assets/000/047/183/medium640/temperature_Screen_Shot_2017-10-12_at_11.07.34_AM.png?1507831704)

3. Once rebooted and connected to a network we can secure shell into our Pi remotely or use the command line directly to update our Linux distro and Raspberry Pi 3 firmware. The default uersname is "pi", default password is "raspberry ". To get the Pi's IP, use the command "ifconfig" or nmap your subnet for new ssh services. However you connect, update your Pi with the following commands and change your pi's default password with the "passwd" command if you so choose.

   *Get root access for updates*


    sudo -s 

   *This step can take a while due to the number of packages installed by default on the Pi, feel free to uninstall the wolfram-engine, browsers, office applications, etc. at your discretion before running the updates*

    apt update &amp;&amp; apt upgrade &amp;&amp; apt dist-upgrade

    
   *Update the pi firmware (most likely requires a reboot after completion)*

    rpi-update &amp;&amp; reboot

 *note: you can change most boot, bus and, interface options with a curses interface as well using ***sudo raspi-config** *i.e. enabling the i2c interface* 

---

## Enabling Cloud IoT Core AP, installing the Google Cloud SDK and registering your first device
Before you proceed please ensure you are logged into Google via your browser with the same userid and password you used with gcloud init on your development machine.


The Google Cloud SDK can be installed on another host machine or the Pi itself. These steps will get the gcloud command installed on the Pi but it can just as easily be done on any machine that you do your development on.


1. Create a Cloud Platform project and enable the Cloud IoT Core API using these&nbsp;**"[Before you begin](https://cloud.google.com/iot/docs/how-tos/getting-started)"**&nbsp;directions.

2. Install&nbsp;**[the latest Google Cloud Tools](https://cloud.google.com/sdk/docs/#deb)**&nbsp;with the included directions. In Linux some of the additions require "sudo gcloud" to be used so you'll need to authorize your root account with sudo in addition to your 'pi' account so instructions from here will diverge from those included [here](https://cloud.google.com/iot/docs/device_manager_guide#install_the_gcloud_cli). Simply follow the directions below instead if you are installing&nbsp;gcloud&nbsp;on the Pi rather than another host machine. SSHing into your Pi (headless) is&nbsp; **strongly** &nbsp;advised in order facilitate authentication of your accounts with your normal desktop browser using copy/paste.

`sudo gcloud components repositories add https://storage.googleapis.com/cloud-iot-gcloud/components-json`

3. Create shell variables with your specific project name from step 1 as well as&nbsp;region, registry, device, subscription and event names. Fill in your project ID from step 1, the rest can remain as is below and used in your .profile or .bashrc. i.e.

`project=my-project-name-1234`  
`region=us-central1`  
`registry=example-registry`  
`device=my-rs256-device`  
`device2=my-es256-device`  
`mysub=my-sub`  
`events=events`

4. Create a new registry using the&nbsp;gcloud&nbsp;command.

`gcloud iot registries create $registry \`  
`--project=$project \`  
`--region=$region \`  
`--pubsub-topic=projects/$project/topics/$events`

5. Create a public/private key pair for your device and create a new device in your project and registry. Or, stretch goal, register one programmatically with [these code samples](https://cloud.google.com/iot/docs/device\_manager\_samples).

`openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out rsa_cert.pem`

`gcloud iot devices create $device \`  
`--project=$project \`  
`--region=$region \`  
`--registry=$registry \`  
`--public-key path=rsa_cert.pem,type=rs256`

`openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem`  
`openssl ec -in ec_private.pem -pubout -out ec_public.pem`

`gcloud iot devices create $device2 \`  
`--project=$project \`  
`--region=$region \`  
`--registry=$registry \`  
`--public-key path=ec_public.pem,type=es256`

6. Create a new&nbsp;pubsub&nbsp;subscription to an event

`gcloud pubsub subscriptions create projects/$project/subscriptions/$mysub --topic=$events`

7. Download the CA root certificates from pki.google.com into the same directory as the example script you want to use:

`wget https://pki.google.com/roots.pem`

## Dependencies
Our initial examples for this kit will focus on Python but it is entirely possible to use Ruby, Java, C and other languages to work with Google Cloud IoT. Dependencies include a JSON Web Token and MQTT library as well as a SSL/TLS library like OpenSSL. You'll need the following to run any of the examples included in this repository.

    sudo -s
    apt install build-essential libssl-dev libffi-dev python-dev
    pip install pyjwt paho-mqtt cryptography
    pip install --upgrade google-api-python-client
    pip install --upgrade google-cloud-core
    pip install --upgrade google-cloud-pubsub
    exit


## Hello World - Temperature example

See [CPUTemp example's code](https://github.com/ARM-software/Cloud-IoT-Core-Kit-Examples/tree/master/CPUTemp) to verify your device can communicate with your gcloud project.

## CPUTemp Example
---

This example is the our "Hello World" for our Raspberry Pi 3 setup. This should verify that you are able to send JWT encoded messages with MQTT to your Google Cloud project registery topic

 On your Pi export or set $project $registry and $device varialbes to your own and run:

    pi_cpu_temp_mqtt.py --project_id=$project --registry_id=$registry --device_id=$device --private_key_file=rsa_private.pem --algorithm=RS256

gcloud command to fetch CPU temperature:

    gcloud pubsub subscriptions pull --auto-ack projects/$project/subscriptions/$mysub


![](https://cdn-learn.adafruit.com/assets/assets/000/047/290/medium800/temperature_Screen_Shot_2017-10-13_at_12.41.09_PM.png?1507923731)

Find more [samples](https://cloud.google.com/iot/docs/samples/) and [documentation](https://cloud.google.com/iot/docs/) at the Google Cloud Platform IoT site.

## Useful code

[Python driver for BME280](https://github.com/adafruit/Adafruit_Python_BME280) Temp/Pressure/Humidity Sensor
[Adafruit Python GPIO library](https://github.com/adafruit/Adafruit_Python_GPIO)
[Servo control with wiringpi](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor/software)
[Servo control with Tkinter](http://razzpisampler.oreilly.com/ch05.html)
[Small Pixel Screen (8x8 Backpack) driver](https://github.com/adafruit/Adafruit_Python_LED_Backpack)
[Adafruit MCP3008 ADC Library](https://github.com/adafruit/Adafruit_Python_MCP3008) ([Setup Guide](https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008))
[Adafruit Fritzing Library](https://github.com/adafruit/Fritzing-Library)

# Arm-based IoT Kit for Cloud IoT Core - Getting Started

## Pubsub thermostat example

This example will use the kit's temperature/pressure/humidity sensor to monitor temperature and control a fan in a complete IoT system with both a server and device component. The devices in this system (your Cloud IoT Core kit(s) in this case) publish temperature data on their pubsub registry feeds and individual device IDs. A server python application, which you can run from any machine you like, consumes the telemetry data from your Cloud Pub/Sub topic and events. The server then decides whether to turn on or off the individual devices' fans via a Cloud IoT Core configuration update. 

This example requires i2c to be enabled in order to read the [temperature sensor included with this kit](https://www.adafruit.com/product/2652). If you haven't already enabled i2c during your initial setup, please run 

    sudo raspi-config

Go to Interfacing Options-&gt;I2C and enable. 

![](https://cdn-learn.adafruit.com/assets/assets/000/047/291/medium800/temperature_Screen_Shot_2017-10-13_at_12.49.50_PM.png?1507924396)

![](https://cdn-learn.adafruit.com/assets/assets/000/047/292/medium800/temperature_Screen_Shot_2017-10-13_at_12.50.04_PM.png?1507924417)

Exit out of raspi-config and run:

    sudo i2cdetect -F 1



![](https://cdn-learn.adafruit.com/assets/assets/000/047/293/medium800/temperature_Screen_Shot_2017-10-13_at_12.55.54_PM.png?1507924602)

Connect the RasPi Cobbler board to your breadboard and the 40 pin cable to your Pi 3 [as pictured here](https://cdn-shop.adafruit.com/970x728/2029-01.jpg). The keyed end in the cobbler is obvious, the white striped end of the cable and 90° angle of the cable coming off the RasPi (which is not keyed) are useful visual queues. Connect the Temp/Pressure/Humidity Sensor to the breadboard and connect the 3.3v and ground pins to the cobbler. Then connnect the i2c clock and data pins: 

![](https://cdn-learn.adafruit.com/assets/assets/000/046/785/medium800/temperature_end-to-end-thermo_bmc.png?1506726632)

On the [Pi Cobbler SDA is data pin and SCL is clock pin. On the BME280 sensor SDI is the data pin and SCK is the clock pin](https://cdn-learn.adafruit.com/assets/assets/000/046/724/medium800/temperature_end-to-end-thermo.png).

Verify i2c is enabled. 

    sudo i2cdetect -y 1
    
Will display a grid showing what address any devices are using on the i2C bus. 

![](https://cdn-learn.adafruit.com/assets/assets/000/047/294/medium800/temperature_Screen_Shot_2017-10-13_at_12.57.53_PM.png?1507924726)

You can dump more information about any of the addresses shown with:
    
    sudo i2cdump -y 1 0x77    &lt;--- hex number shown from previous command
    
Install the Adafruit_Python_GPIO and Adafruit_Python_BME280 abstraction librabies

    sudo apt-get install build-essential python-pip python-dev python-smbus git
    cd ~ &amp;&amp; mkdir dev
    cd dev
    git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
    cd Adafruit_Python_GPIO
    sudo python setup.py install
    cd ..
    git clone https://github.com/adafruit/Adafruit_Python_BME280.git
    cd Adafruit_Python_BME280
    sudo python setup.py install
If you wish to sanity check your i2c wiring and sensor further:

    python Adafruit_BME280_Example.py 



![](https://cdn-learn.adafruit.com/assets/assets/000/047/295/medium800/temperature_Screen_Shot_2017-10-13_at_1.00.31_PM.png?1507924883)

Now connect an LED to GIPO 21 and one of the GND pins with a resistor in series on your breadboard. i.e Pin 21 on your Cobbler -&gt; the long pin of the blue LED -&gt; resistor -&gt; GND rail or pin row:

![](https://cdn-learn.adafruit.com/assets/assets/000/046/784/medium800/temperature_end-to-end-thermo_led.png?1506726469)

The included 560 Ohm and 10K Ohm resistors will both protect the circuit, the latter make the LED dim. You can sanity check your wiring with python using the following commands one by one:

    python
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BCM) 
    GPIO.setup(21, GPIO.OUT)
    GPIO.output(21, GPIO.HIGH)
    GPIO.output(21, GPIO.LOW)
    quit()

Using "GPIO.output(21, GPIO.HIGH)" and "GPIO.output(21, GPIO.LOW)" should toggle your LED on an off. Or sanity check from bash using "gpio -g blink 21"

You'll also need the Python pub/sub library and APIs

    sudo pip install --upgrade google-cloud-pubsub
    sudo pip install google-api-python-client google-auth-httplib2 google-auth google-cloud

[Create an API key and service account named api-tester](https://cloud.google.com/iot/docs/device_manager_samples) and make a service_account.json file (steps 1 and 2 in the link) and put it in this example's directory (scp or rsync over ssh are easy ways to move files to your ssh connected Pi if you've downloaded the json file on a host machine).

Make sure you're authenticated. If you haven't already associated a gcloud project_id with this project, you'll be asked to do so. Use the project you created in the top level readme of this code base.

    gcloud auth application-default login

Change to the directory you've cloned this example to. i.e. "cd ~/Cloud-IoT-Core-Kit-Examples/pubsub-thermostat"
Our control server can run on any host machine, including the RasPi. The "--fan_off" and "--fan_on" arguments are the integer temperatures in °C that will turn on the "fan" LED i.e. when a devices is over 23°C and when it will turn the fan back off i.e. when a device is under 22°C. See optional argument options like "--service_account_json=directory/location" in the code.

    python control_server.py \
     --project_id=$project \
     --pubsub_topic=$events \
     --pubsub_subscription=$mysub \
     --api_key=$apiKey \
     --fan_off=22 \
     --fan_on=23

The client will run on one or many RasPi Cloud IoT kits with unique device ids:

    python pubsub_thermostat.py \
      --project_id=$project \
      --registry_id=$registry \
      --device_id=$device \
      --private_key_file=rsa_private.pem \
      --algorithm=RS256

![](https://cdn-learn.adafruit.com/assets/assets/000/047/296/medium800/temperature_Screen_Shot_2017-10-13_at_2.15.42_PM.png?1507929419)

![](https://cdn-learn.adafruit.com/assets/assets/000/047/297/medium800/temperature_Screen_Shot_2017-10-13_at_2.15.58_PM.png?1507929440)


## Featured Products

### Raspberry Pi 3 - Model B - ARMv8 with 1G RAM

[Raspberry Pi 3 - Model B - ARMv8 with 1G RAM](https://www.adafruit.com/product/3055)
Did you really think the Raspberry Pi would stop getting better? At this point, we sound like a broken record, extolling on the new Pi’s myriad improvements like we’re surprised that the folks at the Raspberry Pi Foundation are continuously making their flagship board better.&nbsp;...

In Stock
[Buy Now](https://www.adafruit.com/product/3055)
[Related Guides to the Product](https://learn.adafruit.com/products/3055/guides)
### ARM-based IoT Kit for Cloud IoT Core

[ARM-based IoT Kit for Cloud IoT Core](https://www.adafruit.com/product/3609)
The **Arm-based Internet&nbsp;of Things&nbsp;Pack for Raspberry Pi 3** is another great addition to our family of [IoT packs](https://www.adafruit.com/category/878). If you want to start using your Raspberry Pi as an IoT-enabled device, then this kit is for...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3609)
[Related Guides to the Product](https://learn.adafruit.com/products/3609/guides)
### ARM-based IoT Kit for Cloud IoT Core - w/ Raspberry Pi 3

[ARM-based IoT Kit for Cloud IoT Core - w/ Raspberry Pi 3](https://www.adafruit.com/product/3594)
The **Arm-based Internet&nbsp;of Things&nbsp;Pack for Raspberry Pi 3** is another great addition to our family of [IoT packs](https://www.adafruit.com/category/878). If you want to start using your Raspberry Pi as an IoT-enabled device, then this kit is for...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3594)
[Related Guides to the Product](https://learn.adafruit.com/products/3594/guides)

## Related Guides

- [Adafruit Ultimate GPS HAT for Raspberry Pi](https://learn.adafruit.com/adafruit-ultimate-gps-hat-for-raspberry-pi.md)
- [DIY Thermal Light Painting - Heat Map Photography](https://learn.adafruit.com/diy-flir-light-painting-heat-map-photography.md)
- [Adafruit PiTFT - 2.8" Touchscreen Display for Raspberry Pi](https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi.md)
- [PyPortal MQTT Sensor Node/Control Pad for Home Assistant](https://learn.adafruit.com/pyportal-mqtt-sensor-node-control-pad-home-assistant.md)
- [Processing on the Raspberry Pi & PiTFT](https://learn.adafruit.com/processing-on-the-raspberry-pi-and-pitft.md)
- [Raspberry Pi Care and Troubleshooting](https://learn.adafruit.com/raspberry-pi-care-and-troubleshooting.md)
- [16x32 RGB Display with Raspberry Pi - part 2](https://learn.adafruit.com/16x32-rgb-display-with-raspberry-pi-part-2.md)
- [Raspberry Pi E-mail Notifier Using LEDs](https://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds.md)
- [Monitor PiCam and temperature on a PiTFT via adafruit.io](https://learn.adafruit.com/monitor-picam-and-temperature-on-a-pitft-via-adafruit-dot-io.md)
- [Adafruit's Raspberry Pi Lesson 6. Using SSH](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh.md)
- [ISS Pin](https://learn.adafruit.com/iss-pin.md)
- [Creepy Face Tracking Portrait](https://learn.adafruit.com/creepy-face-tracking-portrait.md)
- [MagTag Lists From Google Spreadsheets](https://learn.adafruit.com/collaborative-spreadsheets-to-magtag.md)
- [No-Code Seedling Frost Alert Device with Adafruit IO and WipperSnapper](https://learn.adafruit.com/no-code-seedling-frost-and-sun-alert-with-adafruit-io-and-wippersnapper.md)
- [MagTag Sports Schedule Viewer](https://learn.adafruit.com/magtag-sports-schedule-viewer.md)
