I'm having trouble getting my Arduino to query a device on my network. The client.connect
call will return a 0 after several seconds.
If I run curl
on my development machine - which is also connected to the same WiFi network - the call succeeds:
curl 192.168.7.198/api/v1/printer/status
"idle"
Here is the Arduino code I'm using, stripped down to just the WiFi parts. I am using an Arduino Nano 33 IoT.
#include <RTCZero.h>
#include <WiFiNINA.h>
#define SSIDNAME "REDACTED"
#define SSIDPASSWORD "REDACTED"
int status = WL_IDLE_STATUS;
WiFiSSLClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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");
}
connectToAP(); // Connect to Wifi Access Point
printWifiStatus();
}
void loop() {
Serial.println("[Printer] Getting printer status... (attempt 1)");
IPAddress server(192,168,7,198);
int val = client.connect(server, 80);
Serial.print("Client.connect returned: ");
Serial.println(val);
if (val > 0) {
Serial.println("connected to server");
client.println("GET /api/v1/printer/status HTTP/1.1");
client.println("Connection: close");
client.println("Accept: */*");
client.println();
} else {
Serial.println("[Printer] Printer request failed.");
}
Serial.println("[Printer] Sent printer request.");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(30000);
}
void printWifiStatus() {
Serial.print("[WiFi] SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("[WiFi] IP Address: ");
Serial.println(ip);
Serial.print("[WiFi] Firmware version: ");
Serial.println(WiFi.firmwareVersion());
}
void connectToAP() {
// Try to connect to Wifi network
while ( status != WL_CONNECTED) {
Serial.print("[WiFi] Attempting to connect to SSID: ");
Serial.println(SSIDNAME);
// Connect to WPA/WPA2 network
status = WiFi.begin(SSIDNAME, SSIDPASSWORD);
// wait 1 second for connection:
delay(1000);
Serial.println("[WiFi] Connected.");
}
}
And here is the output. Note it's saying I need to upgrade my firmware but I just upgraded to 1.3.0 from the Arduino IDE and nothing newer is available in the dropdown menu.
21:06:01.818 -> Please upgrade the firmware
21:06:01.818 -> [WiFi] Attempting to connect to SSID: BUSSNET
21:06:05.983 -> [WiFi] Connected.
21:06:05.983 -> [WiFi] SSID: BUSSNET
21:06:05.983 -> [WiFi] IP Address: 192.168.7.148
21:06:05.983 -> [WiFi] Firmware version: 1.3.0
21:06:05.983 -> [Printer] Getting printer status... (attempt 1)
21:06:34.447 -> Client.connect returned: 0
21:06:34.447 -> [Printer] Printer request failed.
21:06:34.447 -> [Printer] Sent printer request.
I'm at my wits end and any help you can provide would be much appreciated.