/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the usage of the Node Metrics feature. * - A Signal and a Computed node are created, both named for easier identification. * - After setting the signal multiple times, the metrics for both nodes can be printed. * * - It's necessary to have RXESP32_ENABLE_NODE_METRICS enabled in the configuration. * * - The Serial interface is used to interact with the program: * - 's': Set the signal to new random values multiple times. * - 'g': Get the current value of the signal and computed. * - 'i': Print general node statistics. * - 'p': Print the metrics for the signal and computed nodes. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;#if RXESP32_ENABLE_NODE_METRICS == 0#error "This example requires RXESP32_ENABLE_NODE_METRICS to be enabled"#else/* ---------------------------------------------------------------------------------------------- */// Define a simple signalSignal<uint8_t>number(0,{.name="Number Signal"});// Define a computed value that doubles the signalComputed<uint16_t>doubled([](){uint16_tvalue=number.get()*2;returnvalue;},{.name="Doubled Computed"});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("===============================");Serial.println("ReactiveESP32 - Metrics Example");Serial.println("===============================");// Start the ReactiveESP32 dispatcherif(!Dispatcher::start()){Serial.println("Failed to start ReactiveESP32 Dispatcher!");while(true){delay(1000);}}}voidloop(){serialRead();}voidserialRead(){if(!Serial.available())return;charc=Serial.read();if(c=='\r')return;if(c=='\n')c=' ';Serial.printf("> %c\n",c);switch(c){case'0':{// Restart the ESP32ESP.restart();}break;case's':{// Set the signal to a random value between 0 and 100// 10 times with a 10ms delay between setsfor(inti=0;i<10;i++){uint8_tnew_value=random(0,101);Serial.printf("Setting 'number' signal to: %u\n",new_value);number.set(new_value);delay(10);}}break;case'g':{// Get the current value of signal and computeduint8_tvalue_number=number.get();uint16_tvalue_doubled=doubled.get();Serial.printf("Number - Doubled: %u - %u\n",value_number,value_doubled);}break;case'i':{// Print node stats to SerialUtils::printNodeStats();}break;case'p':{// Print metrics of signal and computedUtils::printNodeMetrics(number);Utils::printNodeMetrics(doubled);}break;}}#endif