#include <TinyGPSplus.h>
#include <SoftwareSerial.h>
//GPS stuff
const byte gps_RX = 7, gps_TX = 6;
TinyGPSPlus gps;
SoftwareSerial NEO6M(gps_RX, gps_TX);
//Sensor Stuff
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const byte motionPin = 13;
const byte buzzPin = 12;
//button stuff
const byte button_Vin = 9;
const byte button_Vout = 11;
//bluetooth stuff
const byte bt_RX = 2, bt_TX = 3;
const byte bt_Vin = 4;
SoftwareSerial BTSerial(bt_RX, bt_TX);
//pressure resistor stuff
const byte pressurePin_Vin = 8;
void setup() {
Serial.begin(9600);// put your setup code here, to run once:
NEO6M.begin(9600);
BTSerial.begin(9600);
//thermistor is connected to A0
pinMode(motionPin, INPUT_PULLUP); //motion sensor
pinMode(buzzPin, OUTPUT); //Buzzer
pinMode(button_Vin, OUTPUT);
pinMode(button_Vout, INPUT_PULLUP);
//read pressure with pin A1;
pinMode(pressurePin_Vin, OUTPUT);
pinMode(bt_Vin, OUTPUT);
digitalWrite(bt_Vin, HIGH); //powers bluetooth
}
void loop() {
int analogValue = analogRead(A0); //thermistor uses analog pins
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
int motion = digitalRead(motionPin);
int pressure = analogRead(A1);
int buzzTime = 250; //buzzer rhythm
static int trackingMode = 0; //remembers if someone was in the backseat
static int trackingTime = 150;
static String msg_to_bt = "";
static String msg_from_bt = "";
static String atm = "";
static byte dByte = "";
if (digitalRead(button_Vout) == 0) {
Serial.println("Button Test Successful");
BTSerial.write("->Hey!");
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
}
//GPS location and time
while (NEO6M.available() > 0){
if (gps.encode(NEO6M.read())) displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
}
//---------------------
//pressure sensor
Serial.print(pressure);
Serial.print(" ");
//motion sensor
Serial.print(motion);
Serial.print(" ");
//thermistor
Serial.print(celsius);
Serial.print(" ");
if (motion == 1 || pressure < 15){
trackingMode = 1; //someone is in the backseat
trackingTime = 10; //memory of someone in backseat
}
if (trackingMode == 1) {
Serial.print("Passenger?");
Serial.print(" ");
}
//alert siren for heat stroke or hypothermia threat
if (trackingMode == 1 && (celsius >= 37.8 || celsius <= 0)) {
Serial.print("DANGER!");
Serial.print(" ");
BTSerial.write("HELP!! ");
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
} else delay(buzzTime); //delay compensates for buzzer usage
Serial.println();
if (trackingMode == 1) trackingTime--; //passenger memory fades
if (trackingTime == 0) trackingMode = 0;
//command line -> bluetooth module
msg_to_bt = "";
while(Serial.available()){
dByte = Serial.read();
if (dByte >= 97 && dByte <= 122) dByte -= 32; //capitalizes message
Serial.print((char)dByte);
if (dByte > 31 && dByte < 127) msg_to_bt += (char)dByte; //only stores printing bytes
}
if (msg_to_bt != "") BTSerial.println(msg_to_bt);
//since HC-06 is always in slave mode when paired, Arduino must emulate AT functions
if(msg_to_bt == "AT") BTSerial.write(" OK\n");
if(msg_to_bt == "AT+NAME") BTSerial.write(" Backseat Guardian\n");
if(msg_to_bt == "AT+VERSION") BTSerial.write(" LINVORV1.8\n");
if(msg_to_bt == "AT+BAUD") BTSerial.write(" 9600\n");
//bluetooth module -> serial monitor
msg_from_bt = "";
while(BTSerial.available()){
dByte = BTSerial.read();
if (dByte >= 97 && dByte <=122) dByte -= 32; //capitalizes message
Serial.print((char)dByte);
if (dByte > 31 && dByte < 127) msg_from_bt += (char)dByte; //only stores printing bytes
}
atm = "";
delay(100);
atm += msg_from_bt[0];
atm += msg_from_bt[1];
atm += msg_from_bt[2];
if (msg_from_bt != "") {
//since HC-06 is always in slave mode when paired, Arduino must emulate AT functions
if (atm == "AT+") {
if(msg_from_bt == "AT+NAME") BTSerial.write("+NAME:Backseat Guardian\n"); //"AT+NAME"
if(msg_from_bt == "AT+VERSION") BTSerial.write("LINVORV1.8\n"); //"AT+VERSION"
if(msg_from_bt == "AT+BAUD") BTSerial.write("9600\n"); //"AT+BAUD"
Serial.print(".");
}
if(msg_from_bt == "AT") BTSerial.write("OK\n"); //"AT"
if(msg_from_bt == "HELLO") BTSerial.write("Welcome!\n");
if(msg_from_bt == "LOCATION") BTSerial.write("I'm right here: \n");
if(msg_from_bt == "BYE") {
digitalWrite(bt_Vin, LOW); //disconnects bluetooth
delay(1000);
BTSerial.read();
digitalWrite(bt_Vin, HIGH);
delay(1000);
}
}
}
//--------end of the main loop----------
void displayInfo(){
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
} else Serial.print(F("INVALID"));
Serial.print(F(" Date "));
if (gps.date.isValid()) {
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else Serial.print(F("INVALID"));
Serial.println();
}