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@7:6ee530637c90, 2017-07-05 (annotated)
- Committer:
- jonathanyost
- Date:
- Wed Jul 05 23:58:11 2017 +0000
- Revision:
- 7:6ee530637c90
- Parent:
- 6:4b1ecb3a95c5
- Child:
- 8:1703f45edfa2
https://media.giphy.com/media/UsYE7dqM2oq52/giphy.gif
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 | 0:4354437a1033 | 19 | |
| jonathanyost | 3:51b128605eaa | 20 | // Define Constants / Macros |
| jonathanyost | 0:4354437a1033 | 21 | #define node_id (0x01) |
| jonathanyost | 0:4354437a1033 | 22 | #define tx (PA_9) |
| jonathanyost | 0:4354437a1033 | 23 | #define rx (PA_10) |
| jonathanyost | 0:4354437a1033 | 24 | |
| jonathanyost | 2:2bdf15b94f18 | 25 | const char nodeID = '1'; |
| jonathanyost | 5:ad41288051e7 | 26 | const int node_id_test = 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 | 4:8c15903a5581 | 35 | bool pir_trigger = false; |
| jonathanyost | 4:8c15903a5581 | 36 | |
| jonathanyost | 3:51b128605eaa | 37 | // Declare Serial Interfaces |
| jonathanyost | 0:4354437a1033 | 38 | Serial xb(tx, rx); |
| jonathanyost | 0:4354437a1033 | 39 | Serial pc(D1, D0); |
| jonathanyost | 0:4354437a1033 | 40 | |
| jonathanyost | 4:8c15903a5581 | 41 | // send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise); |
| jonathanyost | 4:8c15903a5581 | 42 | // "id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8" |
| jonathanyost | 5:ad41288051e7 | 43 | void SendSensorData(int t, int p, int h, int u, int ir, int v, int n){ |
| jonathanyost | 5:ad41288051e7 | 44 | //" id:2, te:%2,pr:%3,uv:%5,ir:%6,vi:%7,no:%8" |
| jonathanyost | 5:ad41288051e7 | 45 | xb.printf( ",ni:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",node_id_test,t,p,h,u,ir,v,n); |
| jonathanyost | 5:ad41288051e7 | 46 | pc.printf("send: id:%d,te:%d,pr:%d,hu:%d,uv:%d,ir:%d,vi:%d,no:%d\n\r",node_id_test,t,p,h,u,ir,v,n); |
| jonathanyost | 1:423c669d0306 | 47 | } |
| jonathanyost | 0:4354437a1033 | 48 | |
| jonathanyost | 4:8c15903a5581 | 49 | void SendPirFlag(){ |
| jonathanyost | 6:4b1ecb3a95c5 | 50 | xb.printf("ni:%c,mo:1\r\n",nodeID); |
| jonathanyost | 3:51b128605eaa | 51 | } |
| jonathanyost | 3:51b128605eaa | 52 | |
| jonathanyost | 4:8c15903a5581 | 53 | // PIR interrupt. Set the pir_trigger to true when interrupted. |
| jonathanyost | 4:8c15903a5581 | 54 | void PIR(void){ |
| jonathanyost | 6:4b1ecb3a95c5 | 55 | |
| jonathanyost | 4:8c15903a5581 | 56 | pir_trigger = true; |
| jonathanyost | 0:4354437a1033 | 57 | } |
| jonathanyost | 1:423c669d0306 | 58 | |
| jonathanyost | 0:4354437a1033 | 59 | int main() { |
| jonathanyost | 1:423c669d0306 | 60 | pc.printf("echo!!\n\r"); |
| jonathanyost | 4:8c15903a5581 | 61 | |
| jonathanyost | 4:8c15903a5581 | 62 | // Attach the PIR function to interrupt |
| jonathanyost | 5:ad41288051e7 | 63 | //MotionDetect.rise(&PIR); |
| jonathanyost | 4:8c15903a5581 | 64 | |
| jonathanyost | 0:4354437a1033 | 65 | char pc_data = 'e'; |
| jonathanyost | 7:6ee530637c90 | 66 | char buffer[5]; |
| jonathanyost | 7:6ee530637c90 | 67 | |
| jonathanyost | 2:2bdf15b94f18 | 68 | int temp = 1; |
| jonathanyost | 2:2bdf15b94f18 | 69 | int pressure = 2; |
| jonathanyost | 2:2bdf15b94f18 | 70 | int humidity = 3; |
| jonathanyost | 2:2bdf15b94f18 | 71 | int uv = 5; |
| jonathanyost | 2:2bdf15b94f18 | 72 | int ir = 6; |
| jonathanyost | 2:2bdf15b94f18 | 73 | int vis = 7; |
| jonathanyost | 2:2bdf15b94f18 | 74 | int noise = 8; |
| jonathanyost | 2:2bdf15b94f18 | 75 | |
| jonathanyost | 2:2bdf15b94f18 | 76 | bool send_enable = false; |
| jonathanyost | 1:423c669d0306 | 77 | |
| jonathanyost | 0:4354437a1033 | 78 | while(true){ |
| jonathanyost | 3:51b128605eaa | 79 | // Debug Controls Input |
| jonathanyost | 0:4354437a1033 | 80 | if(pc.readable()){ |
| jonathanyost | 0:4354437a1033 | 81 | pc_data = pc.getc(); |
| jonathanyost | 6:4b1ecb3a95c5 | 82 | pc.printf("PC: %c", pc_data); |
| jonathanyost | 5:ad41288051e7 | 83 | if(xb.writeable()){xb.printf("%c", pc_data);} |
| jonathanyost | 0:4354437a1033 | 84 | } |
| jonathanyost | 6:4b1ecb3a95c5 | 85 | |
| jonathanyost | 4:8c15903a5581 | 86 | if(pir_trigger){ |
| jonathanyost | 5:ad41288051e7 | 87 | // DEBUG |
| jonathanyost | 5:ad41288051e7 | 88 | //pc.printf("pir_trigger TRUE\r\n"); |
| jonathanyost | 6:4b1ecb3a95c5 | 89 | if(xb.writeable()){ |
| jonathanyost | 6:4b1ecb3a95c5 | 90 | SendPirFlag(); |
| jonathanyost | 6:4b1ecb3a95c5 | 91 | pir_trigger = false; |
| jonathanyost | 6:4b1ecb3a95c5 | 92 | pc.printf("PIR FLAG \r\n"); |
| jonathanyost | 6:4b1ecb3a95c5 | 93 | } |
| jonathanyost | 4:8c15903a5581 | 94 | } |
| jonathanyost | 6:4b1ecb3a95c5 | 95 | |
| jonathanyost | 4:8c15903a5581 | 96 | |
| jonathanyost | 3:51b128605eaa | 97 | //Read in data from XBee |
| jonathanyost | 7:6ee530637c90 | 98 | /* |
| jonathanyost | 0:4354437a1033 | 99 | if(xb.readable()){ |
| jonathanyost | 6:4b1ecb3a95c5 | 100 | wait(0.01); |
| jonathanyost | 6:4b1ecb3a95c5 | 101 | xb.gets(buffer, 6); |
| jonathanyost | 5:ad41288051e7 | 102 | pc.printf("recv: %s\n\r", buffer); |
| jonathanyost | 2:2bdf15b94f18 | 103 | |
| jonathanyost | 6:4b1ecb3a95c5 | 104 | if((buffer[4] == nodeID) || (buffer[3] == nodeID) || (buffer[2] == nodeID) || (buffer[1] == nodeID)){ |
| jonathanyost | 2:2bdf15b94f18 | 105 | send_enable = true; |
| jonathanyost | 2:2bdf15b94f18 | 106 | } |
| jonathanyost | 7:6ee530637c90 | 107 | memset(buffer, '\0', sizeof(buffer)); |
| jonathanyost | 7:6ee530637c90 | 108 | }*/ |
| jonathanyost | 7:6ee530637c90 | 109 | |
| jonathanyost | 7:6ee530637c90 | 110 | |
| jonathanyost | 7:6ee530637c90 | 111 | // testing out using getc() instead of gets() |
| jonathanyost | 7:6ee530637c90 | 112 | if(xb.readable()){ |
| jonathanyost | 7:6ee530637c90 | 113 | wait(0.01); |
| jonathanyost | 7:6ee530637c90 | 114 | for(int i = 0; i < 5; i++){ |
| jonathanyost | 7:6ee530637c90 | 115 | buffer[i] = xb.getc(); |
| jonathanyost | 7:6ee530637c90 | 116 | pc.printf("%c",buffer[i]); |
| jonathanyost | 7:6ee530637c90 | 117 | } |
| jonathanyost | 7:6ee530637c90 | 118 | |
| jonathanyost | 7:6ee530637c90 | 119 | pc.printf("\r\n"); |
| jonathanyost | 7:6ee530637c90 | 120 | if((buffer[4] == nodeID)){ |
| jonathanyost | 6:4b1ecb3a95c5 | 121 | send_enable = true; |
| jonathanyost | 7:6ee530637c90 | 122 | } |
| jonathanyost | 5:ad41288051e7 | 123 | memset(buffer, '\0', sizeof(buffer)); |
| jonathanyost | 1:423c669d0306 | 124 | } |
| jonathanyost | 1:423c669d0306 | 125 | |
| jonathanyost | 2:2bdf15b94f18 | 126 | if(xb.writeable() && send_enable){ |
| jonathanyost | 5:ad41288051e7 | 127 | SendSensorData(temp, pressure, humidity, uv, ir, vis, noise); |
| jonathanyost | 2:2bdf15b94f18 | 128 | send_enable = false; |
| jonathanyost | 0:4354437a1033 | 129 | } |
| jonathanyost | 3:51b128605eaa | 130 | |
| jonathanyost | 3:51b128605eaa | 131 | // Take in new measurements |
| jonathanyost | 5:ad41288051e7 | 132 | // Read in values from the BME280 board |
| jonathanyost | 3:51b128605eaa | 133 | temp = thp_sensor->getTemperature(); |
| jonathanyost | 3:51b128605eaa | 134 | pressure = thp_sensor->getPressure(); |
| jonathanyost | 3:51b128605eaa | 135 | humidity = thp_sensor->getHumidity(); |
| jonathanyost | 3:51b128605eaa | 136 | |
| jonathanyost | 3:51b128605eaa | 137 | //Sensor data from the SI1145 board |
| jonathanyost | 3:51b128605eaa | 138 | uv = uiv_sensor->getUV(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 139 | ir = uiv_sensor->getIR(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 140 | vis = uiv_sensor->getVIS(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 141 | |
| jonathanyost | 3:51b128605eaa | 142 | noise = 404; |
| jonathanyost | 0:4354437a1033 | 143 | } |
| jonathanyost | 0:4354437a1033 | 144 | } |
| jonathanyost | 0:4354437a1033 | 145 |