## 3 Materials and Methods
### Task Scheduling Algorithm  
From the software architecture perspective, the IoT weather station consists of three components: a microcontroller, weather sensor, and network module. All components communicate via the digital line depending on their interface. Figure 4 shows the sequence diagram and its method. The method in the diagram comprises two categories: executing in the module and communicating between modules. The main idea of our design method is scheduling between the sensing and sending modules. The sensing module is executed in the microcontroller, while the sending module  
Figure 3: The low-cost disaster monitoring system and its peripheral sensor. Figure 2: The system architecture of the IoT weather station. The data line is represented with the dotted line to turn the network module on/off via relay. is executed in the network module. The network module consumes 95% of the overall power consumption, while 5% is used for other activities, including sensing and computing, in the sensing module. The sequence diagram in Figure 4 shows that the first six instructions are looped in the microcontroller module. When the bootstrap code is finished, the microcontroller retrieves the sensing data from the sensor via the getData() function. The sensor modules sense the environmental data in round-robin mode. During the sensing process, there is the latency from the sensor and the delay function. The process A is repeated \(N\) times to average the collection of the sensing data based on Algorithm 1. ```
proceduregetData(\(N\), \(t_{a}\)) \(S\leftarrow\phi\) for\(i\gets 1\): \(i\leq N\): \(i=i+1\)do \(s_{i}\leftarrow sense()\) \(S=S\bigcup S_{i}\) \(poll\_delay(t_{a})\) endfor if\(S=\phi\)then return Error else return\(S\_average()\) endif endprocedure
```  
**Algorithm 1** Get Data Algorithm is to collect the sensing data from the sensor node  
The delay function in processes A, B, and C is implemented with the polling technique, while process D is selected between the polling and deep sleep technique. Each technique affects the power consumption in the microcontroller. Using the traditional delay() function in the microcontroller for the short delay has significant drawbacks, especially ESP8266. Thus, we propose the poll_delay() with the polling technique in Algorithm 2. Algorithm 2 is a proposed polling delay function that replaces the traditional delay() function in the microcontroller for short delays. It is designed to be used in processes A, B, and C, where a few milliseconds delay is required. Figure 4: The sequence diagram of the software architecture. ```
procedurepull_delay(\(t\)) \(ts_{pre}\gets 0\) repeat \(ts_{cur}\gets getMillis()\) until\(ts_{cur}-ts_{pre}>t\) endprocedure
```  
**Algorithm 2** poll_delay(t) is more precise delay function  
The microcontroller activates the network module using the turnOn() function. To reduce energy consumption during standby mode, the wireless functionality is disabled. Under typical conditions, the IoT device transmits data once or twice daily, increasing transmission frequency during crises. Subsequently, the device attempts to establish a network connection in process B. This process employs a polling technique to delay execution while awaiting network connectivity, retrying up to \(T\) times in case of connection errors. The process is aborted if the microcontroller cannot connect to the internet after \(T\) attempts. The algorithm for the turnOn() function is detailed in Algorithm 3. ```
procedureturnOn(\(T,t_{c}\)) \(wifi\_status=FALSE\) for\(i<1\): \(i\leq T\): \(i=i+1\)do \(wifi\_status=turnOn()\) if\(wifi\_status=TRUE\)then break endif \(poll\_delay(t_{c})\) endfor return\(wifi\_status\) endprocedure
```  
**Algorithm 3** turnOn connects to WiFi  
Upon establishing a network connection, the IoT device transmits data using the sendData() function, as outlined in Algorithm 4. The microcontroller awaits a status reply message. If the sending status is false, the process attempts to resend the data to the cloud for up to \(K\) rounds.