99 FL_WARN(
"FastLED Networking Tutorial started - 10 LEDs set to dark red");
100 FL_WARN(
"Learning HTTP fetch API with TWO different async patterns:");
101 FL_WARN(
" APPROACH 1: Promise-based (.then/.catch_) with explicit types");
102 FL_WARN(
" APPROACH 2: fl::await_top_level pattern with explicit types");
103 FL_WARN(
"Toggles between approaches every 10 seconds for comparison...");
104 FL_WARN(
"LED colors indicate status: Red=Error, Green=Promise Success, Blue=Await Success");
110 FL_WARN(
"APPROACH 1: Promise-based pattern with explicit types");
120 FL_WARN(
"SUCCESS [Promise] HTTP fetch successful! Status: "
127 FL_WARN(
"CONTENT [Promise] Content-Type: " << *content_type);
132 if (response_body.
length() >= 100) {
133 FL_WARN(
"RESPONSE [Promise] First 100 characters: " << response_body.
substr(0, 100));
135 FL_WARN(
"RESPONSE [Promise] Full response (" << response_body.
length()
136 <<
" chars): " << response_body);
143 FL_WARN(
"ERROR [Promise] HTTP Error! Status: "
153 .catch_([](
const fl::Error& network_error) {
155 FL_WARN(
"ERROR [Promise] Network Error: " << network_error.
message);
165 FL_WARN(
"APPROACH 2: await_top_level pattern with explicit types");
171 .
header(
"User-Agent",
"FastLED/NetTest-Tutorial");
188 FL_WARN(
"SUCCESS [Await] HTTP fetch successful! Status: "
195 FL_WARN(
"CONTENT [Await] Content-Type: " << *content_type);
200 if (response_body.
length() >= 100) {
201 FL_WARN(
"RESPONSE [Await] First 100 characters: " << response_body.
substr(0, 100));
203 FL_WARN(
"RESPONSE [Await] Full response (" << response_body.
length()
204 <<
" chars): " << response_body);
222 FL_WARN(
"APPROACH 3: JSON Response handling with fl::Json integration");
228 FL_WARN(
"SUCCESS [JSON Promise] HTTP fetch successful! Status: "
234 FL_WARN(
"DETECTED [JSON Promise] Response contains JSON data");
244 int slide_count = data[
"slideshow"][
"slides"].
size();
246 FL_WARN(
"JSON [Promise] Slideshow Author: " << slideshow_url);
247 FL_WARN(
"JSON [Promise] Slideshow Title: " << slideshow_title);
248 FL_WARN(
"JSON [Promise] Slide Count: " << slide_count);
252 fl::Json slides = data[
"slideshow"][
"slides"];
257 FL_WARN(
"JSON [Promise] First slide: " << first_slide_title <<
" (" << first_slide_type <<
")");
265 FL_WARN(
"INFO [JSON Promise] Response is not JSON format");
287 FL_WARN(
"APPROACH 4: JSON Response with await pattern");
301 FL_WARN(
"SUCCESS [JSON Await] HTTP fetch successful! Status: "
306 FL_WARN(
"DETECTED [JSON Await] Response contains JSON data");
316 FL_WARN(
"JSON [Await] Request Origin IP: " << origin_ip);
317 FL_WARN(
"JSON [Await] Request URL: " << request_url);
325 FL_WARN(
"JSON [Await] User-Agent: " << user_agent);
326 FL_WARN(
"JSON [Await] Accept: " << accept);
332 if (
args.size() > 0) {
333 FL_WARN(
"JSON [Await] Query parameters found: " <<
args.size());
335 FL_WARN(
"JSON [Await] No query parameters in request");
343 FL_WARN(
"INFO [JSON Await] Response is not JSON format");
363 unsigned long current_time = millis();
373 FL_WARN(
"CYCLE: Demonstrated Promise-based pattern (Green LEDs on success)");
376 FL_WARN(
"CYCLE: Demonstrated Await-based pattern (Blue LEDs on success)");
379 FL_WARN(
"CYCLE: Demonstrated JSON Promise pattern (Blue LEDs on success)");
382 FL_WARN(
"CYCLE: Demonstrated JSON Await pattern (Cyan LEDs on success)");
387 FL_WARN(
"NEXT: Will switch to next approach in 10 seconds...");
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
central include file for FastLED, defines the CFastLED class/object
static u32 last_request_time
void test_promise_approach()
void test_json_await()
APPROACH 4: JSON Response with await pattern Same JSON handling but using await_top_level for synch...
void test_await_approach()
void test_json_response()
APPROACH 3: JSON Response Handling with FastLED's ideal JSON API This demonstrates fetch responses wi...
static int approach_counter
bool contains(size_t idx) const
StrN substr(fl::size start, fl::size length) const
fetch_options & timeout(int timeout_ms)
Set timeout in milliseconds.
fetch_options & header(const fl::string &name, const fl::string &value)
Add header.
Fetch options builder (fluent interface)
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
fl::Json json() const
Response body parsed as JSON (JavaScript-like API)
fl::optional< fl::string > get_content_type() const
Get content type convenience method.
const fl::string & status_text() const
HTTP status text (like JavaScript response.statusText)
int status() const
HTTP status code (like JavaScript response.status)
bool ok() const
Check if response is successful (like JavaScript response.ok)
const fl::string & text() const
Response body as text (like JavaScript response.text())
bool is_json() const
Check if response appears to contain JSON content.
HTTP response class (unified interface)
const T & value() const
Get the success value (const)
bool ok() const
Check if the result is successful.
fl::string error_message() const
Get the error message as a convenience.
Result type for promise operations.
Unified HTTP fetch API for FastLED (cross-platform)
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
fl::result< T > await_top_level(fl::promise< T > promise)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
@ GRB
Green, Red, Blue (0102)
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Generic asynchronous task management for FastLED.
Representation of an RGB pixel (Red, Green, Blue)