design_methodology_and_implementation.tex 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. % !TeX root = main.tex
  2. \iffalse
  3. This should include background theory, justification for the design choices and any assumptions made.
  4. These should relate to each IoT layer under consideration.
  5. Block diagrams, schematic diagrams and flowcharts are excellent illustration tools.
  6. This section should not include screenshots nor detailed “how-to” operations
  7. (these should appear in an Appendix if necessary)
  8. * Have you identified the problem you are addressing?
  9. * Have you provided an overview of how you are addressing this problem?
  10. * Have you provided detailed steps of what you did in a logical order?
  11. * Have you described the methods you used to solve the problem and their purpose?
  12. * Have you identified the parameters for designing your system?
  13. * Have you identified the metrics for evaluating the performance of your system?
  14. * Have you used illustrations to give the reader a visual interpretation of your system?
  15. \fi
  16. \newpage
  17. \section{Design Methodology and Implementation}
  18. \subsection{The Problem}
  19. UCL currently has many cafes: George Farha, Engineering Café, IoE, Gordon’s Café, Bloomsbury, Print Room, to name a few.
  20. These establishments are open to the student and academic
  21. body during working hours and due to their proximity to lecture rooms and libraries, they are
  22. often used for as a grab and go, but many students also use it as a place to sit down and have a
  23. break, eat lunch or conduct group meetings. As a result, these places tend to get quite crowded,
  24. therefore, the cafés can become an unpleasant area to stay at. UCL students usually have limited time to buy coffee and just can not go through all the
  25. cafés until they find one that has the shortest queue.
  26. The problem is that the students and staff do not know how busy the cafés are at given time. To address this problem we have developed a phone application (app) that shows how busy cafés are or will be in near future. An IoT device collects multiple metrics from café's environment which is sent to the cloud system. Inside the cloud raw information is analysed and processed to be represented on the app.
  27. \begin{figure}[H]
  28. \centering
  29. \includegraphics[scale=0.4]{graphics/simplified.eps}
  30. \caption{A simplified overall system diagram.}
  31. \label{fig:simplified}
  32. \end{figure}
  33. Currently, there seem to be a lack of solutions on detecting crowds or how busy an indoor place is. Some solutions have been proposed by using complicated computer vision to analyse people count \autocite{8473458}. However, such solution would require complex and sophisticated program, and powerful hardware, which brings other problems with battery life and overall project expenses. This project has proposed a simpler solution, detecting how busy indoor environment is by measuring air quality which include $CO_2$, TVOC, $H_2$, Ethanol, Temperature and Humidity readings; and WiFi activity. We expect to see correlation between air quality and café's activity due to $CO_2$ and TVOC increase from people and coffee making process. WiFi activity should correlate with amount of people staying inside and using their phones or laptops.
  34. \subsection{Background Theory}
  35. \subsubsection{IoT Stack}
  36. % explain the IoT stack and each layer
  37. \begin{figure}[H]
  38. \centering
  39. \begin{subfigure}[b]{0.45\textwidth}
  40. \centering
  41. \includegraphics[scale=0.55]{graphics/iot_model1.eps}
  42. \caption{Model 1}
  43. \label{fig:iot_model1}
  44. \end{subfigure}
  45. \begin{subfigure}[b]{0.45\textwidth}
  46. \centering
  47. \includegraphics[scale=0.55]{graphics/iot_model2.eps}
  48. \caption{Model 2}
  49. \label{fig:iot_model2}
  50. \end{subfigure}
  51. \caption{IoT conceptual models}\label{fig:iot_models}
  52. \end{figure}
  53. There are different versions for the IoT stack and may of these versions will agree on many areas. However, there is no formal standard IoT stack. \autoref{fig:iot_model1} and \autoref{fig:iot_model2} and two such examples of the IoT stack. These models may seem similar but they are different. Below an explanation into both the models will be done and which model was chosen for this project. \\
  54. \textbf{{\small Model 1}}
  55. This model contains three layers. The bottom layer is sensors, the middle layer is for connectivity and finally the top layer is for data analytics. In this model each layer is quite specific as to what should exist.
  56. \textbf{{\small Model 2}}
  57. This model is more generalised than the other model. Like the other model this model also has three layers. The top layer of the model is for cloud services such as database storage, real time streaming. This also encapsulates data analytics. Thus called application layer. The middle layer is for the gateway. The bottom layer is known as the object layer, this does not only cover the sensors but this also could be motes.
  58. % talk about which model was chosen for the project
  59. \textbf{{\small Chosen Model}}
  60. For this project the first model is chosen as the IoT stack. This is a more specific for this project.
  61. \subsubsection{Characteristics of Sensors}
  62. \textbf{{\small Range}}
  63. Maximum and minimum value range over which a sensor works well. Sensors may work well outside this range, but require additional calibration. e.g. the output may no longer be linear. \\
  64. \textbf{{\small Accuracy}}
  65. How well the sensor measures the environment in an absolute sense, i.e. how good the data is when compared with a recognized standard. e.g. a temperature sensor accurate to 0.001oC is expected to agree within 0.001oC with a known temperature standard. This is what you want to compare results with other observations. \\
  66. \textbf{{\small Resolution}}
  67. The ability of a sensor to see small differences in readings. e.g. a temperature sensor may have a resolution of $\mbox{0.000,01}^{o}\mbox{C}$, but only be accurate to $\mbox{0.001}^{o}\mbox{C}$. Can detect relatively small changes in temperature, smaller than the accuracy of the sensor. Resolution in often controlled by the quantisation in digitising the signal so is not a function of the sensor itself, but of the sampling process. \\
  68. \textbf{{\small Repeatability}}
  69. This is the ability of a sensor to repeat a measurement when put back in the same environment. It is often directly related to accuracy, but a sensor can be inaccurate, yet be repeatable in making observations. \\
  70. \textbf{{\small Drift/Stability}}
  71. This is the low frequency change in a sensor with time, i.e., with a given input you always get the same output. It is often associated with electronic aging of components or reference standards in the sensor. \\
  72. \textbf{{\small Response time}}
  73. A simple estimate of the frequency response of a sensor assuming a change in the measurement. \\
  74. \textbf{{\small Output}}
  75. What output is given for a change in the parameter being measured. For example, a voltage range e.g. 0 to 5 volts for an input range of 0 to $\mbox{30}^{o}\mbox{C}$. \\
  76. \textbf{{\small Power Consumption}}
  77. What is needed to power the sensor, quite often specified as the current draw. \\
  78. \textbf{{\small Setting Time}}
  79. After being switched on, how long before a valid measurement is ready. \\
  80. \textbf{{\small Sampling time required}}
  81. How often do we need to repeat measurements to get an accurate picture of the phenomenon being measured. \\
  82. \subsubsection{Communication Protocol for sensor data}
  83. % Give a small general introduction to the topic
  84. \textbf{SPI}
  85. % SPI - image
  86. \textbf{$\mbox{I}^{2}\mbox{C}$}
  87. % I2C - image
  88. \subsubsection{Methods for accessing external devices}
  89. Below two methods for accessing an external device will be described. The methods are used to see if there is a change in the state of an external device. \\
  90. % Polling
  91. \textbf{{\small Polling}}
  92. Polling checks for the change in state in an external device by simply checking if the state of the device has changed in a loop. For example there can be a loop where there will be some code to carry out a certain operation, then code to check if the state has changed and what to do if that is the case and then there can be some other code for some other operation. This entire code will be repeated until an exit condition is met. \\
  93. % Interrupts
  94. \textbf{{\small Interrupts}}
  95. Interrupts work differently. Instead of always checking if the state of the external device has changed or not, the processor will carry out the main code but when a change does occur then the processor is interrupted or notified that there is a change. Once the current instruction has finished executing the interrupt is handled and the processor will go back to executing the main code. \\
  96. \textbf{{\small Chosen method}}
  97. For this project interrupts was chosen. This was because the main code will not have to check continuously. In addition to this polling will have a delay when checking if there is a change to the state of an external device since the loop will contain other code which is not responsible for checking the state of the external device.
  98. \subsubsection{Low Power Mode}
  99. \subsubsection{WiFi Activity}\label{subsec:wifi}
  100. IEEE 802.11 standard describes that WiFi modules has eight operation modes. Monitor mode allows WiFi module capturing WiFi packets that are transmitted around it. These packets can provide information such as source and destination MAC addresses, data rate, SNR and a number of other fields about network. Packets are grouped into three types: management, control and data. Management and control packets are dedicated for coordination, access point authentication and any other operation that does not include user sent data. These packets can be used to detect how many unique connected devices are in the surrounding area.
  101. \subsubsection{Cryptography}\label{subsec:crypto}
  102. This project uses symmetric encryption which uses one and the same key to encrypt and decrypt a message. As cryptographic key is installed directly into IoT device's ROM, this key is never sent over non-secure media and therefore no need for asymmetric encryption is needed. An unpredictable random nonce is provided by server upon new connection as shown in \autoref{fig:crypto}. It is used as input by chiper which ensures that identical message with different nonce would yield a completely different encrypted data. The same nonce value is then reused inside the cloud again to decrypt a message. Without such mechanism a reply attack is possible. If malicious source capture an encrypted message and resend it to the cloud, it could not differentiate between legitimate and repeated messages.
  103. \begin{figure}[H]
  104. \centering
  105. \includegraphics[scale=0.4]{graphics/ecrypto.eps}
  106. \caption{Diagram representing cryptographic nonce and encrypted message exchange between the could and the thing.}
  107. \label{fig:crypto}
  108. \end{figure}
  109. \subsubsection{Checksum}
  110. This project uses cyclic redundancy check (CRC) error-detecting code. This is very simple and fast algorithm that creates seemingly a random result from provided input. A message can be processed with this algorithm which result attached to the message before sending it. When this message is received, it can be processed again with same algorithm and new result is compared to attached CRC result. If results are different, it indicates that error has happened during transmission.
  111. \begin{figure}[H]
  112. \centering
  113. \includegraphics[scale=0.4]{graphics/checksum.eps}
  114. \caption{Block diagram of CRC checksum logic.}
  115. \label{fig:checksum}
  116. \end{figure}
  117. \subsection{System Design}
  118. This section describes the overall design of our IoT device.
  119. \subsubsection{Layer 1}
  120. The IoT core component is \texttt{Feather HUZZAH ESP8266} module containing a \texttt{ESP8266} microcontroller with built-in Wi-Fi. This module also has a built-in Lithium Polymer (LiPo) battery charging circuit, USB connectivity with serial converter to charge and program microcontroller and linear converter to supply 3.3V rail. The air quality \texttt{SGP30} and temperature-humidity \texttt{BME280} sensors were both connected via $I^2C$ bus. An additional \texttt{IO16} pin was connected to the reset pin, which enables deep-sleep mode wakeup timer. \autoref{fig:thing_diagram} shows the circuit diagram connecting all the components of the IoT device.
  121. \begin{figure}[H]
  122. \centering
  123. \includegraphics[scale=0.55]{graphics/thing_diagram.eps}
  124. \caption{Circuit of IoT device.}
  125. \label{fig:thing_diagram}
  126. \end{figure}
  127. % We need to talk about why we chose to use these sensors. For example talk about why use an air quality sensor. What information will that give to show if the cafe is busy or not. We need to do this for all the sensors
  128. With this configuration, even in deep-sleep mode both sensor modules are still powered as 3.3V bus is not affected. This is intentional as both sensors have low-power modes. \texttt{BME280} sensor outputs temperature, humidity and pressure readings. This sensor has three modes, sleep which does no operation and is at the lowest power, forced which perform one measurement, stores the results and returns to sleep and normal mode for perpetual cycling of measurements. Additional oversampling and IIR filter settings can be set. The weather monitoring configuration that was recommended by the datasheet was used, this uses forced mode with one sample per minute without oversampling and IIR filter. This achieved reasonable noise and performance with average current consumption of $160nA$. \\
  129. \texttt{SGP30} sensor was equipped to measure carbon dioxide (from 400 ppm), total volatile organic compounds (in ppb range), relative $H_2$ and ethanol values. After the sensor has been powered it goes into low power sleep mode. When the microcontroller sends a measurement request the high current measurement mode is activated. Internally the sensor takes a sample at 1Hz. This was used to calculate a baseline. During experimentation, it was seen that it takes around 16 seconds since booting to read an accurate value. This lead to the decision of not disconnecting the power from this sensor during sleep as it will not be able to give an accurate value when requested by the microcontroller. This sensor also features an on-chip humidity compensation calculation, however, it requires the absolute humidity value to be provided by the microcontroller. This value was calculated from the \texttt{BME280} sensor readings using the following equation (\autoref{eq:ah}).
  130. \begin{equation}\label{eq:ah}
  131. AH = 216.7 \times \frac{\frac{RH}{100} \times 6.112^{\frac{17.62t}{243.12+t}}}{273.15+t}
  132. \end{equation}
  133. Where AH is absolute humidity in $g/m^3$, RH is relative humidity in percent, and t is temperature in $^{\circ} C$.
  134. \begin{figure}[h]
  135. \centering
  136. \includegraphics[width=\linewidth]{graphics/program.eps}
  137. \caption{Functional diagram of IoT device program. Dashed line indicates data taken by logic block.}
  138. \label{fig:program}
  139. \end{figure}
  140. \autoref{fig:program} shows a flowchart for the implemented software on the IoT device. The program starts by enabling the Wi-Fi monitor mode which executes an interrupt routine every time the microcontroller's Wi-Fi module receives a Wi-Fi packet. This packet is processed and the Wi-Fi packet type counter is increased as described in \autoref{subsec:wifi}. This stores three count for each packet type that has been captured during the period before payload is composed. One second delay before composing the payload is used to give enough time to capture an average WiFi activity. During a small test, while continuously measuring packets it was concluded that one second is sufficient to represent average activity.
  141. After program enables WiFi monitoring routine, it follows serial and sensor initialisation. Serial block represents UART connection setup which is used for debugging purposes. Sensor checks include connecting them via $I^2C$, retrieving their parameters, correcting if they are not right. If either of the sensors do not respond or indicate a problem, an error flag is set high for that sensor.
  142. The program then goes onto gathering sensor readings and composes a payload message with CRC32 checksum. If \texttt{BME280} sensor is faulty, setting absolute humidity for \texttt{SGP30} is skipped. Then the Wi-Fi mode is changed to connect to an access point which allowed the IoT device to be connected to the server.
  143. % I sort of understand this on an abstract level. It will definately be better if more information is added so that the reader understands
  144. Upon a new TCP connection, server sends a cryptographic nonce which microcontroller uses to encrypt payload and send it to server, this logic is explained in \autoref{subsec:crypto}.
  145. Afterwards the server and Wi-Fi connections are gradually disconnected, meaning it sends end of connection packet to TCP and deauthentication instruction to WiFi. Then a timer and deep sleep mode is set. When timer time expires, program is reset.
  146. \subsubsection{Layer 2}
  147. \begin{center}
  148. \begin{tabular}{r@{}l@{}ll}
  149. && \textbf{Content Name} & \textbf{Size and type} \\
  150. \hline
  151. && Device ID & 8bits \\
  152. && Payload length* & 8bit unsigned integer\\
  153. \multirow{13}{2cm}{Encrypted Payload \textbf{24-bytes}} & \ldelim\{{13}{*} &
  154. Temperature Reading& 32bit float \\
  155. && Humidity Reading& 32bit float \\
  156. && Pressure Reading& 32bit float \\
  157. && WiFi Managed packets & 16bit unsigned integer \\
  158. && WiFi Control packets & 16bit unsigned integer \\
  159. && WiFi Data packets & 16bit unsigned integer \\
  160. && $CO_2$ Reading& 16bit unsigned integer \\
  161. && $H_2$ Reading& 16bit unsigned integer \\
  162. && TVOC Reading& 16bit unsigned integer \\
  163. && Ethanol Reading& 16bit unsigned integer \\
  164. && Status Flags& 8bits \\
  165. && \textit{reserved.} & 8bits \\
  166. && CRC32 Checksum & 32bits \\
  167. \end{tabular}
  168. \captionof{table}{List of message content sent from IoT device to server via TCP socket. \textbf{*} Payload length is represented in multiples of 12bits, value of 2 in this case.}
  169. \label{tab:message_content}
  170. \end{center}
  171. The breakdown of the message that will be sent to the server is shown in \autoref{tab:message_content}.
  172. % It will be better to expand more on this
  173. Sending data in raw binary was chosen as suppose to encoded with data interchange format such as JSON or XML due to bandwidth efficiency, computation time and simplicity of implementation with C.
  174. ChaCha20 cipher was used for encryption with 12 byte nonce. This nonce size requires payload to be in multiples of 12 bytes blocks which is the reason for having "reserved" byte in payload.
  175. Each IoT device is designed to have a unique built-in symmetric key. Device ID is not encrypted and used by server to lookup key dictionary and decrypt the payload. Checksum is also crucial in this case to ensure that used key is correct, because when using an incorrect key resulting message would be "garbage" meaning that the message and CRC would not match.
  176. Since this IoT device will not be sending any sensitive information and encryption may seem unnecessary.
  177. However, this solution ensures that received information was authenticated.
  178. This also prevents malicious parties from sending false readings to show that certain cafe is more or less busy, which could have financial advantage.
  179. \subsubsection{Layer 3}
  180. \begin{figure}[H]
  181. \centering
  182. \includegraphics[scale=0.4]{graphics/cloud.eps}
  183. \caption{General flow diagram of whole system connectivity.}
  184. \label{fig:cloud}
  185. \end{figure}
  186. % this section needs to be expanded. It is hard for a reader to follow. Mor information is required
  187. The cloud contains four main parts: connector, database, API and Grafana User Interface, general block diagram is shown in \autoref{fig:cloud}. Connector is responsible for recieving raw TCP packets, decrypting payload and sending formatted content to Influx Database. This database is chosen because of its time series nature, real-time monitoring, rich documentation and advanced capabilities to analyse data.
  188. Data from database is then used by Grafana UI for this platform administrators to perform analysis and monitoring. API block is a simple HTTP server that formats and serves all necessary data from database to user application, such as day predictions and current readings. These responses are formatted with JSON. This block also acts as a security layer so database could not be accessed directly.