Xbee-Smart-Home-Inside RX Test
Dependencies: Si7021 mbed-rtos JPEGCamera mbed
Fork of Xbee-Smart-Home-Inside by
main.cpp
- Committer:
- pkoirala3
- Date:
- 2017-04-26
- Revision:
- 5:81a1920e5f3d
- Parent:
- 4:df159b3382e3
- Child:
- 6:6a43e2fdb8aa
File content as of revision 5:81a1920e5f3d:
#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(p16); DigitalOut light(p17); Si7021 tempHum(p28, p27); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut turnSpkOff(p21); PwmOut speaker(p22); char outBuffer[50]; // char imageBuff[4800]; // For camera buffer char inBuffer; volatile bool lightOnOff = false; volatile bool requestIn = false; volatile bool intrusion = false; // Mutex serialMutex; Thread t1; Thread t2; void getcommand() { while(1) { if(xbee.readable()) { led1 = 1; inBuffer = xbee.getc(); requestIn = true; led1 = 0; } Thread::wait(1000); } } void sendcommand(char outBuff[]) { if(xbee.writeable()){ led2 = 1; int i = 0; while(outBuff[i] != ','){ xbee.putc(outBuff[i]); i++; } led2 = 0; } } void statusUpdate(){ tempHum.measure(); float temp = (tempHum.get_temperature()/1000.00); float hum = (tempHum.get_humidity()/1000.00); float lightStatus = !light; char delimit = '|'; char terminate = ','; sprintf(outBuffer, "%2.2f%c%2.1f%c%0.2f%c",temp, delimit, hum, delimit, lightStatus, terminate); sendcommand(outBuffer); } /* // 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 & play sound turnSpkOff = 1; speaker = 0.2; intrusion = true; Thread::wait(1000); speaker = 0; turnSpkOff = 0; } */ /* // 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() { light = 0; 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); speaker.period(1.0/800.0); turnSpkOff = 0; // If someone enter key in touchpad this interrupt will be evoked // interrupt.fall(&fallInterrupt); // interrupt.mode(PullUp); bool ledUpdate = false; while(1){ if(requestIn || ledUpdate || intrusion){ // Check for input request msg led1 = 0; if(inBuffer == 's' || ledUpdate) { // Send the status statusUpdate(); ledUpdate = false; } else if(inBuffer == 'l') { lightOnOff = !lightOnOff; // Toggle light on/off // light = (lightOnOff == true) ? ambient.read() : 0; light = (lightOnOff == true) ? 1 : 0; ledUpdate = true; led1 = 1; } else if(inBuffer == 'c' || intrusion) { // Capture pic & Send it // cameraImage(); // sendcommand(imageBuff); // Need to work more on sendcommand function to support this intrusion = false; } inBuffer = ' '; // Clear the buffer } Thread::wait(1000); } }