Skip to content

Instantly share code, notes, and snippets.

@miwied
Last active August 26, 2022 09:38
Show Gist options
  • Save miwied/4fea68dc27db63d038cefd8c0a753a1a to your computer and use it in GitHub Desktop.
Save miwied/4fea68dc27db63d038cefd8c0a753a1a to your computer and use it in GitHub Desktop.
Tested the asyncHTTPrequest.h library. The ESP32 crashes on the second request attempt.
/*
dependencies:
https://github.com/me-no-dev/AsyncTCP.git
https://github.com/tzapu/WiFiManager.git
https://github.com/boblemaire/asyncHTTPrequest.git
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiManager.h>
#include <asyncHTTPrequest.h>
const char *ssid = "///";
const char *password = "///";
WiFiManager wifiManager;
asyncHTTPrequest request;
void sendRequest()
{
// here I have intentionally placed a typo to simulate a faulty endpoint
static bool requestOpenResult = request.open("GET", "http://worldTESTdimeapi.org/api/timezone/Europe/London.txt");
if (request.readyState() == 0 || request.readyState() == 4)
{
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
request.send();
Serial.println("request send");
}
else
{
Serial.println("Can't send bad request");
}
}
else
{
Serial.println("Can't send request");
}
}
void requestCB(void *optParm, asyncHTTPrequest *request, int readyState)
{
(void)optParm;
if (readyState == 4)
{
Serial.println(request->responseText());
Serial.println();
}
}
void setup()
{
pinMode(15, INPUT);
Serial.begin(115200);
WiFi.setAutoConnect(true);
WiFi.begin();
while (WiFi.status() != WL_CONNECTED)
{
wifiManager.setConfigPortalTimeout(180);
Serial.println("Connecting with WiFiManager");
wifiManager.autoConnect(ssid, password);
yield();
}
request.setDebug(true);
request.onReadyStateChange(requestCB);
}
void loop()
{
// when I press the button, it should make a HTTP request
if (digitalRead(15) == LOW)
{
sendRequest();
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment