examples/Asio/Server/README.md
Demonstrates a minimal HTTP server for FastLED using the fl::HttpServer API.
fl::function<> callbacksloop()http_request and http_response classes| Method | Path | Description |
|---|---|---|
| GET | / | Returns "Hello from FastLED!" |
| GET | /status | Returns LED status as JSON |
| POST | /color | Sets LED color from JSON body |
| GET | /ping | Health check (returns "pong") |
| Color | State | Meaning |
|---|---|---|
| Blue (pulse) | STARTING | Server initializing |
| Green (solid) | RUNNING | Server ready |
| Cyan (flash) | REQUEST_RECEIVED | HTTP request received |
| Purple (flash) | RESPONDED | Response sent |
| Red (solid) | ERROR | Server failed to start |
bash compile posix --examples Server
.build/meson-quick/examples/Server.exe
Expected output:
HTTP Server Example
Server started on http://localhost:8080/
uv run python examples/Asio/Server/test_client.py
Output:
FastLED Network Example Test Client
Server: http://localhost:8080
Waiting for server at http://localhost:8080/ping...
✓ Server is ready!
Sending 5 requests to http://localhost:8080/...
Request 1: ✓ 200 (2.3 ms)
Response: 'Hello from FastLED!'
...
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Metric ┃ Value ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ Total Requests │ 5 │
│ Successful │ 5 (100%) │
│ Failed │ 0 (0%) │
│ Min Response Time │ 1.8 ms │
│ Max Response Time │ 3.2 ms │
│ Avg Response Time │ 2.4 ms │
└───────────────────┴──────────┘
✓ All tests passed!
# GET /
curl http://localhost:8080/
# Output: Hello from FastLED!
# GET /status
curl http://localhost:8080/status
# Output: {"num_leds":10,"brightness":64,"uptime_ms":12345}
# POST /color (set LEDs to red)
curl -X POST http://localhost:8080/color -d '{"r":255,"g":0,"b":0}'
# Output: Color updated
# GET /ping
curl http://localhost:8080/ping
# Output: pong
#include "fl/stl/asio/http/server.h"
#include <FastLED.h>
fl::HttpServer server;
void setup() {
// Register GET route
server.get("/", [](const fl::http_request& req) {
return fl::http_response::ok("Hello from FastLED!\n");
});
// Register POST route with JSON response
server.post("/api/data", [](const fl::http_request& req) {
fl::json response = fl::json::object();
response.set("status", "ok");
response.set("message", "Data received");
return fl::http_response::ok().json(response);
});
// Start server on port 8080
if (server.start(8080)) {
Serial.println("Server started!");
}
}
void loop() {
// Process pending HTTP requests (non-blocking)
server.update();
// Regular FastLED operations
FastLED.show();
delay(10);
}
Methods:
bool start(int port = 8080) - Start server on specified portvoid stop() - Stop server and close connectionssize_t update() - Process pending requests (call in loop())void get(path, handler) - Register GET routevoid post(path, handler) - Register POST routevoid put(path, handler) - Register PUT routevoid del(path, handler) - Register DELETE routebool is_running() - Check if server is runningint port() - Get server portstring last_error() - Get last error messageMethods:
const string& method() - HTTP method ("GET", "POST", etc.)const string& path() - Request path ("/api/status")const string& body() - Request body (for POST/PUT)optional<string> header(name) - Get header by nameoptional<string> query(param) - Get query parameterbool is_get() - Check if method is GETbool is_post() - Check if method is POSTMethods:
http_response& status(int code) - Set HTTP status codehttp_response& header(name, value) - Add HTTP headerhttp_response& body(content) - Set response bodyhttp_response& json(data) - Set JSON responseFactory Methods:
http_response::ok(body) - 200 OK responsehttp_response::not_found() - 404 Not Foundhttp_response::bad_request(msg) - 400 Bad Requesthttp_response::internal_error(msg) - 500 Internal Server ErrorThe HTTP server uses non-blocking sockets to integrate with FastLED's event loop:
Minimal HTTP/1.0 implementation for simplicity:
Simple exact-match routing (no regex or wildcards):
server.get("/api/status", handler); // Matches "/api/status" only
For advanced routing (wildcards, parameters), consider extending the HttpServer class.
ERROR: Failed to start server
Error: Failed to bind to port
Solution: Port 8080 may be in use. Try a different port:
server.start(8081);
Or kill the process using port 8080:
# Linux/macOS
lsof -ti:8080 | xargs kill -9
# Windows
netstat -ano | findstr :8080
taskkill /PID <PID> /F
Solution: Ensure the server is running before testing:
# Terminal 1: Run server
.build/meson-quick/examples/Server.exe
# Terminal 2: Run test client
uv run python examples/Asio/Server/test_client.py
Solution: Allow incoming connections on port 8080 in your firewall settings.
fl::fetch