Hello! I'm using an Arduino Nano 33 IoT and am trying to make a GET request using a combination of the WiFiNINA library and the ArduinoHTTPClient Library (code posted below). For right now, I am just testing out one API path but will need to get a lot more later.
The issue I'm having is that the Status Code keeps returning "-2". The Serial Monitor looks like this:
Attempting to connect to WPA SSID: [WIFI]
You're connected to the networkSSID: [WIFI]
signal strength (RSSI):-47
Encryption Type:4
IP Address: [IP Address]
[IP Address]
making GET request
Status code: -2
Response:
Wait five seconds
making GET request
Status code: -2
Response:
Here is the code used:
/*
Simple GET client for ArduinoHttpClient library
Connects to server once every five seconds, sends a GET request
created 14 Feb 2016
modified 22 Jan 2019
by Tom Igoe
this example is in the public domain
*/
#include <ArduinoHttpClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS;
char serverAddress[] = "https://api.subwaynow.app/stops"; // server address
int port = 8080;
WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
void setup() {
Serial.begin(9600);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
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);
}
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
Serial.println("making GET request");
client.get("/D22");
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.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:
}
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:
// 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();
}