## 3 Low-Cost Multi-Sensor
### Firmware Description and Data Preprocessing  
In order for the microcontroller to acquire the sensors data as discussed in Section 2.2, the routine implemented by the firmware running on the microcontroller is outlined in the flowchart reported in Figure 5. The steps reported in the flowchart, which have been numbered from 1 to 7 for the sake of clearness, can be divided between the initialization phase (steps 1-4), the sensor acquisition process (steps 5-6), and the audio processing (step 7). Regarding the initialization phase, it is performed once after the multi-sensor is powered up, and it is required to prepare both hardware and software components for the main program execution. More precisely, all the peripherals of the microcontroller, which are required to interface with the different subblocks shown in Figure 3, are initialized in \begin{table}
\begin{tabular}{l c c} \hline \hline
**Component** & **Price** & **\% of Overall Cost** \\ \hline
3D case printing & 240 USD & 37\% \\ Sensors kit & 164 USD & 25\% \\ Power and connectivity board & 75 USD & 11.5\% \\ Custom board for sensors & 45 USD & 6.5\% \\ Cables, connectors, nuts and bolts & 38 USD & 5.6\% \\ WiFi module with board & 32 USD & 5\% \\ Microcontroller board & 30 USD & 4.5\% \\ LEDs and light pipes & 18 USD & 2.5\% \\ External power supply & 12 USD & 1.8\% \\ \hline Overall cost & 650 USD & \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Cost analysis for the PROMET&O multi-sensor. Figure 5: Flowchart of the algorithm of PROMET&O multi-sensor. The initialization steps (1–4) allow for setting hardware and software components, while the main operations are scheduled in steps 5–6. Audio data processing is performed asynchronously in step 7, which is the corresponding interrupt service routine. step 1. Among the configured peripheral blocks, a timer configured with a 10 ms increment is started. Then, in step 2, the microcontroller reads the configuration text file from the microSD card. Such a configuration file allows users to change the parameters related to the acquisition process, i.e., the sampling and report time, as well as those related to the network connectivity, e.g., the WiFi service set identifier (SSID) and password, and to the cloud server, e.g., the server name, port, username, and password, without the need to reprogram the microcontroller. When the microSD is not inserted, the actual multi-sensor configuration is populated with default values. The microcontroller commands the WiFi module to establish a connection to the WiFi network specified in the microSD parameters and then connects to the broker (step 3). The sensor acquisition process is started in step 4, and from that point on the algorithm continuously check whether sensors have to be sampled. To this purpose, the previously mentioned timebase is exploited to discipline the multi-sensor operations in accordance with the scheme shown in Figure 2. With the sampling time of the \(i\)-th sensors elapsed (step 5), the output of the sensor is acquired and some preprocessing is performed. More precisely, the new data are inserted in the array related to the \(i\)th physical quantity (\(x_{\mathrm{i}}[]\)) to keep it increasing-ordered, then the maximum and minimum values are updated, if needed, and the last value is added to the sum of the previous ones. Once step 5 is concluded, the firmware checks whether some data must be sent to the cloud server. To this purpose, in step 6, all sensors are scanned to verified whether the corresponding report time has elapsed. In that case, the statistical data discussed in Section 2.2 are evaluated. It is worth mentioning that, as the \(x_{\mathrm{i}}[]\) is an ordered array of length \(N=(t_{\mathrm{report,i}}/t_{\mathrm{sampl,i}})+1\), the median (\(M\)), i.e., the central value in an ordered distribution, can be directly evaluated as:  
\[M= \begin{cases}x\left[\frac{N}{2}\right]&\text{N even,}\\ \frac{1}{2}x\left[\frac{N}{2}-1\right]+\frac{1}{2}x\left[\frac{N}{2}+1\right]& \text{N odd.}\end{cases}