Arrow Intern IoT / Mbed 2 deprecated ENV_NODE_REQUEST_POC

Dependencies:   mbed BME280 SI1145

Committer:
jonathanyost
Date:
Wed Jun 28 21:52:34 2017 +0000
Revision:
4:8c15903a5581
Parent:
3:51b128605eaa
Child:
5:ad41288051e7
PIR, SI1145, BME280 all working

Who changed what in which revision?

UserRevisionLine numberNew 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 1:423c669d0306 26
jonathanyost 3:51b128605eaa 27 // I/O Delarations
jonathanyost 3:51b128605eaa 28 I2C i2c(I2C_SDA, I2C_SCL); // D14, D15
jonathanyost 3:51b128605eaa 29 BME280 *thp_sensor = new BME280(i2c);
jonathanyost 3:51b128605eaa 30 SI1145 *uiv_sensor = new SI1145(i2c);
jonathanyost 3:51b128605eaa 31
jonathanyost 4:8c15903a5581 32 // Declare the PIR interface
jonathanyost 4:8c15903a5581 33 InterruptIn MotionDetect(PA_0);
jonathanyost 4:8c15903a5581 34 bool pir_trigger = false;
jonathanyost 4:8c15903a5581 35
jonathanyost 3:51b128605eaa 36 // Declare Serial Interfaces
jonathanyost 0:4354437a1033 37 Serial xb(tx, rx);
jonathanyost 0:4354437a1033 38 Serial pc(D1, D0);
jonathanyost 0:4354437a1033 39
jonathanyost 4:8c15903a5581 40 // send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise);
jonathanyost 4:8c15903a5581 41 // "id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8"
jonathanyost 3:51b128605eaa 42 void SendSensorData(int t, int p, int h, int m, int u, int ir, int v, int n){
jonathanyost 1:423c669d0306 43 //" id:2, te:%2,pr:%3,mo:%4,uv:%5,ir:%6,vi:%7,no:%8"
jonathanyost 3:51b128605eaa 44 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 45 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 46 }
jonathanyost 0:4354437a1033 47
jonathanyost 4:8c15903a5581 48 void SendPirFlag(){
jonathanyost 4:8c15903a5581 49 xb.printf("id:%c,mo:1\r\n",nodeID);
jonathanyost 3:51b128605eaa 50 }
jonathanyost 3:51b128605eaa 51
jonathanyost 4:8c15903a5581 52 // PIR interrupt. Set the pir_trigger to true when interrupted.
jonathanyost 4:8c15903a5581 53 void PIR(void){
jonathanyost 4:8c15903a5581 54 /*
jonathanyost 4:8c15903a5581 55 if(pir_trigger){
jonathanyost 4:8c15903a5581 56 pc.printf("DOUBLE TROUBLE\r");
jonathanyost 4:8c15903a5581 57 } else {
jonathanyost 4:8c15903a5581 58 pc.printf("PIR TRIGGER\r");
jonathanyost 4:8c15903a5581 59 }
jonathanyost 4:8c15903a5581 60 */
jonathanyost 4:8c15903a5581 61 pir_trigger = true;
jonathanyost 0:4354437a1033 62 }
jonathanyost 1:423c669d0306 63
jonathanyost 0:4354437a1033 64 int main() {
jonathanyost 1:423c669d0306 65 pc.printf("echo!!\n\r");
jonathanyost 4:8c15903a5581 66
jonathanyost 4:8c15903a5581 67 // Attach the PIR function to interrupt
jonathanyost 4:8c15903a5581 68 MotionDetect.rise(&PIR);
jonathanyost 4:8c15903a5581 69
jonathanyost 0:4354437a1033 70 char pc_data = 'e';
jonathanyost 1:423c669d0306 71 char buffer[128];
jonathanyost 0:4354437a1033 72
jonathanyost 2:2bdf15b94f18 73 int temp = 1;
jonathanyost 2:2bdf15b94f18 74 int pressure = 2;
jonathanyost 2:2bdf15b94f18 75 int humidity = 3;
jonathanyost 2:2bdf15b94f18 76 int motion = 4;
jonathanyost 2:2bdf15b94f18 77 int uv = 5;
jonathanyost 2:2bdf15b94f18 78 int ir = 6;
jonathanyost 2:2bdf15b94f18 79 int vis = 7;
jonathanyost 2:2bdf15b94f18 80 int noise = 8;
jonathanyost 2:2bdf15b94f18 81
jonathanyost 2:2bdf15b94f18 82 bool send_enable = false;
jonathanyost 1:423c669d0306 83
jonathanyost 0:4354437a1033 84 while(true){
jonathanyost 3:51b128605eaa 85 // Debug Controls Input
jonathanyost 0:4354437a1033 86 if(pc.readable()){
jonathanyost 0:4354437a1033 87 pc_data = pc.getc();
jonathanyost 0:4354437a1033 88 pc.printf("%c", pc_data);
jonathanyost 0:4354437a1033 89 xb.printf("%c", pc_data);
jonathanyost 0:4354437a1033 90 }
jonathanyost 0:4354437a1033 91
jonathanyost 4:8c15903a5581 92 if(pir_trigger){
jonathanyost 4:8c15903a5581 93 pc.printf("pir_trigger TRUE\r\n");
jonathanyost 4:8c15903a5581 94 if(xb.writeable()){SendPirFlag();}
jonathanyost 4:8c15903a5581 95 pir_trigger = false;
jonathanyost 4:8c15903a5581 96 }
jonathanyost 4:8c15903a5581 97
jonathanyost 3:51b128605eaa 98 //Read in data from XBee
jonathanyost 0:4354437a1033 99 if(xb.readable()){
jonathanyost 3:51b128605eaa 100 xb.gets(buffer, 6);
jonathanyost 3:51b128605eaa 101 pc.printf("recv: %s\r", buffer);
jonathanyost 2:2bdf15b94f18 102
jonathanyost 4:8c15903a5581 103
jonathanyost 2:2bdf15b94f18 104
jonathanyost 3:51b128605eaa 105 if(buffer[3] == nodeID || buffer[2] == nodeID){
jonathanyost 2:2bdf15b94f18 106 send_enable = true;
jonathanyost 3:51b128605eaa 107 } else {
jonathanyost 3:51b128605eaa 108 send_enable = false;
jonathanyost 2:2bdf15b94f18 109 }
jonathanyost 1:423c669d0306 110 }
jonathanyost 1:423c669d0306 111
jonathanyost 2:2bdf15b94f18 112 if(xb.writeable() && send_enable){
jonathanyost 3:51b128605eaa 113 SendSensorData(temp, pressure, humidity, motion, uv, ir, vis, noise);
jonathanyost 2:2bdf15b94f18 114 send_enable = false;
jonathanyost 0:4354437a1033 115 }
jonathanyost 3:51b128605eaa 116
jonathanyost 3:51b128605eaa 117 // Take in new measurements
jonathanyost 3:51b128605eaa 118 // Read in values from the BME280 board
jonathanyost 3:51b128605eaa 119 temp = thp_sensor->getTemperature();
jonathanyost 3:51b128605eaa 120 pressure = thp_sensor->getPressure();
jonathanyost 3:51b128605eaa 121 humidity = thp_sensor->getHumidity();
jonathanyost 3:51b128605eaa 122
jonathanyost 4:8c15903a5581 123 if(pir_trigger){
jonathanyost 4:8c15903a5581 124 motion = 1;
jonathanyost 4:8c15903a5581 125 } else {
jonathanyost 4:8c15903a5581 126 motion = 0;
jonathanyost 4:8c15903a5581 127 }
jonathanyost 3:51b128605eaa 128
jonathanyost 3:51b128605eaa 129 //Sensor data from the SI1145 board
jonathanyost 3:51b128605eaa 130 uv = uiv_sensor->getUV(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 131 ir = uiv_sensor->getIR(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 132 vis = uiv_sensor->getVIS(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 133
jonathanyost 3:51b128605eaa 134 /*
jonathanyost 3:51b128605eaa 135 uv = 404;
jonathanyost 3:51b128605eaa 136 ir = 404;
jonathanyost 3:51b128605eaa 137 vis = 404;
jonathanyost 3:51b128605eaa 138 */
jonathanyost 3:51b128605eaa 139
jonathanyost 3:51b128605eaa 140 noise = 404;
jonathanyost 0:4354437a1033 141 }
jonathanyost 0:4354437a1033 142 }
jonathanyost 0:4354437a1033 143