![](/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@7:fdfe30cea3c2, 2017-05-02 (annotated)
- Committer:
- pkoirala3
- Date:
- Tue May 02 21:41:59 2017 +0000
- Revision:
- 7:fdfe30cea3c2
- Parent:
- 6:6a43e2fdb8aa
Final Version
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 | 6:6a43e2fdb8aa | 6 | #include "math.h" |
pkoirala3 | 1:e9068d048f58 | 7 | |
pkoirala3 | 3:415ccd1f7ae1 | 8 | Serial xbee(p9, p10); |
pkoirala3 | 3:415ccd1f7ae1 | 9 | Serial pc(USBTX, USBRX); |
pkoirala3 | 6:6a43e2fdb8aa | 10 | JPEGCamera camera(p13, p14); // RED, Brown, pwr: Gray, gnd: Purple |
pkoirala3 | 6:6a43e2fdb8aa | 11 | LocalFileSystem local("local"); //save images on mbed |
pkoirala3 | 6:6a43e2fdb8aa | 12 | |
pkoirala3 | 3:415ccd1f7ae1 | 13 | DigitalOut rst1(p8); |
pkoirala3 | 6:6a43e2fdb8aa | 14 | |
pkoirala3 | 6:6a43e2fdb8aa | 15 | PwmOut light(p21); |
pkoirala3 | 3:415ccd1f7ae1 | 16 | Si7021 tempHum(p28, p27); |
pkoirala3 | 0:5b1386641010 | 17 | |
pkoirala3 | 6:6a43e2fdb8aa | 18 | InterruptIn interrupt(p26); |
pkoirala3 | 6:6a43e2fdb8aa | 19 | I2C i2c(p28, p27); // Note here we don't need PullsUp coz Si7021 board has |
pkoirala3 | 6:6a43e2fdb8aa | 20 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
pkoirala3 | 6:6a43e2fdb8aa | 21 | |
pkoirala3 | 6:6a43e2fdb8aa | 22 | AnalogIn photocell(p15); |
pkoirala3 | 6:6a43e2fdb8aa | 23 | |
pkoirala3 | 3:415ccd1f7ae1 | 24 | DigitalOut led1(LED1); |
pkoirala3 | 3:415ccd1f7ae1 | 25 | DigitalOut led2(LED2); |
pkoirala3 | 6:6a43e2fdb8aa | 26 | DigitalOut led3(LED3); |
pkoirala3 | 6:6a43e2fdb8aa | 27 | DigitalOut led4(LED4); |
pkoirala3 | 6:6a43e2fdb8aa | 28 | |
pkoirala3 | 6:6a43e2fdb8aa | 29 | int Nums[4] = {0,0,0,0}; |
pkoirala3 | 6:6a43e2fdb8aa | 30 | int counter = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 31 | int attempts = 0; |
pkoirala3 | 0:5b1386641010 | 32 | |
pkoirala3 | 3:415ccd1f7ae1 | 33 | char outBuffer[50]; |
pkoirala3 | 6:6a43e2fdb8aa | 34 | volatile char inBuffer; |
pkoirala3 | 6:6a43e2fdb8aa | 35 | volatile bool lightOnOff = true; |
pkoirala3 | 3:415ccd1f7ae1 | 36 | volatile bool requestIn = false; |
pkoirala3 | 5:81a1920e5f3d | 37 | volatile bool intrusion = false; |
pkoirala3 | 6:6a43e2fdb8aa | 38 | volatile bool unlockstatus = false; |
pkoirala3 | 6:6a43e2fdb8aa | 39 | volatile float photoval = 0.0; |
pkoirala3 | 6:6a43e2fdb8aa | 40 | int fileIndex = 0; |
pkoirala3 | 0:5b1386641010 | 41 | |
pkoirala3 | 3:415ccd1f7ae1 | 42 | Thread t1; |
pkoirala3 | 5:81a1920e5f3d | 43 | Thread t2; |
pkoirala3 | 0:5b1386641010 | 44 | |
pkoirala3 | 3:415ccd1f7ae1 | 45 | void getcommand() |
pkoirala3 | 3:415ccd1f7ae1 | 46 | { |
pkoirala3 | 3:415ccd1f7ae1 | 47 | while(1) { |
pkoirala3 | 3:415ccd1f7ae1 | 48 | if(xbee.readable()) { |
pkoirala3 | 3:415ccd1f7ae1 | 49 | inBuffer = xbee.getc(); |
pkoirala3 | 3:415ccd1f7ae1 | 50 | requestIn = true; |
pkoirala3 | 3:415ccd1f7ae1 | 51 | } |
pkoirala3 | 3:415ccd1f7ae1 | 52 | Thread::wait(1000); |
pkoirala3 | 3:415ccd1f7ae1 | 53 | } |
pkoirala3 | 1:e9068d048f58 | 54 | } |
pkoirala3 | 1:e9068d048f58 | 55 | |
pkoirala3 | 3:415ccd1f7ae1 | 56 | void sendcommand(char outBuff[]) |
pkoirala3 | 3:415ccd1f7ae1 | 57 | { |
pkoirala3 | 6:6a43e2fdb8aa | 58 | if(xbee.writeable()) { |
pkoirala3 | 3:415ccd1f7ae1 | 59 | int i = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 60 | while(outBuff[i] != ',') { |
pkoirala3 | 3:415ccd1f7ae1 | 61 | xbee.putc(outBuff[i]); |
pkoirala3 | 3:415ccd1f7ae1 | 62 | i++; |
pkoirala3 | 3:415ccd1f7ae1 | 63 | } |
pkoirala3 | 3:415ccd1f7ae1 | 64 | } |
pkoirala3 | 1:e9068d048f58 | 65 | } |
pkoirala3 | 5:81a1920e5f3d | 66 | |
pkoirala3 | 6:6a43e2fdb8aa | 67 | void statusUpdate() |
pkoirala3 | 6:6a43e2fdb8aa | 68 | { |
pkoirala3 | 5:81a1920e5f3d | 69 | tempHum.measure(); |
pkoirala3 | 5:81a1920e5f3d | 70 | float temp = (tempHum.get_temperature()/1000.00); |
pkoirala3 | 5:81a1920e5f3d | 71 | float hum = (tempHum.get_humidity()/1000.00); |
pkoirala3 | 6:6a43e2fdb8aa | 72 | float lightStatus = light; |
pkoirala3 | 5:81a1920e5f3d | 73 | char delimit = '|'; |
pkoirala3 | 5:81a1920e5f3d | 74 | char terminate = ','; |
pkoirala3 | 6:6a43e2fdb8aa | 75 | sprintf(outBuffer, "%c%2.2f%c%2.2f%c%0.2f%c",delimit, temp, delimit, hum, delimit, lightStatus, terminate); |
pkoirala3 | 5:81a1920e5f3d | 76 | sendcommand(outBuffer); |
pkoirala3 | 5:81a1920e5f3d | 77 | } |
pkoirala3 | 5:81a1920e5f3d | 78 | |
pkoirala3 | 6:6a43e2fdb8aa | 79 | void checkUnlock(); |
pkoirala3 | 6:6a43e2fdb8aa | 80 | |
pkoirala3 | 6:6a43e2fdb8aa | 81 | void fallInterrupt() |
pkoirala3 | 6:6a43e2fdb8aa | 82 | { |
pkoirala3 | 6:6a43e2fdb8aa | 83 | int key_code=0; |
pkoirala3 | 6:6a43e2fdb8aa | 84 | int i=0; |
pkoirala3 | 6:6a43e2fdb8aa | 85 | int value=mpr121.read(0x00); |
pkoirala3 | 6:6a43e2fdb8aa | 86 | value +=mpr121.read(0x01)<<8; |
pkoirala3 | 6:6a43e2fdb8aa | 87 | i=0; |
pkoirala3 | 6:6a43e2fdb8aa | 88 | for (i=0; i<12; i++) { |
pkoirala3 | 6:6a43e2fdb8aa | 89 | if (((value>>i)&0x01)==1) { |
pkoirala3 | 6:6a43e2fdb8aa | 90 | key_code=i; |
pkoirala3 | 6:6a43e2fdb8aa | 91 | Nums[counter] = key_code; |
pkoirala3 | 6:6a43e2fdb8aa | 92 | counter++; |
pkoirala3 | 6:6a43e2fdb8aa | 93 | } |
pkoirala3 | 6:6a43e2fdb8aa | 94 | } |
pkoirala3 | 6:6a43e2fdb8aa | 95 | |
pkoirala3 | 6:6a43e2fdb8aa | 96 | led4=key_code & 0x01; |
pkoirala3 | 6:6a43e2fdb8aa | 97 | led3=(key_code>>1) & 0x01; |
pkoirala3 | 6:6a43e2fdb8aa | 98 | led2=(key_code>>2) & 0x01; |
pkoirala3 | 6:6a43e2fdb8aa | 99 | led1=(key_code>>3) & 0x01; |
pkoirala3 | 6:6a43e2fdb8aa | 100 | if(counter == 4) { |
pkoirala3 | 6:6a43e2fdb8aa | 101 | checkUnlock(); |
pkoirala3 | 6:6a43e2fdb8aa | 102 | } |
pkoirala3 | 6:6a43e2fdb8aa | 103 | } |
pkoirala3 | 6:6a43e2fdb8aa | 104 | |
pkoirala3 | 6:6a43e2fdb8aa | 105 | void checkUnlock() |
pkoirala3 | 6:6a43e2fdb8aa | 106 | { |
pkoirala3 | 6:6a43e2fdb8aa | 107 | if(Nums[0] == 4 && Nums[1] == 1 && Nums[2] == 8 && Nums[3] == 0) { |
pkoirala3 | 6:6a43e2fdb8aa | 108 | unlockstatus = true; |
pkoirala3 | 6:6a43e2fdb8aa | 109 | attempts = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 110 | } else if(attempts < 1) { |
pkoirala3 | 6:6a43e2fdb8aa | 111 | attempts = attempts + 1; // First attempt |
pkoirala3 | 6:6a43e2fdb8aa | 112 | } else if (attempts == 1) { // Second attempt |
pkoirala3 | 6:6a43e2fdb8aa | 113 | attempts = 0; // Reset attempts |
pkoirala3 | 6:6a43e2fdb8aa | 114 | led1 = 1; |
pkoirala3 | 6:6a43e2fdb8aa | 115 | led2 = 1; |
pkoirala3 | 6:6a43e2fdb8aa | 116 | led3 = 1; |
pkoirala3 | 6:6a43e2fdb8aa | 117 | led4 = 1; |
pkoirala3 | 6:6a43e2fdb8aa | 118 | intrusion = true; |
pkoirala3 | 6:6a43e2fdb8aa | 119 | attempts = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 120 | } |
pkoirala3 | 6:6a43e2fdb8aa | 121 | counter = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 122 | } |
pkoirala3 | 6:6a43e2fdb8aa | 123 | |
pkoirala3 | 5:81a1920e5f3d | 124 | |
pkoirala3 | 3:415ccd1f7ae1 | 125 | void cameraImage() |
pkoirala3 | 3:415ccd1f7ae1 | 126 | { |
pkoirala3 | 6:6a43e2fdb8aa | 127 | if (camera.isReady()) { |
pkoirala3 | 6:6a43e2fdb8aa | 128 | char filename[32]; |
pkoirala3 | 6:6a43e2fdb8aa | 129 | sprintf(filename, "/local/pict%d.jpg",fileIndex); |
pkoirala3 | 6:6a43e2fdb8aa | 130 | fileIndex++; |
pkoirala3 | 6:6a43e2fdb8aa | 131 | printf("Picture: %s ", filename); |
pkoirala3 | 6:6a43e2fdb8aa | 132 | if (camera.takePicture(filename)) { |
pkoirala3 | 6:6a43e2fdb8aa | 133 | while (camera.isProcessing()) { |
pkoirala3 | 6:6a43e2fdb8aa | 134 | camera.processPicture(); |
pkoirala3 | 6:6a43e2fdb8aa | 135 | } |
pkoirala3 | 6:6a43e2fdb8aa | 136 | } |
pkoirala3 | 6:6a43e2fdb8aa | 137 | } |
pkoirala3 | 6:6a43e2fdb8aa | 138 | } |
pkoirala3 | 6:6a43e2fdb8aa | 139 | |
pkoirala3 | 6:6a43e2fdb8aa | 140 | |
pkoirala3 | 6:6a43e2fdb8aa | 141 | int main() |
pkoirala3 | 6:6a43e2fdb8aa | 142 | { |
pkoirala3 | 6:6a43e2fdb8aa | 143 | camera.setPictureSize(JPEGCamera::SIZE320x240); |
pkoirala3 | 6:6a43e2fdb8aa | 144 | light = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 145 | rst1 = 0; |
pkoirala3 | 6:6a43e2fdb8aa | 146 | wait_ms(1); |
pkoirala3 | 6:6a43e2fdb8aa | 147 | rst1 = 1; |
pkoirala3 | 6:6a43e2fdb8aa | 148 | wait_ms(1); |
pkoirala3 | 6:6a43e2fdb8aa | 149 | t1.start(getcommand); |
pkoirala3 | 6:6a43e2fdb8aa | 150 | interrupt.fall(&fallInterrupt); |
pkoirala3 | 6:6a43e2fdb8aa | 151 | interrupt.mode(PullUp); |
pkoirala3 | 6:6a43e2fdb8aa | 152 | inBuffer = 's'; |
pkoirala3 | 3:415ccd1f7ae1 | 153 | while(1) { |
pkoirala3 | 6:6a43e2fdb8aa | 154 | photoval = 0.1/(0.1 + photocell); |
pkoirala3 | 6:6a43e2fdb8aa | 155 | float newlight = (lightOnOff == true) ? photoval : 0.0; |
pkoirala3 | 6:6a43e2fdb8aa | 156 | bool lighton = false; |
pkoirala3 | 6:6a43e2fdb8aa | 157 | if(newlight == 0.0) { |
pkoirala3 | 6:6a43e2fdb8aa | 158 | light = 0.0; |
pkoirala3 | 6:6a43e2fdb8aa | 159 | } else if(fabs(newlight - light) > 0.05) { |
pkoirala3 | 6:6a43e2fdb8aa | 160 | light = newlight; |
pkoirala3 | 6:6a43e2fdb8aa | 161 | lighton = true; |
pkoirala3 | 6:6a43e2fdb8aa | 162 | } |
pkoirala3 | 6:6a43e2fdb8aa | 163 | if(intrusion || requestIn || inBuffer != ' ' || lighton || unlockstatus) { // Check for input request msg |
pkoirala3 | 6:6a43e2fdb8aa | 164 | if(inBuffer == 's' || lighton) { // Send the status |
pkoirala3 | 6:6a43e2fdb8aa | 165 | statusUpdate(); |
pkoirala3 | 6:6a43e2fdb8aa | 166 | inBuffer = ' '; |
pkoirala3 | 6:6a43e2fdb8aa | 167 | } else if(unlockstatus) { |
pkoirala3 | 6:6a43e2fdb8aa | 168 | light = 0.1/(0.1 + photocell); |
pkoirala3 | 6:6a43e2fdb8aa | 169 | sendcommand("u,"); |
pkoirala3 | 6:6a43e2fdb8aa | 170 | unlockstatus = false; |
pkoirala3 | 6:6a43e2fdb8aa | 171 | lightOnOff = true; |
pkoirala3 | 6:6a43e2fdb8aa | 172 | } else if(inBuffer == 'l') { |
pkoirala3 | 6:6a43e2fdb8aa | 173 | lightOnOff = !lightOnOff; // Toggle light on/off |
pkoirala3 | 6:6a43e2fdb8aa | 174 | inBuffer = 's'; |
pkoirala3 | 6:6a43e2fdb8aa | 175 | } else if(intrusion || inBuffer == 'c' ) { |
pkoirala3 | 6:6a43e2fdb8aa | 176 | cameraImage(); // Capture pic & save locally |
pkoirala3 | 6:6a43e2fdb8aa | 177 | if(intrusion) { |
pkoirala3 | 6:6a43e2fdb8aa | 178 | sendcommand("i,"); |
pkoirala3 | 6:6a43e2fdb8aa | 179 | intrusion = false; |
pkoirala3 | 6:6a43e2fdb8aa | 180 | } else { |
pkoirala3 | 6:6a43e2fdb8aa | 181 | sendcommand("p,"); |
pkoirala3 | 6:6a43e2fdb8aa | 182 | inBuffer = ' '; |
pkoirala3 | 1:e9068d048f58 | 183 | } |
pkoirala3 | 1:e9068d048f58 | 184 | } |
pkoirala3 | 1:e9068d048f58 | 185 | } |
pkoirala3 | 6:6a43e2fdb8aa | 186 | Thread::wait(500); |
pkoirala3 | 1:e9068d048f58 | 187 | } |
pkoirala3 | 0:5b1386641010 | 188 | } |