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

Dependencies:   BME280 SI1145 mbed

main.cpp

Committer:
jonathanyost
Date:
2017-07-18
Revision:
3:07fd499e96b4
Parent:
2:dd4d53687372
Child:
4:7335cd74f3bc

File content as of revision 3:07fd499e96b4:

/*
    Author: Jonathan Yost
    Title: END_NODE_REQUEST_POC
    Date: 6/28/17
    
    Desc: Reads in data from the sensors and sends to the gateway.
    yeah.
*/

// Libraries
#include "mbed.h" 
#include "BME280.h"
#include "SI1145.h"

// Define Constants / Macros
#define node_id (0x01)
#define tx (PA_9)
#define rx (PA_10)

// Declare Noise Sensor Stuff
#define noise_pin (PA_1)

Timer t;

const char nodeID = '1';
uint8_t rx_buffer[64];
bool rx_flag = false;


// I/O Delarations
I2C i2c(I2C_SDA, I2C_SCL); // D4, D5 
BME280 *thp_sensor = new BME280(i2c);
SI1145 *uiv_sensor = new SI1145(i2c);
AnalogIn adc_noise(noise_pin);


// Declare the PIR interface
InterruptIn PIR_sensor(PA_5);
Ticker pir_timer;
bool pir_enable = false;
bool pir_trigger = false;

// Declare Serial Interfaces
Serial xb(tx, rx); // Serial 1
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);
}

void SendPirFlag(){
    if(xb.writeable() && pir_enable){  
        xb.printf("ni:%c,mo:1\r\n",nodeID);
        pc.printf("ni:%c,mo:1\r\n",nodeID);
        pir_trigger = false;
    }
}

// PIR interrupt. Set the pir_trigger to true when interrupted.
void PIR(void){
    //pc.printf("void PIR(void){}\r\n");
    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;
    }
}

void initialize_serial_connections(){
    xb.baud(57600);
    pc.baud(115200);   
}

void rx_read_callback(){
    rx_flag = true;
    pc.printf("%c", xb.getc());
}

int main() {
    initialize_serial_connections();
    memset(rx_buffer, '\0', 64);
    pc.printf("\n\rMic test 1-2, 1-2\n\r");
    
    xb.attach(&rx_read_callback, Serial::RxIrq);
    
    // Attach the PIR function to interrupt
    PIR_sensor.rise(&PIR);
    pir_timer.attach(&PIR_timed_send,3);
    
    char pc_data = 'e';
    char buffer[10];
        
    int temp = 1;
    int pressure = 2;
    int humidity = 3;
    int uv = 5;
    int ir = 6;
    int vis = 7;
    int noise = 8;
    t.reset();
    
    int wait_times[32];
    
    bool send_enable = false;
    
    char read_char;
            
    while(true){
        //pc.printf("top of loop \r\n");
        
        // Debug Controls Input
        if(pc.readable()){
            pc_data = pc.getc();
            pc.printf("%c", pc_data);
            if(xb.writeable()){xb.printf("%c", pc_data);}
        }
        
        if(rx_flag){
            pc.printf("hello");
            
            if(xb.readable()){
                pc.printf("%c",xb.getc());
            }
            
            pc.printf("goodbye");
            rx_flag = false;
        }
        
        
        /*
        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
            
            // 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;
        }
        
        //pc.printf("bottom of loop \r\n");
    }
}