FastLED 3.9.15
Loading...
Searching...
No Matches
ServerReal.h
Go to the documentation of this file.
1
24
25#pragma once
26
27// Note: FASTLED_STUB_MAIN_FAST_EXIT is enabled by default for testing
28// This allows the server to start, verify it's working, and exit cleanly after 5 iterations
29
31#include <FastLED.h>
32
33#define NUM_LEDS 10
34#define DATA_PIN 2
35
38
46
48fl::u32 lastEventTime = 0;
49
50void updateLEDs() {
51 switch (state) {
52 case STARTING:
53 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 0, beatsin8(60))); // Blue pulse
54 break;
55 case RUNNING:
56 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 64, 0)); // Green
57 break;
59 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 128, 128)); // Cyan
60 break;
61 case RESPONDED:
62 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 0, 64)); // Purple
63 break;
64 case ERROR:
65 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 0, 0)); // Red
66 break;
67 }
68}
69
70void autoReset() {
71 // Auto-reset flash states to RUNNING after 200ms
72 if ((state == REQUEST_RECEIVED || state == RESPONDED) &&
73 (fl::millis() - lastEventTime > 200)) {
74 state = RUNNING;
75 }
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 });
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}
151
152void loop() {
153 // Update server multiple times per loop for better responsiveness
154 // Process up to 10 update cycles per loop iteration
155 fl::size total_processed = 0;
156 for (int i = 0; i < 10; ++i) {
157 fl::size processed = server.update();
158 total_processed += processed;
159 if (processed == 0) break; // No more pending requests
160 }
161
162 if (total_processed > 0) {
163 Serial.print("Processed ");
164 Serial.print(static_cast<int>(total_processed));
165 Serial.println(" request(s)");
168 // After processing, flash purple for "responded"
170 }
171
172 autoReset();
173 updateLEDs();
174 FastLED.show();
175
176 delay(10); // 10ms delay between cycles - up to 1000 requests/second possible
177}
#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 autoReset()
Definition ServerReal.h:70
void updateLEDs()
Definition ServerReal.h:50
void setup()
Definition ServerReal.h:78
fl::u32 lastEventTime
Definition ServerReal.h:48
ServerState
Definition ServerReal.h:39
@ RUNNING
Definition ServerReal.h:41
@ REQUEST_RECEIVED
Definition ServerReal.h:42
@ ERROR
Definition ServerReal.h:44
@ RESPONDED
Definition ServerReal.h:43
@ STARTING
Definition ServerReal.h:40
void loop()
Definition ServerReal.h:152
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
LIB8STATIC u8 beatsin8(accum88 beats_per_minute, u8 lowest=0, u8 highest=255, u32 timebase=0, u8 phase_offset=0) FL_NOEXCEPT
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:105
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
asio::http::Server HttpServer
Definition server.h:279
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:304