How to send sensor data to firebase Firestore using google app script — Part 1

Ashna Nizam
5 min readDec 25, 2020

Have you ever wondered how we can send sensor data to a database? Most of you might have come across on how to send sensor data to real time database of google firebase. But the problem? Almost all the data is just getting overwritten and not exactly stored. So how can we store data? Here is the part where google app script comes to play. It acts as a mediator between the database and the sensor circuits.

In the part 1 of the two part tutorial I will be showing you the step by step instructions on how to send sensor data to google sheets (which acts as the mediator here) using app script. Head over to part 2 of this tutorial where I will be showing you how to send data from the google sheets to Firestore.

So before we jump into the project we should know what all are the different tools we use for achieving our goal.

What is google firebase?

According to the google firebase community Google firebase is a Google backend application development software that enables developers to develop iOS, Android and web applications. Firebase provides tools for tracking analytics, reporting and fixing app crashes and many more.

Firebase cloud Firestore logo

Firebase provides 2 data types — Real time database and Cloud Firestore. Both are NoSQL databases. Real time database was the original database and cloud Firestore is the newest database of firebase. To know more about firebase databases click here.

What is google app script?

According to google developers community, Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace. It is based on JavaScript and provides built in libraries for google workspace applications.

It is used to add customized functions to google sheets, customized menus for google docs and forms, create stand alone web applications, interaction with other google services and much more. To know more head over to google app script documentation.

So for our purpose I am using a DHT22 temperature humidity sensor, a NodeMCU, and a bit of coding to achieve our goal.

STEP 1

Setup the connections of the sensor circuit as shown below

Note 1: The resistor need not be used if the dht22 module is used as it has inbuilt resistor.

STEP 2.1

Open Google Sheets and Create a new Spreadsheet.

STEP 2.2

Double click and rename the spreadsheet document. Also rename the sheet name. Here I have named my spreadsheet as ‘Temperature Humidity log’ and sheet name as ‘log’.

Note 2: Be sure to note down the spreadsheet id. We will need this later for setting up sheets using app script.

ID is the characters between ‘d/’ and ‘/edit’

STEP 2.3

Give the required headings for logging the values

STEP 2.4

Now open script editor from tools option and a window will be opened.

Now rename the script editor title with the same name as given for the spreadsheet

Now paste the code given here.

STEP 2.5

Publish the script as a web app by following the steps below

Now you will get a window where you can set the visibility of the web app. Choose the visibility to ‘Anyone, even anonymous’ and click deploy.

Now you will be prompted to authenticate your web application. Click on review permissions and select your account

Then click the advanced option and select ‘Go To (Your spreadsheet name)’.

Then click allow and proceed

Now you will have a window with your web app link. Copy this link which is of the form https://script.google.com/macros/s/gas id/exec

STEP 3:

Install the required libraries for configuring ide with the DHT22 sensor and connecting NodeMCU to the web. Copy paste the code for Arduino IDE from here.

DHT Sensor Libraries

Wi-Fi Libraries

Hope you enjoyed creating a new project which sends sensor data to the google sheets which acts as a mediator here. Follow the part 2 of this tutorial to learn about sending the data from google sheets to Firestore.

--

--