/iot-smart-home-cloud

Sample project using Cloud IoT Core and the Google Assistant

Primary LanguageJavaScriptApache License 2.0Apache-2.0

Smart Home Device Manager

This projects contains sample code that demonstrates the use of Firebase and Google Cloud IoT Core to build a cloud service for consumer smart home devices.

Quickstart

Set up Google Cloud

  1. Select or create a new project in the Google Cloud Console.

  2. Visit the Cloud IoT Core page to enable the API.

  3. Follow these instructions to create a device registry and a new device entry.

    • When creating a new device entry, generate an ES256 key. You can find details on creating an ES256 key pair here.
    • Use device-events as the Default telemetry topic.
  4. Save the project values as environment variables so you can use them later on:

    $ export PROJECT_ID=my-project
    $ export REGION=us-central1
    $ export REGISTRY=my-registry
    $ export DEVICE_ID=my-device
    $ export PRIVATE_KEY_FILE=./ec_private.pem
    
  5. Visit the Firebase console and click Add Project from the dashboard. Choose the name of your existing Google Cloud project under Project name, then select Add Firebase.

Set up Actions on Google

  1. Navigate to the Actions console.

  2. Select Add/import project.

  3. Choose the name of your existing Google Cloud project under Project name, then select IMPORT PROJECT.

  4. Choose Home control, followed by Smart home.

  5. Select SetupInvocation and give your action a name.

  6. Create a unique client id and secret that you will assign to Google for use during smart home account linking.

    $ export CLIENT_ID=my-client-id
    $ export CLIENT_SECRET=my-client-secret
    

NOTE: These values enable the Google Assistant to identify itself to your action during account linking. This is not used to identify any particular user.

Deploy project modules

  1. Deploy the server module to Firebase using the instructions in Device Manager Cloud.
  2. Deploy the web application using the instructions in Device Manager Web.
  3. Deploy the mobile application using the instructions in Device Manager Mobile.
  4. Run a virtual sample device as either a light or thermostat using the private key generated in the previous step.

Complete smart home setup

  1. Navigate to the Actions console, and select the your project.

  2. Configure account linking for your action. Set Linking type to OAuthAuthorization Code, then enter the following Client Information:

    • Client ID: Value exported as CLIENT_ID during initial setup.
    • Client secret: Value generated for CLIENT_SECRET during initial setup.
    • Authorization URL: https://<your-firebase-hosting-app>/link-account
    • Token URL: https://<your-cloud-functions-url>/token
  3. Click SAVE.

  4. Provide fulfillment for your action. Select BuildActions from the sidebar and click ADD YOUR FIRST ACTION. Enter the following Fulfillment URL:

    • https://<your-cloud-functions-url>/fulfillment
  5. Click DONE.

Testing your action

Follow these instructions to enable testing for your smart home action. You can use the Google Home app to link your account and control devices.

Handle online status changes (Optional)

By default, the sample assumes a device is online once it begins receiving state change events from that device. You can use the following steps to export the MQTT logs from Stackdriver, which will report connection state changes.

  1. Enable logging for IoT Core Registry.

    • Navigate to IoT Core Registries page in the Cloud Console.
    • Click the ID of the Registry, and then click EDIT REGISTRY at the top of the page.
    • Under Stackdriver Logging, select Info log level.
    • Click Update.
  2. Visit the Stackdriver Logs in the Cloud Console.

  3. Enter the following advanced filter:

    resource.type="cloudiot_device"
    jsonPayload.eventType="DISCONNECT" OR "CONNECT"
    
  4. Click CREATE EXPORT

  5. Enter a value for Sink Name

  6. Select Cloud Pub/Sub for Sink Service

  7. Create a new Cloud Pub/Sub topic called online-state as the Sink Destination