0

Wio 终端对特定 API api.ambeedata.com 的 GET 请求没有响应。

代码修改自 Seeed 的示例:Reading Github Repository Stats from Wio Terminal。原始的 Github 示例运行良好。

修改代码以传递我的 API 密钥并使用 api.ambeedata.com 端点后,请求不会返回。但是,会建立与服务器的连接。

在 Postman 中使用相同的请求测试请求会返回正确的 JSON 响应。

Realtek RTL8720 固件为 2.1.2,所有依赖库均为最新版本。(Arduino FS、rpcUnified、rpcWiFi、SFUD、mbedtls)

使用不同的 Arduino 板,我复制了代码并从 api.ambeedata.com 获得了正确的 JSON 响应。任何有关下一步尝试的建议将不胜感激!

#include <rpcWiFi.h>
#include <WiFiClientSecure.h>

const char* ssid     = "SSID";     // your network SSID
const char* password = "password"; // your network password

const char* server = "api.ambeedata.com";  // Server URL
const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n"
                           //Removed for the brevity of the post
                           "-----END CERTIFICATE-----\n";

WiFiClientSecure client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial); // Wait for Serial to be ready
  delay(1000);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    Serial.println(WiFi.status());
    WiFi.begin(ssid, password);
    Serial.println(WiFi.status());
    Serial.print(".");
    // wait 3 seconds for re-trying
    delay(3000);
    }
  
  Serial.print("Connected to ");
  Serial.println(ssid);

  client.setCACert(test_root_ca);

  Serial.println("\nStarting connection to server...");
  
  if (!client.connect(server, 443)) {
    Serial.println("Connection failed!");
    } 
  else {
    Serial.println("Connected to server!");
     
  //Send GET request to API
    client.println("GET https://api.ambeedata.com HTTP/1.0");
    client.println("Host: api.ambeedata.com");
    client.println("Connection: close");
    client.println("x-api-key: apikey");
    client.println("Content-type: application/json");
    client.println();

    Serial.println("request sent");

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
  Serial.println("reply was:");
  Serial.println("==========");
  
  String line = client.readStringUntil('\n');
        if (line == "\r") {
            Serial.println("headers received");
        }
  
    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      if (c == '\n') {
        Serial.write('\r');
      }
      Serial.write(c);
    }
    client.stop();
  }
}

void loop() {
  // do nothing
}
4

0 回答 0