Status Code -2 On HTTP Request

I'm very new to Arduino ( 1 day in ) and it's 20 years since I wrote any C so go easy!
Using an MKR1000 WiFi I'm creating a sketch to ultimately send a message to an IFTTT webhook based on a change in a magnetic switch . However, in the interim, to get things working I'm just trying to send an HTTP POST to a RequestBin endpoint.
WiFi connects fine but whenever I make the HTTP request, I get status code -2 returned and no request hits RequestBin. Having hunted around on the internet for what -2 means I've come up empty.

This is what my code looks like:

#include "arduino_secrets.h"
#include <ArduinoHttpClient.h>
#include <WiFi101.h>

const char* serverName = "somesubdomain.m.pipedream.net";

const char* serverPath = "/";
const int port = 443;
int status = WL_IDLE_STATUS;
const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_PASS;    // Network password (use for WPA, or use as key for WEP)

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);

const int sensor = 4;
int state;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  pinMode(sensor, INPUT_PULLUP);
  
  while (status != WL_CONNECTED) {
    Serial.println("Attempting to connect to Network named: ");
    Serial.println(SSID);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(SSID, PASS);
  }
  
    // print the SSID of the network you're attached to:
  Serial.println("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.println("IP Address: ");
  Serial.println(ip);
}

void loop() {
  //Check WiFi connection status
  if(WiFi.status() == WL_CONNECTED){
    
    int newState = digitalRead(sensor);
    Serial.println(String("newState: " + state));

    if(state == HIGH) {
      Serial.println("HIGH");
    }
    if(state == LOW) {
      Serial.println("LOW");
    }
    
    if(state == HIGH && newState == LOW){
      Serial.println("Closing door");
      client.post(serverPath, "application/json", "{\"value1\": \"Opening door\"}");
       
      int statusCode = client.responseStatusCode();
      String response = client.responseBody();
      client.stop();
    
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("Response: ");
      Serial.println(response);
    
    } else if(state == LOW && newState == HIGH) {
      Serial.println("Opening door");      
      client.post(serverPath, "application/json", "{\"value1\": \"Opening door\"}");
     
      int statusCode = client.responseStatusCode();
      String response = client.responseBody();
      client.stop();
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("Response: ");
      Serial.println(response);
    }
    state = newState;
  }
  delay(5000);
}

As always, any help or pointers greatly appreciated

I did come across this:

Which states

This call was made when the HttpClient class wasn't expecting it to be called. Usually indicates your code is using the class incorrectly

However I'm really just following the example so not sure why it's returning that status

Further to this, I've changed the code to the more verbose way of specifying the request and I get the same issue. However, if I switch from WiFiSSLClient to WiFiClient I actually get a response from the endpoint complaining that I've sent HTTP and not HTTPS

400 The plain HTTP request was sent to HTTPS port

So it appears this could be related to the WiFiSSLClient or its interoperation with the ArduinoHttpClient

For reference, code looks like (ignore that lack of DRYness - I'm just hacking at the moment):

#include "arduino_secrets.h"
#include <ArduinoHttpClient.h>
#include <WiFi101.h>

const char serverName[] = "eo1buddftcclsc6.m.pipedream.net";
const char serverPath[] = "/";
const int port = 443;

int status = WL_IDLE_STATUS;
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);

const int sensor = 4;
const int STARTING_STATE = 42;
int state = STARTING_STATE;
String contentType = "application/json";

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  pinMode(sensor, INPUT_PULLUP);

  while (status != WL_CONNECTED) {
    Serial.println("Attempting to connect to Network named: ");
    Serial.println(SSID); // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(SSID, PASS);
  }

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

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.println("IP Address: ");
  Serial.println(ip);
}

void loop() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {

    int newState = digitalRead(sensor);
    Serial.println(String("newState: " + state));

    if (state == HIGH) {
      Serial.println("HIGH");
    }
    if (state == LOW) {
      Serial.println("LOW");
    }
    if (state != STARTING_STATE) {
      if (state == HIGH && newState == LOW) {
        String data = "{\"value1\": \"Closing door\"}";
        Serial.println("Closing door");
        client.beginRequest();
        client.post(serverPath);
        client.sendHeader("Content-Type", contentType);
        client.sendHeader("Content-Length", contentType.length());
        client.beginBody();
        client.print(data);
        client.endRequest();
        
        int statusCode = client.responseStatusCode();
        String response = client.responseBody();

        Serial.print("Status code: ");
        Serial.println(statusCode);
        Serial.print("Response: ");
        Serial.println(response);

      } else if (state == LOW && newState == HIGH) {
        Serial.println("Opening door");
        String data = "{\"value1\": \"Opening door\"}";
        Serial.println("Opening door");
        client.beginRequest();
        client.post(serverPath);
        client.sendHeader("Content-Type", contentType);
        client.sendHeader("Content-Length", contentType.length());
        client.beginBody();
        client.print(data);
        client.endRequest();
        
        int statusCode = client.responseStatusCode();
        String response = client.responseBody();
        
        Serial.print("Status code: ");
        Serial.println(statusCode);
        Serial.print("Response: ");
        Serial.println(response);
      }
    }
  state = newState;
  }
  
  delay(5000);
}

You can dig through the libraries that you use; grep / grepWin are you friends. Search all .h and .cpp files for -2 to see what shows up.

Hiya. Yeah, I found

This call was made when the HttpClient class wasn't expecting it to be called. Usually indicates your code is using the class incorrectly
static const int HTTP_ERROR_API =-2;

Which sadly isn't very helpful!

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