Vous êtes sur la page 1sur 25

Web Home Automation through Bluetooth

Overview
This project aims to allow the home owner to get information about home temperatures and control lighting entirely through a web-based interface. On one end a home !" is set to run an A#!.$%T web server that sends commands to the &'() microcontroller via a Bluetooth #erial !ort !rofile *#!!+. On the other end the microcontroller interfaces with a !hilips B,B-'. Bluetooth module using /A0T. The microcontroller calculates room temperature through the built-in )--bit A1" using a thermistor and also controls the status of various household lights. "ommands from the !" can tell the microcontroller to turn on or off lights and report the state of any light or the room temperature.

%2uipment
*)+ #ilicon 3abs "&'()4''(#15 %valuation Board with /#B 1ebug Adapter *)+ #par64un W03-'&787 Bluetooth #91 9odule based on !hilips B,B-'. *)+ #par64un W03-'')(' Bluetooth /#B 9odule *)+ B(8&:)9')(-;''' 3eaded $T" thermistor *.+ 7.86Ohm resistors

#chematic

Building the "ircuit


There are a couple of different Bluetooth modules available for purchase. The W03-'&787 used here is a surface mount device so you will need a brea6out board or some other way to connect the module to your microcontroller.

).

"onnect pin T< on the module to 0< on the microcontroller and 0< to T<. This is the basic setup for simple serial communication. =n this microcontroller configuration the /A0T T< is assigned to pin !'.' and 0< is assigned to pin !'.). On the module connect 0T# to "T# pin. This bypasses any flow control setting on the Bluetooth module. Build a Wheatstone bridge circuit with the 7.86Ohm resistors and the thermistor. The voltage to be measured should be connected to A=O' and A=O) on the green analog =>O bloc6. #et jumpers on the ;? connecter such that A=O'-) is connected to the microcontroller A=$'-).

-.

..

7.

#oftware
This device re2uires use of the microcontroller@s /A0T and A1" subsystems. The software needs to initialiAe these systems and also setup the Bluetooth module through the /A0T interface using the B,B-'.@s AT command set. 4irst we will ta6e a loo6 at the A1" initialiAation. #et the B=A#% bit to enable the A1". The "&'()4''( already contains an internal ).-B voltage reference which we can use by setting the 0%4B% bit. The internal reference goes through a gain - buffer amplifier for an input range from --.7.B to -.7.B. #et A1" conversion to start upon write of ) to the A1B/#C bit in the A1"'"$ control register. 0emember to enable the A1" end-ofconversion interrupt bit ) on the %=%- register. !eriodic readings of the thermistor are triggered by Timer . interrupt which sets A1B/#C. We initialiAe the reload value such that an interrupt is triggered approDimately every second. The Timer . =#0 simply clears the interrupt flag and sets A1B/#C. To enable the /A0T the "rossBar needs to be configured so that T< and 0< are assigned to port =>O pins. The eDact pins depend on what other functionality is enabled but in this setup T< is assigned to !'.' and 0< to !'.). This is done by setting the /A0T%$ bit in the <B0' register. Additionally the T< pin needs to be set to push-pull in the !0T'"4 register. The 0< pin is automatically set to open drain when the /A0T is enabled regardless of what is in !0T'"4. $eDt we need to set the baud rate. The baud cloc6 in this case is derived from Timer - running in Baud "loc6 ,enerator mode. The Bluetooth module can communicate at most standard baud rates. However the internal oscillator on the "&'()4''( runs at a speed of )?9HA which is not cleanly divisible into these standard speeds. Cou can purchase eDternal oscillators such as those that run at )).'(:9HA in order to access a wider range of communication speeds. We can calculate the baud rate with the following e2uationE Baud rate F *)>.-+G*cloc6 speed+G*)>*-H)?-reload value++ =n order to ensure successful communication try to stay within .I error in baud rate. Table J gives a list of usable baud rates and reload values for the internal oscillator versus that for an )).'(:-9HA eDternal oscillator. #tandard 0ate ))(-'' (8?'' .&7'' )?9HA reload 'D444" 'D4448 'D444. )?9HA error &.()I -..((I '.)?I )).'(:-9HA reload 'D4441 'D444A 'D4448 )).'(:9HA error 'I 'I 'I

):-'' :?''

'D44%? 'D44""

'.)?I '.)?I

'D44%% 'D441"

'I 'I

As you can see the )).'(:-9HA cloc6 allows for faster /A0T communication even though it is slower than the internal oscillator. Also the internal oscillator does not run at eDactly )?9HA so you may need to try a couple of reload values before hitting on one that wor6s for your microcontroller. 3astly we need to implement the /A0T =#0 called when either 0= or T= flags are set. The 0= is the receive interrupt set when the microcontroller has finished receiving a byte of data in the #B/4 register. The T= is the transmit interrupt which is set when a byte of data has finished transmitting. #ince 0= and T= both trigger the same interrupt the =#0 must handle both cases. On a receive interrupt the software clears the 0= flag and attempts to place the received byte into a buffer array. =f the buffer is full the data is simply lost. =n case of a transmit interrupt the software clears the T= flag and chec6s is there is more data in the transmit buffer. =f it is empty the software clears T<B/#C a variable that 6eeps trac6 of whether the microcontroller is in a transmit se2uence and eDits the =#0. =f there is more data the neDt byte in the buffer is written to #B/4 which automatically initiates a transmission.

=nterfacing the B,B-'.


The Bluetooth module is controlled by sending commands through the /A0T interface. By default from the !hilips manufacturer the /A0T is configured for a baud rate of ))(-'' & data bits no parity ) stop bit and no flow control. However the module as received from #par64un will have default baud rate of :?'' instead. =f you remember the internal oscillator on the development board could not hit a ))(-'' baud rate so this is ideal. All commands must begin with the KATL prefiD eDcept for the escape se2uence. Also commands must end with a carriage return character heDadecimal 'D'1. The standard command se2uence is shown below. M!refiDNM"ommandNM"0N The escape se2uence 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 !" over Bluetooth. The default se2uence is KOO OL. The module may also fall bac6 into command mode if it loses its Bluetooth #!! connection with the !". Before the module is ready to communicate with the !" we need to issue a couple of configuration commands. OOO ATP4 0eset configuration to default settings stored in on-chip 4lash. ATOBT3$9FL#!#/-BTL "hange Bluetooth module local name to K#!#/-BTL ATOBTA/TF) ' #ets automatic connection mode. The module will not leave data mode upon lost connection

ATOBT/0TF:?'' & ' ) ' #ets /A0T baud rate to :?'' ATOBT#%"F' 1isable security mode such that pairing is unnecessary ATOBT43# #tores current configuration into 4lash ATOBT#0BF#tarts the Bluetooth #!! server At this point you are ready to communicate with the !" from the microcontroller. Once the !" establishes an #!! connection any data sent from the &'() will show up on a virtual serial port on the !" and vice versa.

9easuring Ambient 0oom Temperature


The B(8&:)9')(-;''' 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. =nstead of mathematically calculating the temperature the software will use an &-point loo6up table based off the thermistor datasheet and estimate intermediate values using linear interpolation. The thermistor is placed in a Wheatstone bridge with three 7.86Ohm 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 )--bit A1" in the "&'()4''( converts the potential A=$' Q A=$) into a digital value based off the internal -.7B voltage reference. Thus in the differential input mode the range of the A1" is from --.7B to -.7B. We can use 5irchoff@s circuit laws to determine the resistance of the thermistor. BFBcc>- -BccG0>*7.86O0+ B-Bcc>-FBccG0>*7.86O0+ *B-Bcc>-+G7.86O*B-Bcc>-+G0FBccG0 B-Bcc>-+G7.86F0*.GBcc>--B+ 0F7.86G*B-Bcc>-+>*BccG.>--B+ By using the above e2uation we can build a loo6up table from the data sheet to relate voltage to temperature. Temperature '.' (.' )'.' )(.' -'.' -(.' .'.' .(.' 0esistance 7.'' .7.? -8?? --.& )&-. )('' )-7( )'.' Boltage '.'(?: '.-7' '.7)) '.(?: '.8)) '.&.? '.:77 ).'7

7'.'

&(?.(

).).

The A1" samples an un6nown input by using a small capacitor. =t continuously trac6s the voltage level of the input. When it is ready to perform a conversion the A1" 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 A1" uses a techni2ue called successive approDimation. =t utiliAes a )--bit 1A" and a comparator. The A1" gets cloc6ed )- times by the system cloc6. %ach cycle determines the value of one bit starting with the most significant. 4or eDample on the first cloc6 cycle the most significant bit of the 1A" is set and its voltage compared to the input. =f the voltage of the input is higher than the 1A" output then we 6now the most significant bit should be one. Otherwise that bit should be '. An A1" interrupt is triggered at the end of each conversion. The =#0 ta6es the new reading and adds it to an accumulator variable. %very & readings the accumulator is averaged and the averaged value stored in the global variable Rresult@. #uch an averaging filter helps reduce the effect of noise and produce a more stable temperature reading.

"onnecting to the !"


=nstall the Blue#oleil software that comes with the Bluetooth /#B module. !lug in the Bluetooth module and open the Blue#oleil main window by double clic6ing on the tray icon.

$eDt clic6 on the orange orb to search for Bluetooth devices. 1on@t forget to have your microcontroller Bluetooth module powered on at this pointS $ote that the #par64un module might show up as only a device address at first. Wait until the Blue#oleil program finishes its search and right clic6 on the module then select 0efresh #ervices. After that right clic6 again and select "onnect-NBluetooth #erial !ort #ervice.

The first time you connect to the #!! service a Tuic6 "onnect dialogue will appear. Write down or remember which "O9 port it is connected to. =f at any time you forget you can find out which port the device is assigned by going to Tools-N"onfiguration-NTuic6 "onnectU $ow you can open up any terminal program and send data to the microcontroller by connecting to that "O9 port with the same baud rate and data settings as the Bluetooth module on the device.

Web =nterface using A#!.$%T


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 A#!.$%T to build a dynamic web page that can interact with the virtual serial port on the web server.

The website organiAes and shows the user the status of every component connected to the microcontroller. The two update buttons update the status variables by 2uerying the &'() over the Bluetooth. The microcontroller processes 2uery strings and returns the relevant data. #imilarly the website uses a form interface to allow the user to send command strings over Bluetooth which the microcontroller processes and effects any necessary changes.

"ode
main.c: #include <c8051f000.h> #include <stdio.h> #include "uart_fifo.h" sbit LED = P1 !" ## $D%0 definitions #define &$'LE()*E 8 #define )+&DE% 8 unsi,ned char a-,'uffer.8/" unsi,ned short a-,&e01erature" unsi,ned char a-,2olta,e" unsi,ned short su02olta,e" unsi,ned short accu0ulator" unsi,ned short result" char -olta,e&able.&$'LE()*E/ = 3!45 605 6!5 845 885 745 785 1048" unsi,ned short te01erature&able.&$'LE()*E/ = 31655 9005 9955 9505 9655 4005 4955 4508"

union stran,e 3 unsi,ned short buf1!" char buf8.9/" 8 adc'uffer" ## 0ain definitions #define '$:D;$&E 115900 -oid (<(%L=_)nit>-oid?" -oid P@;&_)nit>-oid?" -oid &)AE;4_)nit>-oid?" -oid $D%0_)nit>-oid?" -oid &)AE;1_)(;>-oid?" -oid &)AE;4_)(;>-oid?" -oid $D%0_)(;>-oid?" -oid re1ort&e01erature>?" char adcrate" char buffer" int i5 B" -oid 0ain >-oid? 3 CD&%+ = 0DDE" CD&%+ = 0D$D" (<(%L=_)nit>?" P@;&_)nit>?" &)AE;4_)nit>?" $D%0_)nit>?" :$;&_)nit>7!00?" )E H= 0D80" ;I'uf)nit>?" &I'uf)nit>?" :$;&_@ut(trin,>"JJJ$&J'&$:&=1Kr"?" :$;&_@ut(trin,>"$&J'&(;2=9Kr"?"" Ehile>1? 3 if>:$;&_)n%har>Lbuffer?? 3 if>buffer == MNM? 3 Ehile>O:$;&_)n%har>Lbuffer?? 3 8 if>buffer == M&M? 3 re1ort&e01erature>?" 8 else if>buffer == MLM? 3

## disable Eatchdo, ti0er ## initialiFe oscillator ## initialiFe crossbar and GP)@ ## initialiFe heartbeat interru1t ## initialiFe $D%0 ## initialiFe :$;& ## ,lobal interru1t enable

Ehile>O:$;&_)n%har>Lbuffer?? 3 8 sEitch>buffer? 3 case M1MP Ehile>O:$;&_)n%har>Lbuffer?? 3 8 if>LED? 3 :$;&_@ut(trin,>"L1P@nKrKn"?" 8 else 3 :$;&_@ut(trin,>"L1P@ffKrKn"?" 8 breaQ" defaultP breaQ" 8 8 8 else if>buffer == M%M? 3 Ehile>O:$;&_)n%har>Lbuffer?? 3 8 if>buffer == MLM? 3 Ehile>O:$;&_)n%har>Lbuffer?? 3 8 sEitch>buffer? 3 case M1MP Ehile>O:$;&_)n%har>Lbuffer?? 3 8 if>buffer == M1M? 3 LED = 1" 8 else if>buffer == M0M? 3 LED = 0" 8 breaQ" defaultP breaQ" 8 8 8 8 8 8 ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## )nitialiFation (ubroutines ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## (<(%L=_)nit ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## ## &his routine initialiFes the sSste0 clocQ to use internal 1!ATF crSstal ## -oid (<(%L=_)nit>-oid? 3

@(%I%+ = 0D00" @(%)%+ = 0D06" 8

## eDternal oscillator off ## internal oscillator set to 1!ATF

##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## P@;&_)nit ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## ## %onfi,ure the %rossbar and GP)@ 1orts ## -oid P@;&_)nit>-oid? 3 ## I';0 Port )#@ %rossbar ;e,ister 0 ## bit 6 %P0E+ = 0 ## bit ! E%)E = 0 ## bit 5R4 P%$0AE = 000 ## bit 9 :$;&E+ = 1 ## bit 1 (P)0@E+ = 0 ## bit 0 (A'0@E+ = 0 I';0 = 0D0U" I';1 = 0D00" I';9 = 0DU0" P;&0%V H= 0D04" P;&1%V H= 0DU0" P;&0%V = 0DVD" P;&1%V = 0DVV" P;&9%V = 0DVV" P;&4%V = 0DVV" P1 = 0D00" 8 ## &i0er 4 ## controls $D%0 con-ersion interru1t -oid &)AE;4_)nit>-oid? 3 ## &A;4%+ &i0er 4 %ontrol ;e,ister ## bit 6 &V4 ## bit !R4 unused ## bit 9 &;4 = 0 disable ti0er 4 ## bit 1 &4A = 0 use sSste0 clocQ#19 ## bit 0 unused &A;4%+ = 0D00" &A;4;LT = >>>RU1!!6? >> 8? L 0DVV?" &A;4;LL = >>RU1!!6? L 0DVV?" &A;4T = &A;4;LT" &A;4L = &A;4;LL" E)E9 = 0D01" &A;4%+ = 0D0U" 8 ## Enable crossbar and EeaQ 1ullRu1s ## set &I 1in as 1ushR1ull ## set heartbeat led as 1ushR1ull

-oid $D%0_)nit>-oid? 3 ## select $)+0R1 differential in1ut $AI0%V = 0D01" $AI0(L = 0D00" ## select $)+0 sin,le ended in1ut ##$AI0%V = 0D00" ##$AI0(L = 0D00" ## $D%0%V $D% %onfi,uration ;e,ister ## bit 6 $D%(%9 = 0 ## bit ! $D%(%1 = 1 ## bit 5 $D%(%0 = 1 ## bit UR4 unused ## bit 9 $APG+9 = 0 ## bit 1 $APG+1 = 0 ## bit 0 $APG+0 = 0 $D%0%V = 0D!0" ## $D%0%+ $D% %ontrol ;e,ister ## bit 6 $D%E+ = 1 ## enable $D%0 ## bit ! $D%&A = 1 ## bit 5 $D%)+& = 0 ## bit U $D':(< = 0 ## bit 4 $D(&A1 = 0 ## con-ersion on Erite to $D':(< ## bit 9 $D(&A0 = 0 ## bit 1 $DC)+& = 0 ## bit 0 $DLW(& = 0 ## ri,htRBustified $D%0%+ = 0D%0" ## $D%0T.6PU/ = 000b ## data in $D%0T.4P0/R$D%0L.6P0/ ## ;EV0%+ ;eference %ontrol ;e,ister ## bit 6R4 unused ## bit 9 &EAPE = 0 ## internal te01erature sensor off ## bit 1 ')$(E = 1 ## internal bias on ## bit 0 ;EV'E = 1 ## use internal -olta,e reference ;EV0%+ = 0D04" E)E9 H= 0D09" 8 -oid $D%0_)(;>-oid? interru1t 15 3 $D%)+& = 0" ## clear interru1t fla, adc'uffer.buf8.0/ = $D%0T" adc'uffer.buf8.1/ = $D%0L" accu0ulator J= adc'uffer.buf1!" if>OB? 3 result = accu0ulator#)+&DE%" ## deci0ate and store ## add neE -alue to accu0ulator ## enable $D%0 endRofRcon-ersion interru1t

B = )+&DE%" accu0ulator = 0" 8 BRR" 8 -oid &)AE;4_)(;>-oid? interru1t 1U 3 &A;4%+ L= X>0D80?" ## a11roDi0atelS 1 con-ersion#second if>>adcrateJJ? == 0D10? 3 $D':(< = 1" adcrate = 0" 8 8 -oid re1ort&e01erature>? 3 ## con-erts raE $D% -alue to -olta,e in units 0.12 a-,2olta,e = >>>>result >> 9?Y!1?#198? L 0DVV?" i = 0" Ehile>i < &$'LE()*E LL a-,2olta,e > -olta,e&able.i/? 3 iJJ" 8

## reset accu0ulator

if>i == 0? 3 ## -alue is beloE 0ini0u0 on looQu1 table a-,&e01erature = te01erature&able.i/" 8 else if>i == &$'LE()*E? 3 ## -alue is abo-e 0aDi0u0 on looQu1 table a-,&e01erature = te01erature&able.&$'LE()*ER1/" 8 else 3 ## linear a11roDi0ation for an inRbetEeen -alue a-,&e01erature = >te01erature&able.i/ R >-olta,e&able.i/ R a-,2olta,e? Y >te01erature&able.i/ R te01erature&able.iR1/? # >-olta,e&able.i/ R -olta,e&able.iR1/??" 8 ## re1orts te01erature in M&PDD.DKrKnM for0at :$;&_@ut(trin,>"&P"?" :$;&_@ut:Dec>a-,&e01erature?" :$;&_@ut(trin,>"KrKn"?" 8 uart_fifo.h:

## V)V@ Definitions -oid ;I'uf)nit>-oid?" int ;I'ufPush>char neEdata?" int ;I'ufPo1>char YdataPt?" int ;I'ufVull>-oid?" int ;I'ufE01tS>-oid?" -oid &I'uf)nit>-oid?" int &I'ufPush>char neEdata?" int &I'ufPo1>char YdataPt?" int &I'ufVull>-oid?" int &I'ufE01tS>-oid?" ## :$;& Definitions -oid :$;&_)nit>int baudrate?" -oid :$;&_)(;>-oid?" -oid :$;&_@ut%har>char outdata?" -oid :$;&_@ut(trin,>char Youtdata?" -oid :$;&_@ut:Dec>unsi,ned short nu0ber?" int :$;&_)n%har>char Yindata?" uart_fifo.c: #include <c8051f000.h> #include "uart_fifo.h" ## V)V@ Definitions #define ;I':V()*E 90 #define &I':V()*E 90 ## V)V@ 2ariables char ;I'uf.;I':V()*E/" char Y;IGetPt" char Y;IPutPt" char &I'uf.&I':V()*E/" char Y&IGetPt" char Y&IPutPt" ## :$;& Definitions ###define (<(%L= 17900000 #define (<(%L= 18000000 char &I'usS" -oid ;I'uf)nit>-oid? 3 ;IPutPt = L;I'uf.0/" ;IGetPt = L;I'uf.0/" 8 int ;I'ufPush>char neEdata? 3 char Yt01Pt = ;IPutPt"

Y>t01Pt? = neEdata" t01PtJJ"

## trS to 1ush data onto buffer

if>t01Pt == L;I'uf.;I':V()*E/? 3 t01Pt = L;I'uf.0/" ## ,o to front of buffer 8 if>t01Pt == ;IGetPt? 3 return 0" 8 else 3 ;IPutPt = t01Pt" return 1" 8 8 int ;I'ufPo1>char YdataPt? 3 if>;IGetPt == ;IPutPt? 3 return 0" ## buffer e01tS5 nothin, to 1o1 8 else 3 Y>dataPt? = Y>;IGetPt?" ;IGetPtJJ" if>;IGetPt == L;I'uf.;I':V()*E/? 3 ;IGetPt = L;I'uf.0/" ## ,o to front of buffer 8 return 1" 8 8 int ;I'ufVull>-oid? 3 char Yt01Pt = ;IPutPt" t01PtJJ" if>t01Pt == L;I'uf.;I':V()*E/? 3 t01Pt = L;I'uf.0/" ## ,o to front of buffer 8 if>t01Pt == ;IGetPt? 3 return 1" 8 else 3 return 0" 8 8 int ;I'ufE01tS>-oid?

## buffer is full5 1ush failed

## set PutPt5 1ush successful

3 return >;IGetPt == ;IPutPt?" 8 -oid &I'uf)nit>-oid? 3 &IPutPt = L&I'uf.0/" &IGetPt = L&I'uf.0/" 8 int &I'ufPush>char neEdata? 3 char Yt01Pt = &IPutPt" Y>t01Pt? = neEdata" t01PtJJ"

## trS to 1ush data onto buffer

if>t01Pt == L&I'uf.&I':V()*E/? 3 t01Pt = L&I'uf.0/" ## ,o to front of buffer 8 if>t01Pt == &IGetPt? 3 return 0" 8 else 3 &IPutPt = t01Pt" return 1" 8 8 int &I'ufPo1>char YdataPt? 3 if>&IGetPt == &IPutPt? 3 return 0" ## buffer e01tS5 nothin, to 1o1 8 else 3 Y>dataPt? = Y>&IGetPt?" &IGetPtJJ" if>&IGetPt == L&I'uf.&I':V()*E/? 3 &IGetPt = L&I'uf.0/" ## ,o to front of buffer 8 return 1" 8 8 int &I'ufVull>-oid? 3 char Yt01Pt = &IPutPt" t01PtJJ" if>t01Pt == L&I'uf.&I':V()*E/? 3

## buffer is full5 1ush failed

## set PutPt5 1ush successful

t01Pt = L&I'uf.0/" 8 if>t01Pt == &IGetPt? 3 return 1" 8 else 3 return 0" 8 8 int &I'ufE01tS>-oid? 3 return >&IGetPt == &IPutPt?" 8

## ,o to front of buffer

##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## :$;&_)nit ##RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR ## ## %onfi,ure the :$;& ## -oid :$;&_)nit>int baudrate? 3 ## (%@+ (erial Port %ontrol ;e,ister ## bit 6 (A0 = 0 Aode 1P 8Rbit5 -ariable baud rate ## bit ! (A1 = 1 ## bit 5 (A9 = 0 ## bit U ;E+ = 1 :$;& rece1tion enabled ## bit 4 &'8 = I ## bit 9 ;'8 = I ## bit 1 &) trans0it interru1t ## bit 0 ;) recei-e interru1t (%@+ = 0D50" &) = 1" E( = 1" ## enable :$;& interru1ts

## &i0er 9 setu1 as baud rate clocQ ## Chen in baud rate ,enerator 0ode5 &i0er 9 ## clocQ source is fiDed at (<(%L=#9 &;9 = 0" ## disable &i0er 9

## &9%@+ &i0er 9 %ontrol ;e,ister ## bit 6 &V9 ## bit ! EIV9 ## bit 5 ;%L= = 1 use ti0er 9 for :$;& recei-e ## bit U &%L= = 1 use ti0er 9 for :$;& trans0it ## bit 4 EIE+9 = 0 disable eDternal interru1t ## bit 9 &;9 = 0 disable ti0er 9 ## bit 1 %#&9 = 0 use internal clocQ ## bit 0 %P#;L9 = 0 set autoRreload 0ode ;%L= = 1"

&%L= = 1" EIE+9 = 0" %&9 = 0" %P;L9 = 0" ## (<(%L= = 17900000 'aud rate = 7!00 ##;%$P9T = >>>R>(<(%L=#49#baudrate??JU? >> 8? L 0DVV" ##;%$P9L = >>R>(<(%L=#49#baudrate??JU? L 0DVV" ## (<(%L= = 18000000 'aud rate = 7!00 ;%$P9T = >>>R>(<(%L=#49#baudrate??? >> 8? L 0DVV" ;%$P9L = >>R>(<(%L=#49#baudrate??? L 0DVV" &L9 = ;%$P9L" &T9 = ;%$P9T" &;9 = 1" 8 -oid :$;&_)(;>-oid? interru1t U 3 if>;)? 3 ;) = 0" if>O;I'ufVull>?? 3 ;I'ufPush>(':V?" 8 8 else if>&)? 3 &) = 0" if>O&I'ufE01tS>?? 3 char &Idata" &I'ufPo1>L&Idata?" (':V = &Idata" &I'usS = 1" 8 else 3 &I'usS = 0" 8 8 8 -oid :$;&_@ut%har>char outdata? 3 Ehile>&I'ufPush>outdata? == 0? 3 8 Ehile>&I'usS == 1? 3 8 &) = 1" 8 -oid :$;&_@ut(trin,>char Youtdata? 3 Ehile>Youtdata?

## clear interru1t fla,

## clear interru1t fla,

## still trans0ittin,

## all trans0it finished

3 :$;&_@ut%har>Youtdata?" outdataJJ" 8 8 ## :$;&_@ut)nte,er ## con-erts an inte,er -alue into its $(%)) character -oid :$;&_@ut)nte,er>unsi,ned char inte,er? 3 sEitch>inte,er? 3 case 0P :$;&_@ut%har>M0M?" breaQ" case 1P :$;&_@ut%har>M1M?" breaQ" case 9P :$;&_@ut%har>M9M?" breaQ" case 4P :$;&_@ut%har>M4M?" breaQ" case UP :$;&_@ut%har>MUM?" breaQ" case 5P :$;&_@ut%har>M5M?" breaQ" case !P :$;&_@ut%har>M!M?" breaQ" case 6P :$;&_@ut%har>M6M?" breaQ" case 8P :$;&_@ut%har>M8M?" breaQ" case 7P :$;&_@ut%har>M7M?" breaQ" defaultP breaQ" 8 8 ## :$;&_@ut:Dec ## out1uts an unsi,ned deci0al in DD.D for0at -oid :$;&_@ut:Dec>unsi,ned short nu0ber? 3 if>nu0ber > 100? 3 :$;&_@ut)nte,er>nu0ber#100?" nu0ber = nu0ber R >nu0ber#100?Y100" 8 if>nu0ber > 10? 3 :$;&_@ut)nte,er>nu0ber#10?" nu0ber = nu0ber R >nu0ber#10?Y10" 8 else 3 :$;&_@ut%har>M0M?" 8 :$;&_@ut%har>M.M?" :$;&_@ut)nte,er>nu0ber?"

8 int :$;&_)n%har>char Yindata? 3 if>;I'ufE01tS>?? 3 return 0" 8 else 3 ;I'ufPo1>indata?" return 1" 8 8

Cebsite
Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.a p!." " #nherit ="ho$e%&e'%app.%Default" %( <)D*C+,PE ht$l P-BL#C ".//W0C//D+D 12+3L 4.5 +ran itional//E6" "http7//&&&.&0.org/+8/!ht$l4/D+D/!ht$l4.tran itional.dtd"( <ht$l !$ln ="http7//&&&.&0.org/4999/!ht$l" ( <head runat=" erver"( <title(Bluetooth 2o$e Auto$ation</title( </head( <'od:( <i$g r"=" p u.header.;pg" alt="<outhern Pol:te"hni" <tate -niver it:" /( <p /( <for$ id="=or$4" a"tion="Default.a p!" $ethod="po t" runat=" erver"( <a p7<"ript3anager #D="<"ript3anager4" runat=" erver" /( <a p7-pdatePanel #D="-pdatePanel4" runat=" erver"( <Content+e$plate( <ta'le 'order="4"( <tr( <td align=""enter"(#n ide +e$perature</td( <td align=""enter"(<'(<a p7La'el #D="+e$perature" +e!t="" runat=" erver" /(>degC</'(</td( </tr( <tr( <td align=""enter"(Living 8oo$ Light </td( <td align=""enter"(<'(<a p7La'el #D="Living8oo$" +e!t="*ff" runat=" erver" /(</'(</td( </tr( </ta'le( </Content+e$plate( </a p7-pdatePanel( <a p7Button #D="-p+e$p" +e!t="-pdate +e$perature" *nCli"?="-pdate+e$perature" runat=" erver" /( <a p7Button #D="-pLight4" +e!t="-pdate Light 4" *nCli"?="-pdateLight4" runat=" erver" /(

<p /( <div( Living 8oo$ Light 7 <a p78adioButton #D="Light4" +e!t="*n" @roup6a$e="rLiving8oo$" runat=" erver" /( <a p78adioButton #D="LightA" +e!t="*ff" runat=" erver" @roup6a$e="rLiving8oo$" /( <'r /( <a p7Button #D="<u'$it" +e!t="<u'$it Change" *nCli"?="Change<tatu " runat=" erver" /( <'r /( </div( <div( <a p7La'el #D="Error3 g" =ont.#tali"="true" =oreColor="8ed" +e!t="" runat=" erver" /( </div( </for$( </'od:( </ht$l( Default.aspx.cs: u u u u u u u u u u u u u u ing ing ing ing ing ing ing ing ing ing ing ing ing ing <: <: <: <: <: <: <: <: <: <: <: <: <: <: te$B te$.Colle"tion B te$.ConfigurationB te$.DataB te$.LinCB te$.We'B te$.We'.<e"urit:B te$.We'.-#B te$.We'.-#.2t$lControl B te$.We'.-#.We'Control B te$.We'.-#.We'Control .We'Part B te$.1$l.LinCB te$.#*.Port B te$.+hreadingB

na$e pa"e ho$e%&e'%app D pu'li" partial "la %Default 7 <: te$.We'.-#.Page D tati" <erialPort pCo$$ = ne& <erialPortEFB prote"ted void Page%LoadEo';e"t enderG EventArg D if E)Page.# Po tBa"?F D pCo$$.Clo eEFB pCo$$.Baud8ate = 9H55B pCo$$.Port6a$e = "C*3I"B pCo$$.DataBit = IB pCo$$.Parit: = Parit:.6oneB pCo$$.2and ha?e = 2and ha?e.6oneB pCo$$.<topBit = <topBit .*neB pCo$$.8ead+i$eout = A555B +e$perature.+e!t = "6/A"B Living8oo$.+e!t = "6/A"B LightA.Che"?ed = trueB eF

J J

Error3 g.+e!t = ""B

prote"ted void -pdate+e$peratureEo';e"t D tr: D <tring r!<tringB 'ool "o$plete = fal eB

enderG EventArg

eF

if E) pCo$$.# *penF D pCo$$.*penEFB J &hile E)"o$pleteF D pCo$$.WriteE"K+LrLn"FB r!<tring = pCo$$.8eadLineEFB if Er!<tring.<tart WithE"+7"FF D "o$plete = trueB +e$perature.+e!t = r!<tring.<u' tringEAFB J J Error3 g.+e!t = ""B J "at"h E+i$eoutE!"eptionF D Error3 g.+e!t = "Co$$uni"ation ti$ed out"B +e$perature.+e!t = "6/A"B J "at"h E-nauthoriMedA""e E!"eption uaeF D Error3 g.+e!t = "Cannot open "onne"tion to Bluetooth devi"e"B +e$perature.+e!t = "6/A"B thro& uaeB J "at"h E<: te$.#*.#*E!"eption iieF D Error3 g.+e!t = "Error "o$$uni"ating &ith Bluetooth devi"e"B +e$perature.+e!t = "6/A"B thro& iieB J finall: D pCo$$.Clo eEFB J J prote"ted void -pdateLight4Eo';e"t D tr: D <tring r!<tringB 'ool "o$plete = fal eB enderG EventArg eF

J "at"h E+i$eoutE!"eptionF D Error3 g.+e!t = "Co$$uni"ation ti$ed out"B Living8oo$.+e!t = "6/A"B J "at"h E-nauthoriMedA""e E!"eption uaeF D Error3 g.+e!t = "Cannot open "onne"tion to Bluetooth devi"e"B Living8oo$.+e!t = "6/A"B thro& uaeB J "at"h E<: te$.#*.#*E!"eption iieF D Error3 g.+e!t = "Error "o$$uni"ating &ith Bluetooth devi"e"B Living8oo$.+e!t = "6/A"B thro& iieB J finall: D pCo$$.Clo eEFB J eF

if E) pCo$$.# *penF D pCo$$.*penEFB J &hile E)"o$pleteF D pCo$$.WriteE"KL4LrLn"FB r!<tring = pCo$$.8eadLineEFB if Er!<tring.<tart WithE"L47"FF D "o$plete = trueB Living8oo$.+e!t = r!<tring.<u' tringE0FB J J Error3 g.+e!t = ""B

prote"ted void Change<tatu Eo';e"t enderG EventArg D tr: D if E) pCo$$.# *penF D pCo$$.*penEFB J if ELight4.Che"?edF D pCo$$.WriteLineE"CL44LrLn"FB J el e if ELightA.Che"?edF D pCo$$.WriteLineE"CL45LrLn"FB J Error3 g.+e!t = ""B J

"at"h E-nauthoriMedA""e E!"eption uaeF D Error3 g.+e!t = "Cannot open "onne"tion to Bluetooth devi"e"B thro& uaeB J "at"h E<: te$.#*.#*E!"eption iieF D Error3 g.+e!t = "Error "o$$uni"ating &ith Bluetooth devi"e"B thro& iieB J finall: D pCo$$.Clo eEFB J J J J

Vous aimerez peut-être aussi