How to send sensor data to firebase Firestore using google app script — Part 2
By: Ashna Nizam
Tags: Google Sheets, Sensors, App Script, Firestore, Google Cloud Platform

Welcome to part 2 of the step by step tutorial on how to send sensor data to Firestore using google app script. Check out the part 1 of this two part tutorial to know how to send data from sensors to google sheets. In this tutorial I will be walking you through sending data dynamically from the google sheets to cloud Firestore as sensor data gets logged.

STEP 4:

After getting sensor data to google sheets we need to setup our cloud Firestore. For this, open google firebase. Sign in using the same google account in which you created the sheets. If you are already signed in then your google profile pic will be visible at the top right corner. After signing in click on ‘Go to console’.

STEP 4.1:

Create a new project. If you already have projects then click Add project

STEP 4.2:

Give a name for your project and click continue

Then enable the google analytics for the project and press Continue

Now choose google analytics account. If you don’t have one then choose the default account for firebase and click on Create project.

Now your project will be created and you will be prompted to a window as shown below. Click Continue to go to your project dashboard

STEP 4.3:

Choose Cloud Firestore from the dashboard.

Now click on create database

Here you will be prompted to choose the mode in which the database is going to be created. As this is for DIY project purpose we will be going with test mode. This can be later changed by changing the firebase rules. After choosing the mode click on Next.

To set up the Firestore location check the region where you are from. This cannot be changed later and must be given correctly in order for the database to work properly especially when the data is timestamped. After choosing the location click on enable to finish setting up your database.

Now your database is ready to accept values

STEP 5:

Now we need to get the project credentials for our firebase project. For this head over to google console. Now search the firebase project you have created and select that.

After selecting the project head over to API overview and choose credentials

Next choose the service account of the project.

Now create a new key for enabling API.

Choose JSON format and click on create.

Now a JSON file would be downloaded with the private keys. From this note down the private_key, client_email and project_id.

STEP 6:

The final step of this project is to add the corresponding app script code to send data received in google sheets to Firestore. For that first we need to install the Firestore library. To do so open script editor and go to Resources->Libraries. Copy the following code for Firestore library in the input box and click Add

1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw

Now select the latest version from the version drop down and enable the Development mode option. Then hit save and the Firestore library would be added to the script editor.

Now copy the code for sending data to Firestore. In the part 1 of the code you might have noticed that I have commented out a statement ‘firestore()’ just before the returning the content service. Make sure to uncomment this line as this line is the one that connects the Firestore part of the code to the main code.

After copying the code you might have to authenticate your web app again as we have added some libraries. To know how to authenticate your web app check the part 1 of this tutorial where I have explained it in detail. Also make sure to update the gas id in your Arduino code. Get the complete script editor code here.

If you did everything correctly and got the output like above, Congratulations! on getting your sensor data to firestore. If you haven’t then don’t worry everything takes some patience and it might be just a small careless mistake. Review what you have done and you would be able to rectify it and get the final result. Hope you have enjoyed my first tutorial. Feel free to ask your queries in the comments.