Firebase
Cloud Computing platform
Firebase is a cloud platform from Google. It's built on top of Google Cloud Platform and it aims to make Cloud Computing easier to access for developers. It provides multiple services but we are going to be using only these:
Hosting
Cloud Functions (Functions as a Service)
Realtime Database (streaming/real-time NoSQL data storage)
Dependencies
You need Node.js (which comes with npm) installed on your computer
Go to nodejs.org and follow the instructions for your Operating System
Firebase project
First, we have to create a Firebase project
Log in with your Google account
Create a Firebase project (it will use the default plan, Spark, which is free)
Make a note of your Project ID. We will need it later.
Let's set up the Firebase CLI so we can easily implement features on the Firebase Platform.
Open a Terminal or Command Prompt
Run
npm install --global firebase-toolsRun the
firebase logincommand and follow the prompt to log in using your Google account
Now let's write de code for our Google Assistant integration.
Create a directory for your code
Inside the directory, run
firebase initUsing the space key to select and the arrow keys to navigate, select only these features:
functions
hosting
Make sure you select your newly created Firebase Project
Select these settings
Setting
Value
What language would you like to use to write Cloud Functions?
JavaScript
Do you want to use ESLint to catch probable bugs and enforce style?
No
Do you want to install dependencies with npm now?
Yes
What do you want to use as your public directory?
public
Configure as a single-page app (rewrite all urls to /index.html)?
Yes
The Firebase CLI will then scaffold and configure a project for you.
Open the directory for your project with your favourite editor or IDE and you should have a directory structure similar to this
firebase.json
public
index.html
functions
index.js
package.json
node_modules
Open the functions/package.json file and add a configuration for the runtime environment
Important! Do not copy and paste the code below. Open the file and add the engines configuration for the Firebase Cloud Functions environment
Real-time Database
Load the initial data
In the navigation menu on the left, click on Develop › Database
Choose Realtime Database. (Firestore is not yet supported on Arduino).
Initially, our database will be empty initially.
We are going to create a data structure that holds the state representation for our smart light bulb. We can create the properties one by one to reflect the JSON below, or just use the import JSON command on the options menu
Create a file named database.json and paste the following content. Save your file and use the import JSON option on the Firebase Console.
Once you seed the data, your database should look like this.

Using the Firebase Console on your browser, you can update your data manually whenever you want.
Set up the database rules
We need to access this data without authenticating for now, let's open our dataset for read and write access
In real-life projects, leaving our database open for read and write access would be a terrible practice. Don't do this!
Go to the rules tab and update the contents with
Make sure you publish the rules

Database secret
In order to connect to our Firebase Realtime Database from our Arduino code, we'll need some basic credentials.
Go to your project settings on the Firebase Console

From here, go to Service Accounts › Database secrets. We need to generate a new database secret and save it for our next step.

Connecting our prototype
Copy the following code, and make sure to update the following settings to match your Wi-Fi settings and Firebase Project.
FIREBASE_HOST
FIREBASE_AUTH
WIFI_SSID
WIFI_PASSWORD
Test run
Upload your code and open Tools > Serial Monitor
You should see your board connect to the internet and receive data from Firebase
Now change some color values in the Firebase Console and watch as your RGB led changes and your Serial Monitor shows you log traces. It's working!

Firebase Cloud Functions
We are going to use Firebase Cloud functions to create two endpoints for the Actions on Google Smart API to connect.
We will need
A token endpoint where Google will perform OAuth 2 authentication
A request endpoint to handle requests from the Smart Home API
Let's add some dependencies to build our functions
Go into the functions directory and run
On your IDE or code editor, update the functions/index.js file with the following code that contains endpoint definitions
And now let's deploy our functions. Go back to your terminal/console, and from the functions directory, run the following command:
Take a note of the endpoint for the two Firebase Cloud functions
Firebase Hosting
We are going to create a simple login page that will take the parameters from Google and redirect an authenticated user.
Update the public/index.html file
Deploy this page with
Take a note of the generated public URL for your Firebase-hosted page
References
Last updated
Was this helpful?