Xbee-Smart-Home-Inside RX Test

Dependencies:   Si7021 mbed-rtos JPEGCamera mbed

Fork of Xbee-Smart-Home-Inside by prana koirala

Revision:
3:415ccd1f7ae1
Parent:
2:21031e885513
Child:
4:df159b3382e3
--- a/main.cpp	Mon Apr 17 18:36:03 2017 +0000
+++ b/main.cpp	Tue Apr 25 22:36:59 2017 +0000
@@ -1,56 +1,134 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "JPEGCamera.h"
-
-AnalogIn ambient(p15);
-Mutex getMutex;
-Mutex putMutex;
+#include "Si7021.h"
+#include "mpr121.h"
 
-Thread t1;
-Thread t2;
+Serial xbee(p9, p10);
+Serial pc(USBTX, USBRX);
+JPEGCamera camera(p13, p14);
+DigitalOut rst1(p8);
+AnalogIn ambient(p15);
+DigitalOut light(p16);
+Si7021 tempHum(p28, p27);
 
-Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10
-JPEGCamera camera(p13, p14); // Camera serial TX, RX
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
 
-DigitalOut rst1(p8); //Digital reset for the XBee, 200ns for reset
+char outBuffer[50];
+// char imageBuff[4800]; // For camera buffer
+char inBuffer;
+bool lightOnOff = true;
+volatile bool requestIn = false;
 
-Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer
+Mutex serialMutex;
+Thread t1;
 
-int main() {
-    rst1 = 0; //Set reset pin to 0
-    wait_ms(1);//Wait at least one millisecond
-    rst1 = 1;//Set reset pin to 1
-    wait_ms(1);//Wait another millisecond
-    
-    t1.start(callback(ambient_sensor()));
-    t2.start(callback(cameraThread()));
+void getcommand()
+{
+    while(1) {
+        if(xbee.readable()) {
+            led1 = 1;
+            serialMutex.lock();
+            inBuffer = xbee.getc();
+            requestIn = true;
+            serialMutex.unlock();
+            led1 = 0;
+        }
+        Thread::wait(1000);
+    }
 }
 
-void ambinet_sensor(){
-    float amb_val = ambient(); 
-    // Scale to proper unit
-    putMutex.lock();
-    xbee.puts(amb_val);
-    putMutex.unlock();
+void sendcommand(char outBuff[])
+{
+    if(xbee.writeable()){
+        led2 = 1;
+        int i = 0;
+        serialMutex.lock();
+        while(outBuff[i] != ','){
+            xbee.putc(outBuff[i]);
+            pc.putc(outBuff[i]);
+            i++;
+        }
+        serialMutex.unlock();
+        led2 = 0;
+    }
 }
 
-void cameraThread(){
+// IRS p26, SDA p9, SCL p10 (SDA & SCL Need 4.7Kohm with 3.3, Vcc --> 3.3
+InterruptIn interrupt(p26);
+I2C i2c(p9, p10);
+Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
+
+// Key hit/release interrupt routine
+void fallInterrupt() {
+  int key_code=0;
+  int i=0;
+  int value=mpr121.read(0x00);
+  value +=mpr121.read(0x01)<<8;
+  // Implemented latter
+  // If wrong passcode then take pic & send it
+}
+
+/*  // Implemented latter
+void cameraImage()
+{
     LocalFileSystem local("local"); //save images on mbed
     camera.setPictureSize(JPEGCamera::SIZE160x120);
-    while(1){
-        if (camera.isReady()){
+    while(1) {
+        if (camera.isReady()) {
             char filename[32];
             sprintf(filename, "/local/pict.jpg");
             printf("Picture: %s ", filename);
-            if (camera.takePicture(filename)){
-                while (camera.isProcessing()){
+            if (camera.takePicture(filename)) {
+                while (camera.isProcessing()) {
                     camera.processPicture();
                 }
-                putMutex.lock();
-                // xbee.putc(filename()); // Send image from xbee
-                putMutex.unlock();
-                break;
             }
         }
+        // Load image from mbed to imageBuff;
+        // Need jpg decoder and scale the image to 80*60
+    }
+}
+*/
+
+int main()
+{
+    rst1 = 0; 
+    wait_ms(1);
+    rst1 = 1;
+    wait_ms(1);
+    // xbee.baud(115200);  // May be need to do this to send image
+    t1.start(getcommand);
+    
+    // If someone enter key in touchpad this interrupt will be evoked
+    interrupt.fall(&fallInterrupt); 
+    interrupt.mode(PullUp);
+    
+    while(1){
+        if(requestIn == true){ // Check for input request msg
+            if(inBuffer == 's') { // Send the status
+                tempHum.measure();
+                float temp = (tempHum.get_temperature()/1000.00);
+                float hum = (tempHum.get_humidity()/1000.00);
+                float lightStatus = light;
+                char delimit = '|';
+                char terminate = ',';
+                char status = 's';
+                sprintf(outBuffer, "%c%c%2.2f%c%2.1f%c%0.2f%c",status, delimit, temp, delimit, hum, delimit, lightStatus, terminate);
+                sendcommand(outBuffer);
+            }
+            else if(inBuffer == 'l') {
+                lightOnOff = !lightOnOff;  // Toggle light on/off
+                (lightOnOff == true) ? ambient : 0;
+                
+            } else if(inBuffer == 'c') {
+            // Capture pic & Send it
+            // cameraImage();
+            // sendcommand(imageBuff); 
+            // Need to work more on sendcommand function to support this
+            }
+        }
+        Thread::wait(1000);
     }
 }
\ No newline at end of file