Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Created February 9, 2026 01:22
Show Gist options
  • Select an option

  • Save wilyJ80/e77fa0f779d00ef0fb725b0294efb78b to your computer and use it in GitHub Desktop.

Select an option

Save wilyJ80/e77fa0f779d00ef0fb725b0294efb78b to your computer and use it in GitHub Desktop.
noblock async http
#include "esp32-hal-gpio.h"
#include <Arduino.h>
#include <WiFi.h>
#include <asyncHTTPrequest.h>
asyncHTTPrequest request;
unsigned long lastReq = 0;
const unsigned long interval = 5000;
void requestCB(void* optParm, asyncHTTPrequest* request, int readyState) {
// readyState 4 = Header and Body received
if (readyState == 4) {
Serial.printf("Response Code: %d\n", request->responseHTTPcode());
}
}
void setup() {
Serial.begin(9600);
WiFi.begin("xxxx 2.4G", "xxx");
// Don't do anything until WiFi is solid
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
// Setup the callback
request.onReadyStateChange(requestCB);
pinMode(2, OUTPUT);
}
void loop() {
// 1. Handle the LED (Zero blocking)
static unsigned long ledPrev = 0;
if (millis() - ledPrev > 100) {
ledPrev = millis();
digitalWrite(2, !digitalRead(2));
}
// 2. Trigger the Request (Zero blocking)
// Only trigger if the previous one is finished (readyState 0 or 4)
if (millis() - lastReq > interval) {
if (request.readyState() == 0 || request.readyState() == 4) {
lastReq = millis();
if (request.open("POST", "http://192.168.100.7:8888")) {
request.setReqHeader("Content-Type", "text/plain");
request.send("POSTED ??? ");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment