FastLED 3.9.15
Loading...
Searching...
No Matches
Server Directory Reference
+ Directory dependency graph for Server:

Files

 Server.ino
 HTTP Server Example for FastLED.
 
 ServerReal.h
 
 test_client.py
 
 test_manual.py
 
 test_quick.py
 
 test_stress.py
 

Detailed Description

Demonstrates a minimal HTTP server for FastLED using the fl::HttpServer API.

Features

Routes

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")

LED Status Indicators

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

Usage

1. Compile the Example

bash compile posix --examples Server

2. Run the Server

.build/meson-quick/examples/Server.exe

Expected output:

HTTP Server Example
Server started on http://localhost:8080/

3. Test with Python Client

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!

4. Test with curl

# 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

Code Example

#include <FastLED.h>
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);
}
void setup()
void loop()
fl::HttpServer server
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Response & json(const class json &data)
Set JSON response body with automatic Content-Type header.
static Response ok(const string &body="")
Factory method for 200 OK response.
static json object() FL_NOEXCEPT
Definition json.h:692
asio::http::Request http_request
Definition server.h:280
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98
asio::http::Server HttpServer
Definition server.h:279
#define Serial
Definition serial.h:304

API Reference

HttpServer

Methods:

http_request

Methods:

http_response

Methods:

Factory Methods:

Platform Support

Technical Details

Non-Blocking I/O

The HTTP server uses non-blocking sockets to integrate with FastLED's event loop:

HTTP/1.0 Protocol

Minimal HTTP/1.0 implementation for simplicity:

Route Matching

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.

Troubleshooting

Server won't start

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

Connection refused

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

Firewall blocking connections

Solution: Allow incoming connections on port 8080 in your firewall settings.

Next Steps

See Also