# A TDD Framework for Automated Monitoring in Internet of Things with Machine Learning +
## 5 Case Studies
### Remote Lab  
The first study case is a remote lab whose IoT devices are responsible for temperature and humidity monitoring, and air conditioner control by infra-red signals. As illustrated in the Figure 7, each deployed IoT device sends its status readings to a Mosquito MQTT broker. The status available for the ESP32 devices in this scenario is composed by free memory, processor temperature and Wi-Fi level metrics. A Python agent is integrated with the MQTT broker and it is responsible for the registration of messages in a MySQL database. This database is consulted by a Python notebook for an unsupervised machine learning training. The trained K-Means model is used by a TDD tool that uses the current IoT status readings to assess if the IoT under development has similar status to the deployed IoT devices, according to their historical values. The Mosquito MQTT broker, Python Agent, and MySQL database are deployed in a private cloud computing environment in Brazil, the deployed IoT device is in a remote lab in an Brazilian University, the IoT device under development is installed in a student's home, where the TDD tool and model training Python notebooks are executed. The data collection procedure is detailed in the Figure 8. Each 10 s the air conditioner devices send the status to the MQTT broker, and the humidity and temperature device send the status each 2 s. All messages are registered in the MySQL database by the Python Agent integrated with the Mosquito MQTT broker. Figure 7: Remote lab study case architecture. The TDD procedure is described in the Figure 9. The TDD tool with the trained machine learning model is subscribed in IoT status topics. Each status message from the IoT device under development is relayed to the TDD tool by the MQTT broker, and it is used to compare the current status to the clustered historical values from deployed devices. We collected a total of 1178 readings from two ESP32 devices with two different firmware versions from 9 July 2021 to 13 July 2021. The metrics of the ESP32 devices are freeheap in bytes (i.e., free memory available), internal_temperature in Celsius (i.e., temperature of the processor) and wifi_level in decibels (i.e., local Wi-Fi network level). A summary of the collected metrics of these two devices is presented in the Table 2.  
The correlation matrix of the Figure 10 shows the strong negative correlation between wifi_level and freeheap. \begin{table}
\begin{tabular}{c c c c c c c c c c} \hline \hline \multirow{2}{*}{**Chipid**} & \multicolumn{2}{c}{**Freeheap**} & \multicolumn{4}{c}{**Internal\_Temperature**} & \multicolumn{4}{c}{**Wifi\_Level**} \\ \cline{2-10}  & **Min** & **Mean** & **Max** & **Min** & **Mean** & **Max** & **Min** & **Mean** & **Max** \\ \hline
5,861,220 & 215,592 & 220,818 & 222,332 & 50.0 & 50.4 & 52.2 & \(-\)78.0 & \(-\)70.8 & \(-\)65.0 \\ \hline
13,443,280 & 220,404 & 222,117 & 222,160 & 52.8 & 53.5 & 53.9 & \(-\)83.0 & \(-\)80.6 & \(-\)79.0 \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Remote lab ESP32 metrics summary. Figure 8: Deployed IoT device status data collection with periodic sending. Figure 9: TDD procedure for IoT device under development (remote lab scenario). The negative correlation between wifi_level and freeheap is confirmed by the clusters visualization in Figure 11. The K-Means clustering algorithm was performed using the aforementioned three metrics with the total number of cluster of two corresponding to a good and bad status clusters. After the clustering, the cluster label is performed by the IoT developer based on the visual representation, such as the one represented in Figure 11.  
The clustering results are summarized in Table 3. One may observe that both firmwares presented more bad status occurrences than good status occurrences, with bad status occurrences accounting for around 60% of all readings for both cases. Figure 11: Remote lab K-Means clusters. Red dots are the centroids of each cluster, cluster 0 has yellow dots and cluster 1 has purple dots. Figure 10: Remote lab metrics correlation matrix.