| 76 | } |
| 77 | |
| 78 | void 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 | }); |
nothing calls this directly
no test coverage detected