I'm trying to connect to the nest_API (thermostat) using ESP8266 and the Arduino EDK. But so far with no result.
I've seen somebody asking the same question here before. But the answer to his problem didn't help me.
So here's my code:
Code
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "xxxxx";
const char* password = "xxxxx";
const char* host = "developer-api.nest.com";
const int httpsPort = 443;
const char* BearerKey = "xxxxxxxuB0QSbgw2nsT85dJEHRpwvR7rSyrLHm2E54QpC9vnSzB5PV8OtGDPm0mAh96wgM0MwApmS";
//declaring GPIO's
int gpio13Led = 13;
int gpio12Relay = 12;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "87:CB:F2:E6:44:C0:AA:F2:4C:28:B2:97:85:70:18:92:45:1B:A4:57";
void setup() {
// preparing GPIOs
pinMode(gpio13Led, OUTPUT);
digitalWrite(gpio13Led, HIGH);
pinMode(gpio12Relay, OUTPUT);
digitalWrite(gpio12Relay, HIGH);
Serial.begin(115200);
Serial.println();
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());
// 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");
digitalWrite(gpio13Led, LOW);
} 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" +
"Content-Type: application/json\r\n" +
"Authorization: Bearer " + BearerKey + "\r\n\r\n"
);
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
if (line == "\r") {
Serial.println("headers received");
break;
}
}
while (client.available()) {
String line = client.readStringUntil('\n');
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
}
void loop() {
}
I never get any result from the server. But when I'm using postman, it works. So I wonder. Do I need to use the "host: " + host + ... line?
And is there a way to catch some errors with the WiFiClientSecure.h library. I also once changed the url for host in the get string. Then I received one error from the server. I was already happy there was a response after all.
Serial output
connecting to xxxxx
........
WiFi connected
IP address:
xxxxx
connecting to developer-api.nest.com
certificate matches
requesting URL: /
request sent
HTTP/1.1 307 Temporary Redirect
Content-Type: application/json; charset=UTF-8
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, max-age=0
Pragma: no-cache
Location: https://firebase-apiserver10-tah01-iad01.dapi.production.nest.com:9553/
Connection: close
Authorization: Bearer xxxxx
content-length: 0
headers received
reply was:
==========
==========
closing connection
Now I'm completely stuck. It would be nice if somebody could help me further out. thx