I am trying to do a GET request using the WiFiNINA WiFiClient or the ArduinoHttpClient, but cannot get a response from either (status code -2 Server Error is returned). The code I'm using, which attempts to do the GET request with the WiFiClient AND the ArduinoHttpClient is given below:
#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();
}
For more detail:
- I am using an Arduino WiFi Rev 2.
- The WiFiNINA code is sourced from the Arduino WifiNINA documentation
- The ArduinoHttpClient Code is sourced from here
Debugging steps taken
- I am certain that I am connected to the WiFi.
- I am certain that the webhook works, as proven using Postman
A potential issue is that the WiFi client forces one to enter a port, but the webhook does not have one - does this mean its impossible to do a GET request on a webhook? Or is there a port I can use for MongoDB webhooks?
Is there a way to do a GET request without specifying a port? I tried using the HttpClient without a port as seen in the code above, but with no luck.
Any help would be kindly appreciated!