33#define SERVER_HOST "localhost"
34#define SERVER_PORT 8080
66 while (!
Serial && millis() < 3000) {
70 Serial.println(
"\n=== HTTP RPC Client Example ===\n");
71 Serial.println(
"This example connects to an HTTP RPC server");
72 Serial.println(
"and calls remote methods using all three RPC modes:");
73 Serial.println(
" - SYNC: Immediate response");
74 Serial.println(
" - ASYNC: ACK + later result");
75 Serial.println(
" - ASYNC_STREAM: ACK + updates + final");
94 Serial.println(
"[CALLBACK] Connected to server!");
101 Serial.println(
"[CALLBACK] Disconnected from server!");
110 []() {
return transport->readRequest(); },
116 Serial.println(
"ERROR: Failed to connect to server!");
117 Serial.println(
"Make sure RpcServer example is running.");
123 Serial.println(
"Connected successfully!");
128 Serial.println(
"=== Testing SYNC mode: add(2, 3) ===");
132 request[
"jsonrpc"] =
"2.0";
133 request[
"method"] =
"add";
137 request[
"params"] = params;
147 Serial.println(
"Waiting for response...");
151 Serial.println(
"=== Testing ASYNC mode: longTask(2000) ===");
155 request[
"jsonrpc"] =
"2.0";
156 request[
"method"] =
"longTask";
159 request[
"params"] = params;
168 Serial.println(
"Expecting ACK immediately, then result after 2 seconds...");
172 Serial.println(
"=== Testing ASYNC_STREAM mode: streamData(5) ===");
176 request[
"jsonrpc"] =
"2.0";
177 request[
"method"] =
"streamData";
180 request[
"params"] = params;
189 Serial.println(
"Expecting ACK, then 5 updates, then final result...");
194 if (response.contains(
"id")) {
195 const fl::json& idJson = response[
"id"];
197 auto idOpt = idJson.
as_int();
208 if (response.contains(
"error")) {
209 Serial.println(
"ERROR Response:");
210 Serial.println(response.to_string().c_str());
216 if (response.contains(
"result")) {
217 const fl::json& result = response[
"result"];
219 if (result.contains(
"ack")) {
220 auto ackOpt = result[
"ack"].as_bool();
221 if (ackOpt && *ackOpt ==
true) {
222 Serial.println(
"Received ACK - request accepted");
228 if (result.contains(
"update")) {
229 auto updateOpt = result[
"update"].as_int();
231 int update = *updateOpt;
243 if (result.contains(
"stop")) {
244 auto stopOpt = result[
"stop"].as_bool();
245 if (stopOpt && *stopOpt ==
true) {
246 if (result.contains(
"value")) {
247 Serial.print(
"Received FINAL result: ");
248 Serial.println(result[
"value"].to_string().c_str());
250 Serial.println(
"Received FINAL marker (no value)");
258 Serial.print(
"Received result: ");
259 Serial.println(result.to_string().c_str());
262 Serial.println(
"Received response without result:");
263 Serial.println(response.to_string().c_str());
268 uint32_t now = millis();
301 Serial.println(
"\n--- Cycle complete, starting over ---\n");
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::unique_ptr< fl::Remote > remote
void handleResponse(const fl::json &response)
fl::unique_ptr< fl::net::http::HttpStreamClient > transport
const uint32_t CALL_INTERVAL
void sendAsyncStreamRequest()
void push_back(const json &value) FL_NOEXCEPT
fl::optional< i64 > as_int() const FL_NOEXCEPT
bool is_number() const FL_NOEXCEPT
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
void printf(const char *format, const Args &... args) FL_NOEXCEPT
Printf-like formatting function that prints directly to the platform output.
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NOEXCEPT
@ Green
<div style='background:#008000;width:4em;height:4em;'></div>
@ Red
<div style='background:#FF0000;width:4em;height:4em;'></div>
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>