FastLED 3.10.8
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 78 of file ServerReal.h.

78 {
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
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
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 });
136
137 // Start server
138 if (server.start(8080)) {
139 Serial.println("Server started on http://localhost:8080/");
140 state = RUNNING;
141 } else {
142 Serial.println("ERROR: Failed to start server");
143 Serial.print("Error: ");
144 Serial.println(server.last_error().c_str());
145 state = ERROR;
146 }
147
148 updateLEDs();
149 FastLED.show();
150}
CRGB leds[1]
#define NUM_LEDS
#define DATA_PIN
Definition ClientReal.h:82
fl::HttpServer server
TestState state
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
void updateLEDs()
Definition ServerReal.h:50
@ RUNNING
Definition ServerReal.h:41
@ ERROR
Definition ServerReal.h:44
const string & body() const FL_NO_EXCEPT
Get request body (for POST/PUT requests)
Definition server.h:59
Response & json(const class json &data) FL_NO_EXCEPT
Set JSON response body with automatic Content-Type header.
static Response bad_request(const string &message) FL_NO_EXCEPT
Factory method for 400 Bad Request response.
static Response ok(const string &body="") FL_NO_EXCEPT
Factory method for 200 OK response.
bool is_null() const FL_NO_EXCEPT
Definition json.h:381
static json parse(const fl::string &txt) FL_NO_EXCEPT
Definition json.h:842
static json object() FL_NO_EXCEPT
Definition json.h:857
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NO_EXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:10
constexpr EOrder GRB
Definition eorder.h:19
Status status() FL_NO_EXCEPT
Current connection state.
Definition wifi.cpp.hpp:43
asio::http::Request http_request
Definition server.h:280
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
asio::http::Response http_response
Definition server.h:281
InputGamut g
Definition rgbw.h:125
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:309

References fl::asio::http::Response::bad_request(), fl::asio::http::Request::body(), DATA_PIN, ERROR, FastLED, fill_solid(), GRB, fl::json::is_null(), fl::asio::http::Response::json(), leds, fl::millis(), NUM_LEDS, fl::json::object(), fl::asio::http::Response::ok(), fl::json::parse(), RUNNING, Serial, server, state, and updateLEDs().

+ Here is the call graph for this function: