//code sourced from [Bytes N Bits] & [Ahmad Logs]
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
//GPS stuff
const byte gps_RX = 7, gps_TX = 6;// TXPin must be a PWM pin
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;
//pressure resistor stuff
const byte pressurePin_Vin = 8;
//bluetooth stuff
const byte bt_Vin = 4;
const byte bt_gate = 2; //controls the dataflow of the Bluetooth
void setup() {
Serial.begin(9600); //Bluetooth module now uses Serial pins 0 and 1
NEO6M.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);
pinMode(pressurePin_Vin, OUTPUT); //reads pressure with pin A1;
pinMode(bt_Vin, OUTPUT);
digitalWrite(bt_Vin, HIGH); //powers bluetooth
pinMode(bt_gate, OUTPUT);
digitalWrite(bt_gate, HIGH);
Serial.print("The device is READY.\n");
delay(1000);
}
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);
static int buzzTime = 250; //buzzer rhythm
static byte trackingMode = 0; //remembers if someone was in the backseat
static int trackingTime = 0;
static int alertTime = 50;
//static uint16_t safeTIME;
//static float safeLAT = 29.892, safeLNG = -95.430; //Qamar's Home
//static float safeLAT = 29.722, safeLNG = -95.342; //Technology Annex
static float safeLAT[3] = {29.892, 29.722, 0.000};
static float safeLNG[3] = {-95.430, -95.342, 0.000};
static float safeRange = 0.0072;
static String msg_from_bt = "";
static String atm = "";
static byte dByte = 0;
static byte alertMode = 0;
static bool list = false;
bool theft = true; //assumes theft occurs until verified
while (NEO6M.available() > 0){
if (gps.encode(NEO6M.read())) {} //reads GPS buffer without doing anything
if (millis() > 5000 && gps.charsProcessed() < 10){
Serial.println("No GPS detected: check wiring.");
while(true);
}
}
//button
if (digitalRead(button_Vout) == 0 ) {
Serial.print("\nButton Test Successful");
displayInfo();
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
}
//bt commands test
if (msg_from_bt == "TEST") {
theftInfo();
tone(buzzPin,150,1250);
delay(buzzTime);
//tone(buzzPin,250,50);
}
//---------------------
//pressure sensor
if(list == true){
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(" \n");
} else if(list == true){
Serial.print(" \n");
}
//alert siren for heat stroke or hypothermia threat
if (trackingMode == 1 && (celsius >= 37.8 || celsius <= 10)) {
alertMode = 1;
Serial.print("DANGER!");
Serial.print(" ");
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
if (alertTime != 0) alertTime--;
}
else delay(buzzTime); //delay compensates for buzzer usage
if (trackingMode == 1) trackingTime--; //passenger memory fades
if (trackingTime == 0) trackingMode = 0;
//phone -> bluetooth -> BG
msg_from_bt = "";
while(Serial.available() > 0){
dByte = Serial.read();
if (dByte >= 97 && dByte <= 122) dByte -= 32; //capitalizes message
if (dByte > 31 && dByte < 127) msg_from_bt += (char)dByte; //only stores printing bytes
Serial.print((char)dByte);
}
if (msg_from_bt == "LOC") {
Serial.print("\n");
Serial.print(gps.location.lat(), 6);
Serial.print(", ");
Serial.print(gps.location.lng(), 6);
Serial.print("\n \n");
}
if (msg_from_bt == "LIST"){
list = !list; //flip boolean
}
if (gps.date.isValid() && msg_from_bt == "SAFE"){
readGPS();
safeLAT[2] = gps.location.lat();
safeLNG[2] = gps.location.lng();
Serial.println("Your current location is marked as SAFE for now.");
}
/*
if (gps.date.isValid()){
readGPS();
if ((safeRange < abs(safeLAT[0] - gps.location.lat())) || (safeRange < abs(safeLNG[0] - gps.location.lng()))){
readGPS();
if ((safeRange < abs(safeLAT[1] - gps.location.lat())) || (safeRange < abs(safeLNG[1] - gps.location.lng()))){
readGPS();
if ((safeRange < abs(safeLAT[2] - gps.location.lat())) || (safeRange < abs(safeLNG[2] - gps.location.lng()))){
theftInfo();
}
}
}
}*/
if (gps.date.isValid()){
readGPS();
for(int k = 0; k < 3; k++){
if ((safeRange > abs(safeLAT[k] - gps.location.lat())) && (safeRange > abs(safeLNG[k] - gps.location.lng()))){
theft = false;
}
readGPS();
}
if(theft == true) theftInfo();
}
}
//---------end of the main loop--------------
void displayInfo(){
Serial.print("\nLocation: ");
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(", ");
Serial.print(gps.location.lng(), 6);
}
else
{
Serial.print("N/A");
}
Serial.print("\nDate ");
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.print(gps.date.year());
Serial.print("\n");
}
else Serial.print("N/A \n");
Serial.print("\n");
}
void readGPS(){
while (NEO6M.available() > 0){
if (gps.encode(NEO6M.read())) {} //reads GPS buffer without doing anything
if (millis() > 5000 && gps.charsProcessed() < 10){
Serial.println("No GPS detected: check wiring.");
while(true);
}
}
}
void theftInfo(){
readGPS();
Serial.println("CAR STOLEN!");
Serial.print(gps.location.lat(), 6);
Serial.print(", ");
Serial.println(gps.location.lng(), 6);
}