## 3 Design of Hardware and Software for the Data Monitoring System
### Design of Software for the Data Monitoring
#### 3.2.1 Control Algorithms of Modules  
Algorithms used to provide to the main programs inside the ATMega328 the necessary tools to develop an efficient control of the data-logger have been developed. In order to provide of versatility the board and security to critical variables, all the libraries have been designed as \"Objects\" so they have a \"Constructor\" and a \"Destructor\" which have to be managed by the main program. As control algorithms four libraries have been developed (VDIP1 ART, DS1307 ART, TPS3813 ART and SENSORSART). VDIP1 ART provides a range of functions which ensure a correct communication between the VDIP1 device and the microcontroller in order to write a message inside an USB EEPROM memory. DS1307 ART provides the necessary tools to read and modify the hour and the date of the External Clock (DS1307). Furthermore, it has a function to ensure that the clock is working correctly. The functions inside the TPS3813 ART are designed to offer support to the Watch-Dog device. In this case, the Watch-Dog is running since the power supply starts up, there is no need of a \"Constructor\" to initialize the device, but it is important to restart the countdown of the reset. The reset countdown is controlled by the discharge of a capacitor situated close to the device, if the discharge is complete, a reset occurs. The WDI track is the responsible for the capacitor reloading, but if there is too much time between WDI activations, the capacitor is discharged so the microcontroller reset will be done. SENSORSART library is an ensemble of subclasses assembled to offer support to different kinds of sensors using the controllers of the DS2382-800. Each kind of sensor has its own initialization and as it is an object, it is also possible to define different global calibrations to different sensors (even being the same type). Besides, it provides special functions in a subclass \"DSSearch\" to extract the serial numbers of the sensors connected to the 1-Wire. The program structure of all modules is lineal. The Master module program follows the next structure: constructs the objects, initializes the parameters, waits the next minute, requests data, opens the file, writes the date, and closes the file. To accomplish this aim, the Master needs the VDIP1 ART (to store the data inside the USB memory), the DS1307 ART (to evaluate the moment of extracting data) and the TPS3813 ART (for the failure management). Depending on the application, data could be organised on different ways inside the USB memory. In this application the number of the sensor is indicated by the relative position in the line. When there is a problem collecting data from one channel, the Master writes null values at the position of these sensors. The program structure of the Slave module is the following: constructs the objects, initializes the parameters, reads the sensors data, stores them in the EEPROM memory, waits Master module order, sends data. To accomplish this aim, the Slave needs the SENSORSART (to read sensors data). The program structure of the Wireless module is also lineal: constructs the objects, initializes the parameters, waits an order, interprets the order, sends the order. As this module has two communication channels, wireless and I\({}^{2}\)C channel, the answer is never sent using the channel of receipt. The Wireless does not use any of the previous developed libraries; it only uses Wire library included in Arduino's IDE. A Cyclic Redundancy Check (CRC) is implanted inside 1-Wire and I\({}^{2}\)C communication protocols, between the sensors and the Master module in order to ensure that there has not been any mistake in the communication. An example could be the data collision by the response of two slaves at the same time or interferences. Using this data protection, it is possible to trust with a high level of trustworthiness that the bytes read by the Master are the same that the ones sent by the slaves [41]. So in case of a data error, it must be a problem with the sensor, but not with the transmitted information.