【问题标题】:Calling nest api with esp8266 using arduinoEDK使用 arduinoEDK 使用 esp8266 调用嵌套 API
【发布时间】:2018-02-12 21:04:38
【问题描述】:

我正在尝试使用 ESP8266 和 Arduino EDK 连接到 nest_API(恒温器)。但到目前为止还没有结果。

我以前在这里看到有人问过同样的问题。但是他的问题的答案对我没有帮助。

这是我的代码:

代码

#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() {
}   

我从来没有从服务器得到任何结果。但是当我使用邮递员时,它可以工作。所以我想知道。我需要使用 "host:" + host + ... 行吗?

有没有办法用 WiFiClientSecure.h 库捕获一些错误。 我也曾经在获取字符串中更改了主机的 url。 然后我从服务器收到一个错误。毕竟有回应我已经很高兴了。

串行输出

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

现在我完全被困住了。如果有人可以进一步帮助我,那就太好了。谢谢

【问题讨论】:

  • 串行监视器中究竟显示了什么?
  • @gre_gor 当我禁用最后一个 while() 时,我得到了这个结果:连接到 xxxxx ...... WiFi 连接的 IP 地址:192.xxx.x.xxx 连接到开发人员- api.nest.com 证书匹配请求 URL:/ request sent headers received 回复是: ===== ===== 关闭连接 能够看到服务器给我的错误会很高兴。也可能是我收到的 JSON 代码位只是没有看到吗?
  • 将其添加到问题中。在 cmets 内几乎不可读。并打印出标题。
  • 你是绝对正确的。我的问题已被编辑
  • 返回的标头是什么?

标签: api arduino arduino-esp8266 nest-thermostat


【解决方案1】:

哇。我的天啊!我有回应!你的小费使它工作@gre_gor。

我已经更改了主机名和端口,因为我的回复是这样说的。

这是我的最终代码:

代码:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "xxxxx";
const char* password = "xxxxx";

const char* host = "firebase-apiserver10-tah01-iad01.dapi.production.nest.com";
const int httpsPort = 9553; //443;
const char* BearerKey = "xxxxx";

//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() {
}

串口监视器:

connecting to xxxxx
...........
WiFi connected
IP address: 
xxxxx
connecting to firebase-apiserver10-tah01-iad01.dapi.production.nest.com
certificate doesn't match
requesting URL: /
request sent
HTTP/1.1 200 OK

Content-Type: application/json; charset=UTF-8

Access-Control-Allow-Origin: *

Cache-Control: private, no-cache, no-store, max-age=0

Pragma: no-cache

Connection: close

content-length: 8060



headers received
reply was:
==========
{"devices":{"thermostats":{"exxx":{"humidity":40,"locale":"nl-NL","temperature_scale":"C","is_using_emergency_heat":false,"has_fan":false,"software_version":"5.6.6-4","has_leaf":true,"where_id":"YygkopgUUc_cIFnCNG7GRDIIQnENO0ScVx9Pa78qfG7XyH-9WDdVgA","device_id":"e97ayjdpIOkPa4vZFxHggZMXiHKfhsyU","name":"Downstairs","can_heat":true,"can_cool":false,"target_temperature_c":15.0,"target_temperature_f":59,"target_temperature_high_c":24.0,"target_temperature_high_f":75,"target_temperature_low_c":20.0,"target_temperature_low_f":68,"ambient_temperature_c":15.5,"ambient_temperature_f":61,"away_temperature_high_c":24.0,"away_temperature_high_f":76,"away_temperature_low_c":8.5,"away_temperature_low_f":48,"eco_temperature_high_c":24.0,"eco_temperature_high_f":76,"eco_temperature_low_c":8.5,"eco_temperature_low_f":48,"is_locked":false,"locked_temp_min_c":20.0,"locked_temp_min_f":68,"locked_temp_max_c":22.0,"locked_temp_max_f":72,"sunlight_correction_active":false,"sunlight_correction_enabled":true,"structure_id":"vEJb634MNif-xxx{"access_token":"xxxx","client_version":2,"user_id":"xxxx"}}
==========
closing connection

奇怪的是我的指纹不匹配,但它仍然继续给我信息。

下一步是读取此响应并使用我想要的数据。 :-)

【讨论】:

  • 你将如何发布 Json 数据?
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多