Arrow Intern IoT / Mbed 2 deprecated ENV_NODE_REQUEST_POC

Dependencies:   mbed BME280 SI1145

Committer:
jonathanyost
Date:
Thu Jul 06 15:04:11 2017 +0000
Revision:
9:e619711e05be
Parent:
8:1703f45edfa2
Child:
10:1296cc169c60
PIR and getc() read method not playing together nicely.

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 9:e619711e05be 34 bool pir_enable = true;
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 9:e619711e05be 45 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 46 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 47 }
jonathanyost 0:4354437a1033 48
jonathanyost 4:8c15903a5581 49 void SendPirFlag(){
jonathanyost 9:e619711e05be 50 if(1){
jonathanyost 9:e619711e05be 51 xb.printf("ni:%c,mo:1\r\n",nodeID);;
jonathanyost 9:e619711e05be 52 }
jonathanyost 3:51b128605eaa 53 }
jonathanyost 3:51b128605eaa 54
jonathanyost 4:8c15903a5581 55 // PIR interrupt. Set the pir_trigger to true when interrupted.
jonathanyost 4:8c15903a5581 56 void PIR(void){
jonathanyost 9:e619711e05be 57 if(xb.writeable()){
jonathanyost 9:e619711e05be 58 SendPirFlag();
jonathanyost 9:e619711e05be 59 }
jonathanyost 4:8c15903a5581 60 pir_trigger = true;
jonathanyost 0:4354437a1033 61 }
jonathanyost 1:423c669d0306 62
jonathanyost 9:e619711e05be 63 void initialize_serial_connections(){
jonathanyost 9:e619711e05be 64 xb.baud(57600);
jonathanyost 9:e619711e05be 65 pc.baud(115200);
jonathanyost 9:e619711e05be 66 }
jonathanyost 9:e619711e05be 67
jonathanyost 0:4354437a1033 68 int main() {
jonathanyost 9:e619711e05be 69 initialize_serial_connections();
jonathanyost 9:e619711e05be 70
jonathanyost 8:1703f45edfa2 71 pc.printf("\n\rMic test 1-2, 1-2\n\r");
jonathanyost 9:e619711e05be 72
jonathanyost 4:8c15903a5581 73 // Attach the PIR function to interrupt
jonathanyost 9:e619711e05be 74 MotionDetect.rise(&PIR);
jonathanyost 4:8c15903a5581 75
jonathanyost 0:4354437a1033 76 char pc_data = 'e';
jonathanyost 9:e619711e05be 77 char buffer[10];
jonathanyost 7:6ee530637c90 78
jonathanyost 2:2bdf15b94f18 79 int temp = 1;
jonathanyost 2:2bdf15b94f18 80 int pressure = 2;
jonathanyost 2:2bdf15b94f18 81 int humidity = 3;
jonathanyost 2:2bdf15b94f18 82 int uv = 5;
jonathanyost 2:2bdf15b94f18 83 int ir = 6;
jonathanyost 2:2bdf15b94f18 84 int vis = 7;
jonathanyost 2:2bdf15b94f18 85 int noise = 8;
jonathanyost 2:2bdf15b94f18 86
jonathanyost 2:2bdf15b94f18 87 bool send_enable = false;
jonathanyost 8:1703f45edfa2 88
jonathanyost 0:4354437a1033 89 while(true){
jonathanyost 3:51b128605eaa 90 // Debug Controls Input
jonathanyost 0:4354437a1033 91 if(pc.readable()){
jonathanyost 0:4354437a1033 92 pc_data = pc.getc();
jonathanyost 6:4b1ecb3a95c5 93 pc.printf("PC: %c", pc_data);
jonathanyost 5:ad41288051e7 94 if(xb.writeable()){xb.printf("%c", pc_data);}
jonathanyost 0:4354437a1033 95 }
jonathanyost 6:4b1ecb3a95c5 96
jonathanyost 4:8c15903a5581 97 if(pir_trigger){
jonathanyost 5:ad41288051e7 98 // DEBUG
jonathanyost 6:4b1ecb3a95c5 99 if(xb.writeable()){
jonathanyost 6:4b1ecb3a95c5 100 SendPirFlag();
jonathanyost 6:4b1ecb3a95c5 101 pir_trigger = false;
jonathanyost 6:4b1ecb3a95c5 102 pc.printf("PIR FLAG \r\n");
jonathanyost 6:4b1ecb3a95c5 103 }
jonathanyost 4:8c15903a5581 104 }
jonathanyost 6:4b1ecb3a95c5 105
jonathanyost 7:6ee530637c90 106 // testing out using getc() instead of gets()
jonathanyost 9:e619711e05be 107
jonathanyost 7:6ee530637c90 108 if(xb.readable()){
jonathanyost 7:6ee530637c90 109 wait(0.01);
jonathanyost 9:e619711e05be 110 char temp = xb.getc();
jonathanyost 9:e619711e05be 111 if(temp == 'i' || temp == '\0'){
jonathanyost 9:e619711e05be 112 //if(xb.readable()){
jonathanyost 9:e619711e05be 113 if(1){
jonathanyost 9:e619711e05be 114 for(int i = 1; i < sizeof(buffer); i++){
jonathanyost 9:e619711e05be 115 buffer[i] = xb.getc();
jonathanyost 9:e619711e05be 116 }
jonathanyost 9:e619711e05be 117 }
jonathanyost 7:6ee530637c90 118 }
jonathanyost 9:e619711e05be 119
jonathanyost 9:e619711e05be 120
jonathanyost 7:6ee530637c90 121
jonathanyost 7:6ee530637c90 122 pc.printf("\r\n");
jonathanyost 9:e619711e05be 123 if(buffer[4] == nodeID){
jonathanyost 6:4b1ecb3a95c5 124 send_enable = true;
jonathanyost 7:6ee530637c90 125 }
jonathanyost 9:e619711e05be 126
jonathanyost 9:e619711e05be 127 if(buffer[9] == '1'){
jonathanyost 9:e619711e05be 128 pir_enable = true;
jonathanyost 9:e619711e05be 129 pc.printf("pir_enable = true\r\n");
jonathanyost 9:e619711e05be 130 } else if (buffer[9] == '0'){
jonathanyost 9:e619711e05be 131 pir_enable = false;
jonathanyost 9:e619711e05be 132 pc.printf("pir_enable = false\r\n");
jonathanyost 9:e619711e05be 133 }
jonathanyost 9:e619711e05be 134 pc.printf("RECV: ");
jonathanyost 8:1703f45edfa2 135 for(int i = 0; i < sizeof(buffer); i++){
jonathanyost 8:1703f45edfa2 136 pc.printf("%i,", buffer[i]);
jonathanyost 8:1703f45edfa2 137 }
jonathanyost 9:e619711e05be 138
jonathanyost 8:1703f45edfa2 139 pc.printf("\r\n");
jonathanyost 5:ad41288051e7 140 memset(buffer, '\0', sizeof(buffer));
jonathanyost 1:423c669d0306 141 }
jonathanyost 1:423c669d0306 142
jonathanyost 3:51b128605eaa 143 // Take in new measurements
jonathanyost 5:ad41288051e7 144 // Read in values from the BME280 board
jonathanyost 3:51b128605eaa 145 temp = thp_sensor->getTemperature();
jonathanyost 3:51b128605eaa 146 pressure = thp_sensor->getPressure();
jonathanyost 3:51b128605eaa 147 humidity = thp_sensor->getHumidity();
jonathanyost 3:51b128605eaa 148
jonathanyost 3:51b128605eaa 149 //Sensor data from the SI1145 board
jonathanyost 3:51b128605eaa 150 uv = uiv_sensor->getUV(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 151 ir = uiv_sensor->getIR(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 152 vis = uiv_sensor->getVIS(); // Reads from the SI1145
jonathanyost 3:51b128605eaa 153
jonathanyost 3:51b128605eaa 154 noise = 404;
jonathanyost 9:e619711e05be 155
jonathanyost 9:e619711e05be 156 // Transmit over the xbee if possible and necessary
jonathanyost 9:e619711e05be 157 if(xb.writeable() && send_enable){
jonathanyost 9:e619711e05be 158 SendSensorData(temp, pressure, humidity, uv, ir, vis, noise);
jonathanyost 9:e619711e05be 159 send_enable = false;
jonathanyost 9:e619711e05be 160 }
jonathanyost 9:e619711e05be 161
jonathanyost 0:4354437a1033 162 }
jonathanyost 0:4354437a1033 163 }
jonathanyost 0:4354437a1033 164