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.
-
Select or create a new project in the Google Cloud Console.
-
Visit the Cloud IoT Core page to enable the API.
-
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.
-
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
-
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.
-
Navigate to the Actions console.
-
Select Add/import project.
-
Choose the name of your existing Google Cloud project under Project name, then select IMPORT PROJECT.
-
Choose Home control, followed by Smart home.
-
Select Setup → Invocation and give your action a name.
-
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 the server module to Firebase using the instructions in Device Manager Cloud.
- Deploy the web application using the instructions in Device Manager Web.
- Deploy the mobile application using the instructions in Device Manager Mobile.
- Run a virtual sample device as either a light or thermostat using the private key generated in the previous step.
-
Navigate to the Actions console, and select the your project.
-
Configure account linking for your action. Set Linking type to OAuth → Authorization 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
- Client ID: Value exported as
-
Click SAVE.
-
Provide fulfillment for your action. Select Build → Actions from the sidebar and click ADD YOUR FIRST ACTION. Enter the following Fulfillment URL:
https://<your-cloud-functions-url>/fulfillment
-
Click DONE.
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.
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.
-
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.
-
Visit the Stackdriver Logs in the Cloud Console.
-
Enter the following advanced filter:
resource.type="cloudiot_device" jsonPayload.eventType="DISCONNECT" OR "CONNECT"
-
Click CREATE EXPORT
-
Enter a value for Sink Name
-
Select Cloud Pub/Sub for Sink Service
-
Create a new Cloud Pub/Sub topic called
online-state
as the Sink Destination