PIR still blocked by the XBee reading. Audio sensor not configured yet.

Dependencies:   BME280 SI1145 mbed

Revision:
5:0e4d19f6ba45
Parent:
4:7335cd74f3bc
Child:
6:002e8a4809eb
--- a/main.cpp	Tue Jul 18 23:23:52 2017 +0000
+++ b/main.cpp	Wed Jul 26 13:22:03 2017 +0000
@@ -22,7 +22,7 @@
 
 Timer t;
 
-const char nodeID = '1';
+const char nodeID = '3';
 
 // RX declarations
 uint8_t rx_buffer[64];
@@ -48,11 +48,10 @@
 Serial pc(PB_3, PB_4); // Serial 2
 
 // send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise);
-// "id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8"
 void SendSensorData(int t, int p, int h, int u, int ir, int v, int n){
     //"               ni:XX, te:XX,pr:XX,hu:XX,uv:XX,ir:XX,vi:XX,no:XX"
-    xb.printf(      ",ni:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",1,t,p,h,u,ir,v,n);
-    pc.printf("SEND: ni:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",1,t,p,h,u,ir,v,n);
+    xb.printf(      ",ni:%c,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",nodeID,t,p,h,u,ir,v,n);
+    pc.printf("SEND: ,ni:%c,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",nodeID,t,p,h,u,ir,v,n);
 }
 
 void SendPirFlag(){
@@ -65,13 +64,12 @@
 
 // PIR interrupt. Set the pir_trigger to true when interrupted.
 void PIR(void){
-    //pc.printf("void PIR(void){}\r\n");
+    //pc.printf(" !! ");
     pir_trigger = true;
 }
 
 // This method is attached to a timer
 void PIR_timed_send(){
-    //pc.printf("PIR_timed_send();\r\n");
     if(pir_enable && pir_trigger){
         SendPirFlag();
         pir_trigger = false;
@@ -85,17 +83,17 @@
 
 void rx_read_callback(){
     char c = xb.getc();
+    pc.printf("%c", c);
     
     if(c == '\n'){
         rx_index++;
         rx_flag = true;
         rx_index = 0;
         pc.printf(" EOM \r\n");
-    } else {
-        pc.printf("%c", c);        
+    } else {    
         rx_buffer[rx_index] = c;
         rx_index++;
-    } 
+    }
     
     if(rx_index > 63){
         rx_index = 0;
@@ -111,11 +109,10 @@
     xb.attach(&rx_read_callback, Serial::RxIrq);
     
     // Attach the PIR function to interrupt
-    PIR_sensor.rise(&PIR);
+    //PIR_sensor.rise(&PIR);
     pir_timer.attach(&PIR_timed_send,3);
     
     char pc_data = 'e';
-    char buffer[10];
         
     int temp = 1;
     int pressure = 2;
@@ -138,12 +135,18 @@
             if(xb.writeable()){xb.printf("%c", pc_data);}
         }
         
-        /* Handle the received data
-         * Execute only when the full message has been received.
-         * 
-         * 
-         * 
-         */
+        // Read in values from the BME280 board
+        temp = thp_sensor->getTemperature();
+        pressure = thp_sensor->getPressure();
+        humidity = thp_sensor->getHumidity();
+        
+        //Sensor data from the SI1145 board
+        uv = uiv_sensor->getUV(); // Reads from the SI1145
+        ir = uiv_sensor->getIR(); // Reads from the SI1145
+        vis = uiv_sensor->getVIS(); // Reads from the SI1145
+
+        noise = adc_noise.read();
+        
         if(rx_flag){
             pc.printf("IND:  0123456789ABCDEF\r\n"); // Show indices
             pc.printf("RECV: %s\r\n", rx_buffer); // Show received message
@@ -168,81 +171,9 @@
             memset(rx_buffer, '\0', sizeof(rx_buffer));
         }
         
-        
-        /*
-        if(xb.readable()){
-            pc.printf("=========================================\r\n");
-            
-            for(int i = 0; i < sizeof(buffer); i++){
-                t.start();
-                while(!xb.readable()){
-                // DO NOTHING
-                    //wait(0.01);
-                    //pc.printf("XBee no read\r\n");
-                }
-                buffer[i] = xb.getc();
-                t.stop();
-                wait_times[i] = t.read_ms();
-                t.reset();
-            }
-            
-            pc.printf("after the read loop\r\n");
-            
-            for(int i = 0; i < sizeof(buffer); i++){
-                pc.printf("%c, t = %i\r\n",buffer[i],wait_times[i]);
-                
-            }
-            
-            // Print out a bunch of stuff
+        // Transmit over the xbee if possible and necessary
+        if(xb.writeable() && send_enable) {
             
-            // Print out received message in char and int format 
-            pc.printf("RECV: ");
-            for(int i = 1; i < sizeof(buffer); i++){
-                pc.printf("%c", buffer[i]);
-            }
-            pc.printf("\r\nRECV: ");
-            // Print out message (INT)
-            for(int i = 0; i < sizeof(buffer); i++){
-                pc.printf("%i,", buffer[i]);
-            }
-            pc.printf("\r\n");
-            
-            //Check the node id
-            if(buffer[4] == nodeID){
-                send_enable = true;
-            } else {
-                pc.printf("Ignore, Target Node: #%c\r\n", buffer[4]);
-            }
-            
-            if(buffer[9] == '1'){
-                pir_enable = true;
-                pc.printf("pir_enable = true\r\n");
-            } else if (buffer[9] == '0'){
-                pir_enable = false;
-                pc.printf("pir_enable = false\r\n");
-            }
-            
-            memset(buffer, '\0', sizeof(buffer));
-            
-            pc.printf("*****************************************\r\n");
-        }
-        */
-        
-        // Take in new measurements
-        // Read in values from the BME280 board
-        temp = thp_sensor->getTemperature();
-        pressure = thp_sensor->getPressure();
-        humidity = thp_sensor->getHumidity();
-        
-        //Sensor data from the SI1145 board
-        uv = uiv_sensor->getUV(); // Reads from the SI1145 
-        ir = uiv_sensor->getIR(); // Reads from the SI1145
-        vis = uiv_sensor->getVIS(); // Reads from the SI1145
-        
-        noise = adc_noise.read();
-        
-        // Transmit over the xbee if possible and necessary
-        if(xb.writeable() && send_enable){
             SendSensorData(temp, pressure, humidity, uv, ir, vis, noise);
             send_enable = false;
         }