Use of ANN for Embedded Domotic System Based on IoT

Energy consumption is one factor of risk in the medium term around of the world, that can be minimized by efficient use of electrical devices include its disconnection after use. This article presents a system focused on smart homes, where the concepts of Internet of Things and Artificial Intelligence are applied in the designing of a system that allows a user from a web application to disconnect and connect an electrical network in a node. From the power of the device, time of use and consumption of this, an artificial neural network was designed and trained with the backpropagation algorithm, to discriminate between seven categories (fridge, TV, iron, dryer, lamp, computer and washing machine). A percentage of accuracy of 98.914% was obtained in the training of the network and, thanks to the feedback of the user in the web application, 99.369 and 99.174% were obtained in two cases of retraining of the network.


Introduction
With the demographic increase around the world, electrical energy requirements will imply an exponential demand, many studies are abording this subject, like (Beretta et al., 2020) where that explore pattern recognition techniques to predict electrical consumption. On the other hand and possible solution, users can improve their electrical energy consumption with the use of technology in their homes (smart houses).The development of systems focused on the comfort and safety of people in their homes has gained importance in various research works, especially when the term "smart houses" is incorporated. In Ricquebourg et al. (2006), this concept is discussed, highlighting the idea that smart homes are not restricted only to controlling on-off systems, but also must be able to monitor the devices. While in Chandramohan et al. (2017), the need for the implementation of low-cost smart home systems is highlighted, there is even a system based on an Arduino for the control of light, temperature and alarms, obtaining as a result, a topology that allows the control, and monitoring of lamps and fans. systems, among which the safety of these and user information are highlighted. In Han and Lim (2010a); Han and Lim (2010b), the importance of the implementation of sensors in energy consumption tasks are emphasized, as well as the implementation of wireless communication protocols, wherein both works focus on the implementation of work networks such as the ZigBee, obtaining as results systems that can be used in real environments allowing energy savings.
As a complement to the design of smart homes, in recent years a concept has become important and this is the Internet of Things (IoT). The IoT refers to the connection of elements with the internet, in Gubbi et al. (2013), it presents several potential applications that it has, varying its utility from uses in people for monitoring patients, knowing the traffic in real-time, monitoring of sensors and especially intelligent environments. In He et al. (2014), an application of the IoT focused on vehicles is presented, to offer services in the cloud, they present an intelligent system focused on the parking of vehicles, the system based on the geographical position and availability of the parking spaces allows reserving the parking lot. In Lee et al. (2013), another application of the IoT is presented, where sensors are implemented to predict the production of future harvests from the crop conditions, obtaining as a result, a system that helps the farmers, allowing them to visualize in real-time the monitoring of the crop and prediction of the system. In Kang et al. (2015), a system is implemented for the acquisition of variable information in a house using different types of sensors, such as temperature and gas. The data acquired by these sensors go through data processing and allows viewing the analyzed information through a web platform.
To strengthen and give more meaning to the term intelligent, when talking about smart homes, there are research works that have implemented systems based on artificial intelligence techniques for specific tasks in the home. In Hernandez et al. (2018), a novel system for the control of a light bulb is presented, where the intensity level can be adjusted through the internet and a system based on fuzzy logic is implemented to perform a proportional derivative control for lighting control, it should be noted that the system was implemented in a raspberry pi-3. In Pachón et al. (2018), convolutional neuronal networks are implemented for the detection of products in a refrigerator, wherefrom this detection, the user is notified what products they need to buy, they obtain an accuracy of 96.3% in the prediction of the network. Based on the above, it is shown that the terms smart homes, IoT and artificial intelligence can go hand in hand in the development of systems focused on homes. In Amiribesheli et al. (2015), a review of the state of the art of different techniques based on artificial intelligence that has been implemented in smart homes is presented, among which diffuse systems, support vector machines, and multilayer neural networks are named. Highlighting the application of these methods in different types of activities, such as detection and recognition tasks.
In artificial intelligence techniques, one of the most used and implemented are artificial neural networks (ANN), specifically those that implement an algorithm called backpropagation (BP) which is explained in Rumelhart et al. (1985). In LeCun et al. (1989), one of the first applications with this learning technique is shown, in this case, it is implemented for the detection and classification of written numbers. Recently, in Gordan et al. (2016), the ANN was implemented with the BP learning algorithm, for the prediction of landslides, where results between 72.5% and 93.2 in the accuracy of the predictions were obtained.
In this work, the design of a system is presented, which incorporates some of the benefits of the IoT and artificial intelligence, in a specific task in the home. In this case, based on the electrical devices that a user connects to an electrical outlet, it is intended that a system based on an ANN be able to identify the device that connects and also allows the user through a web page to perform the disconnection of an electrical connection node.
The article is divided into 5 sections, the first presents the general scheme of the system. In the second section, the data acquisition circuit of the devices that are connected to the socket is presented. In the third, the ANN architecture using the BP algorithm for its training. The fourth section presents the web application with which users can send the order of electrical disconnection to a node and feedback to the ANN on the device that was connected. Finally, the re-training of the ANN is presented.

Process Flowchart
In Figure 1, the scheme of the system is shown, whose stages will be explained in detail in subsequent sections. In general, there is a node which, from a signal acquisition circuit and a processing of these in microcontroller system, calculates the power of the appliances and identifies the moments when they are connected and disconnected from the outlet. The data is sent via Bluetooth to an embedded system when requested, to calculate the time of use of the appliances and consumption, to subsequently evaluate the values of power, consumption and time of use in the ANN, to identify which element is being connected. Besides, the embedded system is in charge of communicating with the web platform, where it reads the states of a checkbox to identify if the user wants to disconnect any of the connections of the node, if so, sends to the node an order to not allow the flow of current.
The implementation of an embedded system, as a central processing system, is based on the benefits that some of these offers, such as cost, processing speed, control capacity and communication with multiple devices. In this case, a raspberry pi 3 is selected, which has a 68bit ARM processor with a speed of 1.4 GHz, ethernet connectivity, WiFi and Bluetooth. These embedded systems have been implemented in various applications focused on smart homes. In Vujović and Maksimović (2015), a system is presented in which a raspberry pi performs the reading of nodes of sensors that are found in a house, acquiring data of various variables such as temperature; after reading, the RESTful type web service is used to monitor environmental variables. In Gunawan et al. (2017), an algorithm for the detection of faces using machine learning techniques is implemented in a raspberry, for the opening and closing of a door in a house, obtaining 90% in the accuracy of the system.
Before beginning each of the processes, it is necessary to establish the variables that the node will measure, which will be sent to the embedded system to be classified by the ANN. Based on different tables of energy consumption of household appliances, the power of the connecting element, its consumption and time of use stand out among the variables most used. In this case, it is decided to take seven electronic devices. In Table 1, each of these elements is observed with their corresponding values of power, consumption and time of use.

Node
The first stage focuses on calculating the power of the device, then it is detected if an electronic element has been connected or not. In the second stage, these data are sent by Bluetooth to the embedded system when requested. Finally, according to the state in which the checkboxes are set in the web application, the embedded system sends the order to the node, so that from the implementation of a circuit with relays, the current will be interrupted or allowed. In Figure 2, a general scheme of the main processes of the node is shown. For the calculation of power, it should be remembered that in alternating current (AC), there are three types of power: reactive, apparent and active power. In this case, the latter is the one that is of interest for consumption calculations, since active power refers to the power that is being absorbed by an element in a circuit (Svoboda and Dorf, 2010). In Equation (1), it is shown how to calculate the active power of a device that is connected to the electrical network, it should be remembered that RMS refers to the root mean square value. P = V rms * I rms * cos (∅) The ∅ that is in the equation represents the phase angle between the voltage and current waves, this phase shift occurs when the elements are not resistive, since they can have capacitive or inductive parts. Based on the above, it can be deduced that initially only the value of V rms = 110v √2 is known, the 110v for the value in the voltage between the phase and the neutral, with which one is working.
Equation (2) allows the calculation of the I rms , but for the calculation of the peak current (I peak ), it is necessary to use a current sensor. In this case the ACS712 which is an intrusive hall effect sensor. This sensor allows that from the passage of a current through it, this sends as output a value between 0-5v according to the intensity of the current. This voltage is read in one of the analog inputs of the Arduino and based on the data supplied by the as the sensitivity of the sensor the peak current is calculated.
(2) Finally, for the calculation of the phase angle ∅, the time (td) that passes between the moment in which the voltage wave crosses zero and the moment when the current wave crosses zero is calculated (see Figure 3). To detect the zero crossings of the voltage, an optocoupler H11AA1 is used, with which the zero crossing of an AC voltage can be detected and in this way, when the voltage wave is at zero, it sends a high signal to the Arduino, then a timer starts, and stops until the current sensor detects that the value was zero, to later perform the conversion of time to phase angle. In equation (3), the form to calculate the phase angle is shown, where T refers to the period of the wave.

∅ = td * 360 T
For the detection of moments of connection and disconnection, they are identified the instants where current is detected by the sensor and in those that do not, in order to send these states via Bluetooth to the embedded system to calculate the time the connection lasts.

Brain ANN
In order to have a system capable of recognizing which electronic element has been connected to the electrical network, ANNs are implemented, which from a few inputs, a correct choice of architecture and training parameters can predict which element was connected. In (Goodfellow et al., 2016), the ANNs and even training algorithms such as BP are discussed in more detail.
In Figure 4, a general architecture is shown for the ANN that is wanted to implement. As entrances, it must receive the characteristics selected in previous stages, among which are: the power, time of use of the domestic appliance and consumption. As activation function for each of the neurons of the hidden and output layers, the logarithmic sigmoid is selected, given the properties of its derivative facilitates the learning of the network and programming of the BP, in addition to having output values between 0 and 1, it will facilitate the interpretation of the outputs of the network in terms of percentages of accuracy or belonging to a specific category.
To avoid increasing the complexity of the ANN and taking into account the number of appliances for which the network will be trained, there will only be 3 outputs that will show in binary number the corresponding appliance (see Table 2), thus avoiding having 7 outputs in the last layer of neurons corresponding to each category. Based on the ANN theory, it is necessary to normalize the input values of the network taking into account the selected activation function. In this case, as the logarithmic sigmoid function delivers values between 0 and 1, the characteristics established in Table 1 must have values in these ranges. With Equation (4), the values are normalized, taking into account that c max refers to the maximum value found in a specific characteristic, c min to the minimum and c to the value to be normalized.  (4) It should be noted that the normalization is done by characteristic, therefore for the normalization of the power of the devices, only the power data will be taken into account, but not the time of use or consumption. In Table 3, it is presented the normalized values that will be used for the entries in the ANN. Note that some special cases are exposed for the training like Lamp with zero consumption, in the learning is necessary to orient the network how classifier these unusual cases. For the training of the network, updating of the weights and biases, the algorithm of BP is used. In Equations (5) and (6), the equations for updating the weights and bias are presented, the upper index indicates the layer in which it finds and the lower index the weight to update, α is the learning factor. It is emphasized that the updating of the weights and biases is done starting with the last layer until the first one.
One of the initial problems when using an ANN is to set the number of hidden layers. In (De Villiers and Barnard, 1993), this problem has already been treated, comparing an ANN using BP, having one or two hidden layers, where it is concluded that the behavior is similar, although with two hidden layers the system is more prone to fall in local lows but not in the global one. Another problem that arises is to define the number of neurons in the hidden layers, in this case, the literature tends to vary according to the author and the problem. For example in (Wang, 1994), equation (7) is presented, in (Katz, 1992), it is mentioned that the number of hidden neurons can go between half the number of inputs and three times this value, while in (Kaastra and Boyd, 1996), equation (8) is proposed.
NumberofNeurons = 2 * n in 3 NumberofNeuron = √n in * n out Based on the above, there is no clear consensus on the choice of the number of neurons in the hidden layers, which is why it is decided to apply an iterative process to identify the best training, varying the number of hidden layers up to a maximum of two, and the number of neurons in these layers.
A first test was made with five training sessions with a single hidden layer are presented and the number of neurons in this layer varies. A learning factor of 0.1 is set, as a database only the values mentioned in Table 3 and 1000 training epochs are used. The best training is presented with 5 neurons in the hidden layer, obtaining a 58.6541% in the classification of training data in their respective category. In a second test, the same process is performed with the same parameters of learning factor and number of epochs, for two hidden layers. The number of neurons in both hidden layers are the same and are fully connected. In this case, the best result was obtained with 10 neurons in each of the two hidden layers, presenting an accuracy of 52.89% in its last epoch.
With databases, having only one value per entry in each category is clear that the network will not generalize or learn in a short number of epochs. For this reason, the database is augmented, making random variations between a range of ± 20% over the original values presented in Table 1, but not over the normalized ones, because once the base is augmented, it performs the standardization procedure. Upper values that 20% can increase the time of network learning without improving the accuracy and generate overtraining. In this way, the network will be more robust (accuracy upper 90%) to changes in the time of use, power and consumption that a particular appliance can present.
In agree to previous paragraph a new test is made, Figure 5 shows the training of the ANN with a hidden layer and five neurons, which was the one that had the best behavior in training without increasing the base. In this case, the amount of new data is varied by category.
Based on the training of a hidden layer by increasing the database, it is possible to show that the greater the number of data, the better the behavior of the network. In this case, with 50 new data per category, 97.879% accuracy was obtained in the classification of the training data. The same procedure is performed with the case that obtained a better result with two hidden layers, in this case it is the one corresponding to 10 neurons in each hidden layer. In Figure 6, the training with two hidden layers and augmented database is shown. Figure 6. Training with two hidden layers with augmented database With two hidden layers as well as a hidden layer, the more the bigger the database, the better results are obtained. In this case with 50 new data per category, a 98.914% accuracy in the classification is obtained. The most important difference between using one or two hidden layers is that when using two hidden layers, in a smaller number of times more accurate results are obtained.

Firebase Presentation and Data Tier
The last stage of the system focuses on designing an application that serves as the basis for the electrical disconnection, and in addition to this in case the ANN does not identify the appliance that was connected, it may request information from the user that allows to improve the learning of the network.
A web application is designed, which is supported by a database in the cloud in real time and which can be accessed to read the states of the elements of the web and even change them from the raspberry pi. It is decided to use firebase that allows updating databases in real time and has been implemented in research focused on IoT and smart house. In Li et al. (2018), firebase is highlighted for its multiple services such as user authentication and databases in real time, emphasizing its usefulness as an intermediate web server (Middleware) and final server of the database. In Kumar et al. (2016), in addition to the benefits already mentioned on Google firebase, it highlights the savings in development times, the level of data encryption, the scalability in the projects and even its easy coupling with developments made in Angular or JavaScript.
In Figure 7, the two main panels in the web application are shown. In the panel corresponding to IoT, if the start checkbox is selected, it allows the program in the embedded system to be executed. Depending on which of the connection checkboxes is selected, the embedded system will read this state and send the order to the node to allow or not the flow of the electric current in a particular connection. Finally, if pause the system is required, the checkbox corresponding to this parameter is selected. The home appliance panel is only displayed when the ANN classifies an element, so the user selects the item that was connected, for the ANN to confirm if it is classified in the correct category or it is necessary to update its database and retrain the network so that if the device is reconnected the ANN will classify it in the indicated category.

ANN Retraining
The program designed in the system originally embedded from the data in Table 1, trains the ANN using the BP algorithm. But these values with which the network is trained may not match those of the end-user since it is not the same to compare the consumption of a Led TV to a CRT or a laptop to a desktop computer. That is why once the network performs the classification based on the data of power, time of use and consumption, compares its result with the one provided by the user, if these two do not agree the network will replace the values that exist in their training base, with those calculated by the system. When the replacement is done, the database is augmented again, and the normalization is done to retrain the ANN.
In Figure 8, the retraining of the ANN is shown in blue with the data of a computer where the calculated power was 141.7333 W, the time of use of 8h and its consumption of 1.134 kWh. The re-training of the network with an LCD TV of 113.2672 W, the time of use of 5h and its consumption of 0.566kwh, is shown with orange color.