# A Novel Software Architecture Solution with a Focus on Long-Term IoT Device Security Support
## 4 Device Architecture
### Improving the Hardware Layer
```
//TIMEE timer=timerBegin(0,80,true); timertatchInterrupt(timer,80 mT,true); timerAlarmWrite(timer,4000,true);//timerevery4 ms timerAlarmEnable(timer); voidIRAM_ATHontimer(){//every4 ms portINTER_CRITICAL_ISR(attimerRux); pulse_number[0]=digitalRead(V1); pulse_number[1]=digitalRead(V2); pulse_number[2]=digitalRead(V3); pulse_number[3]=digitalRead(V4); pulse_number[4]=digitalRead(V5); pulse_number[5]=digitalRead(V6); for(inet=0;e<6;e++){ last_last_pulse_number[e]=last_pulse_number[e]; last_pulse_number[e]=pulse_number[e]; if(pulse_number[e]==0 andlast_pulse_number[e]==1){ if(last_last_pulse_number[e]==1){ flow[e]++; } } } } portEXT_CRITICAL_ISR(attimerMux); }
```  
The timer is set to 4 ms, which was calculated to be the shortest signal period on the fastest tap, according to the flow meter specifications from the datasheet [40]. Interrupt timer time is calculated upon maximum flow measurement based on device maximum python pipe capacity. This capacity determines the number of impulses per second using the datasheet data for the number of litres per second in maximum flow. When interrupts occur (every 4 ms), the flow line state is read and the previous two states are taken into account. A flow pulse is added only if the previous two states were in a high logical unit. In this way, errors caused by noise when a logic zero occurs are ignored. Satisfactory measurement accuracy was achieved using the above algorithm. The results are shown in Tables 1 and 2. The results presented are satisfactory over the entire operating range of the flow meter (slow and fast tapping). Positive deviation ranges from 2.12% to 4.5% for slow tapping and from 4.2% to 6.8% for fast tapping. The results shown are averages for multiple measurements taken on multiple flow meters. The minimum (Python #1) and maximum (Python #2) flow meter deviations are presented. The flow meter deviations are compared to calibrated flow meters (Multicon V7x [41]). In order to store the instrument settings and recorded flow on all pipes, the previous model had a fast EEPROM on the main board. This EEPROM was added because the microcontroller internal EEPROM was too slow to allow memory operation during power failure. The new device model uses a microcontroller EEPROM because the writing speed was significantly improved. \begin{table}
\begin{tabular}{c c c c c c} \hline \hline \multicolumn{2}{c}{**Calibrated Flow Meter**} & \multicolumn{2}{c}{**Telemetry Results**} & \multicolumn{2}{c}{**Deviation**} \\ \hline
**Python \#1** & **Python \#2** & **Python \#1** & **Python \#2** & **Python \#1** & **Python \#2** \\ \hline
380 & 373 & 389 & 390 & 9 & 17 \\
375 & 374 & 384 & 391 & 9 & 17 \\
377 & 373 & 386 & 390 & 9 & 17 \\
378 & 375 & 386 & 391 & 8 & 16 \\
377 & 377 & 385 & 393 & 8 & 16 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Number of impulses in slow tapping during 60 s period for 1 L.  
The previous device model had sensors to establish the energy balance of the device. A voltage transformer was used to measure the main voltage and frequency. Current sensors were used to measure fan, compressor and pump (mixer) current. The voltage transformer and current sensors were not able to measure voltage and current accurately. In order to improve the motherboard reliability, the values for the maximum current range were predefined, which resulted in a relatively large error in the current measurements. In addition, the voltage transformer used was not sufficiently calibrated, so detailed voltage information was missing. Measurement accuracy was not a problem as it was not used for precise telemetry. Current sensors allowed the implementation of compressor, fan, and pump (mixer) monitoring that provided information on the current flow present. Alarming events were shown on the unit display. The new motherboard has a connection capability for current meters between the main power supply and the device. The current meter provides voltage, current, frequency, and power factor information over the serial line.