Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed BME280 SI1145
main.cpp@10:1296cc169c60, 2017-07-06 (annotated)
- Committer:
- jonathanyost
- Date:
- Thu Jul 06 16:32:54 2017 +0000
- Revision:
- 10:1296cc169c60
- Parent:
- 9:e619711e05be
- Child:
- 11:0c66b84f8553
fully functional. synchronous.;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jonathanyost | 3:51b128605eaa | 1 | /* |
| jonathanyost | 3:51b128605eaa | 2 | Author: Jonathan Yost |
| jonathanyost | 3:51b128605eaa | 3 | Title: END_NODE_REQUEST_POC |
| jonathanyost | 3:51b128605eaa | 4 | Date: 6/28/17 |
| jonathanyost | 3:51b128605eaa | 5 | |
| jonathanyost | 3:51b128605eaa | 6 | Desc: Reads in data from the sensors and sends to the gateway. |
| jonathanyost | 3:51b128605eaa | 7 | yeah. |
| jonathanyost | 3:51b128605eaa | 8 | */ |
| jonathanyost | 3:51b128605eaa | 9 | |
| jonathanyost | 4:8c15903a5581 | 10 | // ISSUES |
| jonathanyost | 4:8c15903a5581 | 11 | /* TODO!!! SOMETIMES THE XBEE READS IN "i:1" instead of "id:1"... |
| jonathanyost | 4:8c15903a5581 | 12 | No idea why. For now I will accept both, but that's gross. |
| jonathanyost | 4:8c15903a5581 | 13 | */ |
| jonathanyost | 4:8c15903a5581 | 14 | |
| jonathanyost | 3:51b128605eaa | 15 | // Libraries |
| jonathanyost | 0:4354437a1033 | 16 | #include "mbed.h" |
| jonathanyost | 3:51b128605eaa | 17 | #include "BME280.h" |
| jonathanyost | 3:51b128605eaa | 18 | #include "SI1145.h" |
| jonathanyost | 10:1296cc169c60 | 19 | //#include "MODSERIAL.h" |
| jonathanyost | 0:4354437a1033 | 20 | |
| jonathanyost | 3:51b128605eaa | 21 | // Define Constants / Macros |
| jonathanyost | 0:4354437a1033 | 22 | #define node_id (0x01) |
| jonathanyost | 0:4354437a1033 | 23 | #define tx (PA_9) |
| jonathanyost | 0:4354437a1033 | 24 | #define rx (PA_10) |
| jonathanyost | 0:4354437a1033 | 25 | |
| jonathanyost | 2:2bdf15b94f18 | 26 | const char nodeID = '1'; |
| jonathanyost | 1:423c669d0306 | 27 | |
| jonathanyost | 3:51b128605eaa | 28 | // I/O Delarations |
| jonathanyost | 3:51b128605eaa | 29 | I2C i2c(I2C_SDA, I2C_SCL); // D14, D15 |
| jonathanyost | 3:51b128605eaa | 30 | BME280 *thp_sensor = new BME280(i2c); |
| jonathanyost | 3:51b128605eaa | 31 | SI1145 *uiv_sensor = new SI1145(i2c); |
| jonathanyost | 3:51b128605eaa | 32 | |
| jonathanyost | 4:8c15903a5581 | 33 | // Declare the PIR interface |
| jonathanyost | 4:8c15903a5581 | 34 | InterruptIn MotionDetect(PA_0); |
| jonathanyost | 9:e619711e05be | 35 | bool pir_enable = true; |
| jonathanyost | 4:8c15903a5581 | 36 | |
| jonathanyost | 3:51b128605eaa | 37 | // Declare Serial Interfaces |
| jonathanyost | 10:1296cc169c60 | 38 | //Serial xb(tx, rx); |
| jonathanyost | 10:1296cc169c60 | 39 | //Serial pc(D1, D0); |
| jonathanyost | 10:1296cc169c60 | 40 | |
| jonathanyost | 0:4354437a1033 | 41 | Serial xb(tx, rx); |
| jonathanyost | 0:4354437a1033 | 42 | Serial pc(D1, D0); |
| jonathanyost | 0:4354437a1033 | 43 | |
| jonathanyost | 4:8c15903a5581 | 44 | // send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise); |
| jonathanyost | 4:8c15903a5581 | 45 | // "id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8" |
| jonathanyost | 5:ad41288051e7 | 46 | void SendSensorData(int t, int p, int h, int u, int ir, int v, int n){ |
| jonathanyost | 5:ad41288051e7 | 47 | //" id:2, te:%2,pr:%3,uv:%5,ir:%6,vi:%7,no:%8" |
| jonathanyost | 9:e619711e05be | 48 | xb.printf( ",ni:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",1,t,p,h,u,ir,v,n); |
| jonathanyost | 9:e619711e05be | 49 | pc.printf("send: id:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",1,t,p,h,u,ir,v,n); |
| jonathanyost | 1:423c669d0306 | 50 | } |
| jonathanyost | 0:4354437a1033 | 51 | |
| jonathanyost | 4:8c15903a5581 | 52 | void SendPirFlag(){ |
| jonathanyost | 10:1296cc169c60 | 53 | xb.printf("ni:%c,mo:1\r\n",nodeID); |
| jonathanyost | 3:51b128605eaa | 54 | } |
| jonathanyost | 3:51b128605eaa | 55 | |
| jonathanyost | 4:8c15903a5581 | 56 | // PIR interrupt. Set the pir_trigger to true when interrupted. |
| jonathanyost | 4:8c15903a5581 | 57 | void PIR(void){ |
| jonathanyost | 10:1296cc169c60 | 58 | if(xb.writeable() && pir_enable){ |
| jonathanyost | 9:e619711e05be | 59 | SendPirFlag(); |
| jonathanyost | 10:1296cc169c60 | 60 | pc.printf("PIR FLAG ISR\r\n"); |
| jonathanyost | 9:e619711e05be | 61 | } |
| jonathanyost | 0:4354437a1033 | 62 | } |
| jonathanyost | 1:423c669d0306 | 63 | |
| jonathanyost | 9:e619711e05be | 64 | void initialize_serial_connections(){ |
| jonathanyost | 9:e619711e05be | 65 | xb.baud(57600); |
| jonathanyost | 9:e619711e05be | 66 | pc.baud(115200); |
| jonathanyost | 9:e619711e05be | 67 | } |
| jonathanyost | 9:e619711e05be | 68 | |
| jonathanyost | 0:4354437a1033 | 69 | int main() { |
| jonathanyost | 9:e619711e05be | 70 | initialize_serial_connections(); |
| jonathanyost | 9:e619711e05be | 71 | |
| jonathanyost | 8:1703f45edfa2 | 72 | pc.printf("\n\rMic test 1-2, 1-2\n\r"); |
| jonathanyost | 9:e619711e05be | 73 | |
| jonathanyost | 4:8c15903a5581 | 74 | // Attach the PIR function to interrupt |
| jonathanyost | 9:e619711e05be | 75 | MotionDetect.rise(&PIR); |
| jonathanyost | 4:8c15903a5581 | 76 | |
| jonathanyost | 0:4354437a1033 | 77 | char pc_data = 'e'; |
| jonathanyost | 9:e619711e05be | 78 | char buffer[10]; |
| jonathanyost | 7:6ee530637c90 | 79 | |
| jonathanyost | 2:2bdf15b94f18 | 80 | int temp = 1; |
| jonathanyost | 2:2bdf15b94f18 | 81 | int pressure = 2; |
| jonathanyost | 2:2bdf15b94f18 | 82 | int humidity = 3; |
| jonathanyost | 2:2bdf15b94f18 | 83 | int uv = 5; |
| jonathanyost | 2:2bdf15b94f18 | 84 | int ir = 6; |
| jonathanyost | 2:2bdf15b94f18 | 85 | int vis = 7; |
| jonathanyost | 2:2bdf15b94f18 | 86 | int noise = 8; |
| jonathanyost | 2:2bdf15b94f18 | 87 | |
| jonathanyost | 2:2bdf15b94f18 | 88 | bool send_enable = false; |
| jonathanyost | 8:1703f45edfa2 | 89 | |
| jonathanyost | 0:4354437a1033 | 90 | while(true){ |
| jonathanyost | 3:51b128605eaa | 91 | // Debug Controls Input |
| jonathanyost | 0:4354437a1033 | 92 | if(pc.readable()){ |
| jonathanyost | 0:4354437a1033 | 93 | pc_data = pc.getc(); |
| jonathanyost | 6:4b1ecb3a95c5 | 94 | pc.printf("PC: %c", pc_data); |
| jonathanyost | 5:ad41288051e7 | 95 | if(xb.writeable()){xb.printf("%c", pc_data);} |
| jonathanyost | 0:4354437a1033 | 96 | } |
| jonathanyost | 6:4b1ecb3a95c5 | 97 | |
| jonathanyost | 7:6ee530637c90 | 98 | // testing out using getc() instead of gets() |
| jonathanyost | 9:e619711e05be | 99 | |
| jonathanyost | 7:6ee530637c90 | 100 | if(xb.readable()){ |
| jonathanyost | 7:6ee530637c90 | 101 | wait(0.01); |
| jonathanyost | 10:1296cc169c60 | 102 | for(int i = 0; i < sizeof(buffer); i++){ |
| jonathanyost | 10:1296cc169c60 | 103 | buffer[i] = xb.getc(); |
| jonathanyost | 10:1296cc169c60 | 104 | pc.printf("%c", buffer[i]); |
| jonathanyost | 7:6ee530637c90 | 105 | } |
| jonathanyost | 10:1296cc169c60 | 106 | |
| jonathanyost | 7:6ee530637c90 | 107 | pc.printf("\r\n"); |
| jonathanyost | 9:e619711e05be | 108 | if(buffer[4] == nodeID){ |
| jonathanyost | 6:4b1ecb3a95c5 | 109 | send_enable = true; |
| jonathanyost | 7:6ee530637c90 | 110 | } |
| jonathanyost | 9:e619711e05be | 111 | |
| jonathanyost | 9:e619711e05be | 112 | if(buffer[9] == '1'){ |
| jonathanyost | 9:e619711e05be | 113 | pir_enable = true; |
| jonathanyost | 9:e619711e05be | 114 | pc.printf("pir_enable = true\r\n"); |
| jonathanyost | 9:e619711e05be | 115 | } else if (buffer[9] == '0'){ |
| jonathanyost | 9:e619711e05be | 116 | pir_enable = false; |
| jonathanyost | 9:e619711e05be | 117 | pc.printf("pir_enable = false\r\n"); |
| jonathanyost | 9:e619711e05be | 118 | } |
| jonathanyost | 9:e619711e05be | 119 | pc.printf("RECV: "); |
| jonathanyost | 8:1703f45edfa2 | 120 | for(int i = 0; i < sizeof(buffer); i++){ |
| jonathanyost | 8:1703f45edfa2 | 121 | pc.printf("%i,", buffer[i]); |
| jonathanyost | 8:1703f45edfa2 | 122 | } |
| jonathanyost | 9:e619711e05be | 123 | |
| jonathanyost | 8:1703f45edfa2 | 124 | pc.printf("\r\n"); |
| jonathanyost | 5:ad41288051e7 | 125 | memset(buffer, '\0', sizeof(buffer)); |
| jonathanyost | 1:423c669d0306 | 126 | } |
| jonathanyost | 1:423c669d0306 | 127 | |
| jonathanyost | 3:51b128605eaa | 128 | // Take in new measurements |
| jonathanyost | 5:ad41288051e7 | 129 | // Read in values from the BME280 board |
| jonathanyost | 3:51b128605eaa | 130 | temp = thp_sensor->getTemperature(); |
| jonathanyost | 3:51b128605eaa | 131 | pressure = thp_sensor->getPressure(); |
| jonathanyost | 3:51b128605eaa | 132 | humidity = thp_sensor->getHumidity(); |
| jonathanyost | 3:51b128605eaa | 133 | |
| jonathanyost | 3:51b128605eaa | 134 | //Sensor data from the SI1145 board |
| jonathanyost | 3:51b128605eaa | 135 | uv = uiv_sensor->getUV(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 136 | ir = uiv_sensor->getIR(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 137 | vis = uiv_sensor->getVIS(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 138 | |
| jonathanyost | 3:51b128605eaa | 139 | noise = 404; |
| jonathanyost | 9:e619711e05be | 140 | |
| jonathanyost | 9:e619711e05be | 141 | // Transmit over the xbee if possible and necessary |
| jonathanyost | 9:e619711e05be | 142 | if(xb.writeable() && send_enable){ |
| jonathanyost | 9:e619711e05be | 143 | SendSensorData(temp, pressure, humidity, uv, ir, vis, noise); |
| jonathanyost | 9:e619711e05be | 144 | send_enable = false; |
| jonathanyost | 9:e619711e05be | 145 | } |
| jonathanyost | 9:e619711e05be | 146 | |
| jonathanyost | 0:4354437a1033 | 147 | } |
| jonathanyost | 0:4354437a1033 | 148 | } |
| jonathanyost | 0:4354437a1033 | 149 |