Xbee-Smart-Home-Inside RX Test

Dependencies:   Si7021 mbed-rtos JPEGCamera mbed

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

main.cpp

Committer:
pkoirala3
Date:
2017-04-25
Revision:
3:415ccd1f7ae1
Parent:
2:21031e885513
Child:
4:df159b3382e3

File content as of revision 3:415ccd1f7ae1:

#include "mbed.h"
#include "rtos.h"
#include "JPEGCamera.h"
#include "Si7021.h"
#include "mpr121.h"

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);

DigitalOut led1(LED1);
DigitalOut led2(LED2);

char outBuffer[50];
// char imageBuff[4800]; // For camera buffer
char inBuffer;
bool lightOnOff = true;
volatile bool requestIn = false;

Mutex serialMutex;
Thread t1;

void getcommand()
{
    while(1) {
        if(xbee.readable()) {
            led1 = 1;
            serialMutex.lock();
            inBuffer = xbee.getc();
            requestIn = true;
            serialMutex.unlock();
            led1 = 0;
        }
        Thread::wait(1000);
    }
}

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;
    }
}

// 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()) {
            char filename[32];
            sprintf(filename, "/local/pict.jpg");
            printf("Picture: %s ", filename);
            if (camera.takePicture(filename)) {
                while (camera.isProcessing()) {
                    camera.processPicture();
                }
            }
        }
        // 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);
    }
}