## 3 System Overview
### Software Architecture and Adaptive Power Algorithm  
We developed C code, compiled it, and uploaded the resulting program to the ESP board using the Arduino IDE [30]. There are three conditions used to save power in the tissue monitoring system as shown in Figure 5.  
Figure 4: Distance measurement. (**a**) Critical measurements with sample values. (**b**) Distance conversion.  
First, the system has to declare global variables and verify I2C and ADC correctness at initialization time because the system uses the memory of the real-time clock (RTC) to save data. The system reduces the CPU frequency of the ESP32 SoC from 240 MHz to 80 MHz to reduce power consumption. The distance between the sensor installation point and a full roll of tissue is too short, so the sensor has to be set in high precision mode to minimize error. The distance sensor measures the distance between the sensor's laser point and the paper. The distance is converted from mm units to a power supply percentage. However, the sensor wakes up and sleeps periodically to save power. At the same time, a LED blinks to indicate that the system is working. The battery level is only measured once per day, to further minimize power. Battery voltage level values are calculated and verified as either good or low. A good value is stored in RTC memory if the battery voltage is 3.1 V or above. On the other hand, if the voltage value is below 3.1 V, a low value is stored instead. The system uses the past distance compared with the current distance before going to the next step.  
The first condition is used when an over-the-air (OTA) firmware upgrade is needed. In this condition, the distance must be below a pre-defined distance. The LED turns on in order to indicate that the system is ready to update through OTA. If the system does not receive the firmware through an OTA, the system measures only battery voltage level and saves its value to the RTC memory; then, the LED is turned off and sleeps for 2 min.  
When the tissue status is the same as the last status, the system moves to the second condition. This state verifies that the current distance is equal to the past distance. In this condition, the device does not connect to Wi-Fi. It simply goes to sleep for 10 min.  
In the last (third) condition, the CPU frequency changes to 160 MHz so that the ESP32 SoC can connect to Wi-Fi sufficiently quickly. After the system overwrites the current distance data in the RTC memory, the Wi-Fi turns on. The measured paper and battery level variables are aggregated into a single data buffer to minimize transmission power. The five-byte payload is sent to a Node-RED server, running locally or over the Internet using the local Wi-Fi network. Then the system returns to sleep mode for 10 min.  
The five-byte payload is published via the MQTT server. The Node-RED server subscribes to the MQTT topic and receives the data from the TMS. The Node-RED server is connected to an Influx Database (DB). The data in the Influx DB are visualized on the Grafana dashboard. Node-RED receives the TMS payload and splits it into the two  
Figure 5: Software architecture and adaptive power algorithm.  
variables, as shown in Figure 6[31]. The paper supply level and battery voltage are sent directly to the Influx DB. The Influx DB connects to the Grafana dashboard [32], which can pull data directly from the database and show them on the dashboard. Alternatively, the amount of tissue and battery are verified as being below the threshold or as not using code deployed to Node-RED. The paper supply level threshold and battery level threshold are set separately, at 15% and 3.1 V, respectively. Users receive email notifications from the Node-RED service when the battery level or paper supply level are below the threshold. The email notification function can be set up with more recipients, and the topics and content can be adjusted according to the users' needs.