ESP8266 not connecting to script.google.com

Hello, many thanks for your insight.

I have a project with ESP8266

I am trying to log some sensor data from my Arduino + ultrasonic Sensor to google spreadsheet.

I’m using Arduino uno+ LOLIN(WEMOS) D1 R2 Mini Board as my ESP8266 wifi module.

I am using my mobile hotspot to connect to the internet but it keeps telling me “connection failed” meaning it doesn’t connect to script.google.com.

It connects to the internet and gives me an IP but doesn’t connect to script.google.com

Can anyone in the house look at my Arduino code and see if you can help me, please.

This is my arduino code

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiClient.h>
#include <Servo.h>  //servo library

#define trigPin A5
#define echoPin A4

Servo myservo;      



const char* ssid = "<my ssid>";    
const char* password = "<my password>";     
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 = "AKfycbwBPnixqq0_-wXyuMImLLKN0QvtnR3peWpFbifYFkWue_BagmrC";   // Replace by your GAS service id           !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

int trigPin = A5; 
int echoPin = A4; 
float pingTime;  
float targetDistance; 
float speedOfSound = 343; 

void setup()
{

  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  

  //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()
{
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2000); 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(15); 
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  
  pingTime = pingTime / 1000000; 
  targetDistance = speedOfSound * pingTime; 
  targetDistance = targetDistance / 2; 
  targetDistance = targetDistance * 100; 
  Serial.print("DATA,TIME,");
  Serial.println(targetDistance );
  delay(1000);
  sendData(targetDistance);

  delay(2000);
}

// Function for Send data into Google Spreadsheet
void sendData(float targetDistance)
{
  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(targetDistance, 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");
}



Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

You are attempting to connect HTTPS, do you have the correct CA certificate or fingerprint for your server you are making the request of?

If this is the issue, I would get everything working on port 80 first, then move on to your secure request after that.

@SwarlatDee, do not cross-post. Other thread removed.

@SwarlatDee, do not hijack. Do not cross-post. Other post removed.

I dont think th fingerprint is the problem here. As you can see from the code, the error is generated before the fingerprint is verified. You may try to include client.setInsecure();

If that gets it to work, you may still expect the error: "esp8266/Arduino CI has failed" just ignore that. The condition that error tests for is not correct

I had the same problem. You only have to downgrade the esp8266 version at 2.4.2 from the Board Manager

1 Like

hey, thanks man
I got it working on my nodemcu esp12e with certificate not matching but data uploading to google sheet.

no worries:) also in my project the certificate doesn’t match but it works in any case