![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Xbee-Smart-Home-Inside RX Test
Dependencies: Si7021 mbed-rtos JPEGCamera mbed
Fork of Xbee-Smart-Home-Inside by
main.cpp@4:df159b3382e3, 2017-04-26 (annotated)
- Committer:
- pkoirala3
- Date:
- Wed Apr 26 04:10:32 2017 +0000
- Revision:
- 4:df159b3382e3
- Parent:
- 3:415ccd1f7ae1
- Child:
- 5:81a1920e5f3d
Minor Bug Fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pkoirala3 | 1:e9068d048f58 | 1 | #include "mbed.h" |
pkoirala3 | 1:e9068d048f58 | 2 | #include "rtos.h" |
pkoirala3 | 1:e9068d048f58 | 3 | #include "JPEGCamera.h" |
pkoirala3 | 3:415ccd1f7ae1 | 4 | #include "Si7021.h" |
pkoirala3 | 3:415ccd1f7ae1 | 5 | #include "mpr121.h" |
pkoirala3 | 1:e9068d048f58 | 6 | |
pkoirala3 | 3:415ccd1f7ae1 | 7 | Serial xbee(p9, p10); |
pkoirala3 | 3:415ccd1f7ae1 | 8 | Serial pc(USBTX, USBRX); |
pkoirala3 | 3:415ccd1f7ae1 | 9 | JPEGCamera camera(p13, p14); |
pkoirala3 | 3:415ccd1f7ae1 | 10 | DigitalOut rst1(p8); |
pkoirala3 | 4:df159b3382e3 | 11 | AnalogIn ambient(p16); |
pkoirala3 | 4:df159b3382e3 | 12 | DigitalOut light(p17); |
pkoirala3 | 3:415ccd1f7ae1 | 13 | Si7021 tempHum(p28, p27); |
pkoirala3 | 0:5b1386641010 | 14 | |
pkoirala3 | 3:415ccd1f7ae1 | 15 | DigitalOut led1(LED1); |
pkoirala3 | 3:415ccd1f7ae1 | 16 | DigitalOut led2(LED2); |
pkoirala3 | 0:5b1386641010 | 17 | |
pkoirala3 | 3:415ccd1f7ae1 | 18 | char outBuffer[50]; |
pkoirala3 | 3:415ccd1f7ae1 | 19 | // char imageBuff[4800]; // For camera buffer |
pkoirala3 | 3:415ccd1f7ae1 | 20 | char inBuffer; |
pkoirala3 | 4:df159b3382e3 | 21 | bool lightOnOff = false; |
pkoirala3 | 3:415ccd1f7ae1 | 22 | volatile bool requestIn = false; |
pkoirala3 | 0:5b1386641010 | 23 | |
pkoirala3 | 4:df159b3382e3 | 24 | // Mutex serialMutex; |
pkoirala3 | 3:415ccd1f7ae1 | 25 | Thread t1; |
pkoirala3 | 0:5b1386641010 | 26 | |
pkoirala3 | 3:415ccd1f7ae1 | 27 | void getcommand() |
pkoirala3 | 3:415ccd1f7ae1 | 28 | { |
pkoirala3 | 3:415ccd1f7ae1 | 29 | while(1) { |
pkoirala3 | 3:415ccd1f7ae1 | 30 | if(xbee.readable()) { |
pkoirala3 | 3:415ccd1f7ae1 | 31 | led1 = 1; |
pkoirala3 | 3:415ccd1f7ae1 | 32 | inBuffer = xbee.getc(); |
pkoirala3 | 3:415ccd1f7ae1 | 33 | requestIn = true; |
pkoirala3 | 3:415ccd1f7ae1 | 34 | led1 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 35 | } |
pkoirala3 | 3:415ccd1f7ae1 | 36 | Thread::wait(1000); |
pkoirala3 | 3:415ccd1f7ae1 | 37 | } |
pkoirala3 | 1:e9068d048f58 | 38 | } |
pkoirala3 | 1:e9068d048f58 | 39 | |
pkoirala3 | 3:415ccd1f7ae1 | 40 | void sendcommand(char outBuff[]) |
pkoirala3 | 3:415ccd1f7ae1 | 41 | { |
pkoirala3 | 3:415ccd1f7ae1 | 42 | if(xbee.writeable()){ |
pkoirala3 | 3:415ccd1f7ae1 | 43 | led2 = 1; |
pkoirala3 | 3:415ccd1f7ae1 | 44 | int i = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 45 | while(outBuff[i] != ','){ |
pkoirala3 | 3:415ccd1f7ae1 | 46 | xbee.putc(outBuff[i]); |
pkoirala3 | 3:415ccd1f7ae1 | 47 | i++; |
pkoirala3 | 3:415ccd1f7ae1 | 48 | } |
pkoirala3 | 3:415ccd1f7ae1 | 49 | led2 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 50 | } |
pkoirala3 | 1:e9068d048f58 | 51 | } |
pkoirala3 | 4:df159b3382e3 | 52 | /* |
pkoirala3 | 3:415ccd1f7ae1 | 53 | // IRS p26, SDA p9, SCL p10 (SDA & SCL Need 4.7Kohm with 3.3, Vcc --> 3.3 |
pkoirala3 | 3:415ccd1f7ae1 | 54 | InterruptIn interrupt(p26); |
pkoirala3 | 3:415ccd1f7ae1 | 55 | I2C i2c(p9, p10); |
pkoirala3 | 3:415ccd1f7ae1 | 56 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
pkoirala3 | 3:415ccd1f7ae1 | 57 | |
pkoirala3 | 3:415ccd1f7ae1 | 58 | // Key hit/release interrupt routine |
pkoirala3 | 3:415ccd1f7ae1 | 59 | void fallInterrupt() { |
pkoirala3 | 3:415ccd1f7ae1 | 60 | int key_code=0; |
pkoirala3 | 3:415ccd1f7ae1 | 61 | int i=0; |
pkoirala3 | 3:415ccd1f7ae1 | 62 | int value=mpr121.read(0x00); |
pkoirala3 | 3:415ccd1f7ae1 | 63 | value +=mpr121.read(0x01)<<8; |
pkoirala3 | 3:415ccd1f7ae1 | 64 | // Implemented latter |
pkoirala3 | 3:415ccd1f7ae1 | 65 | // If wrong passcode then take pic & send it |
pkoirala3 | 3:415ccd1f7ae1 | 66 | } |
pkoirala3 | 4:df159b3382e3 | 67 | */ |
pkoirala3 | 3:415ccd1f7ae1 | 68 | /* // Implemented latter |
pkoirala3 | 3:415ccd1f7ae1 | 69 | void cameraImage() |
pkoirala3 | 3:415ccd1f7ae1 | 70 | { |
pkoirala3 | 1:e9068d048f58 | 71 | LocalFileSystem local("local"); //save images on mbed |
pkoirala3 | 1:e9068d048f58 | 72 | camera.setPictureSize(JPEGCamera::SIZE160x120); |
pkoirala3 | 3:415ccd1f7ae1 | 73 | while(1) { |
pkoirala3 | 3:415ccd1f7ae1 | 74 | if (camera.isReady()) { |
pkoirala3 | 1:e9068d048f58 | 75 | char filename[32]; |
pkoirala3 | 1:e9068d048f58 | 76 | sprintf(filename, "/local/pict.jpg"); |
pkoirala3 | 1:e9068d048f58 | 77 | printf("Picture: %s ", filename); |
pkoirala3 | 3:415ccd1f7ae1 | 78 | if (camera.takePicture(filename)) { |
pkoirala3 | 3:415ccd1f7ae1 | 79 | while (camera.isProcessing()) { |
pkoirala3 | 1:e9068d048f58 | 80 | camera.processPicture(); |
pkoirala3 | 1:e9068d048f58 | 81 | } |
pkoirala3 | 1:e9068d048f58 | 82 | } |
pkoirala3 | 1:e9068d048f58 | 83 | } |
pkoirala3 | 3:415ccd1f7ae1 | 84 | // Load image from mbed to imageBuff; |
pkoirala3 | 3:415ccd1f7ae1 | 85 | // Need jpg decoder and scale the image to 80*60 |
pkoirala3 | 3:415ccd1f7ae1 | 86 | } |
pkoirala3 | 3:415ccd1f7ae1 | 87 | } |
pkoirala3 | 3:415ccd1f7ae1 | 88 | */ |
pkoirala3 | 3:415ccd1f7ae1 | 89 | |
pkoirala3 | 3:415ccd1f7ae1 | 90 | int main() |
pkoirala3 | 3:415ccd1f7ae1 | 91 | { |
pkoirala3 | 4:df159b3382e3 | 92 | light = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 93 | rst1 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 94 | wait_ms(1); |
pkoirala3 | 3:415ccd1f7ae1 | 95 | rst1 = 1; |
pkoirala3 | 3:415ccd1f7ae1 | 96 | wait_ms(1); |
pkoirala3 | 3:415ccd1f7ae1 | 97 | // xbee.baud(115200); // May be need to do this to send image |
pkoirala3 | 3:415ccd1f7ae1 | 98 | t1.start(getcommand); |
pkoirala3 | 3:415ccd1f7ae1 | 99 | |
pkoirala3 | 3:415ccd1f7ae1 | 100 | // If someone enter key in touchpad this interrupt will be evoked |
pkoirala3 | 4:df159b3382e3 | 101 | // interrupt.fall(&fallInterrupt); |
pkoirala3 | 4:df159b3382e3 | 102 | // interrupt.mode(PullUp); |
pkoirala3 | 3:415ccd1f7ae1 | 103 | |
pkoirala3 | 3:415ccd1f7ae1 | 104 | while(1){ |
pkoirala3 | 3:415ccd1f7ae1 | 105 | if(requestIn == true){ // Check for input request msg |
pkoirala3 | 4:df159b3382e3 | 106 | led1 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 107 | if(inBuffer == 's') { // Send the status |
pkoirala3 | 3:415ccd1f7ae1 | 108 | tempHum.measure(); |
pkoirala3 | 3:415ccd1f7ae1 | 109 | float temp = (tempHum.get_temperature()/1000.00); |
pkoirala3 | 3:415ccd1f7ae1 | 110 | float hum = (tempHum.get_humidity()/1000.00); |
pkoirala3 | 3:415ccd1f7ae1 | 111 | float lightStatus = light; |
pkoirala3 | 3:415ccd1f7ae1 | 112 | char delimit = '|'; |
pkoirala3 | 3:415ccd1f7ae1 | 113 | char terminate = ','; |
pkoirala3 | 4:df159b3382e3 | 114 | sprintf(outBuffer, "%2.2f%c%2.1f%c%0.2f%c",temp, delimit, hum, delimit, lightStatus, terminate); |
pkoirala3 | 3:415ccd1f7ae1 | 115 | sendcommand(outBuffer); |
pkoirala3 | 4:df159b3382e3 | 116 | led1 = 1; |
pkoirala3 | 4:df159b3382e3 | 117 | inBuffer = ' '; |
pkoirala3 | 3:415ccd1f7ae1 | 118 | } |
pkoirala3 | 3:415ccd1f7ae1 | 119 | else if(inBuffer == 'l') { |
pkoirala3 | 3:415ccd1f7ae1 | 120 | lightOnOff = !lightOnOff; // Toggle light on/off |
pkoirala3 | 4:df159b3382e3 | 121 | // (lightOnOff == true) ? ambient : 0; |
pkoirala3 | 4:df159b3382e3 | 122 | light = (lightOnOff == true) ? 1 : 0; |
pkoirala3 | 4:df159b3382e3 | 123 | led1 = 1; |
pkoirala3 | 4:df159b3382e3 | 124 | inBuffer = ' '; |
pkoirala3 | 3:415ccd1f7ae1 | 125 | } else if(inBuffer == 'c') { |
pkoirala3 | 3:415ccd1f7ae1 | 126 | // Capture pic & Send it |
pkoirala3 | 3:415ccd1f7ae1 | 127 | // cameraImage(); |
pkoirala3 | 3:415ccd1f7ae1 | 128 | // sendcommand(imageBuff); |
pkoirala3 | 3:415ccd1f7ae1 | 129 | // Need to work more on sendcommand function to support this |
pkoirala3 | 3:415ccd1f7ae1 | 130 | } |
pkoirala3 | 3:415ccd1f7ae1 | 131 | } |
pkoirala3 | 3:415ccd1f7ae1 | 132 | Thread::wait(1000); |
pkoirala3 | 1:e9068d048f58 | 133 | } |
pkoirala3 | 0:5b1386641010 | 134 | } |