Web Home Automation using Bluetooth Communication with 8051 Microcontroller

cavalcadehorehoundMechanics

Nov 5, 2013 (4 years and 7 days ago)

150 views

S
OUTHERN POLYTECHNIC
STATE UNIVERSITY

Web Home Automation
using Bluetooth
Communication

with
8051 Microcontroller



Bin Zhou





T
his report includes all detailed information of this honor research.
Readers
can do the exact

same research by following
the instruction given in the report.

2

|
P a g e



Table of Contents

Overview

................................
................................
................................
................................
..........

3

Equipment

................................
................................
................................
................................
...

3

Schematic
s

................................
................................
................................
................................
...

4

Construction

................................
................................
................................
................................
....

5

Building the Circuit

................................
................................
................................
......................

6

Software

................................
................................
................................
................................
......

6

Interfacing the BGB203

................................
................................
................................
...............

7

Measuring Ambient Room Temperature

................................
................................
....................

9

Connecting to the PC

................................
................................
................................
.....................

10

Bluetooth Configuration

................................
................................
................................
............

10

Web Interface using ASP.NET

................................
................................
................................
....

14

Associated
Code
s

................................
................................
...............

Error! Bookmark not defined.

Microcontroller
................................
................................
..............

Error! Bookmark not defined.

Website

................................
................................
.........................

Error! Bookmark not defined.

Conclusion

................................
................................
................................
................................
.....

15

Future Uses

................................
................................
................................
................................

15

Future Use Case 1

................................
................................
................................
......................

15

Future Use Case 2

................................
................................
................................
......................

16

Overall evaluation

................................
................................
................................
.....................

16









3

|
P a g e


Web Home Automation through
Bluetooth

Terminal
IIS Server
w
/
ASP
.
NET
Embedded Bluetooth
In Laptop

(
Broadcom

370
Mini Card
)
Bluetooth Module
WRL
-
08474
C
8051
F
005
Development Board
PDA
Laptop
Lighting
T
Temperature
Monitor
...

Cell phone

Overview


This project
aims to

create a pl
atform that

allow
s

the home owner to get information
about home temperatures and control lighting entirely through a web
-
based interface. On one
end, a home PC is set to run an ASP.NET web server that sends commands to the 8051
microcontroller via a Blueto
oth Serial Port Profile (SPP). On the other end, the microcontroller
interfaces with a Philips BGB203 Bluetooth module using UART. The microcontroller calculates
room temperature through the built
-
in 12
-
bit ADC using a thermistor, and also controls the
sta
tus of various household lights. Commands from the PC can tell the microcontroller to turn
on or off lights and
receive feedback
about
the

state of any light or the room temperature

from
the microcontroller
.

Equipment



(1) Silicon Labs C8051F005SDK Evaluati
on Board with USB Debug Adapter



(1) SparkFun WRL
-
08474 Bluetooth SMD Module based on Philips BGB203



(1)
LB21J1M

Fenwal Electronics T
hermistor



(3) 4.7kOhm resistors



(1) Bluetooth receiver

(
Broadcom

370 Mini Card)



(1) Bluetooth enabled Laptop



(1) RadioShac
k Wire Wrapping Tool



(1) RadioShack Solder Tool Kit



(1) RadioShack Insulated Wrapping Wire

4

|
P a g e


Schematic
s


As shown in the figure
, t
he following two figures are schematics of the project.
T
hey
have to two parts.
T
he first one
is the Analog I/O terminal block c
onnection.
A
s it is showed in
the figure, two
jumper
s will be placed on pin 5 & 7 and pin 4 & 8 respectively.



Figure
1

Analog I/O Terminal Block

18
14
AIN5
C8051F005-DK
Thermistor
AIN6
AIN2
Analog to Digital Conversion
AIN0
4.7K
All unconnected Pins hang in the air
DAC1
AIN7
DAC0
4.7K
VREF
4.7K
8
5
AIN1
6
7
8
11
12
AIN3
Power(3.3V)
17
13
AIN4

Figure
2

Analog to Digital Conversion Connection

5

|
P a g e


GND
28
PIO_3
UART_TX
16
18
ANT
VDD
29
PIO_4
UART_RTS
VDD
15
26
11
PIO_5
UART_CTS
VDD
16
SparkFun
WRL-08474
C8051F005F
PIO_6
P0.0
VDD
PCM_OUT
Bluetooth Module and Microcontroller Connection
C8051F005A
23
P0.1
DGND
PCM_IN
1
P0.7
6
13
P0.2
DGND
PCM_SYNC
13
SPI_CSB
2
14
P0.3
DGND
PCM_CLK
13
SPI_CLK
24
11
P0.4
AGND
7
39
SPI_MOSI
19
12
P0.5
DAC0
9
41
20
All unconnected Pins hang in the air
P0.6
DAC1
10
42
22
GND
AGND
48
21
GND
1
8
50
3
GND
12
13
45
5
GND
25
SPI_MISO
RESET
14
47
PIO_7
GND
27
PIO_2
UART_RX
15
17
4

Figu
re
3

Bluetooth Module and Microcontroller Connection

Construction


P
hysically, the whole project can basically be divided into three major parts. The first
one is the microcontroller.
H
ere, it is
the Silicon Lab C8051F005
developme
nt

board.
T
he second
part includes two things.
O
ne is the
Bluetooth

module connected to the
microcontroller board,
the other is the
Bluetooth

embedded in the laptop.
T
hey are in charge of communicating
wirelessly.
T
he final part is the web server in the la
ptop or in the Internet. Overall, the first part
is all hardware; the second part is a combination of hardware and software; the last part is
whole software.
D
ue to the difference between their features,
different
construction
s

need

to
be
used

to implement

their function
s
.

6

|
P a g e


Building the Circuit


There are a couple of different Bluetooth modules available for purchase. The WRL
-
08474 used here is a surface mount device, so you will need a breakout board or some other
way to connect the module to your microcont
roller.

1.

Connect pin TX on the module to RX on the microcontroller and RX to TX. This is the
basic setup for simple serial communication. In this microcontroller configuration,
the UART TX is assigned to pin P0.0 and RX is assigned to pin P0.1.

2.

On the modul
e, connect RTS to CTS pin. This bypasses any flow control setting on
the Bluetooth module.

3.

Build a Wheatstone bridge circuit with the 4.7kOhm resistors and the thermistor.
The voltage to be measured should be connected to AIO0 and AIO1 on the green
analog
I/O block.

4.

Set jumpers on the J6 connecter such that AIO0
-
1 is connected to the
microcontroller AIN0
-
1.

Software


This device requires use of the microcontroller’s UART and ADC subsystems. The
software needs to initialize these systems and also setup the B
luetooth module through the
UART interface using the BGB203’s AT command set.


First we will take a look at the ADC initialization. Set the BIASE bit to enable the ADC.
The C8051F005 already contains an internal 1.2V voltage reference which we can use by s
etting
the REFBE bit. The internal reference goes through a gain 2 buffer amplifier for an input range
from
-
2.43V to 2.43V. Set ADC conversion to start upon write of 1 to the ADBUSY bit in the
ADC0CN control register. Remember to enable the ADC end
-
of
-
con
version interrupt, bit 1 on the
EIE2 register.


Periodic readings of the thermistor are triggered by Timer 3 interrupt, which sets
ADBUSY. We initialize the reload value such that an interrupt is triggered approximately every
second. The Timer 3 ISR simply

clears the interrupt flag and sets ADBUSY.


To enable the UART, the CrossBar needs to be configured so that TX and RX are assigned
to port I/O pins. The exact pins depend on what other functionality is enabled, but in this setup
TX is assigned to P0.0 and

RX to P0.1. This is done by setting the UARTEN bit in the XBR0
register. Additionally, the TX pin needs to be set to push
-
pull in the PRT0CF register. The RX pin
is automatically set to open drain when the UART is enabled regardless of what is in PRT0CF.


Next we need to set the baud rate. The baud clock in this case is derived from Timer 2
running in Baud Clock Generator mode. The Bluetooth module can communicate at most
standard baud rates. However, the internal oscillator on the C8051F005 runs at a spee
d of
16MHz, which is not cleanly divisible into these standard speeds. You can purchase external
7

|
P a g e


oscillators such as those that run at 11.059MHz in order to access a wider range of
communication speeds.


We can calculate the baud rate with the following eq
uation:

Baud rate = (1/32)*(clock speed)*(1/(2^16
-
reload value))


In order to ensure successful communication, try to stay within 3
% error in baud rate.
Table
1
gives a list
of usable baud rates and reload

values for the internal oscillator versus that
for

an 11.0592MHz external oscillator.


Table
1

Microcontroller Clock and
Communication

Speed

Standard Rate

16MHz reload

16MHz error

11.0592MHz
reload

11.059MHz
error

115200

0xFFFC

8.51%

0xFFFD

0%

57600

0xFFF7

-
3.55%

0xFFFA

0%

38400

0xFFF3

0.16%

0xFFF7

0%

19200

0xFFE6

0.16%

0xFFEE

0%

9600

0xFFCC

0.16%

0xFFDC

0%



As you can see, the 11.0592MHz clock allows for faster UART communication even
though it is slower than the internal oscillator.
Additionally
, the internal oscillator doe
s not run
at exactly 16MHz, so you may need to try a couple of reload values before hitting on one that
works for your microcontroller.


Lastly, we need to implement the UART ISR called when either RI or TI flags are set. The
RI is the receive interrupt se
t when the microcontroller has finished receiving a byte of data in
the SBUF register. The TI is the transmit interrupt which is set when a byte of data has finished
transmitting. Since RI and TI both trigger the same interrupt, the ISR must handle both ca
ses.


On a receive interrupt, the software clears the RI flag and attempts to place the received
byte into a buffer array. If the buffer is full, the data is simply lost.


In case of a transmit interrupt, the software clears the TI flag and checks is there

is more
data in the transmit buffer. If it is empty, the software clears TXBUSY, a variable that keeps track
of whether the microcontroller is in a transmit sequence, and exits the ISR. If there is more data,
the next byte in the buffer is written to SBUF

which automatically initiates a transmission.

Interfacing the BGB203


The Bluetooth module is controlled by sending commands through the UART interface.
By default from the Philips manufacturer, the UART is configured for a baud rate of 115200, 8
data bit
s, no parity, 1 stop bit and no flow control. However, the module as received from
8

|
P a g e


SparkFun will have
a
default baud rate of 9600 instead. If you remember, the internal oscillator
on the development board could not hit a 115200 baud rate, so this is ideal.

All commands must begin with the “AT” prefix, except for the escape sequence. Also,
commands must end with a carriage return character, hexadecimal 0x0D. The standard
command sequence is shown below.

<Prefix><Command><CR>


The escape sequence is used to t
ell the module to enter command mode from data
mode. When in data mode, the module sends any input from the microcontroller to the PC over
Bluetooth. The default sequence is “+++”. The module may also fall back into command mode if
it loses its Bluetooth S
PP connection with the PC.


Before the module is ready to communicate with the PC, we need to issue a couple of
configuration commands.

+++

AT&F


Reset configuration to default settings stored in on
-
chip Flash.

AT+BTLNM=”SPSU
-
BT”


Change Bluetooth module l
ocal name to “SPSU
-
BT”

AT+BTAUT=1, 0


Set

automatic connection mode. The module will not leave data mode upon lost
connection

AT+BTURT=9600, 8, 0, 1, 0


Sets UART baud rate to 9600

AT+BTSEC=0


Disable security mode such that pairing is unnecessary

AT+BTFLS


Stores current configuration into Flash

AT+BTSRV=2


Starts the Bluetooth SPP server

9

|
P a g e



At this point,
it is

ready to communicate with the PC from the microcontroller. Once the
PC establishes an SPP connection, any data sent from the 8051 will show up on a
virtual serial
port on the PC, and vice versa.

Measuring Ambient Room Temperature


The B57891M0152J000 thermistor is a negative temperature coefficient device,
meaning its resistance gets smaller as the temperature increases. Thermistors are accurate and
s
table over time, but they are highly nonlinear. Instead of mathematically calculating the
temperature, the software will use an 8
-
point lookup table based off the thermistor datasheet
and estimate intermediate values using linear interpolation.


The thermi
stor is placed in a Wheatstone bridge with three 4.7kOhm resistors. By
measuring the voltage difference between both arms, we can accurately determine small
changes in the thermistor’s resistance. Another advantage of the Wheatstone bridge is that the
volt
age difference is a relative measurement, meaning fluctuations in supply voltage will not
affect the voltage measurement as much.


The 12
-
bit ADC in the C8051F005 converts the potential AIN0


AIN1 into a digital value
based off the internal 2.4V voltage r
eference. Thus in the differential input mode, the range of
the ADC is from
-
2.4V to 2.4V. We can use Kirchoff’s circuit laws to determine the resistance of
the thermistor.

V=Vcc/2
-
Vcc*R/(4.7k+R) V
-
Vcc/2=Vcc*R/(4.7k+R) (V
-
Vcc/2)*4.7k+(V
-
Vcc/2)*R=Vcc*R

V
-
Vcc/2)*4.7k=R(3*Vcc/2
-
V)

R=4.7k*(V
-
Vcc/2)/(Vcc*3/2
-
V)


By using the above equation, we can build a lookup table from the data sheet to relate
voltage to temperature.

Table
2

Resistance VS Temperature

Temperature

Resistance

Voltage

0.0

4300

0.0569

5.0

3436

0.240

10.0

2766

0.411

15.0

2238

0.569

20.0

1823

0.711

25.0

1500

0.836

30.0

1245

0.944

35.0

1030

1.04

40.0

856.5

1.13


The ADC samples an unknown input by using a small capacitor. It continuously tracks the
voltage level of

the input. When it is ready to perform a conversion, the ADC cuts off connection
10

|
P a g e


to the input and holds the charge that represents the analog level. This is commonly referred to
as a sample and hold circuit.

The ADC uses a technique called successive appr
oximation. It utilizes a 12
-
bit DAC and a
comparator. The ADC gets clocked 12 times by the system clock. Each cycle determines the
value of one bit, starting with the most significant. For example, on the first clock cycle, the
most significant bit of the
DAC is set and its voltage compared to the input. If the voltage of the
input is higher than the DAC output, then we know the most significant bit should be one.
Otherwise, that bit should be 0.

An ADC interrupt is triggered at the end of each conversion.
The ISR takes the new
reading and adds it to an accumulator variable. Every 8 readings, the accumulator is averaged
and the averaged value stored in the global variable ‘result’. Such an averaging filter helps
reduce the effect of noise and produce a more
stable temperature reading.

Connecting to the PC


The Bluetooth module embedded in the laptop will be used when communicating with
microcontroller board. A local server will be set up to create the interface between users and
t
he machine. Users can have fu
ll

control by sending command
s

in the web browser which is the
most direct way for most people to use.

Bluetooth Configuration


Bluetooth will be used as SPP in the computer. Without any other software’s help, the
Bluetooth module located in the microcontr
oller board can be found and paired using Windows
Vista OS’s Bluetooth service. The following are the detailed steps showing how to connect the
Bluetooth module using Windows Vista’s service.


Step

1: Open Bluetooth option and start searching findable
Blue
tooth

devices.


Picture
1

Bluetooth Option in Control Panel

11

|
P a g e



Picture
2

Searching Findable Bluetooth Devices


Step 2: As shown
above, a Bluetooth device named SparkFun
-
BT is found.
T
his is the one
locate
s in the microcontroller board. Right click the icon and enter the properties option.


Picture
3

Properties Option of SparkFun
-
BT

12

|
P a g e



Step 3: In the service tab
of

the properties option, click the serial port checkbox.
T
hen a
pop up

window will show up to indicate that the driver for this specific device is
installing
.


Picture
4

Driver for the Bluetooth Module is Installed


Step 4: As show
n
above, the
Bluetooth

is linked as COM8.
W
hen the installation of
d
river is completed, the
Bluetooth

module will show up in the paired devices list showed in the
following.


Picture
5

Paired Devices

13

|
P a g e



Step 5: When the connection is connected for the first time, a notification pop
-
up
window will s
how up to notify the users for security concern.
T
his message needs to be clicked
to allow the connection.


Picture
6

Connection Pop
-
up Notification Window


Step 6: For security concern
s
, a pairing code need
s

to be
entered

to pai
r the device.
Initial code

0000


will pass the test.


Picture
7

Entering Pairing Code


Step 7:
When

the following window is show
n
, then the configuration for the
Bluetooth

has

successfully

completed. Further use will not require

repeating

the process.

14

|
P a g e



Picture
8

Device successfully Paired

Web Interface using ASP.NET

The final step is to build a web interface to allow remote control by the home owner
from anywhere in the world. This is done using ASP.NET

to build a dynamic web page that can
interact with the virtual serial port on the web server.


Picture
9

Webpage Interface

15

|
P a g e


Conclusion

Future Uses

This project is
just
a prototype to exemplify a platform that allows the home owne
rs to
manage their homes and get feedback information entirely through a web
-
based interface.
However, it can actually implement much more functions than turning LED on and off. Since the
hardware prototype had been constructed, little modification needs t
o be
made in the web
interface and microcontroller terminal to implement extended uses.

T
he hardware for
implementing the extended functions can be easily connected to the microcontroller terminal.
The software interface can also be easily updated accordin
g to the new functions


requirement
.
T
he following figure
shows

a
practical

model that how it can be used in real life as a commercial
product.


Figure
4

Practical Web Home Automation Model

Future Use Case 1

Home Anti
-
Theft Securi
ty System

Description
: By using the Infared Sensor or Photoconductive Resistor, the state of the door
(opened or closed) can be detected, so that the security situation of user’s house can be
determined. The owner can check the security state of the house

anywhere in the world via
Internet. Mor importantly, a notification message can be pushed to the owner’s cell phone or
any other portable communication device.

16

|
P a g e



Figure
5

Infrared
Sensing

Model

Implementation:

An infrared detector

will be connected to 8051 microcontroller terminal.
T
he
infrared generator will be
embed
ded in the door while the receiver is
embedded

in the frame.
W
hen door is closed, the detector can detect the infrared signal and the state of the door can be
determin
ed.
B
ut once

the door is open, the detector can
not

detect any infrared signal and
signal

to be sent to
8051 microcontroller
to update
the state

of the door (opened or closed)
to
the
webpage interface.
A
ny change of the state of the door can be notified to
the house owners


portable
devices

to let them know the
latest

state of the house.


Future Use Case 2

Central Air Conditioning System Remote Control

Description
: By using the embedded thermistor in the microcontroller terminal, the indoor
temperature can b
e detected; therefore, an appropriate modification can be made for the
central air conditioning system to keep a consistent room temperature. The home owner can
check the temperature and send control commands to the air conditioning system, without
spacia
l limitation, via Internet access.

Implementation:


Since the microcontroller terminal has the function of detecting the
temperature, the indoor temperature can be
easily

obtained.
T
he air conditioning control panel
will also be connected with the microcon
troller terminal.
H
ouse owner can set the room
temperature range for the air conditioner.
O
nce the room temperature runs out of the range,
specific actions will be taken for the air conditioning system to keep consistent room
temperature.
H
ome owners can e
ven turn off/on the remotely.
A
ll the above functions can be
implemented within the webpage user interface

no matter where the command is sent.

Overall evaluation


8051 microcontroller was invented almost 30 years ago

by Intel
.
I
t is still used nowadays
be
cause it
is powerful enough

t
o implement some complicated functions.
I
n
this

particular

project, the 8051 microcontroller
is in charge of everything including the UART communication,
the a
nalog

to digital conversion calculation and the LED controlling.
U
si
ng the powerful analog
to digital I/O on the development tool,
many extended functions can be implemented easily.

17

|
P a g e



T
he coverage of Internet
access

is in an
incredibly wide

range

now
.
B
eside
s

the
traditional Internet
access

methods such as DSL
and

Dial
-
up,

people are
us
ing 3G, EDGE,
and Wi
-
Fi

wireless technologies to have access to the Internet. This can dramatically expand the web
user interface platforms from
traditional

PC terminal
s

to any Internet enable
d

portable devices.
I
n other words, the wide cover
age and usage of the Internet enhances the usability and
reliability

of the project.


T
he use of
Bluetooth wireless communication helps simplify the installation process in
the house.
P
eople do not have to hook the appliances and the microcontroller termin
al up.
T
his
can significantly help the house owner keep their living places tidy and organized.
A
gain, since
all the

connections are

wireless, extended functions can be very easily implemented with little
modification.