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
Diff: main.cpp
- Revision:
- 4:8c15903a5581
- Parent:
- 3:51b128605eaa
- Child:
- 5:ad41288051e7
--- a/main.cpp Wed Jun 28 21:05:29 2017 +0000
+++ b/main.cpp Wed Jun 28 21:52:34 2017 +0000
@@ -7,6 +7,11 @@
yeah.
*/
+// ISSUES
+/* TODO!!! SOMETIMES THE XBEE READS IN "i:1" instead of "id:1"...
+ No idea why. For now I will accept both, but that's gross.
+*/
+
// Libraries
#include "mbed.h"
#include "BME280.h"
@@ -17,7 +22,6 @@
#define tx (PA_9)
#define rx (PA_10)
-
const char nodeID = '1';
// I/O Delarations
@@ -25,39 +29,47 @@
BME280 *thp_sensor = new BME280(i2c);
SI1145 *uiv_sensor = new SI1145(i2c);
+// Declare the PIR interface
+InterruptIn MotionDetect(PA_0);
+bool pir_trigger = false;
+
// Declare Serial Interfaces
Serial xb(tx, rx);
Serial pc(D1, D0);
-//send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise);
-//"id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8"
+// send_sensor_data(temp, pressure, humidity, motion, uv, ir, vis, noise);
+// "id:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8"
void SendSensorData(int t, int p, int h, int m, int u, int ir, int v, int n){
//" id:2, te:%2,pr:%3,mo:%4,uv:%5,ir:%6,vi:%7,no:%8"
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);
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);
}
-void SendPirData(int pir){
- xb.printf("id:%c,mo:%d",nodeID,pir);
+void SendPirFlag(){
+ xb.printf("id:%c,mo:1\r\n",nodeID);
}
-float GetTemp(){
- // Returns the temp value read from the BME280 sensor board
- return thp_sensor->getTemperature();
+// PIR interrupt. Set the pir_trigger to true when interrupted.
+void PIR(void){
+ /*
+ if(pir_trigger){
+ pc.printf("DOUBLE TROUBLE\r");
+ } else {
+ pc.printf("PIR TRIGGER\r");
+ }
+ */
+ pir_trigger = true;
}
int main() {
pc.printf("echo!!\n\r");
- // initialize read data chars
+
+ // Attach the PIR function to interrupt
+ MotionDetect.rise(&PIR);
+
char pc_data = 'e';
char buffer[128];
- /* Sensor Data
- format: "ID:2,te:2,pr:3,mo:4,uv:5,ir:6,vi:7,no:8"
-
-
- */
-
int temp = 1;
int pressure = 2;
int humidity = 3;
@@ -67,7 +79,6 @@
int vis = 7;
int noise = 8;
- bool pir_trigger = false;
bool send_enable = false;
while(true){
@@ -78,15 +89,18 @@
xb.printf("%c", pc_data);
}
+ if(pir_trigger){
+ pc.printf("pir_trigger TRUE\r\n");
+ if(xb.writeable()){SendPirFlag();}
+ pir_trigger = false;
+ }
+
//Read in data from XBee
if(xb.readable()){
xb.gets(buffer, 6);
pc.printf("recv: %s\r", buffer);
- /* TODO!!! SOMETIMES THE XBEE READS IN "i:1" instead of "id:1"...
- No idea why. For now I will accept both, but that's gross.
-
- */
+
if(buffer[3] == nodeID || buffer[2] == nodeID){
send_enable = true;
@@ -95,12 +109,6 @@
}
}
-
- if(xb.writeable() && pir_trigger){
- pc.printf("pir_trigger TRUE");
- }
-
-
if(xb.writeable() && send_enable){
SendSensorData(temp, pressure, humidity, motion, uv, ir, vis, noise);
send_enable = false;
@@ -112,7 +120,11 @@
pressure = thp_sensor->getPressure();
humidity = thp_sensor->getHumidity();
- motion = 404; // TODO: Attach this to an interrupt...
+ if(pir_trigger){
+ motion = 1;
+ } else {
+ motion = 0;
+ }
//Sensor data from the SI1145 board
uv = uiv_sensor->getUV(); // Reads from the SI1145