An Integrated Presence Detection Sensor and Android Mobile Application System for Energy Management and Control

Objectives: It is a common responsibility for individuals and organizations to effectively manage energy in buildings for economic and environmental concerns. In this study, the development of an energy management system integrated with a presence detecting sensor and an android mobile application for remote control of electrical appliances is undertaken. Methods/ findings: The integrated system is basically composed of a human presence detecting Passive Infrared (PIR) sensor mounted on a WeMos D1 microcontroller and remotely monitored and controlled from a web server using an android mobile application via a wireless network. The developed system provides multiple benefits. It eliminates human involvement and manpower often required to manually toggle power outlets and appliances on/off. Application: Furthermore, it conserves electrical energy and prevents wastages by ensuring that energy is only consumed when needed. The developed system is recommended for residential and small office applications.


Introduction
Electricity is a necessary requirement for everyday living as virtually all human activities rely on it for operation and effectiveness. These activities are commonly classified amongst residential and business lines. Lighting, cooking, entertainment, heating, ventilation, and air conditioning are prominent domestic or residential activities requiring electricity while for the business or industrial activities; agriculture, commerce, and manufacturing are examples.
Absence of electric energy grossly affects the quality of life; hence the provision of adequate, reliable, and sustainable energy has always been both the focus as well as the challenge for electricity providers. This challenge is commonly evident in the experiences of supply-demand imbalance and their undesirable effects [1][2][3]. Improper use of appliances and wastages contribute significant portions of consumed energy in residential and commercial buildings with that of the residential being 40% higher than the commercial buildings [4]. This realization underscores the need for energy management in buildings.
In recent times, Information and Communication Technology have been leveraged for energy management in buildings. Consequently, the design and development of Energy Management and Control Systems (EMCSs) are on the increase. EMCSs are computerized control systems designed to regulate energy consumption in buildings by controlling the operation of energy consuming systems such as heating, ventilation, and air conditioning (HVAC), lighting and water heating systems. They are also capable of monitoring environmental and system loads, and adjusting HVAC operations in order to optimize energy usage and respond to demand response signals [5]. Furthermore as explained in Ref. [6], they add intelligence to the home environment, provide an excellent level of comfort and significant energy savings. The design and implementation of an integrated presence detection sensor and android mobile application system for energy management and control which has been undertaken herein, is hinged on the knowledge that energy wastage as well as its conservation is a direct consequence of human activities, and the need for a smart means of recognition of human activities in buildings. This will ensure that power is made available when needed so as to prevent wastages and provide effective power conservation. Furthermore, the android mobile phone application component of the design makes for a smart interface to the system and for the remote control of appliances in line with a consumer's energy conservation plans.
An Ethernet-based intelligent automated system for conserving electrical energy using an Intel Galileo 2nd generation development board was implemented in Ref. [7]. The system remotely monitors and controls electrical devices and switches without any human intervention using internet or intranet enabled web server located at a control room. The system, however, could not to be remotely controlled and monitored using a smartphone unlike that developed in this article.
The "Presence Detection System using loT in Smart Homes", developed by Ref. [8], comprised of a single camera, a micro-controller, and a smartphone. The authors categorized the system in phases. In the training phase, the camera continuously records the performance of the users in the room in the form of images and they are used as the predefined database the real-time data are compared with the predefined database. Once the activities are compared and if these are similar then the corresponding appliances are turned ON automatically. The last phase of the system uses a smartphone and loT-based micro-controller for appliances control in a house using Wi-Fi. The advantages the system had was in the reduction of energy usage and conservation of power as it monitored the activities of users and automatically turn the lights ON or OFF but it is not cost-effective as a PIR sensor as used in this article is cheaper in price and can also be used to monitor and detect human presence. Also, storing camera images take up lots of memory space and sometimes could be lost leading to malfunction of the system, especially during the comparison between the real-time data and predefined database. This situation will not occur with a sensor as its ability to detect human activities is not based on its interaction with a database.
In Ref. [9], a bluetooth-based home automation system using android and arduino was designed and implemented. The system included three main components: an Arduino microcontroller for connecting the appliances, a bluetooth module for signal transfer, and a smartphone with the android application to control home appliances. Remote control of appliances was achieved; however, the use of bluetooth with its limited range would entail users to be within 10-100 m before they can control the system. This limitation does not arise in the system developed in this article which employs the wifi as its remote communication technology.
In Ref. [10], an android-based home automation system using a raspberry pi is developed. Their system remotely controlled home electrical appliances such as light and fans via a 3G mobile network. The status of the appliances is collected by the raspberry pi which served as the main processor and data are accessed using an android application operating on smartphone. Although this system creates a user-friendly mobile interface for the remote control of appliances, it lacked unlike the system developed in this article, a means for the control of appliances using the monitoring and detection of human motion, which makes for better energy conservation.
An architecture and execution strategy for Home Energy Management System (HEMS) using Wi-Fi and RF Communication are presented in Ref. [11]. The developed HEMS just as the system developed in this article, provide an easy to use interface for saving energy and allow the user to remotely control their home appliances using an Android App. In addition, the HEMS also has features for scheduled controlling of devices.

Materials
The following materials were used in the realization of the system viz: (a) WeMos D1 micro-controller, (b) PIR motion sensors, (c) Two-channel relay module.
Also, (d) Web server and database, and (e) Android mobile application, although developed were also used in the actualization of the integrated system.

WeMos D1 Microcontroller
The WeMos D1 micro-controller as shown in Figure 1 was employed as the microcontroller. It primarily coordinates the entire activity of the system. Based on the ATmega328P with an ESP8266 Wi-Fi module, it has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 160 MHz ceramic resonator, a USB connection, a power jack, an ICSP header, and a reset button [12]. Its operating voltage and current are respectively 3.3V and 80mA while its operating temperature ranges between −40 °C and 125 °C. Its integration with the TCP/IP protocol stack enables it to access Wi-Fi network.

PIR Motion Sensor
For the monitoring and detection of motion, the PIR sensor HC-SR501 was used. It has an operating current of 50 μA, operating voltage range of 5 V-20 V, and temperature range of −15 °C to 70 °C. Its principle of operation, just like most PIR sensors is hinged on the reception and measurement of ambient temperature in the form of infrared radiation, usually emitted from the bodies of living things. Figure 2 [13] is a typical block diagram of the PIR motion sensor.

Two-Channel Relay Module
This acts as an electrically operated switch controlled with a small signal from the WeMos. Through the microcontroller, it controls the flow of electricity to the lighting points. It operates within an input range of the WeMos' output and handles a load voltage of 240VAC. Its operating voltage range is between 5 V and 12 V while that of its current is between 15 and 20 mA/channel [14]. A two-channel relay is shown in Figure 3.

Web Server and Database
A web server is a system that delivers content or services to end-users over the internet. A web server consists of a physical server, server operating system (OS), and software used to facilitate Hypertext Transfer Protocol (HTTP) communication. The node.js software was used as web server in the implementation of this work. This software allows a personal computer to be used as a local server or local host. An online web server can also be used. A database was created for the user for granting access to the designed mobile app whenever the correct login details are provided. The "MongoDB" database was used. It extracts information from the database in response to queries. The EMCS system database consists of the "user" which contains information such as username and password. These are supplied by the registered user to enable access to the mobile application. After the creation of the database, the software "brackets" was used to create a node.jsscript called "register". This enables the user to register with the system and have the details saved in the database. Whenever the user logs in, proper authentication is carried out to prevent unauthorized users from having access to the system. Also, two other node.js scripts work in conjunction with the register node.js script. They are the "connect" node.js script which ensures that the connection between the server and database is made and the "add user" node.js script which ensures that users who have filled in their details are added to the database.
A login node.js script is written to allow registered users have access to the control page of the mobile application. A dashboard node.js script is written to display the control buttons of the outlets. By clicking the ON button, logic 1 is uploaded to the web server by the microcontroller and, the outlet is turned ON.
To enable communication between the microcontroller and the web server, a Java library is imported in the microcontroller code to make HTTP connection to the web server. The postman software was used to test the various Application Programming Interface (API) endpoints used in building the backend/server of the mobile application. Postman enables you test your endpoints by allowing you make http requests.

Android Mobile Application
The android mobile application was developed with the Java programming language notable for its reliability and simplicity. The mobile app is first installed on the mobile phone or tablet. From the mobile app interface, the user can have access to remotely control the outlets using onscreen buttons. The different interfaces of the mobile app were tested to ensure compliance with their specified requirements without requiring knowledge of their inner design or logic. The test was also carried out to identify logical errors in the software To control and monitor the outlets through the mobile application, the user must be authenticated first. The user does that by entering his/her username and password. The authentication is done for security and access to only authorized users. Figure 6 shows a flow chart of the authentication process. The user should also be able to control his   The two-channel relay is connected to the GPIO pin of the 2ndWeMos D1 ESP8266 Wifi microcontroller through a 220Ω resistor and is controlled by the actions of the PIR sensor. On the detection of motion by the PIR sensor, the microcontroller triggers the two-channel relay to turn on the connected devices through the power outlets. It infinitely loops the process until it is powered off. Also, if motion is not detected, the system can be turned ON or OFF with the android mobile application remotely.
The two microcontrollers communicate via the internet (cloud). When human presence is detected, the microcontroller connected to the sensor (1st microcontroller) sends a logic 1 signal to the database while the microcontroller connected to the actuator (2nd microcontroller) retrieves the value from the database and performs the necessary action. There are hardware connection between the Wemos D1 microcontroller, PIR sensor, twochannel relay, LEDS and resistors. Figure 9 is the flow chart illustrating the sequence of operation of the system. The system algorithm was realized using a Java script.

Results and Discussion
On completion, the designed prototype performed as expected. The system controlled energy supply to the power outlets in line with human presence detected by the PIR. Furthermore, the power outlets could also be remotely controlled using the developed mobile app. These control mechanisms ensured better energy management as electric power is supplied to the outlets only when needed. Table 1 shows the command sent from the mobile application and the observations, while Figure 10 is a screenshot of the control page of the mobile app.

Conclusion
Detection of human presence or motion in spaces can be made possible using a PIR sensor integrated system to automatically switch ON or OFF an appliance in order to conserve energy as demonstrated in this study. The inclusion of the mobile app in the developed system enables the remote control of appliances. It gives improved convenience and control in the conservation of electric energy. The developed system is recommended for residential and small office applications.