home automation system - theseus
TRANSCRIPT
Giang Tran
Home Automation System Using Raspbeery-Pi Through Web Application
Metropolia University of Applied Sciences
Bachelor’s Degree
Electronics
Thesis
21 March 2021
Abstract
Author(s) Title
Giang Tran
Home Automation System Using Raspberry-Pi Through Web Application
Number of Pages Date
33 pages + 2 appendices
21 March 2021
Degree Bachelor's Degree
Degree Programme Electronics
Specialization option
Instructor(s) Anssi Ikonen, Senior Lecturer
This thesis aims to clarify home automation's history, the sensors' theoretical framework and
introduce a simple, low-cost home automation system operating on Raspberry-Pi. The end
goal is to show the simplicity and cost-efficiency of building home automation in the current
modern-day.
The thesis presents the hardware and software needed to construct mentioned home
automation system. It introduces NodeJS as a back-end server as well as a data logger.
ReactJS is used to shape the User Interface and display the server's data on the front-end.
The Raspberry-Pi serves as a controller for the system by storing the server and the main
program itself. Testing is done at the end to prove that the prototype works as intended.
The result is a prototype of a low-cost home automation system with essential functionalities
such as motion-detecting, flame-detecting, temperature and humidity measurement.
Contents
Terms, Acronyms, and Definitions
1 Introduction 1
2 Milestones of Home Automation 1
2.1 The Start of Home Automating Ideas 1
2.2 The Future of Home Automation 2
2.3 Examples of Home Automation's Applications 2
2.3.1 Lighting in Home Automation 2
2.3.2 Doors in Home Automation 3
2.3.3 Thermometers in Home Automation 3
2.4 Smart Home Downsides and Risks 3
3 Theoretical Study of Home Automation System 3
3.1 Sensors 4
3.1.1 PIR Structure 7
3.1.2 The Lens of the PIR Sensor 10
3.2 The Controller of the System 13
3.2.1 Introducing Raspberry Pi 14
3.2.2 Raspberry Pi Programming Languages 14
3.2.3 GPIO 15
3.2.4 Linux Operating System 16
4 Building a Cost-efficient Home Automation 17
4.1 Hardware 18
4.1.1 Temperature and Humidity Sensor 18
4.1.2 Motion Sensor 20
4.1.3 Flame Sensor 22
4.1.4 Raspberry Pi 24
4.2 Software 25
4.2.1 Express Server Using NodeJS 25
4.2.2 REST API 25
4.2.3 Raspberry Pi Programming 26
4.2.4 Building a Web App Using ReactJS + ReactJS Hooks 27
4.3 Testing the Prototype 28
4.3.1 Test Plan 28
4.3.2 Testing the PIR Motion Sensor 28
4.3.3 Testing the Flame Sensor 29
4.3.4 Testing the Temperature and Humidity Sensor 30
4.3.5 Testing Conclusion 30
4.4 The Project's Budget 31
5 Discussion 32
6 Conclusion 32
References 34
Appendices
Appendix 1. ExpressJS Server Code
Appendix 2. Raspberry Pi Main Program
Terms, Acronyms, and Definitions
AI Artificial intelligence or automated intelligence
API Application programming interface
ARM Advanced RISC Machines
DOM Document Object Model, defines the logical structure of HTML and XML
documents and the way a document is accessed and manipulated.
GPIO General Purpose Input Output
HDMI High-Definition Multimedia Interface
I2C A synchronous, multi-master, multi-slave, packet-switched, single-ended,
serial communication bus
IFTTT If This, Then That, a service that allows a user to program a response to
events in the world of various kinds.
IR Infrared sensor
IoT Internet of things
JFET Junction-gate field-effect transistor
JS JavaScript
JavaScript A programming language that conforms to the ECMAScript specification
NodeJS An open-source, cross-platform, back-end JavaScript runtime
environment
OS Operating System
OTP One time programmable
PIR Passive infrared sensor
Python An interpreted, high-level, and general-purpose programming language
REST Representational state transfer
RaspPi Raspberry Pi, a series of small single-board computers
ReactJS An open-source, front-end JavaScript library for building user interfaces or
UI components.
SPI Serial Peripheral Interface
USB Universal Serial Bus
1
1 Introduction
By definition, home automation is building automation for a home, called a smart home
or smart house. A home automation system will control lighting, climate, entertainment
systems, and appliances. It may also include home security, such as access control and
alarm systems. When connected with the Internet, home devices are an essential
constituent of the Internet of Things. (1.) Home automation is the world's new trend. It
offers a lot to humanity, and its pros outweigh its cons in many ways.
The Digital Revolution marked a new era for humankind. Almost everything is now
digitalized and automated. Thanks to robots' and Ais' assistance, people's quality of life
increases significantly over time. A home is a place where people spend most of the time
feeling comfortable and protected from unwanted visitors. With the help of technology, it
can be transformed into a smart home to provide further convenience and security.
This document aims to study the background of Home Automation technology, have a
detailed look at sensors, especially PIR sensors, and demonstrate the simplicity in
building an elementary home automation system using Raspberry Pi.
2 Milestones of Home Automation
2.1 The Start of Home Automating Ideas
Within a few hundred years, humans have had a giant leap in digital development.
Engineers shrunk down the computer from the size of a room to a small, carriable
suitcase.
From the early 1900s, the first home appliances, ranging from the clothes iron to
electrical washing machines, were invented in the Industrial Revolution. Though they
were not "smart," they lent a hand in improving life quality. A few decades later, in the
1930s, inventors introduced automated homes and intelligent appliances concepts. The
idea was indeed found fascinating by the spectators.
2
In 1966, ECHO IV (2) was invented as the world's first home automation system.
Although it was never marketed, it showed the progression and desire towards building
a home automation system.
The 1980s saw the growth of automated technology in garage doors, temperature
control, et cetera. Though the technology appears to be subtle, it follows humankind to
the current date.
Finally, the world started a whole new millennium of smart devices in the year 2000.
Smart appliances, smartphones, especially, have been more advanced and affordable
for people worldwide. (3.)
2.2 The Future of Home Automation
The definition of IoT is required in order to understand how home automation works.
"Internet of Things" is an umbrella term used for all technologies that enable connecting
a device to the Internet. Such systems depend on the collection of data. The data is then
used to monitor, control, and transfer information to other devices via the Internet. It
allows specific actions to be automatically activated whenever certain situations arise.
(4.) As a simple example, a smart fire alarm will push an urgent notification to the user if
it detects an unwanted ongoing fire situation.
By applying the same concept to our homes and appliances, we can effortlessly create
an IoT-powered smart home. Nowadays, almost all smart devices allow us to control
them using an app interface or voice control. Big companies are using and developing
voice-controlled personal AI assistants such as Google's Google Assistant, Amazon's
Alexa, and Apple's Siri.
2.3 Examples of Home Automation's Applications
2.3.1 Lighting in Home Automation
Lighting plays a vital role in one's living routine. While switching a light on and off seems
lightweight and straightforward, repeating in at certain times every day tends to be
tedious. With IFTTT enabled, the lights can respond differently to each different situation.
For example, a light will turn off in the daytime and turn on during nighttime without user
3
interaction. It can also be programmed to turn on only if there are people nearby and turn
off automatically when the area is empty.
2.3.2 Doors in Home Automation
Doors are a way for us to protect our privacy. In that sense, they can be integrated with
face-recognition to guard against any unwanted visitors. They can also keep a log of
activity to monitor who has been tampering with the user's private space. Nonetheless,
they can be implemented together with the lighting solution to save electricity and reduce
CO2 emission, for instance, the lights turn on when the door opens, and they will turn off
after some given time.
2.3.3 Thermometers in Home Automation
A thermometer is one of many great tools to be implemented into a smart home. It
measures the indoor temperature and, perhaps, adjusts the air conditioner to match the
user preferences. There are many other ways that the intelligent system can actively
adapt itself to our personalized settings.
2.4 Smart Home Downsides and Risks
Although the mentioned benefits are significant, we cannot deny the main drawbacks of
using a smart home: security and data breach. No matter how secured a smart device
is, there is always a risk that it can be hacked into or tampered with by hackers. In which
case, a data breach is inevitable as the third party gathers information of the user's
conversation or personalized environment. The more devices the system has to manage,
the more maintenance it needs to ensure no security breach and data exposure.
For the smart home to work uninterruptedly, a stable internet connection is a
requirement. Without it, the system cannot send and retrieve its data to the user and,
therefore, nullify the system's benefits. (5.)
3 Theoretical Study of Home Automation System
The document has clarified some milestones in the timeline of home automation
development throughout the years. However, what are the fundamental elements of the
4
system? The following section covers the essential elements needed for the home
automation complex to be functional in detail.
3.1 Sensors
A sensor is a device, module, machine, or subsystem whose purpose is to detect events
or changes in its environment and send the information to other electronics, frequently a
computer processor. A sensor is always used with other electronics. (6.)
Sensors are widely used in everyday applications. They become so common that we
do not even recognize that they are there, and we take them for granted. For instance,
people's smartphones for mundane work are composed of many sensors, such as
ambient light sensors, accelerometers, and gyroscope sensors. Sensors are almost
always digital. They are accepted widely in temperature measurement, smoke
detection, fire detection, and many more use cases.
Sensors react to changing physical conditions by altering their electrical properties.
Therefore, most artificial sensors rely on electronic systems to capture, analyze and relay
information about the environment. In short, a sensor converts surrounding property such
as heat, light, and sound into electrical signals. These signals are usually converted into
binary code that a computer can process. (7.)
Electronic sensors are divided into two types: Analog sensors and digital sensors.
Analog sensors turn data from the environment into an analog signal, while digital
sensors convert physical data to a digital signal. Analog sensors are more accurate
than their digital counterparts simply because digital signals are limited to a finite set of
possible values, while analog has an infinite set of values.
5
Figure 1 Analog vs. Digital Signal (8)
Both analog and digital signals are superb for transferring information. Both have
different benefits and downsides. As shown in Figure 1, an analog signal is depicted by
a sine wave shape representing physical measurement, whereas square waves draw a
digital signal. While analog signals can have an infinite set of values, digital signals are
stuck with either HIGH or LOW as their most basic form. On the contrary, high-bit
digital signals can have as much as 2 to the power of bit count different configurations
to represent the information; for instance, 224 sets for a 24-bit digital signal. (8.)
Based on their fundamental differences, noise can reduce the analog measurement
accuracy. Digital signals' accuracy depends on a variety of different concepts. First of
all, the number of states available. The total number of states can reach 2n, where n is
1, 2, 3…. What is next is how many times the signal is sampled during the conversion
step. The more times, the more accurate the result would be. Nevertheless, the bitrate.
The bitrate is the amount of information recorded every time the signal is sampled. In
that sense, a higher bitrate means more information recorded and hence, more
accuracy. (8.)
6
A sensor must always obey the following rules: Firstly, it must be sensitive to the
measured property. Secondly, it must be nonreactive to other properties that are likely
to be encountered in its application. Lastly, it must not influence the measured property.
(6.) For example, a temperature measuring sensor must be sensitive to the temperature
changes in the surrounding environment. Besides, its operation should not create heat
that affects the environment in any way.
Most sensors use waves likes ultrasonic waves or infrared waves to detect objects and
changes in their environment. They have an energy source inside that allows them to
emit radiation to their target. The radiation is reflected by the object and detected by the
sensor. This kind of sensor is considered to be active sensors. (7.)
Passive sensors do not send out radiation or waves. They detect radiation emitted from
other objects, such as heat or thermal infrared radiation. (7.) Let us take an in-depth look
into one of the most common passive sensors, the PIR sensor. For a quick look at the
PIR sensor, see the following Figure 2.
7
Figure 2 Typical PIR representation (9)
A typical PIR sensor usually presents a white plastic "hat" covering the sensing window
underneath, together with three pins. These pins are usually Ground, Supply Voltage,
and Output Signal pins.
3.1.1 PIR Structure
The IR sensor is sheltered within an airtight metal container, improving accuracy against
noise, temperature, and humidity. On top of the metallic container goes the silicon-coated
IR-transmissive material protecting the fundamental core sensing element. The two
sensors are located inside the container. (9.) The illustration is below in Figure 3.
8
Figure 3 PIR Structure (9)
The sensing parts are made of pyroelectric material, meaning it is sensitive to
temperature. In an ambient environment, it generates no voltage. However, if there are
any changes in temperature in said environment, a voltage is generated temporarily. In
that sense, the motion created by an IR source translates into the voltage created by the
material.
Figure 4 Cross-sections of PIR with two sensing elements (9)
Figure 4 shows two sensing elements position of a PIR sensor. Usually, a PIR sensor
has two or four sensing parts; each part acts as an "eye" monitoring different areas. Two
sensing elements can only detect horizontal motions, while four elements can detect
both horizontal and vertical.
9
Figure 5 Internal PIR diagram (9)
Figure 5 illustrates that the two sensing elements are wired in an inverted serial, meaning
one part's positive is connected to the other's positive (or vice versa). This way of wiring
achieves two things. Firstly, the IR coming from surfaces in a still environment does
cause the pyroelectric materials to create voltages. However, the voltages generated are
equal and in a reverted direction. In this case, they cancel each other out. Secondly,
when an IR source comes to the field of view of one of the two elements, one of the
sensing elements creates more voltage than the other due to IR exposure. The voltage
difference at the JFET transistor gate leads to a voltage pouring to the drain port. The
illustration is shown below in Figure 6.
10
Figure 6 PIR working mechanism Illustration and output result (9)
While being idle, the PIR detects steady IR waves from the walls and ambient
environment to its two mentioned slots. When an IR source, for example, emitted from
humans or animals, passes by, it intercepts one of the slots in the PIR sensor. The event
causes a positive differential between the two halves. When the IR source leaves the
active area, the process happens in reverse, meaning the PIR instead produces a
negative differential. (9.)
3.1.2 The Lens of the PIR Sensor
On the first look, the 'hat' of PIR sensors appears to be a cheap small plastic piece attach
on top of a PIR sensor. Although small and low-cost, it plays a significant role in
complementing the sensor's functionality. The lens impacts the width, range, detecting
11
pattern effortlessly. It is made of multiple scattered Fresnel lenses, named after a French
physicist Augustin-Jean Fresnel. (10.)
The Fresnel lenses are usually seen in imaging, illuminating and on stage. Its design
allows the construction of lenses with a large aperture and short focal length without the
mass and volume of material that a lens would require in the conventional design (see
Figure 7). In short, a Fresnel lens does almost the same thing as its Plano-convex lens
counterpart, with the leverage of being more lightweight. It can be made much thinner
than an equivalent conventional lens, and in some cases taking the form of a flat sheet.
(10.)
The Fresnel lens is likely to be made of glass on staging and illuminating due to the heat
generated. However, the PIR sensor lens can be made of plastic because it does not
heat up during operation.
Figure 7 Fresnel lens versus Plano-convex lens (10)
The detecting area is two parallel rectangles, as seen in the sensor's cross-sections
depicted in Figure 4. In most use cases, a broader and larger detecting area is preferred,
and that is where the lens is needed. The small, thin, and moldable plastic lens acts as
a camera lens: it condenses an extensive area into a compact one, in this case, the
silicon-coated window. Although this may add unwanted distortion, the downside is
neglectable for the mentioned benefits.
12
Figure 8 Light condensed by Fresnel lens (10)
Figure 8 shows the Fresnel lens serving as a conventional lens to increase the sensing
element range significantly.
Figure 9 Macro shot shows the inside of the dome lens (10)
Figure 9 features the inside the 'hat' of the PIR sensors. In there, various Fresnel lenses
are responsible for dividing the detection zone into many smaller rectangle zones. It is
preferred to have many small but scattered detection zones than one big one. (9.)
13
Figure 10 Scattered PIR detection zones with Fresnel lens (11)
The separated zones (Figure 10) help the sensors to recognize motion from an IR source
more broadly and directionally. As said, if only two elements are presented, the
movement can only be in one direction. Many detection zones mean the movement can
be detected in any orientation. When an IR source comes into the zones, the ambient
temperature changes to body temperature, causing the voltage differences, as
mentioned in the previous subchapter.
3.2 The Controller of the System
A control unit is a must for every home automation system. Its roles are to control and
update the information received from sensor components. Without it, it will be challenging
to handle and customize the data to our wishes.
While a full-size desktop can achieve many things, including operating home automation,
the size and cost are unsuited for such tasks. The control unit is preferred to be cost-
efficient, small yet powerful enough to handle data. Luckily, two noticeable low-cost
pocket-size computers meet the requirements available in the market, being Arduino and
Raspberry-Pi.
14
While Arduino is known for its in-depth technical potential, RaspPi is recognized for its
beginner friendliness and Python programming language. As a foundation for the later
demonstration project, let us look more in-depth into the RaspPi.
3.2.1 Introducing Raspberry Pi
The Raspberry Pi (Figure 11) is a credit-card-sized computer that uses the standard
keyboard and mouse. It comes with HDMI ports for monitor display, USB ports for
keyboard and mouse, a 3.5mm port for speakers, and an ethernet port for internet
connection. Its capability is almost limitless, for instance, exploring the Internet, learning
new programming languages, or playing games.
Figure 11 A Raspberry Pi without its cover (12)
3.2.2 Raspberry Pi Programming Languages
RaspPi is recommended to be used with Python programming language due to its
simplicity and beginner friendliness. Of course, any programming language which can
be compiled for ARM v6 can run on the RaspPi. The adaption to new programming
15
language from the RaspPi is growing throughout the years. There are numerous
preinstalled languages, for example, Java, C, C++, Scratch, and Python.
3.2.3 GPIO
Raspberry Pi features 40 GPIO pins along the top edge of the device. The pins are
designated to be inputs and outputs for various intents. For details, see the following
illustration in Figure 12.
Figure 12 Raspberry Pi GPIO pins (12)
There are two 5V, two 3.3V, and a handful of GND pins presented on the board. All of
them are unconfigurable. The rest are general-purpose 3v3 pins. These pins can be set
to HIGH (at 3.3V) or LOW (at 0V) as output, or they can be read as HIGH and LOW for
input. (12.)
Besides simple input and output devices, the GPIO pins can be used with a range of
alternative functions. Some are available on all pins, others on specific pins. See the
following functions list (12).
• PWM (pulse-width modulation)
o Software PWM available on all pins
o Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
• SPI
16
o SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8),
CE1 (GPIO7)
o SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18);
CE1 (GPIO17); CE2 (GPIO16)
• I2C
o Data: (GPIO2); Clock (GPIO3)
o EEPROM Data: (GPIO0); EEPROM Clock (GPIO1)
• Serial
o TX (GPIO14); RX (GPIO15).
3.2.4 Linux Operating System
Linux is a family of open-source Unix-like operating systems based on the Linux kernel;
an operating system kernel first released on 17 September 1991 by Linus
Torvalds. Linux is typically packaged in a Linux distribution.
Distributions include the Linux kernel and supporting system software and libraries,
many of which are provided by the GNU Project. Many Linux distributions use the word
"Linux" in their name, but the Free Software Foundation uses GNU/Linux to emphasize
the importance of GNU software, causing some controversy.
Popular Linux distributions include Debian, Fedora, and Ubuntu. Commercial
distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.
Desktop Linux distributions include a windowing system such as X11 or Wayland and
a desktop environment such as GNOME or KDE Plasma. Distributions intended
for servers may omit graphics altogether or include a solution stack such as LAMP.
Because Linux is freely redistributable, anyone may create a distribution for any purpose.
(13.)
Raspbian is a free operating system based on Debian optimized for the Raspberry Pi
hardware. An operating system is the set of basic programs and utilities that make
Raspberry Pi run. However, Raspbian provides more than a pure OS: it comes with over
35,000 packages, pre-compiled software bundled in a friendly format for easy installation
on the Raspberry Pi.
17
The initial build of over 35,000 Raspbian packages, optimized for best performance on
the Raspberry Pi, was completed in June of 2012. However, Raspbian is still under active
development, emphasizing improving the stability and performance of as many Debian
packages as possible.
Raspbian is not affiliated with the Raspberry Pi Foundation. Raspbian was created by a
small, dedicated team of developers that are fans of the Raspberry Pi hardware, the
educational goals of the Raspberry Pi Foundation, and, of course, the Debian Project.
(14.)
4 Building a Cost-efficient Home Automation
Next, this document will demonstrate the making of a low-cost home automation system
from hardware installation to software programming. The scopes of this project are:
❖ To measure the temperature and humidity of the environment using DHT11.
❖ To log motions activity detected by Grove PIR Motion Sensor
❖ To log flame activity detected by Grove Flame Sensor
❖ To send all of the measured and collected data to the NodeJS server.
❖ To display the data through a web application
See the diagram below in Figure 13 to have an overall idea of the system design.
18
Figure 13 Home Automation dataflow diagram
The three sensors used in this project are all connected to the Raspberry Pi. The NodeJS
server is also running on Raspberry Pi. It is designed in order for the sensors to be on
the same device as the server. The front-end web application then picks up data from
NodeJS and renders it on the website for phones, PCs, or other devices.
4.1 Hardware
4.1.1 Temperature and Humidity Sensor
Grove - DHT11 Temperature & Humidity Sensor is a high-quality, low-cost digital
temperature and humidity sensor based on the DHT11 module (15). The overall look of
the sensor can be seen in Figure 14 below.
19
Figure 14 A look at temperature and Humidity Sensor (15)
DHT11 is the most common temperature and humidity module for Arduino and
Raspberry Pi. Hardware enthusiasts favor it for its many advantages, such as low power
consumption and excellent long-term stability. Relatively high measurement accuracy
can be obtained at a low cost. The single-bus digital signal is output through the built-in
ADC, which saves the I/O resources of the control board. (15.)
Its specifications are suitable for the demonstration. Its dimension and weigh fit well with
Raspberry Pi. It operates on 3.3V or 5V, which can be provided from Raspberry-Pi pins.
More detail can be seen in Figure 15 below.
20
Figure 15 DHT Technical specifications (15)
Moreover, Grove - Temperature & Humidity Sensor uses the upgraded version of
DHT11. The new version of the DHT11 module replaces resistive humidity components
with capacitive humidity components. The temperature and humidity measurement
range is broader, and the temperature resolution is higher than its previous versions.
(15.) The collected data is sent through pin 17 of the Raspberry Pi and is saved on the
NodeJS server.
4.1.2 Motion Sensor
The Grove - PIR Motion Sensor (Figure 16) can detect infrared signals caused by motion.
If the PIR sensor notices the infrared energy, the motion detector is triggered, and the
sensor outputs HIGH on its SIG pin. The detecting range and response speed can be
adjusted by two potentiometers soldered on its circuit board. The response speed is from
0.3s - 25s, and max 6 meters of detecting range. (16.)
21
Figure 16 Grove PIR motion sensor (16)
The Grove - PIR Motion Sensor is an easy-to-use motion sensor with Grove compatible
interface. It can be used as a suitable motion detector for Arduino and Raspberry Pi
projects, for example, in security alarm systems and automatic lighting applications. (16.)
In this project, the Grove PIR motion sensor, shown in Figure 16 is used to detect and
log possible motions. The sensor has the following specifications in Figure 17.
22
Figure 17 PIR Motion Sensor Specifications (16)
When the motion is detected, a HIGH output signal is received on pin 14 of the RaspPi.
The RaspPi then sends an API request to the server, asking the server to log to a log
file.
4.1.3 Flame Sensor
The Grove - Flame Sensor (Figure 18) can detect fire sources or other light sources of
the wavelength range of 760nm - 1100 nm. It is based on the YG1006 sensor, a high-
speed and highly sensitive NPN silicon phototransistor. Due to its black epoxy, the
sensor is exposed to infrared radiation. (17.)
23
Figure 18 Grove Flame Sensor (17)
The flame sensors will play a crucial part in the fire alarm for this demonstration. Figure
19 below shows its specifications.
Figure 19 Grove - Flame Sensor specifications (17)
Likewise, when there is a flame detected in the field of view, the RaspPi asks the server
to log down the activity and save it to display to the user later on. It is connected through
pin 23.
24
4.1.4 Raspberry Pi
Finally, we have the Raspberry Pi in the setup as a central control unit. Data collected
from sensors will be processed and controlled by the Raspberry Pi.
Figure 20 Raspberry model 3B+ (18)
We will use Raspberry Pi model 3B+ (Figure 20). With an upgraded processor boasting
impressive new packaging and improved networking capabilities, the new Raspberry Pi
3B+ has a generally better specification than its predecessor, the Raspberry Pi 3. (19.)
The detailed specifications can be seen in the following Figure 21.
25
Figure 21 RaspPi 3B+ Specifications (19)
The RaspPi collects data from the sensors and instructs the server to log it down. It also
serves as a place to store the log files.
4.2 Software
4.2.1 Express Server Using NodeJS
Express is a minimal and flexible Node.js web application framework that provides a
robust set of features for web and mobile applications (20).
A server is crucial for users to communicate with RaspPi, thus communicate with the
sensors. For simplicity, the server will also serve as a database recording the history of
PIR motion and Grove Flame sensor detections.
4.2.2 REST API
REST API has a vital role in the communication of the web app and the server. On the
web app first load, it hits the server and requests the needed information. The server
then returns the information and displays them on the front end.
For this project, several API routes needed to be built, three of which are needed for
flame, motion, and temperature and humidity sensors. One more is needed for the front
end to call whenever it needs a server's data update. The entire program can be found
in Appendix 1.
26
4.2.3 Raspberry Pi Programming
After setting up the server, the next needed thing is to program the Raspberry Pi, so it
works as intended. We will create a looping program to check the states of the sensors.
If any sensor is tripped, we will send a REST API call to the server to update the data.
The temperature and humidity sensor continuously gets updated regardless of the other
sensors' activity. See the following flowchart in Figure 22.
Figure 22 In-depth details data flowchart
The loop goes first to check if the motion sensor state has recently changed from LOW
to HIGH. If it does, then RaspPi sends a PUT request to
27
localhost:3000/api/motionSensor. Likewise, if the flame sensor is triggered,
RaspPi sends a PUT request to localhost:3000/api/flameSensor. Temperature
and humidity data also get sent to the server if there are any changes. The server will
pick up the PUT requests, log down the activity and data, ready to serve them to the
front-end.
The whole process will be looped every 1 second, ensuring that the server's data is up-
to-date. The server file and the main program will be located on the Raspberry Pi. They
will be initialized after the RaspPi boots, which can be achieved by modifying
/etc/rc.local file. See the Appendix 2 for the Python full program's code.
4.2.4 Building a Web App Using ReactJS + ReactJS Hooks
React is an open-source, front-end JavaScript library for building user interfaces or UI
components. It is maintained by Facebook and a community of individual developers and
companies. React can be used as a base in the development of single-page or mobile
applications. (22.)
There are plenty of documents explaining in-depth how React works. However, it is out
of the scope of this piece of work. In short, developers write "components" for a website
and place them together on a browser's DOM. The website is managed through a state
management system. In simplicity, every time the state changes, the content" re-render"
or refreshes itself, therefore, display the latest information.
When a user opens the website, it first fetches the server's data, updates the
components, and continues to fetches data every 4 seconds afterward, hence making
the components re-render, displaying the newest data. However, the weather is fetched
every 10 minutes from an external source (openweathermap.org) due to the lack of
budget.
A complete front-end code and components can be found in the following URL:
https://github.com/Kyokatarz/thesis-frontend
28
4.3 Testing the Prototype
4.3.1 Test Plan
Several tests will be conducted to prove that the system is working as intended:
• Check the motion sensor functionality
o Create movement in front of the PIR sensor and check the motion logs
o Measure the detecting range of the PIR sensor (optional)
o Measure the detecting angle of the PIR sensor (optional)
• Check the flame sensor functionality
o Create a heat source in front of the flame sensor and check the flame logs
o Measure the detecting range (optional)
• Check the temperature and humidity sensitivity
o Create a heat or coolness around the DHT11 sensor and check for
changes in the application
o Increase and decrease environment humidity around the DHT11 and
check for changes in the application
Disclaimer: Fire hazards are not to be taken lightly while conducting the tests. Reproduce
the tests at one's own risks.
4.3.2 Testing the PIR Motion Sensor
In order to test the PIR motion sensor, create a motion in front of it. In this study, a motion
was created by waving in front of the sensor. The application then showed the time when
the motion was detected, as shown in Figure 23.
29
Figure 23 Server logs of the most recent movements
Further testing was done to measure the detecting distance and detecting the angle of
the sensor. The sensor is efficient at around 2.7m to 3m. A distance further than that
causes the detection to be unreliable. The sensor can also detect motion around 60
degrees each side from its center, which makes 120 degrees in total. The results seem
to match the specifications in Figure 17.
4.3.3 Testing the Flame Sensor
By creating a fire source and directly pointing the black epoxy of sensor to the fire source
direction, we can test whether the flame sensor is functional. In this case, candles were
used to conduct the test. After doing so, the flame sensor log was checked, as shown in
the following Figure 24.
Figure 24 Flame logs of the most recent activities
30
More testing was also done to measure the detecting range of the flame sensor. Based
on the specifications, theoretically, the sensor can detect flame up to 1 meter. However,
while conducting the test, it is found that the sensor efficiently works around 0.75 meters,
further than that seems to give a fluctuating result.
4.3.4 Testing the Temperature and Humidity Sensor
The log in the application seems to show an expected value of the environment
temperature and humidity, as shown below in Figure Figure 25:
Figure 25 Room temperature and humidity (sampled on 16 February 2021)
Further testing was done to test whether the result updates as the temperature and
humidity change. One of the methods is to breathe briefly into the sensor. The warmth
and moisture of a human breath should be sufficient for the sensor to pick up.
Doubtlessly, the temperature rises to 30-32 degree Celsius, and the humidity rises to
95%. The actual humidity may have been even higher than the recorded number, but
due to the sensor technical limit (as shown in Figure 17), it can only read to up to 95%
humidity.
4.3.5 Testing Conclusion
After evaluating the product's results, it is safe to say that the prototype is working as
expected and is now complete. By putting all of the application components together, the
final version of the web application overall look is as following in Figure 26
31
Figure 26 Fullscreen screenshot of the final application
The flame log, motion logs and temperature and humidity section are to monitor the
indoor environment. The Weather section is an extra feature to show the outdoor
temperature, making the application more appealing.
4.4 The Project's Budget
The following table from Figure 27 shows the current time market price for the
components needed for this project. Prices tend to be different from site to site.
Component Price (€)
Raspberry Pi 3B+ (18) 44.90
Grove Motion Sensor (16) 6.51
Grove Flame Motion Sensor (17) 5.69
Grove DHT11 Temperature and Humidity Sensor (15) 4.86
Total: 61.96
Figure 27 Price table of the components on 12 February 2021
32
5 Discussion
The result proves that it is possible to build an elementary and straightforward working
home automation system from simple components. Nonetheless, the system has
limitations that are yet to be discussed.
It is not easy to match the sensor pulse with the program's delay on the Raspberry Pi
programming. As a result, the data is only collected if the sensor pulses and the delay
are matched. On further testing, it does not seem to affect the outcome. However, it is a
mentionable flaw of the system.
The NodeJS server logging method upon API call is rather primitive and not
performance-optimized. It is functional on a small-scale application such as this one.
However, if it were to scale to be in a more significant project, some refactorings are
necessary.
The front-end application generally looks acceptable. Nevertheless, a more aesthetic
design is always welcomed.
6 Conclusion
The demonstration of a home automation system shows that with fundamental sensors,
a RaspPi, and a basic programming level, one can build a simple home automation and
security system effortlessly.
The project, undoubtedly, has much room for improvement. It can be implemented with
a lightbulb for automated lighting or connect to the phone for immediate warning about
motions and flame. The AI voice assistant technology can also be applied for even more
alleviated using experience. For now, the system only works within the local network.
Perhaps it can be exposed to the Internet for better user monitoring. On the other hand,
although it lacks functionalities compared to a complex home automation system, it is an
excellent base for more advanced applications to be built on top of it.
33
To conclude with a thought, technology and science are emerging to a point beyond our
imagination. If 50 years ago, owning a personal computer is senseless, then it might be
true that home automation technology is just beginning to bloom. Smart homes will
indeed become more advanced to assist humanity in their daily living basis.
34
References
1. Psiborg. IOT BASED HOME AUTOMATION AND SECURITY. [Online]. URL: https://psiborg.in/iot-based-home-automation-and-security/. Accessed 2021 18 February.
2. Smart Offices & Smart Homes. [Online]. URL: https://smartofficesandsmarthomes.com/smart-home-technology-1966/. Accessed 2021 6 January.
3. My Alarm Center. My Alarm Center. [Online]. URL: https://myalarmcenter.com/blog/the-history-of-home-automation/. Accessed 2021 6 January.
4. IotNOW. IotNOW. [Online]. URL: https://www.iot-now.com/2020/06/10/98753-iot-home-automation-future-holds/. Accessed 2021 7 January.
5. EnvirontmentalConscience. E&C. [Online]. URL: https://environmental-conscience.com/smart-homes-pros-cons/. Accessed 2021 30 March.
6. Wikipedia. Sensor. [Online]. URL: https://en.wikipedia.org/wiki/Sensor. Accessed 2021 16 January.
7. Science Learning Hub. Electricity and sensors. [Online]. URL: https://www.sciencelearn.org.nz/resources/1602-electricity-and-sensors. Accessed 2021 17 February.
8. Cunningham C. Analogue and Digital Representation and Conversion. [Online]. URL: https://www.chris-cunningham.co.uk/uni/2017/analogue-and-digital-representation-
and-conversion. Accessed 2021 17 February.
9. Adufruit. How PIRs Work. [Online]. URL: https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/how-pirs-
work. Accessed 2021 13 January.
35
10. Wikipedia. Fresnel lens. [Online]. URL: https://en.wikipedia.org/wiki/Fresnel_lens. Accessed 2021 13 February.
11. Fresnelfactory. PD15-36005A,WIDE ANGLE PIR FRESNEL LENS. [Online]. URL: https://www.fresnelfactory.com/motion-switch-lens-pd15-36005a.html. Accessed 2021 21 March.
12. RaspberryPi. Raspberry Pi documentation. [Online]. URL: https://www.raspberrypi.org/documentation/usage/gpio/. Accessed 2021 22 January.
13. Wikipedia. Linux. [Online]. URL: https://en.wikipedia.org/wiki/Linux. Accessed 2021 14 January.
14. Raspbian. Frontpage. [Online]. URL: https://www.raspbian.org/. Accessed 2021 10 January.
15. Seeed. Grove - Temperature & Humidity Sensor (DHT11). [Online]. URL: https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-DHT11.html. Accessed 2021 13 January.
16. Seeed. Grove - PIR Motion Sensor. [Online]. URL: https://www.seeedstudio.com/Grove-PIR-Motion-Sensor.html. Accessed 2021 13 January.
17. Seeed. Grove - Flame Sensor. [Online]. URL: https://wiki.seeedstudio.com/Grove-Flame_Sensor/. Accessed 2021 15 February.
18. Verkkokauppa. Raspberry Pi 3 model B+ - yhden piirilevyn tietokone. [Online]. URL: https://www.verkkokauppa.com/fi/product/24605/kcjmm/Raspberry-Pi-3-model-B-
yhden-piirilevyn-tietokone. Accessed 2021 20 January.
19. MagPi. Raspberry Pi 3B+ Specs and Benchmarks. [Online]. URL: https://magpi.raspberrypi.org/articles/raspberry-pi-3bplus-specs-benchmarks. Accessed 2021 20 January.
20. Express. Express - Node.js web application framework. [Online]. URL: https://expressjs.com/. Accessed 2021 16 January.
36
21. Wikipedia. Representational state transfer. [Online]. URL: https://en.wikipedia.org/wiki/Representational_state_transfer. Accessed 2021 13 January.
22. Wikipedia. React (JavaScript library). [Online]. URL: https://en.wikipedia.org/wiki/React_(JavaScript_library). Accessed 2021 24 January.