# A Methodological Approach for the Smartification of a University Campus: The Smart ESI Use Case
## 4 Smart ESI: A New Model of School
### System Architecture
#### 4.3.2 Data Layer: Integration, Persistence and Storing  
The Data Layer is implemented by a server whose software architecture is depicted in Figure 4. This layer provides functionalities for data integration, persistent storing, processing, and multiple interfaces for data access. Such functionalities are supplied by the next software stack, from bottom to up: MQTT, Chirpstack, and Home Assistant. These three components were installed as Docker containers in the server, in order to ease the installation. The first two components are related to the data collection proceeding from the WiFi and Lora devices, while the last one is in charge of the storing, processing and data access interface. Next, we provide the implementation details.  
Message Queue Telemetry Transport (MQTT) is a protocol for IoT device communication based on the publish/subscribe model. The broker or MQTT server was installed in the server and, therefore, assumes the role of the subscriber while the WiFi devices act as MQTT clients, or publishers, that connect to the MQTT server with a given topic. MQTT runs over TCP/IP, thus supporting reliable bidirectional communications. Data collected by the WiFi devices are encapsulated into an MQTT message, which aggregates two headers, a fixed one, with a length of 5 bytes, and an optional header of variable length to a payload with a maximum of 256 Mbits. MQTT does not provide any mechanism for data security, so the data is transmitted raw to the server.  
ChirpStack is the protocol stack that allows the management of LoRaWAN communications. The protocol stack includes three major components, from bottom to up: (1) a _Gateway Bridge_, which interfaces with the UG67 LoRa gateway to collect all messages forwarded from there; (2) a _Network Server_, which filters duplicate messages from other gateways, and (3) an _Application Server_, which interacts with the MQTT server by forwarding all LoRa data to the TCP/IP network. The architecture of Chirpstack is represented in Figure 4.  
Home Assistant is a free and open-source software platform for home automation designed to be a central control system (hub) for smart home devices with a focus on local control and privacy. One of the main advantages of Home Assistant is that offers the capability of controlling a very large number of devices (sensors and actuators) manufactured by a large number of companies through multiple supported wireless communication protocols such as WiFi, Bluetooth, Zigbee, and Z-Wave. Additionally, it enables the possibility of connecting with other devices by writing customized integrations. All these devices communicate with the controller both to receive management commands and to transmit periodically their data, which are stored in a database (MariaDB in our implementation). Home Assistant also provides the capability of managing the data provided by devices through the mechanism of automation, which are scripts that implement some functionality and that are triggered when a certain event occurs (e.g., the temperature is higher 25 \({}^{\circ}\)C). On the other hand, Home Assistant offers a web-based user interface that may be easily designed as a dashboard, called Lovelace, by means of different cards to display information and control devices. Additionally, it can be accessed through voice commands via a supported virtual assistant such as Google Assistant or Amazon Alexa.  
In our case study, most of the devices we deployed in our school were not supported by Home Assistant, so it was necessary to write integrations for them. Such integrations consisted of processing the data, by providing the suitable format to the received data, according to the instructions given by the manufacturer (see an example in Figure 6).