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
- Committer:
- jonathanyost
- Date:
- 2017-07-05
- Revision:
- 6:4b1ecb3a95c5
- Parent:
- 5:ad41288051e7
- Child:
- 7:6ee530637c90
File content as of revision 6:4b1ecb3a95c5:
/*
Author: Jonathan Yost
Title: END_NODE_REQUEST_POC
Date: 6/28/17
Desc: Reads in data from the sensors and sends to the gateway.
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"
#include "SI1145.h"
// Define Constants / Macros
#define node_id (0x01)
#define tx (PA_9)
#define rx (PA_10)
const char nodeID = '1';
const int node_id_test = 1;
// I/O Delarations
I2C i2c(I2C_SDA, I2C_SCL); // D14, D15
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"
void SendSensorData(int t, int p, int h, int u, int ir, int v, int n){
//" id:2, te:%2,pr:%3,uv:%5,ir:%6,vi:%7,no:%8"
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);
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);
}
void SendPirFlag(){
xb.printf("ni:%c,mo:1\r\n",nodeID);
}
// PIR interrupt. Set the pir_trigger to true when interrupted.
void PIR(void){
pir_trigger = true;
}
int main() {
pc.printf("echo!!\n\r");
// Attach the PIR function to interrupt
//MotionDetect.rise(&PIR);
char pc_data = 'e';
char buffer[6];
int temp = 1;
int pressure = 2;
int humidity = 3;
int uv = 5;
int ir = 6;
int vis = 7;
int noise = 8;
bool send_enable = false;
while(true){
// Debug Controls Input
if(pc.readable()){
pc_data = pc.getc();
pc.printf("PC: %c", pc_data);
if(xb.writeable()){xb.printf("%c", pc_data);}
}
if(pir_trigger){
// DEBUG
//pc.printf("pir_trigger TRUE\r\n");
if(xb.writeable()){
SendPirFlag();
pir_trigger = false;
pc.printf("PIR FLAG \r\n");
}
}
//Read in data from XBee
if(xb.readable()){
wait(0.01);
xb.gets(buffer, 6);
pc.printf("recv: %s\n\r", buffer);
if((buffer[4] == nodeID) || (buffer[3] == nodeID) || (buffer[2] == nodeID) || (buffer[1] == nodeID)){
send_enable = true;
}
pc.printf("test after check");
// TODO: CODE WORKS IF WE SEND IT '1111', BUT NOT IF WE SEND 'ID:1'
// TODO: unf*** the code
/*
if((buffer[3] == nodeID)){
send_enable = true;
}*/
memset(buffer, '\0', sizeof(buffer));
}
if(xb.writeable() && send_enable){
SendSensorData(temp, pressure, humidity, uv, ir, vis, noise);
send_enable = false;
}
// Take in new measurements
// Read in values from the BME280 board
temp = thp_sensor->getTemperature();
pressure = thp_sensor->getPressure();
humidity = thp_sensor->getHumidity();
//Sensor data from the SI1145 board
uv = uiv_sensor->getUV(); // Reads from the SI1145
ir = uiv_sensor->getIR(); // Reads from the SI1145
vis = uiv_sensor->getVIS(); // Reads from the SI1145
noise = 404;
}
}