web home automation using bluetooth communication with 8051 microcontroller

23
SOUTHERN POLYTECHNIC STATE UNIVERSITY Web Home Automation using Bluetooth Communication with 8051 Microcontroller Bin Zhou

Upload: ravi1432

Post on 18-Apr-2015

85 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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.

Page 2: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

Table of ContentsOverview........................................................................................................................................3

Equipment..................................................................................................................................3

Schematics..................................................................................................................................4

Construction...................................................................................................................................6

Building the Circuit.....................................................................................................................7

Software.....................................................................................................................................7

Interfacing the BGB203...............................................................................................................8

Measuring Ambient Room Temperature..................................................................................10

Connecting to the PC....................................................................................................................11

Bluetooth Configuration...........................................................................................................11

Web Interface using ASP.NET...................................................................................................15

Associated Codes..........................................................................................................................16

Microcontroller.........................................................................................................................16

Website....................................................................................................................................28

Conclusion....................................................................................................................................32

Future Uses...............................................................................................................................32

Future Use Case 1.....................................................................................................................33

Future Use Case 2.....................................................................................................................33

Overall evaluation.....................................................................................................................34

2 | P a g e

Page 3: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

Web Home Automation through Bluetooth

OverviewThis 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 (1) RadioShack Insulated Wrapping Wire

3 | P a g e

Page 4: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

SchematicsAs 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

18

14AIN5

C8051F005-DK

The rm is t o r

AIN6

AIN2

Analog to Digital Conversion

AIN0

4 . 7K

All unconnected Pins hang in the air

DAC1

AIN7

DAC04 . 7K

VREF

4 . 7K

8

5

AIN1

6

7

8

11

12AIN3

P owe r(3 . 3V )

17

13AIN4

Figure 2 Analog to Digital Conversion Connection

4 | P a g e

Page 5: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

GND28

PIO_3

UART_TX

16

18ANT

VDD

29

PIO_4

UART_RTS

VDD

1526

11

PIO_5

UART_CTS

VDD

16

SparkFunWRL-08474

C8051F005FPIO_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

13SPI_CSB

2

14

P0.3

DGND

PCM_CLK

13SPI_CLK

24

11

P0.4

AGND

739SPI_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

GND25

SPI_MISO

RESET

14

47

PIO_7

GND

27

PIO_2

UART_RX

15

17 4

Figure 3 Bluetooth Module and Microcontroller Connection

ConstructionPhysically, 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.

5 | P a g e

Page 6: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

Building the CircuitThere 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.

SoftwareThis device requires use of the microcontroller’s UART and ADC subsystems. The

software needs to initialize these systems and also setup the Bluetooth 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 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 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

6 | P a g e

Page 7: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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

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 BGB203The 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

7 | P a g e

Page 8: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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

Disable security mode such that pairing is unnecessary

AT+BTFLS

Stores current configuration into Flash

AT+BTSRV=2

Starts the Bluetooth SPP server

8 | P a g e

Page 9: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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 TemperatureThe 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 thermistor’s 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 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 Voltage0.0 4300 0.05695.0 3436 0.240

10.0 2766 0.41115.0 2238 0.56920.0 1823 0.711

25.0 1500 0.83630.0 1245 0.94435.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

9 | P a g e

Page 10: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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 PCThe 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 ConfigurationBluetooth will be used as SPP in the computer. Without any other software’s help, the

Bluetooth module located in the microcontroller 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 Bluetooth devices.

Picture 1 Bluetooth Option in Control Panel

10 | P a g e

Page 11: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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.

Picture 3 Properties Option of SparkFun-BT

11 | P a g e

Page 12: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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.

Picture 5 Paired Devices

12 | P a g e

Page 13: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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.

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.

13 | P a g e

Page 14: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

Picture 8 Device successfully Paired

Web Interface using ASP.NETThe 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

14 | P a g e

Page 15: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

Conclusion

Future UsesThis 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.

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

15 | P a g e

Page 16: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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 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 2Central 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 evaluation8051 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.

16 | P a g e

Page 17: Web Home Automation Using Bluetooth Communication With 8051 Microcontroller

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.

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.

17 | P a g e