MCPcopy Create free account
hub / github.com/FastLED/FastLED / setup

Function setup

examples/Asio/Server/ServerReal.h:78–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76}
77
78void setup() {
79 Serial.begin(115200);
80 Serial.println("HTTP Server Example");
81
82 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
83 FastLED.setBrightness(64);
84
85 // ROUTE 1: GET / - Hello message
86 server.get("/", [](const fl::http_request& req) {
87 Serial.println("[GET /] Hello request");
88
89 fl::http_response response;
90 response.status(200)
91 .header("Content-Type", "text/plain")
92 .body("Hello from FastLED!\n");
93
94 return response;
95 });
96
97 // ROUTE 2: GET /status - LED status as JSON
98 server.get("/status", [](const fl::http_request& req) {
99 Serial.println("[GET /status] Status request");
100
101 fl::json status = fl::json::object();
102 status.set("num_leds", NUM_LEDS);
103 status.set("brightness", FastLED.getBrightness());
104 status.set("uptime_ms", static_cast<fl::i64>(fl::millis()));
105
106 return fl::http_response::ok().json(status);
107 });
108
109 // ROUTE 3: POST /color - Set LED color
110 server.post("/color", [](const fl::http_request& req) {
111 Serial.println("[POST /color] Color change request");
112
113 fl::json body = fl::json::parse(req.body());
114 if (body.is_null()) {
115 return fl::http_response::bad_request("Invalid JSON");
116 }
117
118 int r = body["r"] | 0;
119 int g = body["g"] | 0;
120 int b = body["b"] | 0;
121
122 fill_solid(leds, NUM_LEDS, fl::CRGB(r, g, b));
123
124 Serial.print("Color set to RGB(");
125 Serial.print(r); Serial.print(", ");
126 Serial.print(g); Serial.print(", ");
127 Serial.print(b); Serial.println(")");
128
129 return fl::http_response::ok("Color updated\n");
130 });
131
132 // ROUTE 4: GET /ping - Health check
133 server.get("/ping", [](const fl::http_request& req) {
134 return fl::http_response::ok("pong\n");
135 });

Callers

nothing calls this directly

Calls 15

headerMethod · 0.80
statusMethod · 0.80
postMethod · 0.80
last_errorMethod · 0.80
updateLEDsFunction · 0.70
millisFunction · 0.50
fill_solidFunction · 0.50
CRGBClass · 0.50
beginMethod · 0.45
printlnMethod · 0.45
setBrightnessMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected