In this blog, let's send the readings of a temperature and humidity sensor to Google Sheets using an Adafruit Huzzah ESP8266 for interfacing and creating internet connection to send the data to its destination.
- Sending data to google sheet cell is much useful and basic way which saves the data in spreadsheets online.
- Instead of using any kind of special bridge like pushing a box or MQTT NODE RED or any other kind of REST API, Send your data by using google Apps script which easily receives the readings from the sensor by using script Links of google scripts, which we will be discussing further.
- The scripting is not only limited to google sheet, but as per till what I have learnt - Internet docs(Specially open source) or even to Internet Drives, Email IDs and many other platforms. Any kind of web-based application can be created using this basic start.
- No extra charges to be paid unlike the other online servers here and you can store the data up to 10 GB Maximum in the user account.
- One of the best efforts is you can learn about connecting the data to the internet with this kind of real-time application easily.
- We are going to use the 2 Wire connection(I2C) which performs the master-slave protocol.
- The I2C protocol platform connects the sensor hardware and works with 7bits or 10 bits addressing modes uses 2 wired pull up to transfer (SDA, SCL) the sensor 7 bits data frame.
Fetching the real-time readings of sensors interfacing with ESP8266 and send the data to different Cloud platforms is much simple.
We are going to use Wire.h library in Arduino IDE for creating the two wired communication between Adafruit Huzzah Board and SHT25 sensor I2C module and I2C cable.
Note:
To avoid the complex wiring structure, I am going to use the I2C adapter for Adafruit Huzzah designed to connect the I2C sensor.
For the Newbie to set up the Esp8266 yo must go through the ESP8266 setup.
First Initialize the libraries:
- I2C : #include <Wire.h>
- ESP8266 WiFi : #include <ESP8266WiFi.h>
- HTTPS WiFi Client : #include <WiFiClientSecure.h>
After Initializing libraries, We will be defining the I2C process use to fetch the sensor readings to convert them and shift the 8 bits data as per the requirements:
- Initialize the registers in two wire I2C protocol for the I2C sensor module
#define Addr 0x40
- Start I2C transmission and initialize the registers and request for 2 bytes data from where we will read the sensor data.
- If 2 bytes data will be available then read the sensor data and using mentioned below formulas we are converting the desired values
float humidity = (((data[0] * 256.0 + data[1]) * 125.0) / 65536.0) - 6;
float cTemp = (((data[0] * 256.0 + data[1]) * 175.72) / 65536.0) - 46.85;
float fTemp = (cTemp * 1.8) + 32;
- Print the values in a serial monitor screen
After the execution I2C modules, Let's learn about how to fetch the data by using WiFi libraries and send data to online host id using specific API key:
- Define WiFi credentials globally in ESP8266 which will help us to connect the board with internet
- As we are going to use the HTTP client and will be defining HTTPS = 443 protocol for securing the HTTP path as the script will work in the secure path only.
- Initialize the host details in code
const char* host = "script.google.com";const int httpsPort = 443; String SCRIPT_ID = "<GScript Link ID>";
Note:
Script ID is mentioned in “web apps URL” while Gscript code will be published, just copy and paste the id mentioned below in the topic about "automate the google sheet" and initialize in above commands
/s/<Script ID>/exec
- Initialize variable globally which fetch the data from the I2C module and send it to the URL script which will further send the data to the destination.
- Using ESP8266 WiFi Library we will be able to connect the board with internet
- The sensor data will be hosted to a local server after every 5s.
- With the help of URL script, the data will be hosted to google script published link Active page after every 15 seconds.
As we all have a Google account to sign in to google sheet with your account
- Mention the values which you need to get from the sensor connected with ESP8266
- Go to Tools>Script Editor
- In Script Editor page mention the name
- Use “doGet” function to receive the events
- In “doGet” function initialize the spreadsheet API key which connect the Active Sheet where you want to send the sensor values
- With the help of even function mentioned in the code, we will be able to represent the data in rows and columns easily.
- Finally, save the data and click on “Publish”>> Click “Deploy as web apps”
- Make sure whenever there will be any change to select the “project version” >> “New”>> press “update”
Current web app URL will appear as below:
https://script.google.com/macros/s/”<GScript API Key>”/exec:
Further used in ESP8266 code for fetching the data from sensors
Hosting ConnectionsWe are going to use the HTTPS Get request to connect the data to the host id mentioned in the gscript editor where we coded our data further to connect with google sheet.
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 = '<google spreadsheet Api Key>'; // Spreadsheet ID
var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
var newRow = sheet.getLastRow() + 1;
var rowData = [];
}
Logger.log(JSON.stringify(rowData)); // Write new row below
var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
newRange.setValues([rowData]);
}
Limitations- This project is only limited to store the data of I2C sensor in Google Sheets.
- We are using HTTPS GET request to receive the values through I2C functions.
- We have to change the value in string format and then send the data to gscript URL link.
- https://github.com/ControlEverythingCommunity/SHT25
- https://developers.google.com/apps-script/articles/tutorials
- https://ncd.io/thingspeak-weather-app-using-esp8266/
- http://pdacontrolen.com/update-https-redirect-version-2-0-esp8266-google-spreadsheets/
Always Give respect to the to the things which give you the urge to start research and pen down the work you have been doing to share with the world.
Will be looking for your comments. Have a great day! :-)
Comments