Closed adriens closed 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
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);
}
Same behavior with DweetPost
code
Maybe is it linked to https://github.com/arduino-libraries/ArduinoHttpClient/issues/29 ?
ALso, my server address is
#define HOSTIFTTT "maker.ifttt.com"
@Rocketct can you try to reproduce the issue?
I can play any code you want also on my side.
Here is the curl
command :
Hi guys, any chance to get a hint for this week-end ? 🙏
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
Hi @mceraso , thanks for the idea. Unfortulanely, it did not work 😿 Do you have any running code snippet that run on your side ?
In the README
I can see :
means it will work with WiFiClient, EthernetClient and GSMClient.
... so, does it support WiFiSSLClient
❔
It seems like it does but no way to make it run actually...
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);
}
Looks interesting, do you have a MKR1000
? The spec (https://www.arduinolibraries.info/types/official) show the following compatibility matrix :
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
@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 ...
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
).
Great news to know it can reproduce it. First step for better news. Thank you very much 💟 @sandeepmistry
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().
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?
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 ?
no no @adriens i'll do it don't worry, only lets wait the resposne of @c0dehunter, thank you a lot @adriens
fixed by https://github.com/arduino-libraries/WiFi101/pull/274 @c0dehunter if you need open new issue.
👏 😻 🚀
Hi,
Your library perfectly matches my needs to
POST
on IFTTT, I'm connecting thtroughWiFiSSLClient
and have imported certificates :I can successfuly post from
curl
but no way to make it run from mkr1000, always returning -2 return code :Here is the loop source code in case it can help :
I just can't figure out what's wrong with my code 😿
ANy help would be greatly appreciated 🙏