Basic IoT demo of sensor data going to one phone and data push is controlled by another phone.
Dependencies: HTU21D SCP1000 TMP36 mbed
main.cpp@1:bb2fc6537dcc, 2016-03-08 (annotated)
- Committer:
- hippi345
- Date:
- Tue Mar 08 14:18:34 2016 +0000
- Revision:
- 1:bb2fc6537dcc
- Parent:
- 0:8ed55ae36c54
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hippi345 | 0:8ed55ae36c54 | 1 | //Joel Shearon and Van Mang ECE 4180 Mini Project |
hippi345 | 0:8ed55ae36c54 | 2 | #include "mbed.h" |
hippi345 | 0:8ed55ae36c54 | 3 | #include "HTU21D.h" |
hippi345 | 0:8ed55ae36c54 | 4 | #include "TMP36.h" |
hippi345 | 0:8ed55ae36c54 | 5 | #include "string" |
hippi345 | 0:8ed55ae36c54 | 6 | #include "SCP1000.h" |
hippi345 | 0:8ed55ae36c54 | 7 | SCP1000 scp1000(p5,p6,p7,p8); |
hippi345 | 0:8ed55ae36c54 | 8 | TMP36 tmp36(p20); |
hippi345 | 0:8ed55ae36c54 | 9 | Ticker flipper; |
hippi345 | 0:8ed55ae36c54 | 10 | AnalogIn photocell(p15); |
hippi345 | 0:8ed55ae36c54 | 11 | string sun; |
hippi345 | 0:8ed55ae36c54 | 12 | PwmOut myled(LED1); |
hippi345 | 0:8ed55ae36c54 | 13 | RawSerial dev(p28,p27); |
hippi345 | 0:8ed55ae36c54 | 14 | RawSerial ble(p13,p14); |
hippi345 | 0:8ed55ae36c54 | 15 | DigitalOut led1(LED3); |
hippi345 | 0:8ed55ae36c54 | 16 | DigitalOut led4(LED4); |
hippi345 | 0:8ed55ae36c54 | 17 | Serial pc(USBTX, USBRX); |
hippi345 | 0:8ed55ae36c54 | 18 | HTU21D temphumid(p9, p10); // Temp Module || sda, SCL |
hippi345 | 0:8ed55ae36c54 | 19 | string options[7] = {"moonless cloudy night \n\r" |
hippi345 | 0:8ed55ae36c54 | 20 | ,"moonlit night \n\r" |
hippi345 | 0:8ed55ae36c54 | 21 | ,"dark room\n\r" |
hippi345 | 0:8ed55ae36c54 | 22 | ,"dark overcast day \n\r" |
hippi345 | 0:8ed55ae36c54 | 23 | ,"overcast day \n\r" |
hippi345 | 0:8ed55ae36c54 | 24 | ,"full day \n\r" |
hippi345 | 0:8ed55ae36c54 | 25 | ,"full sun \n\r"}; |
hippi345 | 0:8ed55ae36c54 | 26 | |
hippi345 | 0:8ed55ae36c54 | 27 | void dev_recv() |
hippi345 | 0:8ed55ae36c54 | 28 | { |
hippi345 | 0:8ed55ae36c54 | 29 | led1 = !led1; |
hippi345 | 0:8ed55ae36c54 | 30 | while(dev.readable()) { |
hippi345 | 0:8ed55ae36c54 | 31 | pc.putc(dev.getc()); |
hippi345 | 0:8ed55ae36c54 | 32 | } |
hippi345 | 0:8ed55ae36c54 | 33 | } |
hippi345 | 0:8ed55ae36c54 | 34 | |
hippi345 | 0:8ed55ae36c54 | 35 | void pc_recv() |
hippi345 | 0:8ed55ae36c54 | 36 | { |
hippi345 | 0:8ed55ae36c54 | 37 | led4 = !led4; |
hippi345 | 0:8ed55ae36c54 | 38 | while(pc.readable()) { |
hippi345 | 0:8ed55ae36c54 | 39 | dev.putc(pc.getc()); |
hippi345 | 0:8ed55ae36c54 | 40 | } |
hippi345 | 0:8ed55ae36c54 | 41 | } |
hippi345 | 0:8ed55ae36c54 | 42 | |
hippi345 | 0:8ed55ae36c54 | 43 | void flip() { |
hippi345 | 0:8ed55ae36c54 | 44 | float readPhoto = photocell.read()*3.3; |
hippi345 | 0:8ed55ae36c54 | 45 | if (readPhoto < 0.15f) |
hippi345 | 0:8ed55ae36c54 | 46 | { sun = options[0]; |
hippi345 | 0:8ed55ae36c54 | 47 | } |
hippi345 | 0:8ed55ae36c54 | 48 | else if (readPhoto < 0.412f) |
hippi345 | 0:8ed55ae36c54 | 49 | { sun = options[1]; |
hippi345 | 0:8ed55ae36c54 | 50 | } |
hippi345 | 0:8ed55ae36c54 | 51 | else if (readPhoto < 1.65f) |
hippi345 | 0:8ed55ae36c54 | 52 | { sun = options[2]; |
hippi345 | 0:8ed55ae36c54 | 53 | } |
hippi345 | 0:8ed55ae36c54 | 54 | else if (readPhoto < 1.34f){ |
hippi345 | 0:8ed55ae36c54 | 55 | sun = options[3]; |
hippi345 | 0:8ed55ae36c54 | 56 | } |
hippi345 | 0:8ed55ae36c54 | 57 | else if (readPhoto < 3.2f) |
hippi345 | 0:8ed55ae36c54 | 58 | { sun = options[4]; |
hippi345 | 0:8ed55ae36c54 | 59 | } |
hippi345 | 0:8ed55ae36c54 | 60 | else if (readPhoto < 3.26f) |
hippi345 | 0:8ed55ae36c54 | 61 | { sun = options[5]; |
hippi345 | 0:8ed55ae36c54 | 62 | } |
hippi345 | 0:8ed55ae36c54 | 63 | else if (readPhoto >= 3.26f) |
hippi345 | 0:8ed55ae36c54 | 64 | { sun = options[6]; |
hippi345 | 0:8ed55ae36c54 | 65 | } |
hippi345 | 0:8ed55ae36c54 | 66 | } |
hippi345 | 0:8ed55ae36c54 | 67 | //start of main |
hippi345 | 0:8ed55ae36c54 | 68 | |
hippi345 | 0:8ed55ae36c54 | 69 | int main() |
hippi345 | 0:8ed55ae36c54 | 70 | { |
hippi345 | 0:8ed55ae36c54 | 71 | char bnum=0; |
hippi345 | 0:8ed55ae36c54 | 72 | char bhit=0; |
hippi345 | 0:8ed55ae36c54 | 73 | pc.baud(9600); |
hippi345 | 0:8ed55ae36c54 | 74 | dev.baud(9600); |
hippi345 | 0:8ed55ae36c54 | 75 | float tempC, tempF; |
hippi345 | 0:8ed55ae36c54 | 76 | pc.attach(&pc_recv, Serial::RxIrq); |
hippi345 | 0:8ed55ae36c54 | 77 | dev.attach(&dev_recv, Serial::RxIrq); |
hippi345 | 0:8ed55ae36c54 | 78 | flipper.attach(&flip, 1); |
hippi345 | 0:8ed55ae36c54 | 79 | while(1) { |
hippi345 | 0:8ed55ae36c54 | 80 | wait(1); |
hippi345 | 0:8ed55ae36c54 | 81 | //conversion to degrees C - from sensor output voltage per LM61 data sheet |
hippi345 | 0:8ed55ae36c54 | 82 | tempC = tmp36.read(); |
hippi345 | 0:8ed55ae36c54 | 83 | //printf("percentage: %3.3f%%\n", tmp36.read()); |
hippi345 | 0:8ed55ae36c54 | 84 | //wait(0.5); |
hippi345 | 0:8ed55ae36c54 | 85 | //convert to degrees F |
hippi345 | 0:8ed55ae36c54 | 86 | tempF = tmp36.readFah(); |
hippi345 | 0:8ed55ae36c54 | 87 | |
hippi345 | 0:8ed55ae36c54 | 88 | if (ble.getc()=='!') { |
hippi345 | 0:8ed55ae36c54 | 89 | if (ble.getc()=='B') { //button data packet |
hippi345 | 0:8ed55ae36c54 | 90 | bnum = ble.getc(); //button number |
hippi345 | 0:8ed55ae36c54 | 91 | bhit = ble.getc(); //1=hit, 0=release |
hippi345 | 0:8ed55ae36c54 | 92 | if (ble.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
hippi345 | 0:8ed55ae36c54 | 93 | myled = bnum - '0'; //current button number will appear on LEDs |
hippi345 | 0:8ed55ae36c54 | 94 | switch (bnum) { |
hippi345 | 0:8ed55ae36c54 | 95 | case '1': //number button 1 |
hippi345 | 0:8ed55ae36c54 | 96 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 97 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 98 | } else { |
hippi345 | 0:8ed55ae36c54 | 99 | //dev.printf("1"); |
hippi345 | 0:8ed55ae36c54 | 100 | tempC = tmp36.read(); |
hippi345 | 0:8ed55ae36c54 | 101 | tempF = tmp36.readFah(); |
hippi345 | 0:8ed55ae36c54 | 102 | dev.printf("%5.2F C %5.2F F \n\r", tempC, tempF); |
hippi345 | 0:8ed55ae36c54 | 103 | } |
hippi345 | 0:8ed55ae36c54 | 104 | break; |
hippi345 | 0:8ed55ae36c54 | 105 | case '2': //number button 2 |
hippi345 | 0:8ed55ae36c54 | 106 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 107 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 108 | } else { |
hippi345 | 0:8ed55ae36c54 | 109 | //dev.printf("2"); |
hippi345 | 0:8ed55ae36c54 | 110 | dev.printf("Humidity Is: %i %%\n\r", temphumid.sample_humid()); |
hippi345 | 0:8ed55ae36c54 | 111 | } |
hippi345 | 0:8ed55ae36c54 | 112 | break; |
hippi345 | 0:8ed55ae36c54 | 113 | case '3': //number button 3 |
hippi345 | 0:8ed55ae36c54 | 114 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 115 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 116 | } else { |
hippi345 | 0:8ed55ae36c54 | 117 | //dev.printf("3"); |
hippi345 | 0:8ed55ae36c54 | 118 | dev.printf("the sun level is: %s", sun); |
hippi345 | 0:8ed55ae36c54 | 119 | } |
hippi345 | 0:8ed55ae36c54 | 120 | break; |
hippi345 | 0:8ed55ae36c54 | 121 | case '4': //number button 4 |
hippi345 | 0:8ed55ae36c54 | 122 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 123 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 124 | } else { |
hippi345 | 0:8ed55ae36c54 | 125 | //dev.printf("4"); |
hippi345 | 0:8ed55ae36c54 | 126 | dev.printf("The pressure is %d Pa", scp1000.readPressure()); |
hippi345 | 0:8ed55ae36c54 | 127 | } |
hippi345 | 0:8ed55ae36c54 | 128 | break; |
hippi345 | 0:8ed55ae36c54 | 129 | case '5': //button 5 up arrow |
hippi345 | 0:8ed55ae36c54 | 130 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 131 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 132 | } else { |
hippi345 | 0:8ed55ae36c54 | 133 | //dev.printf("5"); |
hippi345 | 0:8ed55ae36c54 | 134 | tempC = tmp36.read(); |
hippi345 | 0:8ed55ae36c54 | 135 | tempF = tmp36.readFah(); |
hippi345 | 0:8ed55ae36c54 | 136 | dev.printf("%5.2F C %5.2F F \n\r", tempC, tempF); |
hippi345 | 0:8ed55ae36c54 | 137 | } |
hippi345 | 0:8ed55ae36c54 | 138 | break; |
hippi345 | 0:8ed55ae36c54 | 139 | case '6': //button 6 down arrow |
hippi345 | 0:8ed55ae36c54 | 140 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 141 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 142 | } else { |
hippi345 | 0:8ed55ae36c54 | 143 | //dev.printf("6"); |
hippi345 | 0:8ed55ae36c54 | 144 | dev.printf("Humidity Is: %i %%\n\r", temphumid.sample_humid()); |
hippi345 | 0:8ed55ae36c54 | 145 | } |
hippi345 | 0:8ed55ae36c54 | 146 | break; |
hippi345 | 0:8ed55ae36c54 | 147 | case '7': //button 7 left arrow |
hippi345 | 0:8ed55ae36c54 | 148 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 149 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 150 | } else { |
hippi345 | 0:8ed55ae36c54 | 151 | //dev.printf("7"); |
hippi345 | 0:8ed55ae36c54 | 152 | dev.printf("the sun level is: %s", sun); |
hippi345 | 0:8ed55ae36c54 | 153 | } |
hippi345 | 0:8ed55ae36c54 | 154 | break; |
hippi345 | 0:8ed55ae36c54 | 155 | case '8': //button 8 right arrow |
hippi345 | 0:8ed55ae36c54 | 156 | if (bhit=='1') { |
hippi345 | 0:8ed55ae36c54 | 157 | //add hit code here |
hippi345 | 0:8ed55ae36c54 | 158 | } else { |
hippi345 | 0:8ed55ae36c54 | 159 | //dev.printf("8"); |
hippi345 | 0:8ed55ae36c54 | 160 | dev.printf("The pressure is %d Pa", scp1000.readPressure()); |
hippi345 | 0:8ed55ae36c54 | 161 | } |
hippi345 | 0:8ed55ae36c54 | 162 | break; |
hippi345 | 0:8ed55ae36c54 | 163 | default: |
hippi345 | 0:8ed55ae36c54 | 164 | break; |
hippi345 | 0:8ed55ae36c54 | 165 | } |
hippi345 | 0:8ed55ae36c54 | 166 | } |
hippi345 | 0:8ed55ae36c54 | 167 | } |
hippi345 | 0:8ed55ae36c54 | 168 | } |
hippi345 | 0:8ed55ae36c54 | 169 | } |
hippi345 | 0:8ed55ae36c54 | 170 | } |