Hye Hadi, thank you for the reply. I attach the full code down below as I couldn't answer most of the questions. For accelerometer part, I take the code from Adafruit MPU6050 library > basic reading.
//-----------------------------------------------
// Author: Trieu Le
// Email: lethanhtrieuk36@gmail.com
// Publish date: 29-Oct-2017
// Description: This code for demonstration send data from ESP8266 into Google Spreadsheet
// Modifyed by Moz for Youtube changel logMaker360 for this video: https://youtu.be/fS0GeaOkNRw 24-02-2018
// update ssid, password and GAS_ID
//-----------------------------------------------
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
const char* ssid = "ssid";
const char* password = "pw";
const char* host = "script.google.com";
const int httpsPort = 443; //usually https port uses 443
// Use WiFiClientSecure class to create TLS connection, or create a wifiClientSecure object
WiFiClientSecure client;
String GAS_ID = "GAS service id";
void setup()
{
mpu.begin(); // sensor
/*
Serial.begin(115200); //Serial
Serial.println();
*/
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G: //RANGE_2_G: or 4_G: or 8_G: or 16_G:
Serial.println("+-2G"); //println("+-2G"); or ("+-4G"); or ("+-8G"); or ("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG: //RANGE_250_DEG: or 500_DEG: or 1000_DEG: or 2000_DEG:
Serial.println("+- 250 deg/s"); //println("+- 250 deg/s"); or +- 500 deg/s or +- 1000 deg/s or +- 2000 deg/s
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ: //BAND_260_HZ: or 184_HZ: or 94_HZ: or 44_HZ: or 21_HZ:
Serial.println("260 Hz"); //println("260 Hz"); or 184 Hz or 94 Hz or 44 Hz or 21 Hz or 5 Hz
break;
}
Serial.println("");
delay(100);
//connecting to internet
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setInsecure();
void loop()
{
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
/* Print out the values */
Serial.print("Acceleration_X: "); //try with x-axis only first
Serial.print(a.acceleration.x);
/*Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);*/
Serial.println(" m/s^2");
Serial.print("Rotation_X: ");
Serial.print(g.gyro.x);
/*Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);*/
Serial.println(" rad/s");
float it = a.acceleration.x;
float ih = g.gyro.x;
/* Serial.print("try it: ");
Serial.print(it);
//ih = (int) h;*/
Serial.println("");
{ sendData(it, ih);
delay(500);
// Function for Send data into Google Spreadsheet
// void sendData(int accX, int gyroX) ----> original
void sendData(float accX, float gyroX)}
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String string_Acceleration_X = String(accX, DEC);
String string_Rotation_X = String(gyroX, DEC);
//String url = "/macros/s/" + GAS_ID + "/exec?Acceleration_X=" + string_Acceleration_X + "&Rotation_X=" + string_Rotation_X;
Serial.print("requesting URL: ");
//Serial.println(url);
client. Print(String("GET ") + url + " HTTPS/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
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.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
}
I just started in Arduino and programming. So I just followed the tutorial video from YT
and here: Google Spreadsheet or Google Sheets with ESP8266 Nodemcu for Data Logging
and download the codes from the description of the video.
Meanwhile, the google script is as follows:
//-----------------------------------------------
// 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
// https://script.google.com/macros/s/Gas_ID/exec?Acceleration_X=3&Rotation_X=5
// Error in executes run script. document here :https://developers.google.com/apps-script/api/how-tos/execute
// Modifyed by Moz for Youtube changel logMaker360 for this video: https://youtu.be/fS0GeaOkNRw 24-02-2018
//-----------------------------------------------
/**
* 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 = 'Spreadsheet ID'; // 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 'Acceleration_X': //Parameter
rowData[1] = value; //Value in column B
result = 'Written on column B';
break;
case 'Rotation_X': //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
//-----------------------------------------------
/*
void setup()
void loop()
*/