Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

BRAIN COMPUTER INTERFACED

BASED HOME AUTOMATION SYSTEM

Group Members

Muhammad Fahad Khan (150189)

Ali Arshad (150192)

Muhammad Siddique (150279)

Bachelor of Electrical Engineering

(2015-2019)

Project Supervisor:

Dr. Ayesha Salman

DEPATMENT OF ENGINEERING
FACULTY OF ENGINEERING
AIR UNIVERSITY, ISLAMABAD
A Final Year Project Report
presented to
AIR University
in partial fulfillments of the requirements for
the degree of

Bachelor of Electrical Engineering


BRAIN COMPUTER INTERFACED BASED
HOME AUTOMATION SYSTEM

Submitted By

Muhammad Fahad Khan (150189)

Ali Arshad (150192)

Muhammad Siddique (150279)

2015-2019

DEPARTMENT OF ELECTRICAL ENGINEERING


Approval for Submission

It is to certify that the project report titled

“BRAINCOMPUTER INTERFACED
BASED HOME AUTOMATION
SYSTEM”
has met the required standard for submission
in partial fulfillments of the requirements for
the award of degree of
Bachelor of Electrical Engineering at
Air University, Islamabad

Project Supervisor

Dr. Ayesha Salman


Assistant Professor

Head of Department

Dr. Shahryar Saleem


ACKNOWLEDGEMENT
We would like to thank, Almighty Allah, the Creator of knowledge, Who bestowed upon us His
blessings which helped us complete this project successfully. The research for this project was
supported by Dr. Ayesha Salman. We would like to especially thank Dr. Ayesha Salman for her
guidance, suggestion and continuous encouragement throughout our project. We would also like
to show our gratitude to the lab engineers and senior class mates for sharing their pearls of wisdom
with us during the course of this project research. We are also immensely grateful to our families
for their comments and support, although any errors are our own and should not tarnish the
reputations of these esteemed people. We hope we will complete this project and fulfill our desired
expectations.

i
DEDICATION
We dedicate this project to
Our Parents, Teachers and Friends

ii
ABSTRACT
Brain Computer Interface Based
Home Automation System
This project has been designed for Handicapped people as well as for normal people. We want to
use the education we have gained from this degree for a good purpose and to benefit the human
beings. Our main objective is to do something to make life easier for people who cannot move on
their own. This Brain-controlled technology we used in our project is non-invasive which makes
it safe and easy to use. EEG headset used in our project senses the number of surprise facial
expression from user’s Brain and state of home appliances like fans, bulbs and AC are toggled.
The commands are taken directly from the users’ brain via the Headset and sent to the
computer/laptop to decode the command and a device is first selected and the state of this device
is then toggled. Temperature and Humidity of that particular place/room is also measured and is
displayed on an online server thingspeak.com. This is just a prototype and we can add many more
features in this project with time.

iii
Table of Contents
ACKNOWLEDGEMENT ..................................................................................................... i
DEDICATION ...................................................................................................................... ii
ABSTRACT ......................................................................................................................... iii
CHAPTER 1: INTRODUCTION ................................................................................ 1
1.1 Aims and Objectives: .................................................................................................. 1
1.2 Brain Computer Interface: .......................................................................................... 1
1.3 EEG: ............................................................................................................................ 2
1.4 Literature Review:....................................................................................................... 2
1.5 Background: ................................................................................................................ 4
1.6 Objective of Project: ................................................................................................... 5
1.7 Scope of Project: ......................................................................................................... 5
1.8 Motivation: .................................................................................................................. 5
CHAPTER 2: SYSTEM DESCRIPTION......................................................................... 6
2.1 Brain Signal Acquisition Techniques ......................................................................... 6
2.1.1 Invasive BCI ........................................................................................................ 7
2.1.2 Partially Invasive or Semi Invasive BCI .............................................................. 7
2.1.3 Non-Invasive BCI ........................................................................................... 8
2.1.3.1 Blood Flow Based Methods .......................................................................... 8
2.1.3.2 Electromagnetic Based Methods ................................................................... 8
CHAPTER 3: SYSTEM DESIGN ..................................................................................... 9
3.1 Automation of Devices ............................................................................................... 9
3.1.1 Mind-Control: ...................................................................................................... 9
3.1.2 Relays Operation: ............................................................................................... 10
3.1.3 Devices Operation: ............................................................................................. 10
3.2 External Environment Measurement and Display .................................................... 10
CHAPTER 4: EMOTIV INSIGHT HEADSET.............................................................. 11
4.1 Description: ............................................................................................................... 11
4.2 Features: .................................................................................................................... 11
4.3 Technical Specifications: .......................................................................................... 11
4.4 Sensor Software: ....................................................................................................... 11
4.4.1 EMOTIV Control Panel: .................................................................................... 12
4.4.2 EMOTIV EmoKey: ............................................................................................ 12
4.4.3 EMOTIV Composer:.......................................................................................... 13
4.5 Features Used: ........................................................................................................... 14

i
4.6 Sensors Placement: ................................................................................................... 15
4.7 Processing Methodology: .......................................................................................... 18
4.7.1 Headset: .............................................................................................................. 18
4.7.1.1 Data Acquisition: ........................................................................................ 18
4.7.1.2 Pre-Processing and Amplification: ............................................................. 18
4.7.2 Software: ............................................................................................................ 19
4.7.2.1 Feature Extraction: ...................................................................................... 19
4.7.2.2 Classification:.............................................................................................. 19
4.7.2.3 Controller: ................................................................................................... 19
4.7.2.4 Interfacing: .................................................................................................. 19
4.7.2.5 Post-Processing: .......................................................................................... 20
CHAPTER 5: SIMULATION AND MODELING .......................................................... 21
1.5 EEG Headset: ............................................................................................................ 21
5.2 Components Used: .................................................................................................... 21
5.2.1 Arduino Uno: ..................................................................................................... 21
5.2.1.1 Arduino at Client Side: ............................................................................... 23
5.2.1.1.1 Visual Representation of Client-side Arduino ..................................... 23
5.2.1.1.2 Schematic Diagram of Client-side Arduino ......................................... 24
5.2.1.2 Arduino at Relay Side: ................................................................................ 25
5.2.1.2.1 Visual Representation of Relay-side Arduino ..................................... 25
5.2.1.2.2 Schematic Diagram of Relay-side Arduino ......................................... 26
5.2.2 ESP8266 Wi-Fi Modules: .................................................................................. 27
5.2.2.1 Wi-Fi module at Client Side: ...................................................................... 28
5.2.2.2 Wi-Fi module at Client Side: ...................................................................... 29
5.2.3 TFT LCD Modules: ........................................................................................... 30
5.2.3.1 Designed Interfaced of TFT LCD Module: ................................................ 31
5.2.4 DHT22 Temperature & Humidity Sensor:......................................................... 32
CHAPTER 6: RESULTS AND CONCLUSION............................................................. 34
REFERENCES………………………………………………………………………….35

ii
Appendix A: Code for Client-side Arduino….…………………………………..…36
Appendix B: Code for Relays side Arduino …….……………………………...….46
Appendix C: Code for Client-side Wi-Fi Module.…………………………………48
Appendix D: Code for Relays side Wi-Fi Module…………………………………51

iii
List of Figures
Figure 1: Brain Computer Interface ...................................................................................... 2
Figure 2: Robotic Arm control using BCI ............................................................................ 3
Figure 3: Paralyzed woman using thought control robotic arm ............................................ 3
Figure 4: Percentage Distribution of Disabilities .................................................................. 4
Figure 5: Signal Acquisition Techniques .............................................................................. 7
Figure 6: Block Diagram....................................................................................................... 9
Figure 7.1: Emotiv Control Panel ....................................................................................... 12
Figure 8: EMOTIV EmoKey .............................................................................................. 13
Figure 10: EMOTIV Composer .......................................................................................... 14
Figure 11: EMOTIV Features ............................................................................................. 15
Figure 12: Headset Sensor .................................................................................................. 16
Figure 13: Brain Lobes ....................................................................................................... 17
Figure 14: Headset Interface ............................................................................................... 20
Figure 15: Arduino Uno Module ........................................................................................ 22
Figure 16: Arduino at Client Side ....................................................................................... 23
Figure 17: Visual Representation of Arduino at Client Side .............................................. 24
Figure 18: Schematic Diagram of Client-side Arduino ...................................................... 24
Figure 19: Arduino at Relay Side ....................................................................................... 25
Figure 20: Visual Representation of Relay-side Arduino ................................................... 26
Figure 21: Schematic Diagram of Relay-side Arduino ....................................................... 27
Figure 22: Client-side Wi-Fi Module.................................................................................. 29
Figure 23: Relay side Wi-Fi Module .................................................................................. 30
Figure 24: TFT LCD interface ............................................................................................ 32

iv
CHAPTER 1: INTRODUCTION

1.1 Aims and Objectives:


Our Aim was to make a Brain-controlled intelligent Home Automation System for
handicapped people to make their life easier.

1. To make a system operating on brain commands.

2. To make a Home Automation system which will make it easier for handicapped
people to control Home Appliances using brain commands.

3. To make an advanced Home Automation system for handicapped as well as normal


people which will toggle ( ON and OFF) the Home Appliances using commands
from Brain.

This device and the technology behind it has a great potential for further development. It
has opened a world of possibilities for physically impaired people as there is a wide range
of possible modifications in the device according to the requirements. It is introducing a
new area of development based on BCI (Brain Computer Interface) technology. This is a
non-invasive technology which rules out the danger of further damage to the neural network.

1.2 Brain Computer Interface:


Brain-Computer Interface (BCI) establishes a connection between a brain and an electronic
device which controls brain to direct some external activity. BCI system considers particular
patterns in the user’s brain activity and interprets them as commands to control external
devices. These include control of cursor or a prosthetic arm. In order to monitor the brain
functions several techniques are available, for example, electro-encephalography (EEG),
magneto encephalography (MEG), functional magnetic resonance imaging (fMRI) and
positron emission tomography (PET). BCI is also known as mind machine interface, a brain
machine interface (BMI), or sometimes a direct neural interface.

1
Figure 1: Brain Computer Interface

1.3 EEG:
Electroencephalography (EEG) is an electrophysiological monitoring method which is
used to record change in electrical activity of the brain. This technique is noninvasive,
In this technique electrodes are placed on the surface of brain, although invasive
electrodes are used in limited applications Encephalogram measures voltage variations
resulting from electric current within the neurons of the mind. In medicine based
context, EEG refers to the transcription of the brain's spontaneous electrical activity
over a period of time, as recorded from multiple electrode placed on the scalp.
Diagnostic covering generally focus on the spectral content of EEG, that is, the type of
neural oscillations (popularly called "brain waves") that can be observed in EEG sign.

1.4 Literature Review:


The history of BCI is a history of ideas that is almost a century old. BCI comprises of
technologies from various fields like computer science, electrical engineering,
mechatronics engineering, neurosurgery and biomedical engineering. Canton (1875)
discovered the nature of electrical signals on the surface of animal brains and laid the
groundwork for Berger. Berger (1929) then, published his first research paper on
experiments with EEG (The recording of “brain waves in human”). Thus, he established
the brain’s aptitude for electrical signaling. Penfield (1940s), a pioneering
neurosurgeon, mapped the motor cortex for the first time using epilepsy patient as
subject.

Dr. Walter (1964) explained the first BCI. He implanted electrodes directly into the
motor areas of a patient, who was being operated for some other reasons. Walter
2
observed and recorded the patient’s brain activity while the slide projector was being
operated. When patient wanted to press the button, the slide projector was advanced by
the detection ` patients.

Dobell (2002) conducted an experiment on Jens Neumann. He implanted in the patient


for better mapping of phosphates into coherent vision. The patient was interviewed on
CBS’s The Early Show.

Georgopoulos (20th century) did a detailed study and investigated the behavior of
neurons in primary motor cortex and performed experiments on monkeys to train them
to move the hand to visual targets.

Schwartz (2008) along with team of researchers at University of Pittsburgh


demonstrated a monkey to feed itself with a robotic arm. With its arms restrained, the
monkey was still able to eat marshmallows and chunks of fruits.

Figure 2: Robotic Arm control using BCI

Hochberg (2012) enabled a paralyzed woman to make use of thought controlled robotic arm
to reach and grasp coffee mug.

Figure 3: Paralyzed woman using thought control robotic arm

3
1.5 Background:
According to a National survey for disabled people conducted in 1984-85, the crude
disability rate in Pakistan was at 4.9% and this rate has only grown since. A survey of
disabled persons in Islamabad was also carried out in 1986 which gave an estimated rate of
2.5%.

According to the 1998 census data for Islamabad:

Population 805,000 (approx.)

Physically disabled people 6,641 (approx.)

The distribution of different disabilities in the 1986 survey for Islamabad was as follows:

Percentage Distribution of Different


Disabilities in Islamabad
Mental disability
3% Visual disability
19% 21%
Hearing disability
15% Physical disability
33% 9% Multiple/ Complex disability
Not Classified

Figure 4: Percentage Distribution of Disabilities

There has been a rise in the devices using this new BCI (Brain Control Interface)
technology in the past few years. A brain-controlled robotic arm, a brain-controlled hand
glove for people who cannot move their fingers, and a brain-controlled wheelchair for
people who have lost mobility in their legs have been made by groups of university students.
We also want to play our part in the helping of people living with physical disabilities as
life is already difficult for them and we want to do our best job to facilitate them in any way
that we can.

4
1.6 Objective of Project:
The primary objective of this project is to develop and interface a Home Automation
System which operates with a human brain. Enabling people (especially disabled) to
communicate and control devices by mere thinking.

1.7 Scope of Project:


This project has vast applications in the field of health and bio-robotics such as for
paralyzed people. This project can be used for improving the mental stability of patients.
The potential for BCI entertainment is endless.

1.8 Motivation:
The motivation behind this project was to use our knowledge in a positive way that would
make life easier for physically impaired people.

5
CHAPTER 2: SYSTEM DESCRIPTION
Our project details a Home Automation System which will enhance the life trait of
people with serious physical infirmity by using technology of brain computer interface.
Specifically, this project is designed for the persons who feel segregated from entire
world. Segregation from people is a severe issue that anyone can face, especially for
the ones who experience these things. We tried to serve such miserable persons with
the facility of brain computer controlled System and try to develop their social network
and improvement in their personality. As we know that advancement in technology is
increasing day by day. Everyone should move with the society to reap success in life.
In the era of technology our personal and social life is reliant on gadgets that people
have developed. The Home Automation system which we are developing in our project
is not limited to the persons with physical disabilities. Healthy or common people can
also enjoy the perks of this system. People will ask questions like if we have hands and
we can control these devices using our hands then why we use brain to control them.
Answer to this question is if people have legs then why they use transport to move from
one place to other. They should use their legs instead of using transports. With the
passage of time, we have to accept the amendments done in technology to move with
our society. So, this Automation System will entertain miserable as well as healthy
people.

2.1 Brain Signal Acquisition Techniques


There are different techniques used for brain signal acquisition. The elementary goal of
these techniques is to acquire the electrical signals that pass between neurons in the
brain and interpret them as a signal that is sensed by electronic devices. These signals
are then used to control different mechanisms.

6
Figure 5: Signal Acquisition Techniques

2.1.1 Invasive BCI


Invasive BCI is the technique in which electrodes are directly inserted into the cortex
of brain. It is regarded as a technique to acquire highest quality signals but is prone to
scar-tissue build-up, causing the signals to become weaker as body reacts to external
object in the brain.

2.1.2 Partially Invasive or Semi Invasive BCI


Partially invasive BCI is the technique in which electrodes are inserted inside the skull
but remain outside the brain in spite of within the grey matter. Signals acquired by this
technique are comparatively weaker and there is less risk of scar tissue formation when
compared with Invasive BCI technique. Electrocorticography (ECG) or intracranial
electroencephalography (EEG) uses the same technique as noninvasive technique but
the electrodes are placed over cortex and they are implanted in thin plastic pad. ECG
may be performed in intra-operative ECG or extra-operative ECG. Former means
operating during surgery while later deals with outside of surgery.

7
2.1.3 Non-Invasive BCI
Non-Invasive BCI technique is the one in which electrodes are placed on the scalp.
Signals acquired by this technique are weak but this technique is regarded to be safest
when compared to other types.

There are two main methods of Non-invasive Technique.

2.1.3.1 Blood Flow Based Methods


Typical blood flow methods include Functional Magnetic Resonance Imaging (fMRI)
and functional Near-Infrared Imaging Spectroscopy (fNIRs). fMRI aims to determine
neuro-biological correlate of behavior by recognizing the brain regions that become
“active” during performance of specific task. However fNIRs is based upon optical
properties of tissues. It enables the continuous, non-invasive, and safe monitoring of
changes in blood oxygenation and blood volume related to human brain function.

2.1.3.2 Electromagnetic Based Methods


Electromagnetic based methods include Magneto encephalography (MEG), Electro
encephalography (EEG). MEG is an imaging technique used to measure the magnetic
fields produced by electrical activity in the brain. It is used to detect the abnormalities
related to electrical activity of brain. The recording of signals is obtained by placing
electrodes on scalp with a conductive gel or paste. The numbers of electrodes vary from
few to 128 electrodes.

8
CHAPTER 3: SYSTEM DESIGN
Our project is basically divided into two major portions. One is the Devices Automation
side and the second part is Measurement Display of External Environment. Given
below is the block diagram of our project.

Figure 6: Block Diagram

3.1 Automation of Devices


The first part of our project is the Automation of devices which includes three sections
described below

3.1.1 Mind-Control:
The first part of the project is the Mind-Control part using the EEG headset. The brain
signals are gathered via the headset and then sent to the PC via Bluetooth. EMOTIV
INSIGHT will detect the EEG signals of different required commands from brain which
will be preprocessed using digital signal processing techniques and then required
features will be extracted from the given signal. Then classification of the required
features will be done based on different algorithms designed by experts of the fields. The
9
controller will detect the required signals and process it depending on the programming
and generates the demanded output.

3.1.2 Relays Operation:


Brain commands are received at PC and then this data is transferred on to Arduino and via
Wi-Fi modules this data is forwarded to Relays for further operation.

3.1.3 Devices Operation:


Brain commands received from EEG headset are used to operate relays and based on these
commands, action is performed by the devices i.e toggle (ON or OFF) the state of the
device selected by the relays.

3.2 External Environment Measurement and


Display
The second part of our project is Measurement and display of the Loads and external
environment. Room temperature and Humidity is sensed and is displayed on the Home
Internet Router continuously.

10
CHAPTER 4: EMOTIV INSIGHT HEADSET
4.1 Description:
EMOTIV Insight is a 5-channel mobile EEG headset that records brainwaves and translates
them into meaningful data that you can understand. It is the only device in its category that
offers 5 EEG sensors and 2 reference sensors. This high spatial resolution provides in-depth
information of your brain activity. It is designed for extended use in the research, BCI (Brain
Computer Interface) and self-assessment cases.

4.2 Features:
Some features of this EMOTIV Insight Headset are:

• Easy-to-use, intuitive design.

• Offers high spatial resolution ensuring complete measurement of brain activity.


• Offers wireless connectivity via Bluetooth to Tablet, PC, Mobile phones.
• 4 hours minimum battery run time that enables you to maintain continuous
performance.
• 9 axis motion sensors for precision measurement of head position and movement.
• Offers control by Performance Metrics, Mental Commands and Facial Expressions

4.3 Technical Specifications:


Some Technical Specifications of the Headset we are using in our project are as under:

• 5 channels: AF3, AF4, T7, T8, PZ.


• 2 references: In the CMS/DRL noise cancellation configuration.
• Data transmission rate: 128 samples per second per channel.
• Frequency response: 1-43Hz.
• Minimum voltage resolution: 0.51μV least significant bit.
• Wireless: Bluetooth 4.0 LE.

4.4 Sensor Software:


EMOTIV headset comes with many interfacing software and applications which can be used
in scientific and academic research. EMOTIV provides their user a wide range of software
and SDKs which helps user in BCI program to expand the medium of operations. We are
11
using XAVIER software which is designed especially for Emotiv insight headsets. It has
three parts:

1. Control Panel
2. Emokey
3. Composer

4.4.1 EMOTIV Control Panel:


The Control Panel performs the main function. It trains the device according to our brain
and various mental states.

Figure 7.1: Emotiv Control Panel

4.4.2 EMOTIV EmoKey:


EmoKey takes the input commands from brain and uses it as a trigger to perform the task
we have programmed it to perform by selecting a target.

12
Figure 8: EMOTIV EmoKey

4.4.3 EMOTIV Composer:


The Emotiv Composer makes sure that the headset and the pc, control panel and EmoKey
are connected and working properly.

13
Figure 9: EMOTIV Composer

4.5 Features Used:


The software offers mental commands and commands via facial expressions as well.
However, we need commands from surprise facial expression for our project as it fulfills
our requirements.

14
Figure 10: EMOTIV Features

4.6 Sensors Placement:


The Headset we used in our project is 5-Channel mobile EEG headset with 2 reference
sensors. The electrode placement is done via 10-20 system. It is regarded as an
internationally accepted and recognized system that involves the placement of
electrodes on the scalp. The "10" and "20" depicts that the actual distances between
adjacent electrodes are either 10% or 20% of the total front–back or right–left distance
of the skull. 10 particularly refer to 10% of the circumference while 20 particularly refer
to 20% of the inner electrode distance.

The five sensors are placed in the 10-20 System ratio generally used for sensor
placement in headsets.

• 2 reference sensors placed on the Cerebellum

• 2 sensors on the forehead placed on the Frontal Lobe

• 2 sensors on the Temporal Lobe

15
• 1 sensor on the Parietal Lobe

Figure 11: Headset Sensor

The sensors are placed on the Major Lobes in the Brain:

• Frontal Lobe associated with reasoning, motor skills, higher level cognition, and
expressive language
• Parietal Lobe associated with processing tactile sensory information such as
pressure, touch, and pain

16
• Temporal Lobe processes sensory input, visual processing

• Occipital Lobe interprets visual stimuli and information

• Cerebellum associated with motor movement and control

Figure 12: Brain Lobes

The location of lobe is represented by a letter and hemisphere by a number. Basically,


central lobe doesn’t exist; it is just for identification purpose. The right hemisphere is
denoted by set of even numbers 2, 4, 6, 8 and left hemisphere is denoted by set of odd
numbers 1, 3, 5, 7. “Z” refers to the electrodes placed on the midline of the scalp.

Representation of lobes of human brain


Frontal F

Temporal T

Central C

Parietal P

Occipital O

17
4.7 Processing Methodology:
Modern EEGs function by measuring the voltage fluctuations along the surface of the
scalp caused by ionic current flows within the neurons. A neuron is a cell in the brain
that transmits nerve impulses from the body. Neurons can receive different kinds of
signals from the body, either through a chemical change, such as a response from the
olfactory receptors, or from a physical change such as a touch receptor in the skin or a
light receptor in retina of eye. The signals that neuron receives causes Sodium and
Potassium ions to move across the neuron's plasma membrane, which in turn causes an
electrical current flow, which can be used to evaluate the activity of the brain, and is
therefore read directly by the EEG. The current flow is measured by electrodes on the
scalp, which operate under a standard sensitivity of 7 μ-volts/mm. The readings gathered
by these electrodes are then reported by the EEG as a measure of frequency, along
number of different consistent electrode placements. Decisions for Electrode placement
are based on number of different standards, but the most common system is the
International 10-20 system, developed in 1958 by a committee of the International
Federation of Societies for Electroencephalography and Clinical Neurophysiology. The
International 10-20 system uses anatomic landmarks on the scalp to give a consistency
of electrode placement among varying individuals.
The processing of the signal obtained from the brain is done in the following steps:

4.7.1 Headset:
4.7.1.1 Data Acquisition:
There should be a device proficient of grabbing data from brain signals and then filter
data and send it to some software to control the cursor/paddle. We use EMOTIV Insight
EEG headset to collect the date from brain. EMOTIV Insight headset has 5 channels.

4.7.1.2 Pre-Processing and Amplification:


To prepare the EEG signal for feature extraction and classification, different processes
need to be done, e.g., Band pass filtering, infinite impulse response filter, Independent
component analysis and Common spatial pattern filter. Signals gathered from EEG are
down sampled from 2082HZ to 32HZ by choosing every 64th sample from band pass
18
filtering data. Data is processed in four sessions. All type of filtration is done within the
software. Amplification of the signal is then done to bring it to a certain detectable value
as originally it’s in milli-Volts(mV).

4.7.2 Software:
4.7.2.1 Feature Extraction:
The process of extracting representative characteristics i.e. mean, variance, and power
spectral density (PSD) of a signal is known as feature extraction. The extracted features
are then stored as “feature vectors” and they are passed to the classifiers. This process
of feature extraction helps us to get required data and to facilitate the classification of
this data into classes of interest.

4.7.2.2 Classification:
The process of identifying patterns from the data based on statistical models built using
the training data is known as classification of signal. In this process signal are classified
into different classes by identifying their patterns from the statistical models built using
training data. It is actually done to differentiate between different brain commands. It is
done using LDA (Linear Discriminant Analysis) and comparing features from inter-
class mean and intra-class variance to differentiate between different brain signals.

4.7.2.3 Controller:
After data classification, controller gets the cleaned and filtered signal which performs
tasks according to the signal. Controller is a processor which processes the received
clean brain signal as input and generates output as per instructions installed by the user.

4.7.2.4 Interfacing:
Interfacing with any other software is done using EmoKey part of the Xavier software.
This step is done to send the brain command as a trigger for further use. Interfacing is
done by sending generated commands to Arduino serial port. Commands are generated
by EMOTIV software. Signal filtering and signal amplification is done by EMOTIV
software itself. Arduino reads commands from serial port which control further action.

19
Figure 13: Headset Interface

4.7.2.5 Post-Processing:
Based on the programming downloaded by the user in controller, controller process the
received data to detect which output should be turned on based on the brain signal
acquired by the headset. The controller makes the decision based on the input given to
the controller through serial port of Arduino, which after processing will generate the
specified output.

20
CHAPTER 5: SIMULATION AND MODELING

1.5 EEG Headset:


EMOTIV Insight headset (sensor) is used to gather data from brain signals. EMOTIV

Insight is a product of EMOTIV Inc. which is a USA based Bioinformatics Company


famous for Revolutionizing the understanding of human brain activity through
electroencephalography (EEG). EMOTIV Insight is a revolutionary brain computer
interface (BCI) high resolution EEG. It is a wireless EEG based system with direct
connectivity to pc through Bluetooth dongle.

Activity Table

Thinking Activity Output

Neutral No Action

Surprise facial expression Device Selected

Concentration Toggle Device State

5.2 Components Used:


5.2.1 Arduino Uno:
The reason behind why we used Arduino modules is that it is easy to interface with
ESP8266 Wi-Fi module and using Arduino libraries we can easily get date from sensor.
We used two Arduino Uno modules in our project. One at the client side connected to
PC which receives brain commands from EEG Headset and the second one at the Relay
side to control Devices.

21
Figure 14: Arduino Uno Module

Some features of Arduino Uno are shown below in the following table:

Specifications of Arduino Uno microcontroller

Digital input/output pins 14

Analog input pins 6

Crystal oscillator 16 MHz

Serial ports 4

22
5.2.1.1 Arduino at Client Side:
Arduino at client side is connected directly with the PC/Laptop, a TFT LCD module and
a Wi-Fi Module. This Arduino receives brain commands from EEG Headset that are
received at PC via Bluetooth, displays the data on to the TFT LCD attached directly
with it as well as sends the data to wi-Fi modules which is transferred to the relay side
Wi-Fi module via wireless communication.

Figure 15: Arduino at Client Side

5.2.1.1.1 Visual Representation of Client-side Arduino


Visual representation of Arduino Module at client side attached with TFT LCD and Wi-
Fi Module is shown below in figure:

23
Figure 16: Visual Representation of Arduino at Client Side

5.2.1.1.2 Schematic Diagram of Client-side Arduino


Schematic Diagram of Arduino Module at client side attached with TFT LCD and Wi-
Fi Module is shown below:

Figure 17: Schematic Diagram of Client-side Arduino

24
5.2.1.2 Arduino at Relay Side:
Arduino at relay side is connected directly with the relays, a DHT22 Temperature and
Humidity Sensor module and a Wi-Fi Module. This Arduino receives signal received at
wi-Fi module of relay side from client-side Wi-Fi module, and control the Relays ny
actuating them to ultimately control the devices attached to Relays. This Arduino also
receives data from DHT22 Temperature and Humidity sensor values and forward it to
client side via Wi-Fi, as well as displays it on to Home internet router on an online server
thingspeak.com

Figure 18: Arduino at Relay Side

5.2.1.2.1 Visual Representation of Relay-side Arduino


Visual representation of Arduino Module at Relay side attached with DHT22 Temperature
and Humidity sensor, Relays and Wi-Fi Module is shown below in figure:

25
Figure 19: Visual Representation of Relay-side Arduino

5.2.1.2.2 Schematic Diagram of Relay-side Arduino


Schematic Diagram of Arduino Module at Relay side attached with DHT22 Temperature
and Humidity sensor, Relays and Wi-Fi Module is shown below:

26
Figure 20: Schematic Diagram of Relay-side Arduino

5.2.2 ESP8266 Wi-Fi Modules:


The ESP8266 ESP-01 Wi-Fi module allows microcontrollers access to a Wi-Fi network.
It is a self-contained SOC (System on a Chip).

Some of the features of ESP8266 ESP-01 Wi-Fi module are as under:

• Supports 802.11 b/g/n protocol

• Integrated TCP/IP protocol stack

• Input power: 3.3V

• Regular operation current draw: ~70mA

• Peak operating current draw: ~300mA

• +19.5dBm output in 802.11b mode

• 32-bit Tensilica Xtensa LX106 CPU running 80MHz

• Flash Memory Size: 1MB

• WiFi security modes: WPA, WPA2

27
Just like two Arduino Modules, we used two Wi-Fi modules in our project. One at the
client side connected to PC and the second one at the Relay side.

5.2.2.1 Wi-Fi module at Client Side:


Wi-Fi module at client side is connected directly with the PC/Laptop. Brain commands
from EEG Headset that are received at PC via Bluetooth. This data is forwarded to wi-
Fi module which transfers it to the relay side Wi-Fi module via wireless communication.

28
Figure 21: Client-side Wi-Fi Module

5.2.2.2 Wi-Fi module at Client Side:


Wi-Fi module at relay side is connected to the Relay side Arduino and Home Internet
Router. Relay side Arduino creates communication channel between Relay side Arduino
and Client side. As well as it creates channel for Online Server thingspeak.com to
display data on to the Home Internet Router.

29
Figure 22: Relay side Wi-Fi Module

5.2.3 TFT LCD Modules:


Thin-film-transistor liquid-crystal display (TFT LCD) uses thin-film-transistor (TFT)
technology to improve image qualities such as contrast and addressability.

Some features of RFR LCD are as shown below:

TFT LCF Features

Size 3.5 inches

Resolution 320×480

Interface 8-bit parallel

30
5.2.3.1 Designed Interfaced of TFT LCD Module:
Designed Interface of TFT LCD Module shows:

• Current room Temperature & Humidity


• Devices and their State.

Thin-film-transistor liquid-crystal display (TFT LCD) module shows yellow circle when
the device is selected, a red circle when the selected device is in OFF state and a Green
circle when the selected device is in ON state.

TFT LCD Interface

Color State

Yellow Circle Device Selected

Red Circle Device OFF

Green Circle Device ON

31
Figure 23: TFT LCD interface

5.2.4 DHT22 Temperature & Humidity Sensor:


DHT22 is a low-cost sensor which measures Temperature & Humidity approximately
up-to accurate value.

Some features of DHT22 are shown here in following table:

32
TFT LCF Features

I/O Power 3 to 5 Volts

Max. Current Draw 2.5 milli-Amp

0-100% with -2 to -55%


Humidity Reading Accuracy
accuracy

Sampling Rate 0.5 Hz

Temperature Reading -40 to 80°C with ±0.5°C


Accuracy accuracy

33
CHAPTER 6: RESULTS AND CONCLUSION

The crucial need of every handicapped person is to enjoy the perks of life like normal people
i.e. without the support of anyone. BCI provides a promising solution for the patients who
suffer with physical challenges. The main idea was to detect the brain signals followed by
utilization of these signals to control the Home Appliances like TV, Bulbs, Fans, AC etc.
The detection of these acquired EEG signals serves as a medium for transferring
information. The device that is used for capturing EEG signal is the EMOTIV Insight
headset. The acquired brain signals are generated due to the firing of neurons in the brain.
In order to detect these electrical activities, EEG headset is placed on the scalp of patients.
The electrical activities are then transferred wirelessly to the laptop/computer for the
purpose of signal processing. After these signals are processed, useful features are extracted
and then classified according to the control thoughts/actions. On the basis of classification,
different control commands are generated by the laptop/computer and given to the control
Relays for Automation of Home Appliances. The accuracy and stability of the system
depends upon the thoughts generated by the subject, the more the training the better the
results.

34
REFERENCES
• https://www.emotiv.com/product/emotiv-insight-5-channel-mobile-eeg/#tab-
description
• https://www.researchgate.net/figure/Components-of-a-BCI-system-signals-from-
the-users-brain-are-acquired-and-processed-to_fig1_267792090
• https://cb-electronics.com/products/touch-lcd-3-5/
• http://www.mind-your-reality.com/brain_waves.html

35
Appendix A: Code for Client-side Arduino

#include <MCUFRIEND_kbv.h>
#include <SoftEasyTransfer.h>
#include <SoftwareSerial.h>

// wiring with UNO:


//--------------POWER Pins---------------------
// 5V - DC 5V
// GND - Ground
// 3V3 - Not Connected (NC)
//--------------LCD Display Pins---------------
// LCD_RD - Analog pin A0
// LCD_WR - Analog pin A1
// LCD_RS - Analog pin A2
// LCD_CS - Analog pin A3
// LCD_RST - Analog pin A4
// LCD_D0 - digital pin 8
// LCD_D1 - digital pin 9
// LCD_D2 - digital pin 2
// LCD_D3 - digital pin 3
// LCD_D4 - digital pin 4
// LCD_D5 - digital pin 5
// LCD_D6 - digital pin 6
// LCD_D7 - digital pin 7
//--------------SD-card Pins ------------------
// SD_SS - digital pin 10
// SD_DI - digital pin 11
// SD_DO - digital pin 12
// SD_SCK - digital pin 13

#define LCD_RESET A4
#define LCD_CS A3 // Chip Select - Analog pin 3
#define LCD_CD A2 // Command/Data - Analog pin 2
#define LCD_WR A1 // LCD Write - Analog pin 1
#define LCD_RD A0 // LCD Read - Analog pin 0

// Assign names to some common 16-bit color values:


#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

char choice;
int S1=0;
int S2=0;
int S3=0;
36
int S4=0;

SoftwareSerial mySerial(10, 11);

MCUFRIEND_kbv tft;

SoftEasyTransfer ETin, ETout;

struct SENSOR_DATA_STRUCTURE{
//put your variable definitions here for the data you want
to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
float temp;
float hum;
};

SENSOR_DATA_STRUCTURE sensor_data;

struct DEVICE_DATA_STRUCTURE{
//put your variable definitions here for the data you want
to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int8_t device;
int8_t state;
};

DEVICE_DATA_STRUCTURE device_data;

const unsigned char PROGMEM AULogo [] = {


0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0f, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xff, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff,
0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x1f, 0xff, 0xff, 0xd0,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x27, 0x00, 0x1f, 0xff, 0x9f, 0xff,
0xf8, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff,
0xe0, 0x00, 0x00,
37
0x00, 0x00, 0x00, 0x00, 0xff, 0xf0, 0x7f, 0xff, 0xf8, 0x7f,
0xc0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7f, 0xfd, 0xff, 0xff, 0xff, 0xfa,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xbf, 0xff, 0xff, 0xfd, 0xff, 0xfe,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xbf, 0xf8,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xe7, 0xe0,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xef, 0xfe, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xf7, 0xfc, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xfd, 0xf0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xf8, 0x00, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xef, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xe7, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0xc3, 0xc0, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0xc3, 0xe0, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x81, 0xf0, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x01, 0xf0, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10, 0x1f, 0x00, 0xf8, 0x10, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x3e, 0x00, 0xf8, 0x1c, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x3e, 0x00, 0x7c, 0x18, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1c, 0x7c, 0x00, 0x3c, 0x78, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1f, 0x7c, 0x00, 0x3e, 0xf8, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x16, 0xfa, 0x00, 0x7f, 0xe0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x36, 0xfd, 0x00, 0xbf, 0x4c, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3d, 0xf1, 0xbc, 0x8f, 0xb8, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0b, 0xf6, 0xfe, 0x6f, 0xf0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xf9, 0x7d, 0x9f, 0xc0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0xfc, 0x7e, 0x3f, 0xc0, 0x00, 0x00,
0x3e, 0x00, 0x00,

38
0x00, 0x00, 0x00, 0x07, 0xfe, 0x38, 0x7f, 0xe0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0x8f, 0x01, 0xe1, 0xf0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0x03, 0x83, 0x81, 0xf0, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1f, 0x00, 0xe7, 0x00, 0xf8, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3e, 0x00, 0x3c, 0x00, 0xf8, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3e, 0x00, 0x38, 0x00, 0x7c, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7c, 0x00, 0x44, 0x00, 0x3c, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x00, 0x3e, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0xf8, 0x01, 0xff, 0xff, 0x1f, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0xf0, 0x07, 0xff, 0xff, 0xdf, 0x00, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x01, 0xf0, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00,
0x3e, 0x00, 0x00,
0x00, 0x00, 0x03, 0xe1, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,
0x7c, 0x00, 0x00,
0x00, 0x00, 0x03, 0xe7, 0xff, 0xc0, 0x00, 0x7f, 0xe0, 0x00,
0xfc, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x0f, 0xf0, 0x00,
0xfc, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xfe, 0x03,
0xf8, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xf8, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xf0, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff,
0xc0, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff,
0x00, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0xd8, 0xbe, 0x09, 0xbc, 0x91, 0x7d, 0xf7, 0xb7,
0xb4, 0x00, 0x00,
0x00, 0x01, 0x08, 0xf3, 0x09, 0xa6, 0xd3, 0x61, 0xb3, 0x33,
0x1c, 0x00, 0x00,
0x00, 0x02, 0x2c, 0xfe, 0x09, 0xa6, 0xda, 0x7d, 0xf1, 0xb3,
0x18, 0x00, 0x00,

39
0x00, 0x00, 0x64, 0xf2, 0x09, 0xa6, 0xce, 0x61, 0x30, 0xf3,
0x18, 0x00, 0x00,
0x00, 0x00, 0x7e, 0xf3, 0x0f, 0xa6, 0xcc, 0x7d, 0x17, 0xb3,
0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
ETin.begin(details(sensor_data), &mySerial);
ETout.begin(details(device_data), &mySerial);
tft.reset();
tft.begin(0x9486);
tft.setRotation(2);
tft.fillScreen(BLACK);
delay(500);

drawBitmap(0, 10, AULogo, 100, 75, WHITE);

tft.setCursor(110,15);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("BRAIN COMPUTER");

tft.setCursor(110,35);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("INTERFACE BASED");

tft.setCursor(110,55);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("HOME AUTOMATION");

tft.setCursor(10,95);
40
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.print("Ali Arshad(150192),Fahad (150189),
Siddique(150279)");

tft.setCursor(65,120);
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.print("TEMPERATURE");

tft.setCursor(180,145);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("o");

tft.setCursor(200,160);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("C");

tft.setCursor(80,190);
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.print("HUMIDITY");

tft.setCursor(200,220);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("%");

tft.setCursor(65,260);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("FAN");

tft.fillCircle(240,270,20,RED);
tft.drawCircle(240,270,20,WHITE);

tft.setCursor(65,320);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("LIGHT");

tft.fillCircle(240,330,20,RED);
tft.drawCircle(240,330,20,WHITE);

tft.setCursor(65,380);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("AC");

tft.fillCircle(240,390,20,RED);
tft.drawCircle(240,390,20,WHITE);
41
tft.setCursor(65,440);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("TV");

tft.fillCircle(240,450,20,RED);
tft.drawCircle(240,450,20,WHITE);

tft.drawRect(0,0,320,480,WHITE);

delay(500);
}

void loop()
{
if (ETin.receiveData())
{
tft.setCursor(90,160);
tft.setTextColor(WHITE,BLACK);
tft.setTextSize(3);
tft.print(sensor_data.temp);
delay(100);

tft.setCursor(90,220);
tft.setTextColor(WHITE,BLACK);
tft.setTextSize(3);
tft.print(sensor_data.hum);
}

if (Serial.available())
{
choice = Serial.read();
if (choice == 'd')
{
device_data.device++;
if (device_data.device == 1)
{
tft.setCursor(65,260);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("FAN");
tft.setCursor(65,440);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("TV");
}
else if (device_data.device == 2)
{
tft.setCursor(65,320);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("LIGHT");
42
tft.setCursor(65,260);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("FAN");
}
else if (device_data.device == 3)
{
tft.setCursor(65,380);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("AC");
tft.setCursor(65,320);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("LIGHT");
}
else if (device_data.device == 4)
{
tft.setCursor(65,440);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("TV");
tft.setCursor(65,380);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("AC");
}
else if (device_data.device > 4)
{
device_data.device = 1 ;
tft.setCursor(65,260);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("FAN");
tft.setCursor(65,440);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("TV");
}
Serial.println(device_data.device);
}
if (choice == 's')
{
if ( device_data.device == 1)
{
if (S1==0)
{
S1=~S1;
device_data.state=1;
tft.fillCircle(240,270,20,GREEN);
tft.drawCircle(240,270,20,WHITE);
}
else
43
{
S1=~S1;
device_data.state=0;
tft.fillCircle(240,270,20,RED);
tft.drawCircle(240,270,20,WHITE);
}
}
else if ( device_data.device == 2)
{
if (S2==0)
{
S2=~S2;
device_data.state=1;
tft.fillCircle(240,330,20,GREEN);
tft.drawCircle(240,330,20,WHITE);
}
else
{
S2=~S2;
device_data.state=0;
tft.fillCircle(240,330,20,RED);
tft.drawCircle(240,330,20,WHITE);
}

}
else if ( device_data.device == 3)
{
if (S3==0)
{
S3=~S3;
device_data.state=1;
tft.fillCircle(240,390,20,GREEN);
tft.drawCircle(240,390,20,WHITE);
}
else
{
S3=~S3;
device_data.state=0;
tft.fillCircle(240,390,20,RED);
tft.drawCircle(240,390,20,WHITE);
}

}
else if ( device_data.device == 4)
{
if (S4==0)
{
S4=~S4;
device_data.state=1;
tft.fillCircle(240,450,20,GREEN);
tft.drawCircle(240,450,20,WHITE);
}
else
44
{
S4=~S4;
device_data.state=0;
tft.fillCircle(240,450,20,RED);
tft.drawCircle(240,450,20,WHITE);
}
}
ETout.sendData();
}
}
}

void drawBitmap(int16_t x, int16_t y,const uint8_t *bitmap,


int16_t w, int16_t h, uint16_t color)
{
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for(j=0; j<h; j++) {


for(i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j *
byteWidth + i / 8);
if(byte & 0x80) tft.drawPixel(x+i, y+j, color);
}
}
}

45
Appendix B: Code for Relays side Arduino
#include <SoftEasyTransfer.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 7
#define D1 8
#define D2 9
#define D3 10
#define D4 11
DHT dht(DHTPIN, DHT22);

SoftwareSerial mySerial(2, 3);

SoftEasyTransfer ETin, ETout ;

SimpleTimer timer;

struct SENSOR_DATA_STRUCTURE{
//put variable definitions here for the data we want to
send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
float temp;
float hum;
};

SENSOR_DATA_STRUCTURE sensor_data;

struct DEVICE_DATA_STRUCTURE{
//put variable definitions here for the data we want to
receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int8_t device;
int8_t state;
};

DEVICE_DATA_STRUCTURE device_data;

void setup() {
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
mySerial.begin(9600);
Serial.println("******** BEGIN ***********");
Serial.println("- start DHT sensor");
dht.begin();
46
ETin.begin(details(device_data), &mySerial);
ETout.begin(details(sensor_data), &mySerial);
timer.setInterval(1000*25, readDHTSensor);
}

void readDHTSensor() {
delay(200);
sensor_data.temp = dht.readTemperature();
sensor_data.hum = dht.readHumidity();

if (isnan(sensor_data.hum) || isnan(sensor_data.temp)) {
sensor_data.temp = 0.00;
sensor_data.hum = 0.00;
}
ETout.sendData();
}

void loop() {
timer.run();
if(ETin.receiveData()){
if (device_data.device==1)
{
if (device_data.state==0) {digitalWrite(D1,
HIGH);}
else if (device_data.state==1) {digitalWrite(D1,
LOW);}
}
else if (device_data.device==2)
{
if (device_data.state==0) {digitalWrite(D2,
HIGH);}
else if (device_data.state==1) {digitalWrite(D2,
LOW);}
}
else if (device_data.device==3)
{
if (device_data.state==0) {digitalWrite(D3,
HIGH);}
else if (device_data.state==1) {digitalWrite(D3,
LOW);}
}
else if (device_data.device==4)
{
if (device_data.state==0) {digitalWrite(D4,
HIGH);}
else if (device_data.state==1) {digitalWrite(D4,
LOW);}
}
delay(100);
}

47
Appendix C: Code for Client-side Wi-Fi Module
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <EasyTransfer.h>

EasyTransfer ETin, ETout;

struct SENSOR_DATA_STRUCTURE{
//put your variable definitions here for the data you want
to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
float temp;
float hum;
};

SENSOR_DATA_STRUCTURE sensor_data;

struct DEVICE_DATA_STRUCTURE{
//put your variable definitions here for the data you want
to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int8_t device;
int8_t state;
};

DEVICE_DATA_STRUCTURE device_data;

// AP Wi-Fi credentials
const char* ssid = "FYP";
const char* password = "ABCDEF";

// Local ESP web-server address


String serverHost1 = "http://192.168.4.1/device";
String data,data1;

char x;
String myString1, myString2;
// Static network configuration
IPAddress ip(192, 168, 4, 4);
IPAddress gateway(192, 168, 4, 0);
IPAddress subnet(255, 255, 255, 0);

int device=1;
int state=1;

ESP8266WebServer server(80);

48
WiFiClient client;

void setup() {
Serial.begin(9600);
ETin.begin(details(device_data), &Serial);
ETout.begin(details(sensor_data), &Serial);
Serial.println("- set ESP STA mode");
WiFi.mode(WIFI_STA);
Serial.println("- connecting to wifi");
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
Serial.println("- wifi connected");
setupServer();
}
void setupServer(){
Serial.println("** SETUP SERVER **");
Serial.println("- starting server :");

server.on("/", handle_index);
server.on("/feed", handle_feed);
server.begin();
}

void handle_index() {
server.send(200, "text/plain", "Hello from my server!");
}

void handle_feed() {
myString1 = server.arg("temp");
myString2 = server.arg("hum");
sensor_data.temp = myString1.toFloat();
sensor_data.hum = myString2.toFloat();
server.send(200, "text/plain", "This is response to feed
client");
ETout.sendData();
}

void sendHttpRequest1() {
HTTPClient http;
http.begin(serverHost1);
http.addHeader("Content-Type", "application/x-www-form-
urlencoded");
http.POST(data1);
http.writeToStream(&Serial);
Serial.println("");
http.end();
}
49
void buildDataStream1() {
data1 = "device=";
data1 += String(device_data.device);
data1 += "&state=";
data1 += String(device_data.state);
}
void loop() {
server.handleClient();
if(ETin.receiveData())
{
buildDataStream1();
sendHttpRequest1();
delay(100);
}
}

50
Appendix D: Code for Relays side Wi-Fi Module

#include <EasyTransfer.h>
#include <ESP8266WiFi.h>
#include <SimpleTimer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
// IoT platform Credentials
String apiKey = "RW6ZAA2OSRBCFXFE";
const char* logServer = "api.thingspeak.com";

String serverHost = "http://192.168.4.4/feed";


String data;
// Internet router credentials
const char* ssid = "FYP";
const char* password = "ABCDEF";

String myString1, myString2;

EasyTransfer ETin, ETout;

SimpleTimer timer;

struct SENSOR_DATA_STRUCTURE{
//put variable definitions here for the data we want to
receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
float temp;
float hum;
};

SENSOR_DATA_STRUCTURE sensor_data;

struct DEVICE_DATA_STRUCTURE{
//put variable definitions here for the data we want to
send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int8_t device;
int8_t state;
};

DEVICE_DATA_STRUCTURE device_data;

ESP8266WebServer server(80);

WiFiClient client;

void setup() {
Serial.begin(9600);
51
ETin.begin(details(sensor_data), &Serial);
ETout.begin(details(device_data), &Serial);
timer.setInterval(1000*60*60, repeat);
WiFi.mode(WIFI_AP_STA);
setupAccessPoint();
}

void repeat() {
setupStMode(String(sensor_data.temp) ,
String(sensor_data.hum));
}

// Handling the / root web page from server


void handle_index() {
server.send(200, "text/plain", "Hello from server!");
}

// Handling the /device page from server


void handle_device() {
myString1 = server.arg("device") ;
myString2 = server.arg("state") ;
device_data.device = myString1.toInt() ;
device_data.state = myString2.toInt() ;
server.send(200, "text/plain", "Device selection
received");
ETout.sendData();
delay(500);
}

void setupAccessPoint(){

Serial.println("* SETUP ACCESS POINT *");


Serial.println(" disconnect from any other modes");
WiFi.disconnect();
Serial.println("- start ap with SID: "+ String(ssid));
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("- AP IP address is :");
Serial.print(myIP);
setupServer();
}

void setupServer(){
Serial.println("* SETUP SERVER *");
Serial.println(" starting server :");
server.on("/", handle_index);
server.on("/device", handle_device);
server.begin();
}

void setupStMode(String t, String v){


Serial.println("* SETUP STATION MODE *");
Serial.println(" disconnect from any other modes");
52
WiFi.disconnect();
Serial.println();
Serial.println(" connecting to Home Router SID: ABC");
WiFi.begin("ABC", "ABCDEFG");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("- succesfully connected");
Serial.println("- starting client");

WiFiClient client;

Serial.println("- connecting to Database server: " +


String(logServer));
if (client.connect(logServer, 80)) {
Serial.println("- succesfully connected");
String postStr = apiKey;
postStr += "&field1=";
postStr += String(t);
postStr += "&field2=";
postStr += String(v);
postStr += "\r\n\r\n";
Serial.println("- sending data...");
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-
urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
Serial.println("- stopping the client");

void buildDataStream() {
data = "temp=";
data += String(sensor_data.temp);
data += "&hum=";
data += String(sensor_data.hum);
Serial.println("- data stream: "+data);
}

void sendHttpRequest() {
HTTPClient http;
http.begin(serverHost);

53
http.addHeader("Content-Type", "application/x-www-form-
urlencoded");
http.POST(data);
http.end();
}

void loop() {
timer.run();
server.handleClient();
if (ETin.receiveData())
{
buildDataStream();
sendHttpRequest();
delay(100);
}
}

54

You might also like