Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GCP and IoT

GCP and IoT

A quick talk about getting GCP to talk to IoT devices

Terrence Ryan

October 12, 2016
Tweet

More Decks by Terrence Ryan

Other Decks in Technology

Transcript

  1. ‹#›
    @tpryan
    Terrence Ryan
    Developer Advocate
    IoT and Google Cloud
    Platform

    View Slide

  2. ‹#›
    @tpryan
    Who are you?

    View Slide

  3. ‹#›
    @tpryan
    01 Introduction
    What are we trying to do

    View Slide

  4. • Products
    • Nest
    • Fitbit
    • WeMo
    • Phillips Hue
    • Lockitron
    • Building Blocks
    • Arduino
    • Raspberry Pi
    • BeagleBone
    • etc
    • etc
    • Providers
    • AWS
    • GCP
    • Azure
    • Building Blocks
    • Storage
    • Analytics
    • Processing

    View Slide

  5. • Building Blocks
    • Raspberry Pi
    • BeagleBone
    • Providers
    • GCP
    • Building Blocks
    • Cloud Storage
    • App Engine
    • Pub/Sub
    • BigQuery

    View Slide

  6. @tpryan
    Why did you choose these?

    View Slide

  7. @tpryan
    These are the things that made
    sense to use.

    View Slide

  8. @tpryan
    These are the things I could get
    working

    View Slide

  9. @tpryan

    View Slide

  10. @tpryan

    View Slide

  11. @tpryan

    View Slide

  12. ‹#›
    @tpryan
    Beaglebone to
    App Engine to
    Cloud Storage

    View Slide

  13. BeagleBone
    App Engine
    Cloud Storage

    View Slide

  14. ‹#›
    @tpryan
    App Engine
    • Platform as a Service
    • Give it code
    • It gives you a URL running that code
    • Advantages:
    • Scales up and down
    • Simple
    • Disadvantages
    • Language
    • PHP, Java, Python, Go
    • Restrictions
    • No writing to local disk
    • Other restrictions

    View Slide

  15. View Slide

  16. View Slide

  17. ‹#›
    @tpryan
    Demo: App Engine

    View Slide

  18. ‹#›
    @tpryan
    Cloud Storage
    • File Storage
    • Cheap and fast storage.
    • Easy to write
    • Easy to share
    • Can send to most other GCP Techs
    • Advantages:
    • Scales
    • Simple
    • Disadvantages
    • File based storage not best for analytics

    View Slide

  19. BeagleBone
    App Engine
    Cloud Storage

    View Slide

  20. ‹#›
    @tpryan
    Source Code - App Engine - PHP
    use google\appengine\api\cloud_storage\CloudStorageTools;
    // Get app starting variables.
    $default_bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
    $instance = $_SERVER['INSTANCE_ID'];
    $request = $_SERVER['REQUEST_LOG_ID'];
    // Create data structure
    $instance_data = new stdClass();
    $instance_data->instance = $instance;
    $instance_data->request = $request;
    $instance_data->remote_address = $_SERVER['REMOTE_ADDR'];
    $instance_data->remote_host = $_SERVER['REMOTE_HOST'];
    $instance_data->latlon = $_SERVER['HTTP_X_APPENGINE_CITYLATLONG'];
    $instance_data->time = time();
    $instance_data->data = $_REQUEST;
    $instance_json = json_encode($instance_data, JSON_PRETTY_PRINT);
    file_put_contents("gs://${default_bucket}/simple/${instance}/${request}.json", $instance_json);
    use google\appengine\api\cloud_storage\CloudStorageTools;
    // Get app starting variables.
    $default_bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
    $instance = $_SERVER['INSTANCE_ID'];
    $request = $_SERVER['REQUEST_LOG_ID'];
    // Create data structure
    $instance_data = new stdClass();
    $instance_data->instance = $instance;
    $instance_data->request = $request;
    $instance_data->remote_address = $_SERVER['REMOTE_ADDR'];
    $instance_data->remote_host = $_SERVER['REMOTE_HOST'];
    $instance_data->latlon = $_SERVER['HTTP_X_APPENGINE_CITYLATLONG'];
    $instance_data->time = time();
    $instance_data->data = $_REQUEST;
    $instance_json = json_encode($instance_data, JSON_PRETTY_PRINT);
    file_put_contents("gs://${default_bucket}/simple/${instance}/${request}.json", $instance_json);

    View Slide

  21. ‹#›
    @tpryan
    Source Code - Beaglebone - Python
    import time
    import Adafruit_BBIO.GPIO as GPIO
    import urllib2
    # Note: Use P9_22(UART2_RXD) as GPIO.
    # Connect the Grove Button to UART Grove port of Beaglebone Green.
    Button = "P9_22" # GPIO P9_22
    GPIO.setup(Button, GPIO.IN)
    base_url = "https://simple-dot-gcpiotdemo.appspot.com"
    params = "source=beagleboard&type=button&reading=1"
    if __name__== '__main__':
    while True:
    if GPIO.input(Button):
    print "Button is pressed."
    urllib2.urlopen(base_url + "/?" + params).read()
    time.sleep(.1)
    else:
    print "Button is unstuck."
    time.sleep(1)
    import time
    import Adafruit_BBIO.GPIO as GPIO
    import urllib2
    # Note: Use P9_22(UART2_RXD) as GPIO.
    # Connect the Grove Button to UART Grove port of Beaglebone Green.
    Button = "P9_22" # GPIO P9_22
    GPIO.setup(Button, GPIO.IN)
    base_url = "https://simple-dot-gcpiotdemo.appspot.com"
    params = "source=beagleboard&type=button&reading=1"
    if __name__== '__main__':
    while True:
    if GPIO.input(Button):
    print "Button is pressed."
    urllib2.urlopen(base_url + "/?" + params).read()
    time.sleep(.1)
    else:
    print "Button is unstuck."
    time.sleep(1)

    View Slide

  22. ‹#›
    @tpryan
    Demo: Connecting from BeagleBoard to App Engine

    View Slide

  23. ‹#›
    @tpryan
    Lessons Learned
    • Maybe go more mainstream kit
    • Raspberry Pi
    • Arduino
    • With more updated sensors

    View Slide

  24. ‹#›
    @tpryan
    Raspberry Pi to
    Pub/Sub to
    App Engine to
    Cloud Storage

    View Slide

  25. App Engine
    Cloud Storage
    Raspberry Pi
    Pub/Sub

    View Slide

  26. ‹#›
    @tpryan
    Pub/Sub
    • Messenger service
    • Publish and Subscribe Model
    • Advantages:
    • Unlimited Quota
    • Acts as a buffer
    • Disadvantages
    • Requires security on the device

    View Slide

  27. ‹#›
    @tpryan
    Cloud PUB/SUB

    View Slide

  28. ‹#›
    @tpryan

    View Slide

  29. ‹#›
    @tpryan
    Source Code - Raspberry Pi - Python
    #!/usr/bin/env python
    import PCF8591 as ADC
    import RPi.GPIO as GPIO
    import time
    import math
    import urllib2
    import os
    from google.cloud import pubsub
    INTERVAL = .1
    DO = 17
    GPIO.setmode(GPIO.BCM)
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/pi/gcp/creds.json"
    #!/usr/bin/env python
    import PCF8591 as ADC
    import RPi.GPIO as GPIO
    import time
    import math
    import urllib2
    import os
    from google.cloud import pubsub
    INTERVAL = .1
    DO = 17
    GPIO.setmode(GPIO.BCM)
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/pi/gcp/creds.json"

    View Slide

  30. ‹#›
    @tpryan

    View Slide

  31. ‹#›
    @tpryan
    Source Code - creds.json
    {
    "type": "service_account",
    "project_id": "gcpiotdemo",
    "private_key_id": "a098b68643ae4ccda140d907d5a2394a895e8be4",
    "private_key": "-----BEGIN PRIVATE KEY——\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBA….Q..\n——END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "106452013022587438299",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/raspberrypiclient%40gcpiotdemo.iam.gserviceaccount.com"
    }

    View Slide

  32. ‹#›
    @tpryan
    Source Code - Raspberry Pi - Python
    def setup():
    ADC.setup(0x48)
    GPIO.setup(DO, GPIO.IN)
    def loop():
    status = 1
    tmp = 1
    while True:
    analogVal = ADC.read(0)
    Vr = 5 * float(analogVal) / 255
    Rt = 10000 * Vr / (5 - Vr)
    temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25)))
    temp = temp - 273.15
    print 'temperature = ', temp, 'C'
    publish("iotstream", get_serial() + "," +str(temp))
    time.sleep(INTERVAL)
    if __name__ == '__main__':
    try:
    setup()
    loop()
    except KeyboardInterrupt:
    pass
    def setup():
    ADC.setup(0x48)
    GPIO.setup(DO, GPIO.IN)
    def loop():
    status = 1
    tmp = 1
    while True:
    analogVal = ADC.read(0)
    Vr = 5 * float(analogVal) / 255
    Rt = 10000 * Vr / (5 - Vr)
    temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25)))
    temp = temp - 273.15
    print 'temperature = ', temp, 'C'
    publish("iotstream", get_serial() + "," +str(temp))
    time.sleep(INTERVAL)
    if __name__ == '__main__':
    try:
    setup()
    loop()
    except KeyboardInterrupt:
    pass

    View Slide

  33. ‹#›
    @tpryan
    Source Code - Raspberry Pi - Python
    def publish(topic_name, data):
    pubsub_client = pubsub.Client("gcpiotdemo")
    topic = pubsub_client.topic(topic_name)
    message_id = topic.publish(data)
    print('Message {} published.'.format(message_id))

    View Slide

  34. ‹#›
    @tpryan
    Source Code - Raspberry Pi - Python
    def get_serial():
    cpuserial = "0000000000000000"
    try:
    f = open('/proc/cpuinfo','r')
    for line in f:
    if line[0:6]=='Serial':
    cpuserial = line[10:26]
    f.close()
    except:
    cpuserial = "ERROR00000000000"
    return cpuserial

    View Slide

  35. ‹#›
    @tpryan

    View Slide

  36. ‹#›
    @tpryan
    Source Code - App Engine - PHP
    use google\appengine\api\cloud_storage\CloudStorageTools;
    // Get app starting variables.
    $default_bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
    $instance = $_SERVER['INSTANCE_ID'];
    $request = $_SERVER['REQUEST_LOG_ID'];
    $message = json_decode(file_get_contents('php://input'));
    $datastr = base64_decode(base64_decode($message->message->data));
    $arr = explode(",", $datastr);
    // Create data structure
    $instance_data = new stdClass();
    $instance_data->instance = $instance;
    $instance_data->request = $request;
    $instance_data->remote_address = $_SERVER['REMOTE_ADDR'];
    $instance_data->time = time();
    $instance_data->device = $arr[0];
    $instance_data->temp = $arr[1];
    $instance_json = json_encode($instance_data, JSON_PRETTY_PRINT);
    file_put_contents("gs://${default_bucket}/pubsub/${instance}/${request}.json", $instance_json);
    use google\appengine\api\cloud_storage\CloudStorageTools;
    // Get app starting variables.
    $default_bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
    $instance = $_SERVER['INSTANCE_ID'];
    $request = $_SERVER['REQUEST_LOG_ID'];
    $message = json_decode(file_get_contents('php://input'));
    $datastr = base64_decode(base64_decode($message->message->data));
    $arr = explode(",", $datastr);
    // Create data structure
    $instance_data = new stdClass();
    $instance_data->instance = $instance;
    $instance_data->request = $request;
    $instance_data->remote_address = $_SERVER['REMOTE_ADDR'];
    $instance_data->time = time();
    $instance_data->device = $arr[0];
    $instance_data->temp = $arr[1];
    $instance_json = json_encode($instance_data, JSON_PRETTY_PRINT);
    file_put_contents("gs://${default_bucket}/pubsub/${instance}/${request}.json", $instance_json);

    View Slide

  37. ‹#›
    @tpryan
    Demo: Connecting from RaspberryPi

    View Slide

  38. ‹#›
    @tpryan
    Lessons Learned
    • Securing IoT devices is not trivial

    View Slide

  39. ‹#›
    @tpryan
    Data analysis with
    BigQuery

    View Slide

  40. @tpryan
    10 x a second
    * 60 * 60 * 24
    864,000 data points a day
    * 400 sensors
    345,600,000 data points a day
    * 365 days a year
    126,144,000,000 data points a year

    View Slide

  41. @tpryan
    IoT = BigData

    View Slide

  42. ‹#›
    @tpryan
    Big Query
    • Scan Terabytes in seconds
    • Use SQLish Queries
    • REST, Web UI, ODBC

    View Slide

  43. ‹#›
    @tpryan
    Demo: BigQuery

    View Slide

  44. @tpryan
    Count Shakespeare
    SELECT count(word)
    FROM publicdata:samples.shakespeare

    View Slide

  45. @tpryan
    Count to a Million
    SELECT sum(requests) as total
    FROM [fh-bigquery:wikipedia.pagecounts_20151109_18]

    View Slide

  46. @tpryan
    Count to a Billion
    SELECT sum(requests) as total
    FROM [fh-bigquery:wikipedia.pagecounts_201505]

    View Slide

  47. @tpryan
    Count to a Trillion
    SELECT
    SUM(requests) AS total
    FROM
    TABLE_QUERY(
    [fh-bigquery:wikipedia],
    'REGEXP_MATCH(
    table_id,
    r"pagecounts_201[3-4][0-9]{2}$")')

    View Slide

  48. @tpryan
    Run a RegEx on a Hundreds of Billions
    SELECT
    SUM(requests) AS total
    FROM
    TABLE_QUERY(
    [fh-bigquery:wikipedia],
    'REGEXP_MATCH(
    table_id,
    r"pagecounts_201[3-4][0-9]{2}$")')
    WHERE
    (REGEXP_MATCH(title, '.*[dD]inosaur.*'))

    View Slide

  49. ‹#›
    @tpryan
    Demo: Getting our data into BigQuery

    View Slide

  50. ‹#›
    @tpryan
    06 Conclusion
    What now

    View Slide

  51. App Engine
    Cloud Storage
    Raspberry Pi
    Pub/Sub
    Big Query

    View Slide

  52. Raspberry Pi

    View Slide

  53. View Slide

  54. Raspberry Pi

    View Slide

  55. @tpryan ‹#›
    Google Cloud Platform
    Compute
    Connectivity
    Big Data
    Storage
    Developer
    Tools
    Management
    Machine Learning

    View Slide

  56. ‹#›
    @tpryan
    App Engine
    • Flexible runtime
    • Based on Docker containers
    • Very few restrictions
    • Base images can use GCP resources easily
    • Custom can still use them with more setup
    • Scales slower
    • Scales to 1 not zero

    View Slide

  57. ‹#›
    @tpryan
    Data Flow

    View Slide

  58. ‹#›
    @tpryan
    Cloud Vision

    View Slide

  59. ‹#›
    @tpryan
    Demo: Cloud Vision API

    View Slide

  60. ‹#›
    @tpryan
    Cloud ML

    View Slide

  61. ‹#›
    @tpryan
    Stackdriver

    View Slide

  62. ‹#›
    @tpryan
    Lessons Learned
    • IoT = Big Data
    • Once you get it to a cloud provider your only limit is creativity
    • Security doesn’t have a clear path… yet

    View Slide

  63. ‹#›
    @tpryan
    Lessons Implied
    • Maybe reduce your sample size
    • Maybe skip some steps
    • Maybe batch up data
    • Maybe do some processing on the device

    View Slide

  64. ‹#›
    @tpryan
    Next Steps
    • https://cloud.google.com/solutions/iot/
    • https://cloud.google.com/solutions/iot-overview
    • https://googlecloudplatform.github.io/

    View Slide

  65. ‹#›
    @tpryan
    Thank You
    terrenceryan.com
    @tpryan
    This preso: http://bit.ly/tpryan-iot

    View Slide