You are on page 1of 20

SOUTHERN POLYTECHNIC STATE UNIVERSITY

Web Home Automation using Bluetooth Communication with 8051 Microcontroller


Bin Zhou

This report includes all detailed information of this honor research. Readers can do the exact same research by following the instruction given in the report.

Table of Contents
Table of Contents..............................................................................................2 Overview.......................................................................................................... 3 Equipment..................................................................................................... 3 Schematics.................................................................................................... 4 Construction..................................................................................................... 6 Building the Circuit........................................................................................7 Software........................................................................................................ 7 Interfacing the BGB203..................................................................................9 Measuring Ambient Room Temperature.......................................................10 Connecting to the PC......................................................................................11 Bluetooth Configuration...............................................................................11 Web Interface using ASP.NET.......................................................................16 Conclusion...................................................................................................... 17 Future Uses..................................................................................................17 Future Use Case 2........................................................................................19 Overall evaluation........................................................................................19

Web Home Automation through Bluetooth


2|Page

Terminal

Lighting

Laptop

T
Embedded Bluetooth Bluetooth Module IIS Server In Laptop WRL-08474 w/ ASP.NET(Broadcom 370 Mini Card) Temperature C8051F005 Monitor Development Board

Cell phone

...
PDA

Overview
This project aims to create a platform that allows 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 Bluetooth 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 status 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 Evaluation Board with USB Debug Adapter (1) SparkFun WRL-08474 Bluetooth SMD Module based on Philips BGB203 (1) LB21J1M Fenwal Electronics Thermistor (3) 4.7kOhm resistors (1) Bluetooth receiver (Broadcom 370 Mini Card) (1) Bluetooth enabled Laptop (1) RadioShack Wire Wrapping Tool (1) RadioShack Solder Tool Kit

3|Page

(1) RadioShack Insulated Wrapping Wire

Schematics
As shown in the figure, the following two figures are schematics of the project. They have to two parts. The first one is the Analog I/O terminal block connection. As it is showed in the figure, two jumpers will be placed on pin 5 & 7 and pin 4 & 8 respectively.

Figure 1 Analog I/O Terminal Block

4|Page

A n a l o g
C 8 0 5 1 F 0 0 5 - D K V R E F D A C 0 D A C 1 A I N 0 A I N 1 A I N 2 A I N 3 A I N 4 A I N 5 A I N 6 A I N 7 8 5 6 7 8 1 1 1 2 1 3 1 4 1 7 1 8

t o
A l l

D i g i t a l
u n c o n n e c t e d P o w e P i n s r ( 3 . 3

C o n v e r s
h a n g V ) i n

t h e

4 . 7

. 7 K

T h e

r m

i s

t 4o .r 7 K

Figure 2 Analog to Digital Conversion Connection

5|Page

B l u e t o o t h
C 8 0 5 1 F 0 0 5 A V D D V D D V D D D G N D D G N D D G N D 1 1 3 1 3 3 9 4 1 4 2

M o d u l e
A l l

a n d

M i c r o c o n t r o l l e
P i n s h a n g i n t h e

u n c o n n e c t e d S p a r k F u n W R L - 0 8 4 7 4

5 7 8 9 1 0

R E S E T P C M _ C L K

S P I _ C S 2B 3 S P I _ C L 6K

S P I _ M O S 2I P C M _ S Y N C S P I _ M I S 2O 4 P C M _ I N P C M _ O U T U A R T _ R X U A R T _ T X U A R T _ R T S U A R T _ C T S P I O _ 21 9 P I O _ 32 0 P I O _ 42 2 P I O _ 52 1 P I O _ 63 P I O _ 74

D A C 0 D A C 1 A G N D A G N D C 8 0 5 1 F 0 0 5 F P 0 . 0 P 0 . 1 P 0 . 2 P 0 . 3 P 0 . 4 P 0 . 5 P 0 . 6 P 0 . 7

4 8 5 0 4 5 4 7 1 7 1 8 1 5 1 6 1 3 1 4 1 1 1 2

1 3 1 4 1 5 1 6

1 1 1 1 2 2 5 2 7 2 8 2 9

V D D G N D G N D G N D G N D G N D G N D

A N T

2 6

Figure 3 Bluetooth Module and Microcontroller Connection

Construction
Physically, the whole project can basically be divided into three major parts. The first one is the microcontroller. Here, it is the Silicon Lab C8051F005 development board. The second part includes two things. One is the Bluetooth module connected to the microcontroller board, the other is the Bluetooth embedded in the laptop. They are in charge of communicating wirelessly. The final part is the web server in the laptop 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. Due to the difference between their features, different constructions need to be used to implement their functions. 6|Page

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 microcontroller. 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 module, 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 microcontrollers UART and ADC subsystems. The software needs to initialize these systems and also setup the Bluetooth module through the UART interface using the BGB203s 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 setting 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-conversion 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

7|Page

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 speed of 16MHz, which is not cleanly divisible into these standard speeds. You can purchase external 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 equation:

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 115200 57600 38400 19200 9600

16MHz reload 0xFFFC 0xFFF7 0xFFF3 0xFFE6 0xFFCC

16MHz error 8.51% -3.55% 0.16% 0.16% 0.16%

11.0592MHz reload 0xFFFD 0xFFFA 0xFFF7 0xFFEE 0xFFDC

11.059MHz error 0% 0% 0% 0% 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 does 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 set 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 cases. 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. 8|Page

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 bits, no parity, 1 stop bit and no flow control. However, the module as received from 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 tell 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 SPP 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 local 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 9|Page

Disable security mode such that pairing is unnecessary AT+BTFLS Stores current configuration into Flash AT+BTSRV=2 Starts the Bluetooth SPP server 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 stable 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 thermistor 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 thermistors resistance. Another advantage of the Wheatstone bridge is that the voltage 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 reference. Thus in the differential input mode, the range of the ADC is from -2.4V to 2.4V. We can use Kirchoffs 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 0.0 5.0 10.0

Resistance 4300 3436 2766

Voltage 0.0569 0.240 0.411

10 | P a g e

15.0 20.0 25.0 30.0 35.0 40.0

2238 1823 1500 1245 1030 856.5

0.569 0.711 0.836 0.944 1.04 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 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 approximation. 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 the machine. Users can have full control by sending commands 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 softwares help, the Bluetooth module located in the microcontroller board can be found and paired using Windows Vista OSs Bluetooth service. The following are the detailed steps showing how to connect the Bluetooth module using Windows Vistas service. Step 1: Open Bluetooth option and start searching findable Bluetooth devices.

11 | P a g e

Picture 1 Bluetooth Option in Control Panel

Picture 2 Searching Findable Bluetooth Devices

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

12 | P a g e

Picture 3 Properties Option of SparkFun-BT

Step 3: In the service tab of the properties option, click the serial port checkbox. Then 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 shown above, the Bluetooth is linked as COM8. When the installation of driver is completed, the Bluetooth module will show up in the paired devices list showed in the following.

13 | P a g e

Picture 5 Paired Devices

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

Picture 6 Connection Pop-up Notification Window

Step 6: For security concerns, a pairing code needs to be entered to pair the device. Initial code 0000 will pass the test.

14 | P a g e

Picture 7 Entering Pairing Code

Step 7: When the following window is shown, then the configuration for the Bluetooth has successfully completed. Further use will not require repeating the process.

15 | 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.

16 | P a g e

Picture 9 Webpage Interface

Conclusion
Future Uses
This project is just a prototype to exemplify a platform that allows the home owners 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 to be made in the web interface and microcontroller terminal to implement extended uses. The hardware for implementing the extended functions can be easily connected to the microcontroller terminal. The software interface can also be easily updated according to the new functions requirement. The following figure shows a practical model that how it can be used in real life as a commercial product.

17 | P a g e

Figure 4 Practical Web Home Automation Model

Future Use Case 1


Home Anti-Theft Security 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 users 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 owners cell phone or any other portable communication device.

Figure 5 Infrared Sensing Model

Implementation: An infrared detector will be connected to 8051 microcontroller terminal. The infrared generator will be embedded in the door while the receiver is embedded in the frame. When door is closed, the detector 18 | P a g e

can detect the infrared signal and the state of the door can be determined. But once the door is open, the detector cannot 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. Any 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 be 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 spacial limitation, via Internet access. Implementation: Since the microcontroller terminal has the function of detecting the temperature, the indoor temperature can be easily obtained. The air conditioning control panel will also be connected with the microcontroller terminal. House owner can set the room temperature range for the air conditioner. Once the room temperature runs out of the range, specific actions will be taken for the air conditioning system to keep consistent room temperature. Home owners can even turn off/on the remotely. All 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. It is still used nowadays because it is powerful enough to implement some complicated functions. In this particular project, the 8051 microcontroller is in charge of everything including the UART communication, the analog to digital conversion calculation and the LED controlling. Using the powerful analog to digital I/O on the development tool, many extended functions can be implemented easily. The coverage of Internet access is in an incredibly wide range now. Besides the traditional Internet access methods such as DSL and Dial-up, people are using 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 terminals to any Internet enabled portable devices. In other words, the wide coverage and usage of the Internet enhances the usability and reliability of the project.

19 | P a g e

The use of Bluetooth wireless communication helps simplify the installation process in the house. People do not have to hook the appliances and the microcontroller terminal up. This can significantly help the house owner keep their living places tidy and organized. Again, since all the connections are wireless, extended functions can be very easily implemented with little modification.

20 | P a g e

You might also like