Google Spreadsheet or Google Sheets with ESP8266 Nodemcu for Data Logging
ESP8266 and Google Spreadsheet
Table of Contents
Google Spread Sheet with ESP8266, Description:
Google Spreadsheet or Google Sheets with ESP8266 Nodemcu for Data Logging, Temp & Hum DHT11- So far I have covered majority of the IoT platforms for monitoring different types of sensors and for controlling different types of electrical devices. Usually for the real-time sensors monitoring I display the sensors values on the Gauges and for this I mostly use Blynk IoT platform, Ubidots, and Thingspeak etc. In majority of the cases only displaying the sensor values is not enough. There are guys who are interested in the data logging for the post analysis. The sensors data logging service offered by majority of the IoT platforms isn’t as impressive as offered by the Google which is absolutely free, highly secured, fast, and reliable. In this article, I will be using Nodemcu ESP8266 WiFi module with Google Sheets for the sensor data logging. Google Sheets is a spreadsheet program included as part of the free, web-based Google Docs Editors suite offered by Google.
Read my latest Article on How to send data from Google Spreadsheet to ESP8266 or ESP32.
Since this is my first time using the Google sheets for the Sensors data logging; so, I will try my level best to explain each and every detail. I will be using DHT11 Temperature and Humidity sensor as this is the most basic and easily available sensor. Once you learn how to send the temperature and humidity values to the Google Spreadsheet then you can replace this sensor with any other sensor of your choice. So, before, I am going to explain the circuit diagram, programming, and how to get started with the Google sheets, first let me explain; how this projects works.
I am using my homemade 4S lithium Ion battery pack for powering up my Nodemcu ESP8266 Development board. Read my article on how to make 3S and 4S lithium Ion batteries pack, if you want to make the same battery pack. As soon as I powered my Nodemcu ESP8266 module, after a few seconds I started to receive the data. The temperature and humidity values are stored along with the date and time information. For the demonstration purposes I am using a short delay in the programming, if you want you can increase the delay time. It’s totally up to you whether you want to send data after every 1 minute or 10 minutes, or 1 hour.
Anyways, you can also open the Google spreadsheet using your laptop or computer; again it’s up to you whether you want to use your computer or your cell phone for the monitoring and post analysis. Since this is an IoT based project so, you can monitor the temperature and humidity values from anywhere in the world. One thing that I really liked about the Google sheets is that; if you share the same Google account with multiple users then all the users can monitor the same sensor from anywhere in the world.
As this is a getting started tutorial, that’s why I only started with the DHT11 temperature and Humidity sensor, if you want you can use multiple sensors, just go ahead and give it a try and if you face any issues then let me know in a comment. Anyways, now you have got the idea of what exactly you are going to learn after reading this article. Without any further delay, let’s get started!!!
Amazon Links:
DHT11 Temperature and Humidity Module:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
ESP8266 NodeMCU:
ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.
ESP8266 has powerful on-board processing and storage capabilities that allow it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, and the entire solution, including front-end module, is designed to occupy minimal PCB area.
Specifications:
Serial/UART baud rate: 115200 bps
Input power: 3.3V
I/O voltage tolerance: 3.6V Max
Flash Memory Size: 1MB (8Mbit)
WiFi security modes: WPA, WPA2
DHT11 Sensor:
- DHT11 digital temperature and humidity sensor is a digital signal output with a calibrated temperature and humidity combined sensor.
- It uses a dedicated digital modules and acquisition of temperature and humidity sensor technology to ensure that products with high reliability and excellent long term stability.
- Sensor consists of a resistive element and a sense of wet NTC temperature measurement devices, and with a high-performance 8-bit microcontroller connected.
- The product has excellent quality, fast response, anti-interference ability, high cost and other advantages.
The single-wire wiring scheme makes it easy to be integrated to other applications and the simple communication protocol greatly reduces the programming effort required.
Nodemcu ESP8266 and DHT11 Circuit Diagram:
If you are just getting started with the Nodemcu ESP8266 and DHT11 Temperature and Humidity sensor then these are the minimal connections which you will need for this project. You can power up your Nodemcu module using your laptop. Connect the VCC and GND pins of the DHT11 sensor with the 3.3V and GND pins of the Nodemcu Module; and connect the Data pin of the dht11 sensor with the digital pin D1 of the Nodemcu module. Don’t forget to add a 10k ohm resistor between the VCC and Data pins of the DHT11 sensor. Now, let’s take a look at the other circuit diagram.
Nodemcu esp8266 power supply Circuit Diagram:
This circuit diagram is for those guys who want to power up the Nodemcu module using an external power supply. As you can see the DHT11 sensor connections with the Nodemcu ESP8266 WiFi module remains exactly the same. On the left side is the regulated 5v power supply which is based on the 7805 voltage regulator. J1 is the DC female power jack and this is where we connect a 12v adaptor, battery, or a solar panel. Don’t forget to add these 470uF decoupling capacitors.
Here is my Nodemcu ESP8266 WiFi module based development board which I use for testing my IoT based projects. If you want to make the same development board then you can read my previous article. Anyways, I interfaced my DHT11 sensor with the Nodemcu module as per the circuit diagram. My connections are completed and now let’s start with the Google sheets, there is nothing complicated just follow the same exact steps.
Creating Google Spreadsheet:
First of all we will create the Google Spreadsheet for which we will click on the Google apps and in which we will click on the Sheets.
After that we will click on the blank in order to create a new spreadsheet.
After that we will give the title name of the project and then in columns we will write the date, time, temperature, and humidity.
In the date column which is “A” we will receive the date, in column “B” we will receive the time, in column “C” we will receive the temperature, and in the column “D” we will receive the humidity. After that we will open the script editor in the previous versions of spread sheet it was found in the tools but in the new versions of the spread sheet it is in the extensions with the name Apps Script click on it.
After that we will click on the untitled project and give the name to the project which is DHT_monitoring and click on the ok.
After that paste the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function doGet(e) { Logger.log( JSON.stringify(e) ); var result = 'Ok'; if (e.parameter == 'undefined') { result = 'No Parameters'; } else { var sheet_id = ''; // Spreadsheet ID var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet(); var newRow = sheet.getLastRow() + 1; var rowData = []; var Curr_Date = new Date(); rowData[0] = Curr_Date; // Date in column A var Curr_Time = Utilities.formatDate(Curr_Date, "Asia/", 'HH:mm:ss'); rowData[1] = Curr_Time; // Time in column B for (var param in e.parameter) { Logger.log('In for loop, param=' + param); var value = stripQuotes(e.parameter[param]); Logger.log(param + ':' + e.parameter[param]); switch (param) { case 'temperature': rowData[2] = value; // Temperature in column C result = 'Temperature Written on column C'; break; case 'humidity': rowData[3] = value; // Humidity in column D result += ' ,Humidity Written on column D'; break; default: result = "unsupported parameter"; } } Logger.log(JSON.stringify(rowData)); var newRange = sheet.getRange(newRow, 1, 1, rowData.length); newRange.setValues([rowData]); } return ContentService.createTextOutput(result); } function stripQuotes( value ) { return value.replace(/^["']|['"]$/g, ""); } |
In the above code we will make several changes in the var_sheet id we will paste the code copied from the spread sheet main page which is shown below in the diagram.
We can change the time by changing the command
1 |
varCurr_Time = Utilities.formatDate(Curr_Date, "Asia/Karachi", 'HH:mm:ss'); |
As the Pakistan time is based on the Karachi so we will write Asia/Karachi. If you are living in India then you will write Asia/New Dehli. You can search the time zone of your country and replace it. After that we will click on the publish and then click on the deploy as web.
Then we will select the “anyone even anonymous” and click on the Deploy.
Then click on the review permission
Then choose the account the g-mail account with which you have created the spread sheet. After that we will click on the advance.
Then click on the go to dht_monitoring
After that we will click on the allow button.
After that we will copy the script url and click on the ok.
Now the process of the Google spreadsheet is completed and we will open the Arduino IDE. If this is your first time using the Nodemcu ESP8266 WiFi module then you will need to install the Nodemcu board. I have a very detailed article on how to install the Nodemcu board. You will also need to install the DHT11 library for this click on the Sketch Menu then go to the Include Library and then click on the Manage Libraries. Type dht in the search box and then install the dht sensor library.
Google Spreadsheet Nodemcu ESP8266 Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include "DHT.h" #define DHTTYPE DHT11 // type of the temperature sensor const int DHTPin = 5; //--> The pin used for the DHT11 sensor is Pin D1 = GPIO5 DHT dht(DHTPin, DHTTYPE); //--> Initialize DHT sensor, DHT dht(Pin_used, Type_of_DHT_Sensor); #define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router const char* ssid = "AndroidAP3DEC"; //--> Your wifi name or SSID. const char* password = "123456"; //--> Your wifi password. //----------------------------------------Host & httpsPort const char* host = "script.google.com"; const int httpsPort = 443; //---------------------------------------- WiFiClientSecure client; //--> Create a WiFiClientSecure object. String GAS_ID = "AKfycby8Lfzwu-5SBj9tHxbw3_j8ZPZy6P6_3e_BBISC0Jg7SstXLUed"; //--> spreadsheet script ID void setup() { // put your setup code here, to run once: Serial.begin(115200); delay(500); dht.begin(); //--> Start reading DHT11 sensors delay(500); WiFi.begin(ssid, password); //--> Connect to your WiFi router Serial.println(""); pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board //----------------------------------------Wait for connection Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router. digitalWrite(ON_Board_LED, LOW); delay(250); digitalWrite(ON_Board_LED, HIGH); delay(250); //---------------------------------------- } //---------------------------------------- digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router. Serial.println(""); Serial.print("Successfully connected to : "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.println(); //---------------------------------------- client.setInsecure(); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) int h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor !"); delay(500); return; } String Temp = "Temperature : " + String(t) + " °C"; String Humi = "Humidity : " + String(h) + " %"; Serial.println(Temp); Serial.println(Humi); sendData(t, h); //--> Calls the sendData Subroutine } // Subroutine for sending data to Google Sheets void sendData(float tem, int hum) { Serial.println("=========="); Serial.print("connecting to "); Serial.println(host); //----------------------------------------Connect to Google host if (!client.connect(host, httpsPort)) { Serial.println("connection failed"); return; } //---------------------------------------- //----------------------------------------Processing data and sending data String string_temperature = String(tem); // String string_temperature = String(tem, DEC); String string_humidity = String(hum, DEC); String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity; Serial.print("requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: BuildFailureDetectorESP8266\r\n" + "Connection: close\r\n\r\n"); Serial.println("request sent"); //---------------------------------------- //----------------------------------------Checking whether the data was sent successfully or not while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("headers received"); break; } } String line = client.readStringUntil('\n'); if (line.startsWith("{\"state\":\"success\"")) { Serial.println("esp8266/Arduino CI successfull!"); } else { Serial.println("esp8266/Arduino CI has failed"); } Serial.print("reply was : "); Serial.println(line); Serial.println("closing connection"); Serial.println("=========="); Serial.println(); //---------------------------------------- } //============================================================================== |
Upload the above. Power up your Nodemcu ESP8266 module, next open your spreadsheet App on your cell phone or computer and you should be able to receive the temperature and humidity values along with the date and time information.
For detailed explanation and practical demonstration watch video given below.
Watch Video Tutorial:
In the code for google script,there are many misleadingly concatenated words like dofunctionget, varsheet etc. Would be nice if the script worked out of box. Otherwise, great article.
sorry, that was the admin mistake. I reuploaded the code.
Great work Engr… all the best!!
Thank you.
hello there,
how to add moisture and ultrasonic sensor along with this code..
thanks in advance.
do you know how to read data from gg_sheets. thanhs you!
hello. do you know how to read data from google sheets. thanks!
I have done it. Very soon i will publish the article and Video.
I get this error when I tried to run the script
“TypeError: Cannot read property ‘parameter’ of undefined
doGet @ Code.gs:4”
please help
Not able to save Humidity value in column D. How to do that ?
Great tutorial! Thank you for putting it together! I have modified parts of it to send RFID data from an RC522 to google sheets, but I keep getting the “esp8266/Arduino CI has failed” error. Any insight as to what usually causes that?
I am getting same thing. Can you please help me in this
same issue here, i am using a esp32
@Sohail or @Suvam, did you find a solution? @Engr Fahad, any help?
Thanks for the nice and great tutorials.
great article.
I am from INDIA, i have tried this tutorial, working fine.
Thank you
I’m having a problem in the code. Ca you please help me out. I am from India too
I am having a problem that the code is not being able to fetch the headers but it is reding the data sucessfully but data is not going to sheet
Excellent tutorial, I followed each step and received all values, but when i added more variables upto seven, google sheet only displayed five of them, can you please tell me about this problem?
Initially got a “Cache-Control: private, max-age=0” error and then a “esp8266/Arduino CI has failed” error. never uploaded any data
“TypeError: Cannot read property ‘parameter’ of undefined
doGet @ Code.gs:4”
how to solve it ?
hello i am using the Arduino uno and sim800l GSM module to send data on google sheet using the GPRS how to send can anyone help me thank you ?