![](/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@3:415ccd1f7ae1, 2017-04-25 (annotated)
- Committer:
- pkoirala3
- Date:
- Tue Apr 25 22:36:59 2017 +0000
- Revision:
- 3:415ccd1f7ae1
- Parent:
- 2:21031e885513
- Child:
- 4:df159b3382e3
Include most of the library and code except for JPG image decoder
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 | 3:415ccd1f7ae1 | 11 | AnalogIn ambient(p15); |
pkoirala3 | 3:415ccd1f7ae1 | 12 | DigitalOut light(p16); |
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 | 3:415ccd1f7ae1 | 21 | bool lightOnOff = true; |
pkoirala3 | 3:415ccd1f7ae1 | 22 | volatile bool requestIn = false; |
pkoirala3 | 0:5b1386641010 | 23 | |
pkoirala3 | 3:415ccd1f7ae1 | 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 | serialMutex.lock(); |
pkoirala3 | 3:415ccd1f7ae1 | 33 | inBuffer = xbee.getc(); |
pkoirala3 | 3:415ccd1f7ae1 | 34 | requestIn = true; |
pkoirala3 | 3:415ccd1f7ae1 | 35 | serialMutex.unlock(); |
pkoirala3 | 3:415ccd1f7ae1 | 36 | led1 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 37 | } |
pkoirala3 | 3:415ccd1f7ae1 | 38 | Thread::wait(1000); |
pkoirala3 | 3:415ccd1f7ae1 | 39 | } |
pkoirala3 | 1:e9068d048f58 | 40 | } |
pkoirala3 | 1:e9068d048f58 | 41 | |
pkoirala3 | 3:415ccd1f7ae1 | 42 | void sendcommand(char outBuff[]) |
pkoirala3 | 3:415ccd1f7ae1 | 43 | { |
pkoirala3 | 3:415ccd1f7ae1 | 44 | if(xbee.writeable()){ |
pkoirala3 | 3:415ccd1f7ae1 | 45 | led2 = 1; |
pkoirala3 | 3:415ccd1f7ae1 | 46 | int i = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 47 | serialMutex.lock(); |
pkoirala3 | 3:415ccd1f7ae1 | 48 | while(outBuff[i] != ','){ |
pkoirala3 | 3:415ccd1f7ae1 | 49 | xbee.putc(outBuff[i]); |
pkoirala3 | 3:415ccd1f7ae1 | 50 | pc.putc(outBuff[i]); |
pkoirala3 | 3:415ccd1f7ae1 | 51 | i++; |
pkoirala3 | 3:415ccd1f7ae1 | 52 | } |
pkoirala3 | 3:415ccd1f7ae1 | 53 | serialMutex.unlock(); |
pkoirala3 | 3:415ccd1f7ae1 | 54 | led2 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 55 | } |
pkoirala3 | 1:e9068d048f58 | 56 | } |
pkoirala3 | 1:e9068d048f58 | 57 | |
pkoirala3 | 3:415ccd1f7ae1 | 58 | // IRS p26, SDA p9, SCL p10 (SDA & SCL Need 4.7Kohm with 3.3, Vcc --> 3.3 |
pkoirala3 | 3:415ccd1f7ae1 | 59 | InterruptIn interrupt(p26); |
pkoirala3 | 3:415ccd1f7ae1 | 60 | I2C i2c(p9, p10); |
pkoirala3 | 3:415ccd1f7ae1 | 61 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
pkoirala3 | 3:415ccd1f7ae1 | 62 | |
pkoirala3 | 3:415ccd1f7ae1 | 63 | // Key hit/release interrupt routine |
pkoirala3 | 3:415ccd1f7ae1 | 64 | void fallInterrupt() { |
pkoirala3 | 3:415ccd1f7ae1 | 65 | int key_code=0; |
pkoirala3 | 3:415ccd1f7ae1 | 66 | int i=0; |
pkoirala3 | 3:415ccd1f7ae1 | 67 | int value=mpr121.read(0x00); |
pkoirala3 | 3:415ccd1f7ae1 | 68 | value +=mpr121.read(0x01)<<8; |
pkoirala3 | 3:415ccd1f7ae1 | 69 | // Implemented latter |
pkoirala3 | 3:415ccd1f7ae1 | 70 | // If wrong passcode then take pic & send it |
pkoirala3 | 3:415ccd1f7ae1 | 71 | } |
pkoirala3 | 3:415ccd1f7ae1 | 72 | |
pkoirala3 | 3:415ccd1f7ae1 | 73 | /* // Implemented latter |
pkoirala3 | 3:415ccd1f7ae1 | 74 | void cameraImage() |
pkoirala3 | 3:415ccd1f7ae1 | 75 | { |
pkoirala3 | 1:e9068d048f58 | 76 | LocalFileSystem local("local"); //save images on mbed |
pkoirala3 | 1:e9068d048f58 | 77 | camera.setPictureSize(JPEGCamera::SIZE160x120); |
pkoirala3 | 3:415ccd1f7ae1 | 78 | while(1) { |
pkoirala3 | 3:415ccd1f7ae1 | 79 | if (camera.isReady()) { |
pkoirala3 | 1:e9068d048f58 | 80 | char filename[32]; |
pkoirala3 | 1:e9068d048f58 | 81 | sprintf(filename, "/local/pict.jpg"); |
pkoirala3 | 1:e9068d048f58 | 82 | printf("Picture: %s ", filename); |
pkoirala3 | 3:415ccd1f7ae1 | 83 | if (camera.takePicture(filename)) { |
pkoirala3 | 3:415ccd1f7ae1 | 84 | while (camera.isProcessing()) { |
pkoirala3 | 1:e9068d048f58 | 85 | camera.processPicture(); |
pkoirala3 | 1:e9068d048f58 | 86 | } |
pkoirala3 | 1:e9068d048f58 | 87 | } |
pkoirala3 | 1:e9068d048f58 | 88 | } |
pkoirala3 | 3:415ccd1f7ae1 | 89 | // Load image from mbed to imageBuff; |
pkoirala3 | 3:415ccd1f7ae1 | 90 | // Need jpg decoder and scale the image to 80*60 |
pkoirala3 | 3:415ccd1f7ae1 | 91 | } |
pkoirala3 | 3:415ccd1f7ae1 | 92 | } |
pkoirala3 | 3:415ccd1f7ae1 | 93 | */ |
pkoirala3 | 3:415ccd1f7ae1 | 94 | |
pkoirala3 | 3:415ccd1f7ae1 | 95 | int main() |
pkoirala3 | 3:415ccd1f7ae1 | 96 | { |
pkoirala3 | 3:415ccd1f7ae1 | 97 | rst1 = 0; |
pkoirala3 | 3:415ccd1f7ae1 | 98 | wait_ms(1); |
pkoirala3 | 3:415ccd1f7ae1 | 99 | rst1 = 1; |
pkoirala3 | 3:415ccd1f7ae1 | 100 | wait_ms(1); |
pkoirala3 | 3:415ccd1f7ae1 | 101 | // xbee.baud(115200); // May be need to do this to send image |
pkoirala3 | 3:415ccd1f7ae1 | 102 | t1.start(getcommand); |
pkoirala3 | 3:415ccd1f7ae1 | 103 | |
pkoirala3 | 3:415ccd1f7ae1 | 104 | // If someone enter key in touchpad this interrupt will be evoked |
pkoirala3 | 3:415ccd1f7ae1 | 105 | interrupt.fall(&fallInterrupt); |
pkoirala3 | 3:415ccd1f7ae1 | 106 | interrupt.mode(PullUp); |
pkoirala3 | 3:415ccd1f7ae1 | 107 | |
pkoirala3 | 3:415ccd1f7ae1 | 108 | while(1){ |
pkoirala3 | 3:415ccd1f7ae1 | 109 | if(requestIn == true){ // Check for input request msg |
pkoirala3 | 3:415ccd1f7ae1 | 110 | if(inBuffer == 's') { // Send the status |
pkoirala3 | 3:415ccd1f7ae1 | 111 | tempHum.measure(); |
pkoirala3 | 3:415ccd1f7ae1 | 112 | float temp = (tempHum.get_temperature()/1000.00); |
pkoirala3 | 3:415ccd1f7ae1 | 113 | float hum = (tempHum.get_humidity()/1000.00); |
pkoirala3 | 3:415ccd1f7ae1 | 114 | float lightStatus = light; |
pkoirala3 | 3:415ccd1f7ae1 | 115 | char delimit = '|'; |
pkoirala3 | 3:415ccd1f7ae1 | 116 | char terminate = ','; |
pkoirala3 | 3:415ccd1f7ae1 | 117 | char status = 's'; |
pkoirala3 | 3:415ccd1f7ae1 | 118 | sprintf(outBuffer, "%c%c%2.2f%c%2.1f%c%0.2f%c",status, delimit, temp, delimit, hum, delimit, lightStatus, terminate); |
pkoirala3 | 3:415ccd1f7ae1 | 119 | sendcommand(outBuffer); |
pkoirala3 | 3:415ccd1f7ae1 | 120 | } |
pkoirala3 | 3:415ccd1f7ae1 | 121 | else if(inBuffer == 'l') { |
pkoirala3 | 3:415ccd1f7ae1 | 122 | lightOnOff = !lightOnOff; // Toggle light on/off |
pkoirala3 | 3:415ccd1f7ae1 | 123 | (lightOnOff == true) ? ambient : 0; |
pkoirala3 | 3:415ccd1f7ae1 | 124 | |
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 | } |