# A Novel Software Architecture Solution with a Focus on Long-Term IoT Device Security Support
## 4 Device Architecture
### Improving the Hardware Layer
The 12-bit analogue/digital converter provides number values ranging from 0 to 4095. The _measured voltage_ equation is given in (2):  
\[measuredVoltage=(int)\frac{500*readValue}{4095} \tag{2}\]  
The _resistance_ is determined as the maximum magnitude of the analogue/digital converter divided by the _read value_ from the analogue/digital converter as stated in (3):  
\[Resistance=\frac{4095}{readValue}-1 \tag{3}\]  
Since the divider fixed value is set to 10,000 ohm, the _resistance_ is corrected by the fixed amount in (4):  
\[Resistance=\frac{10,000}{Resistance} \tag{4}\]  
The amount is then divided by the factory _resistance_ of the NTC thermistor at a temperature of 25 degrees (10,000 ohms) in (5):  
Figure 6.0  
Finally, the Steinhart-Hart formula (temperature correction) is applied where \(B\) is the coefficient of the NTC thermistor (\(B\) = 3950 according to datasheet) and 25C is the nominal temperature at which the resistance of the thermistor is expressed. The result is obtained in _Kelvin_ (6) and degrees _Celsius_ (7):  
Figure 6.1  
Figure 6.2  
If the Formula (7) is applied in the range around the nominal temperature (25 degrees Celsius), minimal deviations are obtained. However, when applied in the range of 0 degrees Celsius, deviations of up to 5 degrees Celsius may occur. Since the operating temperature of the device is 0 degrees and the allowable deviations, per project requirements, are two degrees Celsius, the error of the sensor operation is almost twice the expected nominal operation. The error results from the exponential curve of the thermistor, where it can be observed that there is an exponential change in resistance at much lower or higher temperatures. This can be confirmed by analysing the factory table of resistance change with the temperature data given in the sensor data sheet. The measured sensor temperature according to real temperature for a previous device model is presented in Figure 6.3  
According to the graph shown, using the Steinhart-Hart equation near 0 degrees Celsius is not an option for accurate temperature telemetry. Since the factory resistance settings in the data sheet are in increments of 5 degrees Celsius, the interpolation of the values between each 5 degrees Celsius is performed. The code shown in Listing 1 first determines the membership of a set of two points: the upper value of the resistor and the lower value of the thermistor temperature resistance. An interpolation of the values between these two points is performed to determine the exact temperature. In Figure 9, it can be seen that after our corrections, the calculated temperatures follow relatively well the real temperature measured with the reference sensor. We calculated the mean absolute error (MAE) for each measured data point. The MAE for the graph represented by Figure 8 was calculated as 3.802, while the MAE for a graph represented by Figure 9 is 0.341, which corresponds to a 11.34-fold temperature precision measurement increase. The most important group of sensors are flow sensors, which enable flow measurement on all copper tubing inside the python tube. With the help of these sensors, it is possible to measure the consumption of beverages and determine the amount of liquid that has flown through the device. The device is equipped with flow sensors shaped in the form of blades that rotate under water pressure and send a pulse to the microcontroller when a complete rotation is achieved. The microcontroller allows the support of interrupts on all 30 pins. Pins 5, 17, 16, 4, 2 and 15 were chosen as the digital input pins for pulse detection on the flow sensors since the device can support six simultaneous beverage lines. These pins were chosen because they have no other important role (I2C, SPI or Serial) on the microcontroller. The previous version of the device used interrupt routines. Each pin triggered its interrupt routine upon the detection of a signal change (pulse) on the pin itself. The signal change was due to a pulse from the flow sensor.