HttpClient Request Results in Status Code "-2"

Hi all

I am trying to send measuring data from my "Arduino Uni Wifi Rev. 2" over Wifi to a Web Server via HTTP Request, have been stuck for weeks with this issue now and would be insanely grateful for any help.

My Hardware:

  • Arduino Uni Wifi Rev. 2

My Libraries:

The Problem:
Every web request I send, no matter where (e.g. a Simple GET to google.ch or POST with data to grafana.net, which is my end goal), results in an empty response and error status code of -2. I can not figure out any details on what the cause for this error message is.

  • The Wifi Connection seems successful based on the logs, I can also visually see that it successfully connects to my iPhone Hotspot based on the blue indicator.
  • The request URL, Port, HTTP Post Types are correct and result in a valid response, I have tested that with Postman.

Serial Monitor Log Info:

1.4.3
** Scan Networks **
number of available networks:9
0) itsfrdm	Signal: -34 dBm	Encryption: WPA2
...
7) yts-32771	Signal: -92 dBm	Encryption: WPA2
WiFi Status - WL_IDLE_STATUS: 0
WiFi Status - WL_SCAN_COMPLETED: 2
Attempting to connect to WPA SSID: itsfrdm
WiFi Status - WL_SCAN_COMPLETED: 2
WiFi Status - WL_CONNECTED: 3
You're connected to the network
IP Address: 172.20.10.4
172.20.10.4
MAC address: 24:62:AB:B9:22:68
SSID: itsfrdm
BSSID: F2:7:97:47:70:A0
signal strength (RSSI):-30
Encryption Type:4

Initialize NTPClient to get time
DHTxx test!

!wifiClient.connected()
Humidity: 52.00%  Temperature: 22.00°C 71.60°F  Heat index: 21.61°C 70.90°F
1
### sendGoogleGET() ###
Sending GET...
Request Status - HTTP_ERROR_API: -2

!wifiClient.connected()
Humidity: 52.20%  Temperature: 22.00°C 71.60°F  Heat index: 21.62°C 70.91°F
1
### sendGoogleGET() ###
Sending GET...
Request Status - HTTP_ERROR_API: -2

Relevant Code:
main code file

#include <SPI.h>

#include <NTPClient.h>
#include <ArduinoHttpClient.h>

#include "DHT.h"

#include "config.h"
#include "util.h"
#include "wifi.h"

DHT dht(DHTPIN, DHTTYPE);

// NTP Client
WiFiUDP ntpUDP;
NTPClient ntpClient(ntpUDP);

// WiFi Client
WiFiClient wifiClient;

// HTTP Clients
HttpClient googleAPI = HttpClient(wifiClient, "http://google.ch", 80);

// HTTP Requests
void sendGoogleGET() {
  Serial.println(googleAPI);
  Serial.println("### sendGoogleGET() ###");

  // Submit POST request
  Serial.println("Sending GET...");

  googleAPI.get("/");
  
  int statusCodeG = googleAPI.responseStatusCode();
  String responseG = googleAPI.responseBody();
  
  printHTTPRequestStatus(statusCodeG);
  Serial.println(responseG);
}

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

  Serial.println(F("Attempting Wifi Connection!"));
  setupWiFi();

  Serial.println(F("Initialize NTPClient to get time"));
  ntpClient.begin();
  
  Serial.println(F("DHTxx test!"));
  dht.begin();
}

void loop() {
  if (wifiClient.available()) {
    char c = wifiClient.read();
    Serial.print(c);
  }

  if (!wifiClient.connected()) {
    Serial.println();
    Serial.println("!wifiClient.connected()");
    /*
    wifiClient.stop();
    for(;;)
      ;
    */
  }
  
  // Time Server - This one works!
  while (!ntpClient.update()) {
    yield();
    ntpClient.forceUpdate();
  }
  unsigned long ts = ntpClient.getEpochTime();

  // *** Here  I perform sensor readings...)

  sendGoogleGET();

  // Pause
  delay(2000);
}

wifi.h

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

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

  listNetworks();

  printWiFiStatus(status);
  printWiFiStatus(WiFi.status());

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);

    printWiFiStatus(WiFi.status());

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, password);
    delay(10000);
  }

  printWiFiStatus(WiFi.status());

  Serial.println("You're connected to the network");

  printWifiData();
  printCurrentNet();
}
1 Like

let the HttpClient object read the data from wifiClient. remove all code accessing wifiClient

Thanks for you answer!

Not sure what exactly I am supposed to change. Aren't I letting the HttpClient object read the data from wifiClient by passing it at the beginning of my main code file? And where in my code am I accessing wifiClient and should not do it?

// WiFi Client
WiFiClient wifiClient;

// HTTP Clients
HttpClient googleAPI = HttpClient(wifiClient, "http://google.ch", 80);
1 Like

see what you do in loop() as first

Aren’t I supposed to check the WiFi status? I tried commenting out that part but did not make a difference. Would really appreciate some context/more details in your answer, thanks!

if (wifiClient.available()) {
    char c = wifiClient.read();
    Serial.print(c);
  }

try to change

HttpClient googleAPI = HttpClient(wifiClient, "http://google.ch", 80);

to

HttpClient googleAPI = HttpClient(wifiClient, "google.ch", 80);
2 Likes

Yes, thanks, that was exactly it! :partying_face: Looks like HttpClient doesn't like the protocol then, makes sense when I think about it, as the Port is defined in the third parameter...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.