How to send sensor data to firebase Firestore using google app script — Part 1
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 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?
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.
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.
Open Google Sheets and Create a new Spreadsheet.
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.
Give the required headings for logging the values
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.
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
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
An Arduino library for the DHT series of low-cost temperature/humidity sensors. You can find DHT tutorials here…
Many small embedded systems exist to collect data from sensors, analyse the data, and either take an appropriate action…
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.