Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TinyGPSPlus.h>
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #include <MPU6050.h>
- #define redLED 12
- #define buzzer 13
- #define btn1 2
- static const int RXPin = 6, TXPin = 5;
- String s = "www.google.com/maps/dir/";
- static const uint32_t GPSBaud = 9600;
- String Lat = "";
- String Lng = "";
- int SendSMS_Now = 0;
- const size_t BUFSIZE = 300;
- char f_buffer[BUFSIZE];
- float *f_buf = (float*)f_buffer;
- TinyGPSPlus gps;
- SoftwareSerial ss(RXPin, TXPin);
- MPU6050 mpu;
- void setup(){
- Serial.begin(9600);
- Wire.begin();
- pinMode(btn1, INPUT_PULLUP);
- pinMode(buzzer, OUTPUT);
- pinMode(redLED, OUTPUT);
- digitalWrite(redLED, LOW);
- redLEDOK(5);
- attachInterrupt(digitalPinToInterrupt(btn1), send_SMS_NOW, CHANGE);
- setupA9G();
- mpu.initialize();
- Serial.println("Setup Executed");
- }
- void send_SMS_NOW() {
- SendSMS_Now = 1;
- }
- void loop() {
- fullGPSSerial();
- mpu_Data();
- //readGPS_Data();
- showLATLON();
- /*
- if (SendSMS_Now == 1) {
- get_gps_data();
- digitalWrite(buzzer, HIGH);
- sendSMSwithLocation1();
- delay(2000);
- sendSMSwithLocation2();
- delay(2000);
- s = "www.google.com/maps/dir/";
- SendSMS_Now = 0;
- //send_gps_data();
- }
- */
- digitalWrite(buzzer, LOW);
- }
- void get_gps_data() {
- Serial.print("Latitude (deg): ");
- Lat = String(gps.location.lat(), 6);
- Serial.println(Lat);
- Serial.print("Longitude (deg): ");
- Lng = String(gps.location.lng(), 6);
- Serial.println(Lng);
- Serial.println();
- s += String(gps.location.lat(), 6);
- s += ",";
- s += String(gps.location.lng(), 6);
- s += "/";
- Serial.println(s);
- }
- void setupA9G() {
- ss.begin(GPSBaud);
- Serial.println("Starting...");
- ss.println("\r");
- ss.println("AT\r");
- delay(10);
- ss.println("\r");
- ss.println("AT+GPS=1\r");
- delay(100);
- ss.println("AT+CREG=2\r");
- delay(6000);
- //ss.print("AT+CREG?\r");
- ss.println("AT+CGATT=1\r");
- delay(6000);
- ss.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
- delay(6000);
- // ss.println("AT+LOCATION=1\r");
- ss.println("AT+CGACT=1,1\r");
- delay(6000);
- //Initialize ends
- //Initialize GPS
- ss.println("\r");
- ss.println("AT+GPS=1\r");
- delay(1000);
- //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
- ss.println("AT+GPSRD=10\r");
- delay(100);
- // set SMS mode to text mode
- ss.println("AT+CMGF=1\r");
- delay(1000);
- //ss.println("AT+LOCATION=2\r");
- Serial.println(F("FullExample.ino"));
- Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
- Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
- Serial.println(F("by Mikal Hart"));
- Serial.println();
- Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
- Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
- Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
- }
- /////////////////////////////////////////////////////////
- void sendSMSwithLocation1() {
- Serial.println("WARNING! Vehicle Collision Detected");
- ss.println("AT+CMGF=1\r");
- delay(1000);
- ss.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- ss.print("AT+CMGS=\"+966559070115\"\r");//Replace this with your mobile number
- delay(1000);
- ss.print("WARNING! Vehicle Collision Detected\n");
- ss.print(s);
- ss.write(0x1A);
- delay(1000);
- }
- void sendSMSwithLocation2() {
- Serial.println("WARNING! Vehicle Collision Detected");
- ss.println("AT+CMGF=1\r");
- delay(1000);
- ss.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- ss.print("AT+CMGS=\"+966591772557\"\r");//Replace this with your mobile number
- delay(1000);
- ss.print("WARNING! Vehicle Collision Detected \n");
- ss.print(s);
- ss.write(0x1A);
- delay(1000);
- }
- ////////////////////////////////////////////////////////////
- void sendSMSFlipLocation() {
- Serial.println("WARNING! Vehicle Overturned");
- ss.println("AT+CMGF=1\r");
- delay(1000);
- ss.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- ss.print("AT+CMGS=\"+966559070115\"\r");//Replace this with your mobile number
- delay(1000);
- ss.print("WARNING! Vehicle Overturned \n");
- ss.print(s);
- ss.write(0x1A);
- delay(1000);
- s = "www.google.com/maps/dir/";
- }
- ////////////////////////////////////////////////////////////
- void readGPS_Data() {
- smartDelay(2000);
- if (millis() > 5000 && gps.charsProcessed() < 10)
- Serial.println(F("No GPS data received: check wiring"));
- }
- void mpu_Data() {
- int16_t ax, ay, az;
- mpu.getAcceleration(&ax, &ay, &az);
- if (az < 0) {
- digitalWrite(buzzer, HIGH);
- get_gps_data();
- sendSMSFlipLocation();
- }
- else {
- digitalWrite(buzzer, LOW); // Turn off the buzzer
- }
- Serial.print("X = ");
- Serial.print(ax);
- Serial.print(" Y = ");
- Serial.print(ay);
- Serial.print(" Z = ");
- Serial.println(az);
- //delay(500); // Delay for stability
- }
- void showLATLON() {
- Serial.print("Latitude (deg): ");
- Lat = String(gps.location.lat(), 6);
- Serial.println(Lat);
- Serial.print("Longitude (deg): ");
- Lng = String(gps.location.lng(), 6);
- Serial.println(Lng);
- Serial.println();
- }
- void fullGPSSerial() {
- static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
- printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
- printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
- printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
- printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
- printInt(gps.location.age(), gps.location.isValid(), 5);
- printDateTime(gps.date, gps.time);
- printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
- printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
- printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
- printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
- unsigned long distanceKmToLondon =
- (unsigned long)TinyGPSPlus::distanceBetween(
- gps.location.lat(),
- gps.location.lng(),
- LONDON_LAT,
- LONDON_LON) / 1000;
- printInt(distanceKmToLondon, gps.location.isValid(), 9);
- double courseToLondon =
- TinyGPSPlus::courseTo(
- gps.location.lat(),
- gps.location.lng(),
- LONDON_LAT,
- LONDON_LON);
- printFloat(courseToLondon, gps.location.isValid(), 7, 2);
- const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
- printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
- printInt(gps.charsProcessed(), true, 6);
- printInt(gps.sentencesWithFix(), true, 10);
- printInt(gps.failedChecksum(), true, 9);
- Serial.println();
- smartDelay(1000);
- if (gps.charsProcessed() > 63) {
- redLEDOK(3);
- }
- if (millis() > 5000 && gps.charsProcessed() < 10)
- Serial.println(F("No GPS data received: check wiring"));
- }
- // This custom version of delay() ensures that the gps object
- // is being "fed".
- static void smartDelay(unsigned long ms)
- {
- unsigned long start = millis();
- do
- {
- while (ss.available())
- gps.encode(ss.read());
- } while (millis() - start < ms);
- }
- static void printFloat(float val, bool valid, int len, int prec)
- {
- if (!valid)
- {
- while (len-- > 1)
- Serial.print('*');
- Serial.print(' ');
- }
- else
- {
- Serial.print(val, prec);
- int vi = abs((int)val);
- int flen = prec + (val < 0.0 ? 2 : 1); // . and -
- flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
- for (int i = flen; i < len; ++i)
- Serial.print(' ');
- }
- smartDelay(0);
- }
- static void printInt(unsigned long val, bool valid, int len)
- {
- char sz[32] = "*****************";
- if (valid)
- sprintf(sz, "%ld", val);
- sz[len] = 0;
- for (int i = strlen(sz); i < len; ++i)
- sz[i] = ' ';
- if (len > 0)
- sz[len - 1] = ' ';
- Serial.print(sz);
- smartDelay(0);
- }
- static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
- {
- if (!d.isValid())
- {
- Serial.print(F("********** "));
- }
- else
- {
- char sz[32];
- sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
- Serial.print(sz);
- }
- if (!t.isValid())
- {
- Serial.print(F("******** "));
- }
- else
- {
- char sz[32];
- sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
- Serial.print(sz);
- redLEDOK(1);
- }
- printInt(d.age(), d.isValid(), 5);
- smartDelay(0);
- }
- static void printStr(const char *str, int len)
- {
- int slen = strlen(str);
- for (int i = 0; i < len; ++i)
- Serial.print(i < slen ? str[i] : ' ');
- smartDelay(0);
- }
- void redLEDOK(int j) {
- for (int i = 0; i < j; i++) {
- digitalWrite(redLED, HIGH);
- delay(100);
- digitalWrite(redLED, LOW);
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement