56 {
59 ;
60 }
61
62 Serial.println(
"\n==============================================");
63 Serial.println(
" HTTP RPC SERVER - Streaming Example");
64 Serial.println(
"==============================================\n");
65
67
68
71
73
75
77 );
79
80
83
84
86 Serial.println(
"✓ Client connected");
87 });
88
90 Serial.println(
"✗ Client disconnected");
91 });
92
93
94
95 remote.bind(
"add", [](
int a,
int b) ->
int {
102 return a + b;
103 });
104
105 remote.bind(
"setLed", [](
int index,
int r,
int g,
int b) {
106 if (index >= 0 && index <
NUM_LEDS) {
108 Serial.print(
"✓ Set LED ");
117 } else {
118 Serial.print(
"✗ LED index ");
120 Serial.println(
" out of range");
121 }
122 });
123
124 remote.bind(
"fill", [](
int r,
int g,
int b) {
126 Serial.print(
"✓ Filled all LEDs with RGB(");
133 });
134
140 Serial.println(
"✓ Status requested");
142 });
143
144
145
147
149 ack.
set(
"ack",
true);
151
152 Serial.println(
"⏳ Long task started (ASYNC mode)");
153
154
155 int duration = 1000;
157 auto durationOpt = params[0].
as_int();
158 if (durationOpt) {
159 duration = *durationOpt;
160 }
161 }
162
163
167 }
168
169
172 result.set(
"duration", duration);
174
175 Serial.print(
"✓ Long task complete (");
179
180
181
183
185 ack.
set(
"ack",
true);
187
188 Serial.println(
"📊 Stream started (ASYNC_STREAM mode)");
189
190
191 int count = 10;
193 auto countOpt = params[0].
as_int();
194 if (countOpt) {
195 count = *countOpt;
196 }
197 }
198
199
200 for (int i = 0; i < count; i++) {
202 update.
set(
"update", i);
203 update.
set(
"progress", (i * 100) / count);
205
209 Serial.print((i * 100) / count);
211
213 }
214
215
217 final.set("done", true);
218 final.set("count", count);
220
221 Serial.println(
"✓ Stream complete");
223
224
226 Serial.print(
"✓ HTTP server listening on port ");
228 } else {
229 Serial.print(
"✗ Failed to start HTTP server on port ");
231 }
232
233 Serial.println(
"\n=== Available RPC Methods ===");
235 Serial.println(
" add(a, b) -> sum");
236 Serial.println(
" setLed(index, r, g, b)");
237 Serial.println(
" fill(r, g, b)");
238 Serial.println(
" getStatus() -> {numLeds, brightness, millis}");
240 Serial.println(
" longTask(duration) -> ACK + {value, duration}");
241 Serial.println(
"ASYNC_STREAM:");
242 Serial.println(
" streamData(count) -> ACK + updates + {done, count}");
244
245 Serial.println(
"Test with curl:");
246 Serial.println(
" curl -X POST http://localhost:8080/rpc \\");
247 Serial.println(
" -H \"Content-Type: application/json\" \\");
248 Serial.println(
" -H \"Transfer-Encoding: chunked\" \\");
249 Serial.println(
" -d '{\"jsonrpc\":\"2.0\",\"method\":\"add\",\"params\":[2,3],\"id\":1}'");
251 Serial.println(
"Ready for incoming HTTP connections...\n");
252}
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::unique_ptr< fl::Remote > remote
fl::unique_ptr< fl::net::http::HttpStreamClient > transport
static fl::shared_ptr< fl::net::http::HttpStreamServer > * pTransport
static fl::Remote * pRemote
JSON-RPC server with scheduling support.
void send(const fl::json &result)
Send a single response (for ASYNC mode)
void sendFinal(const fl::json &result)
Send final response and mark stream as complete (for ASYNC_STREAM mode)
void sendUpdate(const fl::json &update)
Send intermediate streaming update (for ASYNC_STREAM mode)
Helper class for sending responses in async/streaming RPC methods.
fl::optional< i64 > as_int() const FL_NOEXCEPT
bool is_array() const FL_NOEXCEPT
size_t size() const FL_NOEXCEPT
void set(const fl::string &key, const json &value) FL_NOEXCEPT
static json object() FL_NOEXCEPT
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)