Esp8266IOT Projects

Sending Data from ESP8266 NodeMCU to Google Drive

Send Data from ESP8266  to Google Drive

Here, is a quick tutorial on sending data from ESP8266 NodeMCU and directly saving it to Google’s Spreadsheet in Google drive.

We will discuss three different code.

In the first code, data are sent to the google spreadsheet and are automatically updated in particular columns according to the tags defined in our code.

In the second code, we will attach a push button to nodemcu that will send the data whenever the button is pressed to the spreadsheet.

In our last code, we will use the DHT11 sensor, that can read Humidity and Temperature and will send that data to google drive in every few seconds interval(here, let it be like 5 seconds).

Components Required:

  • ESP8266 based dev boards ( Here, I am using NodeMCU 12E)
  • Breadboard
  • Jumper wires
  • Push Button
  • DHT11 Sensor ( optional for part three of this project)

Steps to follow :

Step1: Log in to your google drive, create a new Blank Spreadsheet and rename it to the name of your choice.

Step2: In the spreadsheet type the name of the columns in the first row, here I have written Date, Value1, and Value2 but you can enter any name of your choice. (Note: This header row name is very important for GAS and ESP8266 coding)

Step3: Then go to  Tools and select Script editor.

A new Google Apps Script project will be opened, rename it such as the name of your Spreadsheet.

Step4: Copy the code given below and then paste it into the code.js project. Here, Replace sheet_id by your Sheet ID.

To find Sheet ID, go to your google spreadsheet, which you have created in step 1 and in its  URL, copy the text marked below. This is your required sheet ID  and pastes it in the code.js project file.

//-----------------------------------------------
// Author: Trieu Le
// Email: lethanhtrieuk36@gmail.com
// Publish date: 07-Oct-2015
// Description: This code for demonstration send data from ESP8266 into Google Spreadsheet
// GET request syntax:
// https://script.google.com/macros/s/<gscript id>/exec?header_here=data_here
//-----------------------------------------------
/**
* Function doGet: Parse received data from GET request, 
  get and store data which is corresponding with header row in Google Spreadsheet
*/
function doGet(e) { 
  Logger.log( JSON.stringify(e) );  // view parameters
  var result = 'Ok'; // assume success
  if (e.parameter == 'undefined') {
    result = 'No Parameters';
  }
  else {
    var sheet_id = 'YOUR_GAS_ID_HERE'; 		// Spreadsheet ID
    var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();		// get Active sheet
    var newRow = sheet.getLastRow() + 1;						
    var rowData = [];
    rowData[0] = new Date(); 											// Timestamp in column A
    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 'Value1': //Parameter
          rowData[1] = value; //Value in column B
          result = 'Written on column B';
          break;
        case 'Value2': //Parameter
          rowData[2] = value; //Value in column C
          result += ' ,Written on column C';
          break;  
        default:
          result = "unsupported parameter";
      }
    }
    Logger.log(JSON.stringify(rowData));
    // Write new row below
    var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
    newRange.setValues([rowData]);
  }
  // Return result of operation
  return ContentService.createTextOutput(result);
}
/**
* Remove leading and trailing single or double quotes
*/
function stripQuotes( value ) {
  return value.replace(/^["']|['"]$/g, "");
}
//-----------------------------------------------
// End of file
//-----------------------------------------------

 

 

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close