Home automation using Xbee radios

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd

Link to Notebook Page

Revision:
7:15cbbbe6105c
Parent:
1:230c1ec1c9bb
Parent:
6:438291b14122
--- a/main.cpp	Tue Nov 26 01:40:05 2013 +0000
+++ b/main.cpp	Tue Nov 26 06:16:25 2013 +0000
@@ -1,12 +1,108 @@
-#include "mbed.h"
+#include "XbeeCommLib.h"
+
+PinDetect  buttonUp ( p15  );
+PinDetect  buttonDown ( p12  );
+PinDetect  buttonRight ( p16  );
+PinDetect  buttonLeft ( p13  );
+
+DigitalOut led1( LED1 );
+DigitalOut led2( LED2 );
+DigitalOut led3( LED3 );
+DigitalOut led4( LED4 );
+
+Serial xbee1(p9, p10);
+C12832_LCD lcd;
+LM75B sensor(p28,p27);
+PwmOut green(p24);
+PwmOut blue(p25);
+PwmOut red(p23);
+
+unsigned char data[500];
+int dataCounter = 0;
+bool clear = false;
+
+unsigned int  router0_addrHigh    = 0x0013a200;
+unsigned int  router0_addrLow     = 0x4079d00b;
+
+unsigned int  router1_addrHigh    = 0x0013a200;
+unsigned int  router1_addrLow     = 0x4079d023;
+
 
-DigitalOut myled(LED1);
+void xbeeSerialCallback() {
+    if(clear){
+        dataCounter = 0;
+        clear = false;
+    }
+    if(dataCounter < 500){
+        while(xbee1.readable() == true && dataCounter < 500){
+            data[dataCounter] = xbee1.getc();
+            dataCounter++;
+        }
+    }
+    else{
+        printf("Serial data buffer overflow. Resetting buffer...\n");
+        dataCounter = 0;
+        data[dataCounter] = xbee1.getc();
+    }
+}
+
+
+
+void up( void ) {
+    led1 = 1;
+    printf("Router1 Light On");
+    digitalWriteXbee(router1_addrHigh, router1_addrLow, 0, true);
+}
+
+void down( void ) {
+    led1 = 0;
+    printf("Router1 Light Off");
+    digitalWriteXbee(router1_addrHigh, router1_addrLow, 0, false);
+}
+
+void right(void) {
+    led2 = 1;
+    printf("Router0 Light On");
+    digitalWriteXbee(router0_addrHigh, router0_addrLow, 0, true);
+}
+
+void left(void) {
+    led2 = 0;
+    printf("Router0 Light Off");
+    digitalWriteXbee(router0_addrHigh, router0_addrLow, 0, false);
+}
+
+
 
 int main() {
-    while(1) {
-        myled = 0;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
+
+    printf("\n\nStarting...\n\n");
+
+    xbee1.attach(&xbeeSerialCallback);
+    
+    blue = 0.7;
+    green = 1.0;
+    red = 1.0;
+    
+    buttonUp.mode( PullDown );
+    buttonUp.attach_asserted( &up );
+    buttonUp.setSampleFrequency(); // Defaults to 20ms.
+
+    buttonDown.mode( PullDown );
+    buttonDown.attach_asserted( &down );
+    buttonDown.setSampleFrequency(); // Defaults to 20ms.
+    
+    buttonRight.mode( PullDown );
+    buttonRight.attach_asserted( &right );
+    buttonRight.setSampleFrequency(); // Defaults to 20ms.
+
+    buttonLeft.mode( PullDown );
+    buttonLeft.attach_asserted( &left );
+    buttonLeft.setSampleFrequency(); // Defaults to 20ms.
+
+    while( 1 ) {
+        wait( 0.2 );
+        
+        monitorXbee();
     }
 }