-->
Page 1 of 2

WiFiClientSecure / google problem?

PostPosted: Thu Mar 24, 2016 2:41 pm
by nikant
Well.. if someone could offer some enlightenment.. can't find the error in the following (modified host from the original of course..)

It hangs at client.connect for some time.. and then connection fails.
the same happens in "google.com" or "www.google.com"

other hosts work and reply normally

Is it my network?

Code: Select all/*
 *  HTTP over TLS (HTTPS) example sketch
 *
 *  This example demonstrates how to use
 *  WiFiClientSecure class to access HTTPS API.
 *  We fetch and display the status of
 *  esp8266/Arduino project continuous integration
 *  build.
 *
 *  Created by Ivan Grokhotkov, 2015.
 *  This example is in public domain.
 */

#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid = ".....";
const char* password = ".........";

const char* host = "calendar.google.com";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
const char* fingerprint = "99 70 D6 7C B8 42 71 18 DC A6 88 DB DC C8 69 66 96 9D 51 88"; //google

void setup() {
  Serial.begin(57600);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  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());
 
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  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 url = "/";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\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');

  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}



output is

Code: Select allconnecting to ssid
..
WiFi connected
IP address:
192.168.1.11
connecting to calendar.google.com
connection failed

Re: WiFiClientSecure / google problem?

PostPosted: Tue Mar 29, 2016 1:10 am
by electronicsguy
Did you get a solution to this problem?

Re: WiFiClientSecure / google problem?

PostPosted: Tue Mar 29, 2016 7:46 am
by danbicks
This is very interesting, are you trying to receive calendar events for a given day and time?

Please keep us posted on your progress.

Cheers

Dans

Re: WiFiClientSecure / google problem?

PostPosted: Tue Mar 29, 2016 10:44 am
by nikant
danbicks wrote:This is very interesting, are you trying to receive calendar events for a given day and time?

Please keep us posted on your progress.

Cheers

Dans


Yes.. :)

I'm working on a project (and hopefully I'll post here in a few days) and was thinking of working with Google calendar.
Unfortunately https connections to Google have proven very unreliable and also there seems to be some bugs in the ESP/Arduino code.
I've reported it here https://github.com/esp8266/Arduino/issues/1816

Connections are unreliable because: 1. Google changes its SHA1 certificate depending on the server you hit..
2. resolving calendar.google.com goes to a pool of servers (so not one known IP) or even to cache.google.com sometimes.. and returns a "connection refused"