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@8:1703f45edfa2, 2017-07-06 (annotated)
- Committer:
- jonathanyost
- Date:
- Thu Jul 06 00:27:22 2017 +0000
- Revision:
- 8:1703f45edfa2
- Parent:
- 7:6ee530637c90
- Child:
- 9:e619711e05be
gif related to the code working well. It consistently requires two requests for the first set of data, but after that it works. Not sure how to fix, but it's late and I want to food.;
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 | 8:1703f45edfa2 | 60 | pc.printf("\n\rMic test 1-2, 1-2\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 | 8:1703f45edfa2 | 66 | char buffer[6]; |
| 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 | 8:1703f45edfa2 | 77 | |
| jonathanyost | 0:4354437a1033 | 78 | while(true){ |
| jonathanyost | 3:51b128605eaa | 79 | // Debug Controls Input |
| jonathanyost | 8:1703f45edfa2 | 80 | |
| jonathanyost | 0:4354437a1033 | 81 | if(pc.readable()){ |
| jonathanyost | 0:4354437a1033 | 82 | pc_data = pc.getc(); |
| jonathanyost | 6:4b1ecb3a95c5 | 83 | pc.printf("PC: %c", pc_data); |
| jonathanyost | 5:ad41288051e7 | 84 | if(xb.writeable()){xb.printf("%c", pc_data);} |
| jonathanyost | 0:4354437a1033 | 85 | } |
| jonathanyost | 6:4b1ecb3a95c5 | 86 | |
| jonathanyost | 4:8c15903a5581 | 87 | if(pir_trigger){ |
| jonathanyost | 5:ad41288051e7 | 88 | // DEBUG |
| jonathanyost | 5:ad41288051e7 | 89 | //pc.printf("pir_trigger TRUE\r\n"); |
| jonathanyost | 6:4b1ecb3a95c5 | 90 | if(xb.writeable()){ |
| jonathanyost | 6:4b1ecb3a95c5 | 91 | SendPirFlag(); |
| jonathanyost | 6:4b1ecb3a95c5 | 92 | pir_trigger = false; |
| jonathanyost | 6:4b1ecb3a95c5 | 93 | pc.printf("PIR FLAG \r\n"); |
| jonathanyost | 6:4b1ecb3a95c5 | 94 | } |
| jonathanyost | 4:8c15903a5581 | 95 | } |
| jonathanyost | 6:4b1ecb3a95c5 | 96 | |
| jonathanyost | 7:6ee530637c90 | 97 | // testing out using getc() instead of gets() |
| jonathanyost | 7:6ee530637c90 | 98 | if(xb.readable()){ |
| jonathanyost | 7:6ee530637c90 | 99 | wait(0.01); |
| jonathanyost | 8:1703f45edfa2 | 100 | for(int i = 0; i < sizeof(buffer); i++){ |
| jonathanyost | 7:6ee530637c90 | 101 | buffer[i] = xb.getc(); |
| jonathanyost | 7:6ee530637c90 | 102 | pc.printf("%c",buffer[i]); |
| jonathanyost | 7:6ee530637c90 | 103 | } |
| jonathanyost | 7:6ee530637c90 | 104 | |
| jonathanyost | 7:6ee530637c90 | 105 | pc.printf("\r\n"); |
| jonathanyost | 7:6ee530637c90 | 106 | if((buffer[4] == nodeID)){ |
| jonathanyost | 6:4b1ecb3a95c5 | 107 | send_enable = true; |
| jonathanyost | 7:6ee530637c90 | 108 | } |
| jonathanyost | 8:1703f45edfa2 | 109 | for(int i = 0; i < sizeof(buffer); i++){ |
| jonathanyost | 8:1703f45edfa2 | 110 | pc.printf("%i,", buffer[i]); |
| jonathanyost | 8:1703f45edfa2 | 111 | } |
| jonathanyost | 8:1703f45edfa2 | 112 | pc.printf("\r\n"); |
| jonathanyost | 5:ad41288051e7 | 113 | memset(buffer, '\0', sizeof(buffer)); |
| jonathanyost | 1:423c669d0306 | 114 | } |
| jonathanyost | 1:423c669d0306 | 115 | |
| jonathanyost | 2:2bdf15b94f18 | 116 | if(xb.writeable() && send_enable){ |
| jonathanyost | 5:ad41288051e7 | 117 | SendSensorData(temp, pressure, humidity, uv, ir, vis, noise); |
| jonathanyost | 2:2bdf15b94f18 | 118 | send_enable = false; |
| jonathanyost | 0:4354437a1033 | 119 | } |
| jonathanyost | 3:51b128605eaa | 120 | |
| jonathanyost | 3:51b128605eaa | 121 | // Take in new measurements |
| jonathanyost | 5:ad41288051e7 | 122 | // Read in values from the BME280 board |
| jonathanyost | 3:51b128605eaa | 123 | temp = thp_sensor->getTemperature(); |
| jonathanyost | 3:51b128605eaa | 124 | pressure = thp_sensor->getPressure(); |
| jonathanyost | 3:51b128605eaa | 125 | humidity = thp_sensor->getHumidity(); |
| jonathanyost | 3:51b128605eaa | 126 | |
| jonathanyost | 3:51b128605eaa | 127 | //Sensor data from the SI1145 board |
| jonathanyost | 3:51b128605eaa | 128 | uv = uiv_sensor->getUV(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 129 | ir = uiv_sensor->getIR(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 130 | vis = uiv_sensor->getVIS(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 131 | |
| jonathanyost | 3:51b128605eaa | 132 | noise = 404; |
| jonathanyost | 0:4354437a1033 | 133 | } |
| jonathanyost | 0:4354437a1033 | 134 | } |
| jonathanyost | 0:4354437a1033 | 135 |