Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
main.cpp@7:15cbbbe6105c, 2013-11-26 (annotated)
- Committer:
- chrisisthefish
- Date:
- Tue Nov 26 06:16:25 2013 +0000
- Revision:
- 7:15cbbbe6105c
- Parent:
- 1:230c1ec1c9bb
- Parent:
- 6:438291b14122
Added initial code for Xbee communications.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chrisisthefish | 7:15cbbbe6105c | 1 | #include "XbeeCommLib.h" |
chrisisthefish | 7:15cbbbe6105c | 2 | |
chrisisthefish | 7:15cbbbe6105c | 3 | PinDetect buttonUp ( p15 ); |
chrisisthefish | 7:15cbbbe6105c | 4 | PinDetect buttonDown ( p12 ); |
chrisisthefish | 7:15cbbbe6105c | 5 | PinDetect buttonRight ( p16 ); |
chrisisthefish | 7:15cbbbe6105c | 6 | PinDetect buttonLeft ( p13 ); |
chrisisthefish | 7:15cbbbe6105c | 7 | |
chrisisthefish | 7:15cbbbe6105c | 8 | DigitalOut led1( LED1 ); |
chrisisthefish | 7:15cbbbe6105c | 9 | DigitalOut led2( LED2 ); |
chrisisthefish | 7:15cbbbe6105c | 10 | DigitalOut led3( LED3 ); |
chrisisthefish | 7:15cbbbe6105c | 11 | DigitalOut led4( LED4 ); |
chrisisthefish | 7:15cbbbe6105c | 12 | |
chrisisthefish | 7:15cbbbe6105c | 13 | Serial xbee1(p9, p10); |
chrisisthefish | 7:15cbbbe6105c | 14 | C12832_LCD lcd; |
chrisisthefish | 7:15cbbbe6105c | 15 | LM75B sensor(p28,p27); |
chrisisthefish | 7:15cbbbe6105c | 16 | PwmOut green(p24); |
chrisisthefish | 7:15cbbbe6105c | 17 | PwmOut blue(p25); |
chrisisthefish | 7:15cbbbe6105c | 18 | PwmOut red(p23); |
chrisisthefish | 7:15cbbbe6105c | 19 | |
chrisisthefish | 7:15cbbbe6105c | 20 | unsigned char data[500]; |
chrisisthefish | 7:15cbbbe6105c | 21 | int dataCounter = 0; |
chrisisthefish | 7:15cbbbe6105c | 22 | bool clear = false; |
chrisisthefish | 7:15cbbbe6105c | 23 | |
chrisisthefish | 7:15cbbbe6105c | 24 | unsigned int router0_addrHigh = 0x0013a200; |
chrisisthefish | 7:15cbbbe6105c | 25 | unsigned int router0_addrLow = 0x4079d00b; |
chrisisthefish | 7:15cbbbe6105c | 26 | |
chrisisthefish | 7:15cbbbe6105c | 27 | unsigned int router1_addrHigh = 0x0013a200; |
chrisisthefish | 7:15cbbbe6105c | 28 | unsigned int router1_addrLow = 0x4079d023; |
chrisisthefish | 7:15cbbbe6105c | 29 | |
chrisisthefish | 0:c498b8bcfc46 | 30 | |
chrisisthefish | 7:15cbbbe6105c | 31 | void xbeeSerialCallback() { |
chrisisthefish | 7:15cbbbe6105c | 32 | if(clear){ |
chrisisthefish | 7:15cbbbe6105c | 33 | dataCounter = 0; |
chrisisthefish | 7:15cbbbe6105c | 34 | clear = false; |
chrisisthefish | 7:15cbbbe6105c | 35 | } |
chrisisthefish | 7:15cbbbe6105c | 36 | if(dataCounter < 500){ |
chrisisthefish | 7:15cbbbe6105c | 37 | while(xbee1.readable() == true && dataCounter < 500){ |
chrisisthefish | 7:15cbbbe6105c | 38 | data[dataCounter] = xbee1.getc(); |
chrisisthefish | 7:15cbbbe6105c | 39 | dataCounter++; |
chrisisthefish | 7:15cbbbe6105c | 40 | } |
chrisisthefish | 7:15cbbbe6105c | 41 | } |
chrisisthefish | 7:15cbbbe6105c | 42 | else{ |
chrisisthefish | 7:15cbbbe6105c | 43 | printf("Serial data buffer overflow. Resetting buffer...\n"); |
chrisisthefish | 7:15cbbbe6105c | 44 | dataCounter = 0; |
chrisisthefish | 7:15cbbbe6105c | 45 | data[dataCounter] = xbee1.getc(); |
chrisisthefish | 7:15cbbbe6105c | 46 | } |
chrisisthefish | 7:15cbbbe6105c | 47 | } |
chrisisthefish | 7:15cbbbe6105c | 48 | |
chrisisthefish | 7:15cbbbe6105c | 49 | |
chrisisthefish | 7:15cbbbe6105c | 50 | |
chrisisthefish | 7:15cbbbe6105c | 51 | void up( void ) { |
chrisisthefish | 7:15cbbbe6105c | 52 | led1 = 1; |
chrisisthefish | 7:15cbbbe6105c | 53 | printf("Router1 Light On"); |
chrisisthefish | 7:15cbbbe6105c | 54 | digitalWriteXbee(router1_addrHigh, router1_addrLow, 0, true); |
chrisisthefish | 7:15cbbbe6105c | 55 | } |
chrisisthefish | 7:15cbbbe6105c | 56 | |
chrisisthefish | 7:15cbbbe6105c | 57 | void down( void ) { |
chrisisthefish | 7:15cbbbe6105c | 58 | led1 = 0; |
chrisisthefish | 7:15cbbbe6105c | 59 | printf("Router1 Light Off"); |
chrisisthefish | 7:15cbbbe6105c | 60 | digitalWriteXbee(router1_addrHigh, router1_addrLow, 0, false); |
chrisisthefish | 7:15cbbbe6105c | 61 | } |
chrisisthefish | 7:15cbbbe6105c | 62 | |
chrisisthefish | 7:15cbbbe6105c | 63 | void right(void) { |
chrisisthefish | 7:15cbbbe6105c | 64 | led2 = 1; |
chrisisthefish | 7:15cbbbe6105c | 65 | printf("Router0 Light On"); |
chrisisthefish | 7:15cbbbe6105c | 66 | digitalWriteXbee(router0_addrHigh, router0_addrLow, 0, true); |
chrisisthefish | 7:15cbbbe6105c | 67 | } |
chrisisthefish | 7:15cbbbe6105c | 68 | |
chrisisthefish | 7:15cbbbe6105c | 69 | void left(void) { |
chrisisthefish | 7:15cbbbe6105c | 70 | led2 = 0; |
chrisisthefish | 7:15cbbbe6105c | 71 | printf("Router0 Light Off"); |
chrisisthefish | 7:15cbbbe6105c | 72 | digitalWriteXbee(router0_addrHigh, router0_addrLow, 0, false); |
chrisisthefish | 7:15cbbbe6105c | 73 | } |
chrisisthefish | 7:15cbbbe6105c | 74 | |
chrisisthefish | 7:15cbbbe6105c | 75 | |
chrisisthefish | 0:c498b8bcfc46 | 76 | |
chrisisthefish | 0:c498b8bcfc46 | 77 | int main() { |
chrisisthefish | 7:15cbbbe6105c | 78 | |
chrisisthefish | 7:15cbbbe6105c | 79 | printf("\n\nStarting...\n\n"); |
chrisisthefish | 7:15cbbbe6105c | 80 | |
chrisisthefish | 7:15cbbbe6105c | 81 | xbee1.attach(&xbeeSerialCallback); |
chrisisthefish | 7:15cbbbe6105c | 82 | |
chrisisthefish | 7:15cbbbe6105c | 83 | blue = 0.7; |
chrisisthefish | 7:15cbbbe6105c | 84 | green = 1.0; |
chrisisthefish | 7:15cbbbe6105c | 85 | red = 1.0; |
chrisisthefish | 7:15cbbbe6105c | 86 | |
chrisisthefish | 7:15cbbbe6105c | 87 | buttonUp.mode( PullDown ); |
chrisisthefish | 7:15cbbbe6105c | 88 | buttonUp.attach_asserted( &up ); |
chrisisthefish | 7:15cbbbe6105c | 89 | buttonUp.setSampleFrequency(); // Defaults to 20ms. |
chrisisthefish | 7:15cbbbe6105c | 90 | |
chrisisthefish | 7:15cbbbe6105c | 91 | buttonDown.mode( PullDown ); |
chrisisthefish | 7:15cbbbe6105c | 92 | buttonDown.attach_asserted( &down ); |
chrisisthefish | 7:15cbbbe6105c | 93 | buttonDown.setSampleFrequency(); // Defaults to 20ms. |
chrisisthefish | 7:15cbbbe6105c | 94 | |
chrisisthefish | 7:15cbbbe6105c | 95 | buttonRight.mode( PullDown ); |
chrisisthefish | 7:15cbbbe6105c | 96 | buttonRight.attach_asserted( &right ); |
chrisisthefish | 7:15cbbbe6105c | 97 | buttonRight.setSampleFrequency(); // Defaults to 20ms. |
chrisisthefish | 7:15cbbbe6105c | 98 | |
chrisisthefish | 7:15cbbbe6105c | 99 | buttonLeft.mode( PullDown ); |
chrisisthefish | 7:15cbbbe6105c | 100 | buttonLeft.attach_asserted( &left ); |
chrisisthefish | 7:15cbbbe6105c | 101 | buttonLeft.setSampleFrequency(); // Defaults to 20ms. |
chrisisthefish | 7:15cbbbe6105c | 102 | |
chrisisthefish | 7:15cbbbe6105c | 103 | while( 1 ) { |
chrisisthefish | 7:15cbbbe6105c | 104 | wait( 0.2 ); |
chrisisthefish | 6:438291b14122 | 105 | |
chrisisthefish | 7:15cbbbe6105c | 106 | monitorXbee(); |
chrisisthefish | 0:c498b8bcfc46 | 107 | } |
chrisisthefish | 0:c498b8bcfc46 | 108 | } |