FastLED 3.9.15
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
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 });
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}
#define NUM_LEDS
fl::CRGB leds[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
Get request body (for POST/PUT requests)
Definition server.h:59
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 Response bad_request(const string &message)
Factory method for 400 Bad Request response.
bool is_null() const FL_NOEXCEPT
Definition json.h:238
void set(const fl::string &key, const json &value) FL_NOEXCEPT
Definition json.h:701
static json parse(const fl::string &txt) FL_NOEXCEPT
Definition json.h:677
static json object() FL_NOEXCEPT
Definition json.h:692
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
constexpr EOrder GRB
Definition eorder.h:19
asio::http::Request http_request
Definition server.h:280
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
fl::i64 i64
Definition s16x16x4.h:222
asio::http::Response http_response
Definition server.h:281
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:304

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, fl::json::set(), state, and updateLEDs().

+ Here is the call graph for this function: