Hi,
I am working in a project in which i have to send ultrasonic sensor values to google sheet.Here i am using nodemcu. I have tried it by using google script. My coding is working fine.I am getting sensor values on serial monitor but it shows connecting failed and sensor values are not showing in google sheet.I referred this link Arduino: Log sensor data in the cloud - YouTube.
I paste my code below and also attached serial monitor output.
Thank you
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const int trigPin = 5;
const int echoPin = 4;
//height of the water tank in centimeter
const int height_tank=25;
int led=0;
// defines variables
int distance;
int duration;
const char* ssid = "xxxxx"; // name of your wifi network
const char* password = "xxxx"; // wifi pasword
const char* host = "script.google.com";
const int httpsPort = 443;
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
// SHA1 fingerprint of the certificate, don't care with your GAS service
const char* fingerprint = "46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6";
String GAS_ID = "AKfycbwTZAV3Vp2wME2LhiynXFHo4aHu_Bp6uch9kT84OdRgTyhvHYTP"; // Replace by your GAS service id !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(led,OUTPUT);//Set the Motor Relay Pin as an OUTPUT
Serial.begin(9600); // Starts the serial communication
//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());
}
void loop()
{
distance=ping();
int division_lenght=height_tank/5;
int level=distance/division_lenght;
level=5-level;
Serial.println(level);
Serial.println(distance);
if(level==1)
{
digitalWrite(led,HIGH);
Serial.println(" On");
}else if(level==2)
{
digitalWrite(led,HIGH);
Serial.println(" On");
}else if(level==3)
{
digitalWrite(led,HIGH);
Serial.println("On");
}else if(level==4)
{
digitalWrite(led,HIGH);
Serial.println("On");
}
else{
digitalWrite(led,LOW);
Serial.println("OFF");
}
delay(2000);
}
int ping(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
return distance;
}
// Function for Send data into Google Spreadsheet
void sendData( int dist)
{
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
String string_distance = String(dist, DEC);
String url = "/macros/s/" + GAS_ID + "/exec?distance=" + string_distance;
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");
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");
}