Last active
August 29, 2022 11:20
-
-
Save miwied/f56aca82c4f976f448dde1a8da051650 to your computer and use it in GitHub Desktop.
AsyncHTTPRequest_Generic.h timeout test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic | |
#define ASYNC_HTTP_DEBUG_PORT Serial | |
#define _ASYNC_HTTP_LOGLEVEL_ 4 | |
const char *ssid = "###"; | |
const char *password = "###"; | |
AsyncHTTPRequest request; | |
void sendRequest(const char *url) | |
{ | |
static bool requestOpenResult; | |
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) | |
{ | |
requestOpenResult = request.open("GET", url); | |
if (requestOpenResult) | |
{ | |
// Only send() if open() returns true, or crash | |
request.send(); | |
} | |
else | |
{ | |
Serial.println(F("Can't send bad request")); | |
} | |
} | |
else | |
{ | |
Serial.println(F("Can't send request")); | |
} | |
} | |
void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) | |
{ | |
(void)optParm; | |
if (readyState == readyStateDone) | |
{ | |
Serial.println(F("\n**************************************")); | |
Serial.println(request->responseHTTPcode()); | |
Serial.println(request->responseText()); | |
Serial.println(F("**************************************")); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.println("Connection to WiFi "); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print(F(".")); | |
} | |
Serial.println("Connected to WiFi!"); | |
request.setDebug(true); | |
request.onReadyStateChange(requestCB); | |
Serial.print("Sending a request"); | |
request.setTimeout(3); // it SHOULD timeout in 3 seconds ... | |
sendRequest("http://192.168.178.62/cm?cmnd=Power"); // this is a invalid-endpoint as ip address (18 sec. timeout everytime) | |
// sendRequest("http://exampleasynchttp.com/index.html"); // this works good (immediately "-4" code) | |
} | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment