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@3:51b128605eaa, 2017-06-28 (annotated)
- Committer:
- jonathanyost
- Date:
- Wed Jun 28 21:05:29 2017 +0000
- Revision:
- 3:51b128605eaa
- Parent:
- 2:2bdf15b94f18
- Child:
- 4:8c15903a5581
BME280 and SI1145 working.; TODO: Get PIR working
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 | 3:51b128605eaa | 10 | // Libraries |
| jonathanyost | 0:4354437a1033 | 11 | #include "mbed.h" |
| jonathanyost | 3:51b128605eaa | 12 | #include "BME280.h" |
| jonathanyost | 3:51b128605eaa | 13 | #include "SI1145.h" |
| jonathanyost | 0:4354437a1033 | 14 | |
| jonathanyost | 3:51b128605eaa | 15 | // Define Constants / Macros |
| jonathanyost | 0:4354437a1033 | 16 | #define node_id (0x01) |
| jonathanyost | 0:4354437a1033 | 17 | #define tx (PA_9) |
| jonathanyost | 0:4354437a1033 | 18 | #define rx (PA_10) |
| jonathanyost | 0:4354437a1033 | 19 | |
| jonathanyost | 3:51b128605eaa | 20 | |
| jonathanyost | 2:2bdf15b94f18 | 21 | const char nodeID = '1'; |
| jonathanyost | 1:423c669d0306 | 22 | |
| jonathanyost | 3:51b128605eaa | 23 | // I/O Delarations |
| jonathanyost | 3:51b128605eaa | 24 | I2C i2c(I2C_SDA, I2C_SCL); // D14, D15 |
| jonathanyost | 3:51b128605eaa | 25 | BME280 *thp_sensor = new BME280(i2c); |
| jonathanyost | 3:51b128605eaa | 26 | SI1145 *uiv_sensor = new SI1145(i2c); |
| jonathanyost | 3:51b128605eaa | 27 | |
| jonathanyost | 3:51b128605eaa | 28 | // Declare Serial Interfaces |
| jonathanyost | 0:4354437a1033 | 29 | Serial xb(tx, rx); |
| jonathanyost | 0:4354437a1033 | 30 | Serial pc(D1, D0); |
| jonathanyost | 0:4354437a1033 | 31 | |
| jonathanyost | 1:423c669d0306 | 32 | //send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise); |
| jonathanyost | 1:423c669d0306 | 33 | //"id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8" |
| jonathanyost | 3:51b128605eaa | 34 | void SendSensorData(int t, int p, int h, int m, int u, int ir, int v, int n){ |
| jonathanyost | 1:423c669d0306 | 35 | //" id:2, te:%2,pr:%3,mo:%4,uv:%5,ir:%6,vi:%7,no:%8" |
| jonathanyost | 3:51b128605eaa | 36 | xb.printf("id:%c,te:%d,pr:%d,hu:%d,mo:%d,uv:%d,ir:%d,vi:%d,no:%d\r\n",nodeID,t,p,h,m,u,ir,v,n); |
| jonathanyost | 3:51b128605eaa | 37 | pc.printf("send: id:%c,te:%d,pr:%d,hu:%d,mo:%d,uv:%d,ir:%d,vi:%d,no:%d\r\n",nodeID,t,p,h,m,u,ir,v,n); |
| jonathanyost | 1:423c669d0306 | 38 | } |
| jonathanyost | 0:4354437a1033 | 39 | |
| jonathanyost | 3:51b128605eaa | 40 | void SendPirData(int pir){ |
| jonathanyost | 3:51b128605eaa | 41 | xb.printf("id:%c,mo:%d",nodeID,pir); |
| jonathanyost | 3:51b128605eaa | 42 | } |
| jonathanyost | 3:51b128605eaa | 43 | |
| jonathanyost | 3:51b128605eaa | 44 | float GetTemp(){ |
| jonathanyost | 3:51b128605eaa | 45 | // Returns the temp value read from the BME280 sensor board |
| jonathanyost | 3:51b128605eaa | 46 | return thp_sensor->getTemperature(); |
| jonathanyost | 0:4354437a1033 | 47 | } |
| jonathanyost | 1:423c669d0306 | 48 | |
| jonathanyost | 0:4354437a1033 | 49 | int main() { |
| jonathanyost | 1:423c669d0306 | 50 | pc.printf("echo!!\n\r"); |
| jonathanyost | 0:4354437a1033 | 51 | // initialize read data chars |
| jonathanyost | 0:4354437a1033 | 52 | char pc_data = 'e'; |
| jonathanyost | 1:423c669d0306 | 53 | char buffer[128]; |
| jonathanyost | 0:4354437a1033 | 54 | |
| jonathanyost | 1:423c669d0306 | 55 | /* Sensor Data |
| jonathanyost | 2:2bdf15b94f18 | 56 | format: "ID:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8" |
| jonathanyost | 1:423c669d0306 | 57 | |
| jonathanyost | 1:423c669d0306 | 58 | |
| jonathanyost | 1:423c669d0306 | 59 | */ |
| jonathanyost | 0:4354437a1033 | 60 | |
| jonathanyost | 2:2bdf15b94f18 | 61 | int temp = 1; |
| jonathanyost | 2:2bdf15b94f18 | 62 | int pressure = 2; |
| jonathanyost | 2:2bdf15b94f18 | 63 | int humidity = 3; |
| jonathanyost | 2:2bdf15b94f18 | 64 | int motion = 4; |
| jonathanyost | 2:2bdf15b94f18 | 65 | int uv = 5; |
| jonathanyost | 2:2bdf15b94f18 | 66 | int ir = 6; |
| jonathanyost | 2:2bdf15b94f18 | 67 | int vis = 7; |
| jonathanyost | 2:2bdf15b94f18 | 68 | int noise = 8; |
| jonathanyost | 2:2bdf15b94f18 | 69 | |
| jonathanyost | 3:51b128605eaa | 70 | bool pir_trigger = false; |
| jonathanyost | 2:2bdf15b94f18 | 71 | bool send_enable = false; |
| jonathanyost | 1:423c669d0306 | 72 | |
| jonathanyost | 0:4354437a1033 | 73 | while(true){ |
| jonathanyost | 3:51b128605eaa | 74 | // Debug Controls Input |
| jonathanyost | 0:4354437a1033 | 75 | if(pc.readable()){ |
| jonathanyost | 0:4354437a1033 | 76 | pc_data = pc.getc(); |
| jonathanyost | 0:4354437a1033 | 77 | pc.printf("%c", pc_data); |
| jonathanyost | 0:4354437a1033 | 78 | xb.printf("%c", pc_data); |
| jonathanyost | 0:4354437a1033 | 79 | } |
| jonathanyost | 0:4354437a1033 | 80 | |
| jonathanyost | 3:51b128605eaa | 81 | //Read in data from XBee |
| jonathanyost | 0:4354437a1033 | 82 | if(xb.readable()){ |
| jonathanyost | 3:51b128605eaa | 83 | xb.gets(buffer, 6); |
| jonathanyost | 3:51b128605eaa | 84 | pc.printf("recv: %s\r", buffer); |
| jonathanyost | 2:2bdf15b94f18 | 85 | |
| jonathanyost | 3:51b128605eaa | 86 | /* TODO!!! SOMETIMES THE XBEE READS IN "i:1" instead of "id:1"... |
| jonathanyost | 3:51b128605eaa | 87 | No idea why. For now I will accept both, but that's gross. |
| jonathanyost | 3:51b128605eaa | 88 | |
| jonathanyost | 3:51b128605eaa | 89 | */ |
| jonathanyost | 2:2bdf15b94f18 | 90 | |
| jonathanyost | 3:51b128605eaa | 91 | if(buffer[3] == nodeID || buffer[2] == nodeID){ |
| jonathanyost | 2:2bdf15b94f18 | 92 | send_enable = true; |
| jonathanyost | 3:51b128605eaa | 93 | } else { |
| jonathanyost | 3:51b128605eaa | 94 | send_enable = false; |
| jonathanyost | 2:2bdf15b94f18 | 95 | } |
| jonathanyost | 1:423c669d0306 | 96 | } |
| jonathanyost | 1:423c669d0306 | 97 | |
| jonathanyost | 3:51b128605eaa | 98 | |
| jonathanyost | 3:51b128605eaa | 99 | if(xb.writeable() && pir_trigger){ |
| jonathanyost | 3:51b128605eaa | 100 | pc.printf("pir_trigger TRUE"); |
| jonathanyost | 3:51b128605eaa | 101 | } |
| jonathanyost | 3:51b128605eaa | 102 | |
| jonathanyost | 3:51b128605eaa | 103 | |
| jonathanyost | 2:2bdf15b94f18 | 104 | if(xb.writeable() && send_enable){ |
| jonathanyost | 3:51b128605eaa | 105 | SendSensorData(temp, pressure, humidity, motion, uv, ir, vis, noise); |
| jonathanyost | 2:2bdf15b94f18 | 106 | send_enable = false; |
| jonathanyost | 0:4354437a1033 | 107 | } |
| jonathanyost | 3:51b128605eaa | 108 | |
| jonathanyost | 3:51b128605eaa | 109 | // Take in new measurements |
| jonathanyost | 3:51b128605eaa | 110 | // Read in values from the BME280 board |
| jonathanyost | 3:51b128605eaa | 111 | temp = thp_sensor->getTemperature(); |
| jonathanyost | 3:51b128605eaa | 112 | pressure = thp_sensor->getPressure(); |
| jonathanyost | 3:51b128605eaa | 113 | humidity = thp_sensor->getHumidity(); |
| jonathanyost | 3:51b128605eaa | 114 | |
| jonathanyost | 3:51b128605eaa | 115 | motion = 404; // TODO: Attach this to an interrupt... |
| jonathanyost | 3:51b128605eaa | 116 | |
| jonathanyost | 3:51b128605eaa | 117 | //Sensor data from the SI1145 board |
| jonathanyost | 3:51b128605eaa | 118 | uv = uiv_sensor->getUV(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 119 | ir = uiv_sensor->getIR(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 120 | vis = uiv_sensor->getVIS(); // Reads from the SI1145 |
| jonathanyost | 3:51b128605eaa | 121 | |
| jonathanyost | 3:51b128605eaa | 122 | /* |
| jonathanyost | 3:51b128605eaa | 123 | uv = 404; |
| jonathanyost | 3:51b128605eaa | 124 | ir = 404; |
| jonathanyost | 3:51b128605eaa | 125 | vis = 404; |
| jonathanyost | 3:51b128605eaa | 126 | */ |
| jonathanyost | 3:51b128605eaa | 127 | |
| jonathanyost | 3:51b128605eaa | 128 | noise = 404; |
| jonathanyost | 0:4354437a1033 | 129 | } |
| jonathanyost | 0:4354437a1033 | 130 | } |
| jonathanyost | 0:4354437a1033 | 131 |