0

我正在尝试使用 WiFiNINA WiFiClient 或 ArduinoHttpClient 执行 GET 请求,但无法从任何一个获得响应(状态代码 -2 服务器错误我返回)。我正在使用的代码尝试使用 WiFiClient 和 ArduinoHttpClient 执行 GET 请求,如下所示:

#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>

#include "arduino_secrets.h" 

char ssid[] = SECRET_SSID;       // network SSID (name)
char pass[] = SECRET_PASS;       // network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status


// Optional: use the numeric IP instead of the name for the server:
//IPAddress serverAddress(192,168,0,0);  // numeric IP 
char serverAddress[] = "https://webhooks.mongodb-realm.com/api/client/v2.0/app/*/*/*/incoming_webhook";    // name address (using DNS)

WiFiClient wifiClient;
HttpClient httpClient = HttpClient(wifiClient, serverAddress);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // Connected:
  Serial.print("Connected to wifi");
  printCurrentNet();
  printWifiData();

  Serial.println("\nStarting connection to server...");

  // if you get a connection, report back via serial:
  if (wifiClient.connect(serverAddress, 8080)) {
    Serial.println("connected to server");
    
    // Make a HTTP request:
    wifiClient.println("GET /getPatients");
    wifiClient.println("Host: https://webhooks.mongodb-realm.com/api/client/v2.0/app/*/*/*/incoming_webhook");
    wifiClient.println("Connection: close");
    wifiClient.println();
  }

}

void loop() {
  // if there are incoming bytes available

  // from the server, read them and print them:
  while (wifiClient.available()) {
    char c = wifiClient.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!wifiClient.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    wifiClient.stop();
  }
  
  Serial.println("making GET request");
  httpClient.get("/getPatients");

  // read the status code and body of the response
  int statusCode = httpClient.responseStatusCode();
  String response = httpClient.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
  Serial.println("Wait five seconds");
  delay(5000);
}

void printWifiData() {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  printMacAddress(mac);
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  printMacAddress(bssid);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}

更多详情:

  • 我正在使用 Arduino WiFi Rev 2。
  • WiFiNINA 代码来自这里
  • ArduinoHttpClient代码来自这里

采取的调试步骤

  • 我确定我已连接到 WiFi。
  • 我确信 webhook 可以工作,正如使用 Postman 所证明的那样

Webhook 上的 Postman GET 请求

一个潜在的问题是 WiFi 客户端强制一个端口进入,但 webhook 没有 - 这是否意味着它不可能在 webhook 上执行 GET 请求?或者是否有我可以用于 MongoDB webhook 的端口?

有没有办法在不指定端口的情况下进行 GET 请求?如上面的代码所示,我尝试使用没有端口的 HttpClient,但没有运气。

任何帮助将不胜感激!

4

0 回答 0