arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

IFTTT POST call failing with Return Code = -2 with WiFiSSLClient #62

Closed adriens closed 5 years ago

adriens commented 5 years ago

Hi,

Your library perfectly matches my needs to POST on IFTTT, I'm connecting thtrough WiFiSSLClient and have imported certificates :

2019-04-14 14_56_44-WiFi101 _ WiFiNINA Firmware_Certificates Updater

I can successfuly post from curl but no way to make it run from mkr1000, always returning -2 return code :

SSID: BABYLON
IP Address: 192.168.1.32
making POST request
Status code: -2
Response: 
Wait five seconds
making POST request
char serverAddress[] = HOSTIFTTT;  // server address
int port = 443;

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

Here is the loop source code in case it can help :

void loop() {
  Serial.println("making POST request");
  String contentType = "application/json";
  String postData = "{\"key1\": \"toto\"}";

  client.post("/trigger/test/with/key/MYPRIVATEKEY", contentType, postData);

  // 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);
}

I just can't figure out what's wrong with my code 😿

ANy help would be greatly appreciated 🙏

adriens commented 5 years ago

According to source code, I'm not using the library the good way :

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

Here is the full sketch :

/*
  Simple POST client for ArduinoHttpClient library
  Connects to server once every five seconds, sends a POST request
  and a request body

  created 14 Feb 2016
  modified 22 Jan 2019
  by Tom Igoe

  this example is in the public domain
 */
#include <ArduinoHttpClient.h>
#include <WiFi101.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;

char serverAddress[] = HOSTIFTTT;  // server address
int port = 443;

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("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.print("SSID: ");
  Serial.println(WiFi.SSID());

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

void loop() {
  Serial.println("making POST request");
  String contentType = "application/json";
  String postData = "{\"key1\": \"toto\"}";

  client.post("/trigger/test/with/key/MYPRIVATEKEY", contentType, postData);

  // 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);
}
adriens commented 5 years ago

Same behavior with DweetPost code

adriens commented 5 years ago

Maybe is it linked to https://github.com/arduino-libraries/ArduinoHttpClient/issues/29 ?

adriens commented 5 years ago

ALso, my server address is

#define HOSTIFTTT "maker.ifttt.com"

facchinm commented 5 years ago

@Rocketct can you try to reproduce the issue?

adriens commented 5 years ago

I can play any code you want also on my side.

adriens commented 5 years ago

Here is the curl command :

2019-04-15 20_13_48-MINGW64__c_Users_3004SAL

adriens commented 5 years ago

Hi guys, any chance to get a hint for this week-end ? 🙏

mceraso commented 5 years ago

I dont know if this helps but see if removing the "{" and "}" from postData helps.. for some reason it stopped my application from posting properly.. from what i understand you're telling the server to parse your data as a JSON with the content-type header field so you dont need to format the data with the JSON curly braces

adriens commented 5 years ago

Hi @mceraso , thanks for the idea. Unfortulanely, it did not work 😿 Do you have any running code snippet that run on your side ?

adriens commented 5 years ago

In the README I can see :

means it will work with WiFiClient, EthernetClient and GSMClient.

... so, does it support WiFiSSLClient

adriens commented 5 years ago

Found this : https://github.com/arduino-libraries/ArduinoHttpClient/issues/56

adriens commented 5 years ago

And this : https://github.com/arduino-libraries/ArduinoHttpClient/issues/13

adriens commented 5 years ago

It seems like it does but no way to make it run actually...

mceraso commented 5 years ago

Here's the code that works for me - i'm posting a matrix of data to an endpoint. I realize that I don't know about SSL and dont think that I'm using it. I'm using WifiNINA to create the client

/*
  Simple POST client for ArduinoHttpClient library
  Connects to server once every five seconds, sends a POST request
  and a request body

  created 14 Feb 2016
  modified 22 Jan 2019
  by Tom Igoe

  this example is in the public domain
 */
#include <ArduinoHttpClient.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;

char serverAddress[] = "xxx.xxx.x.x";  // server address
int port = 3000;
const int data_len = 10;               // number of data points to test
unsigned long raw_data[data_len][2];   // initialize matrix of data

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

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

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

    //Initialize raw_data
  for (int i = 0; i < data_len; i++) {
    raw_data[i][0] = 100 * i; //1pix/10ms*1000ms/1s //test the timing of this to better understand
    raw_data[i][1] = i;
  }

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

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

void loop() {
  Serial.println("making POST request");
  String contentType = "application/json";
  String postData = "[";                    //don't use JSON {} format. contentType tells the API to parse your txt as JSON
  for (int i = 0; i < data_len; i++) {
      postData += "[";
      postData += raw_data[i][0];
      postData += ", ";
      postData += raw_data[i][1];
      postData += "]";
      if (i < data_len - 1){
        postData += ",";
      }
    }
    postData += "]";

  client.post("/data", contentType, postData);

  // 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 ten seconds");
  delay(10000);
}
adriens commented 5 years ago

Looks interesting, do you have a MKR1000 ? The spec (https://www.arduinolibraries.info/types/official) show the following compatibility matrix :

2019-04-22 07_54_18-Official - Arduino Libraries

2019-04-22 09_05_28-Connecting MKR1000 to AWS IoT - installing Device Certificate (not server cert)

mceraso commented 5 years ago

Ahh, I see you're using an MKR1000. Right now I'm using an UNO Wifi Rev.2 but will be getting a MKR1010 next month

adriens commented 5 years ago

@mceraso : I will buy a arduino-yun-rev-2 so I'll get Python support and a minimalist OS (openwrt) : it should make things much much easier. I'm tired to writing so much code just for a get request ...

sandeepmistry commented 5 years ago

Hi @adriens,

I'm able to reproduce this, the issue is with the WINC1500, it's failing to connect to the "maker.ifttt.com" server even though the cert if flash for it (might be related to the TLS cipher negotiation). I'll open an issue on the WiFi101 repo for this.

One note, client.post("/trigger/test/with/key/MYPRIVATEKEY", contentType, postData) is returning -1 to indicate a HTTP_ERROR_CONNECTION_FAILED error, in the successful connection case 0 would be returned (HTTP_SUCCESS).

adriens commented 5 years ago

Great news to know it can reproduce it. First step for better news. Thank you very much 💟 @sandeepmistry

c0dehunter commented 5 years ago

I am having the same issue. I am using GsmClient to build http client and also not IFTTT but my own server. Also, I am using .get() not .post().

Rocketct commented 5 years ago

the original one was fixed by https://github.com/arduino-libraries/WiFi101/pull/274, @c0dehunter could you try this and check if still not works? if not works could you open a new issue with a description and a sketch to reproduce the problem?

adriens commented 5 years ago

Hi @Rocketct , I'll do so, if not working, I'll open a new issue so the current one can be closed : do you want me to close it now ?

Rocketct commented 5 years ago

no no @adriens i'll do it don't worry, only lets wait the resposne of @c0dehunter, thank you a lot @adriens

Rocketct commented 5 years ago

fixed by https://github.com/arduino-libraries/WiFi101/pull/274 @c0dehunter if you need open new issue.

adriens commented 5 years ago

👏 😻 🚀