Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TinyGPSPlus.h>
- #include <SoftwareSerial.h>
- int state = 0;
- int counter = 0;
- static const int button_pin = 4;
- static const int RXPin = 6, TXPin = 5;
- static const uint32_t GPSBaud = 9600;
- String stringMessage = "";
- String lonGPS = "";
- String latGPS = "";
- char Buf[70];
- TinyGPSPlus gps;
- SoftwareSerial ss(RXPin, TXPin);
- void setup() {
- Serial.begin(9600);
- Serial.println("Starting...");
- pinMode(button_pin, INPUT);
- a9g_init();
- Serial.println("Setup Executed");
- }
- void loop() {
- smartDelay(2000);//read GPS Data
- Serial.println(gps.location.lat(),6);
- state = digitalRead(button_pin);
- Serial.println(state);
- if (state == 0) {
- counter++;
- delay(500);
- if (counter > 5) {
- a9g_loop();//read GPS data and Send SMS
- counter = 0;
- }
- }
- else {
- counter = 0;
- }
- }
- static void smartDelay(unsigned long ms) {
- unsigned long start = millis();
- do
- {
- while (ss.available())
- gps.encode(ss.read());
- } while (millis() - start < ms);
- }
- void send_gps_data() {
- if (gps.location.lat() == 0 || gps.location.lng() == 0) {
- Serial.println("Return Executed");
- //21.913677,39.263130
- ///////////////////////////////////////////////////////////////////////////////////////
- Serial.println("Sending Message");
- ss.println("AT+CMGF=1\r");
- delay(1000);
- ss.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- ss.print("AT+CMGS=\"+966544092008\"\r");//Replace this with your mobile number
- delay(1000);
- ss.print("[SOS] Please Help me I'm at this location: ");
- ss.print("http://www.google.com/maps/place/21.913677,39.263130");
- ss.write(0x1A);
- delay(1000);
- stringMessage = "";
- ///////////////////////////////////////////////////////////////////////////////////////
- return;
- }
- lonGPS = String(gps.location.lng(), 6);
- latGPS = String(gps.location.lat(), 6);
- Serial.print("Lat: ");
- Serial.print(latGPS);
- Serial.print(F(","));
- Serial.print("Lng: ");
- Serial.print(lonGPS);
- Serial.println();
- stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
- stringMessage.toCharArray(Buf, 70);
- ///////////////////////////////////////////////////////////////////////////////////////
- Serial.println("Sending Message");
- ss.println("AT+CMGF=1\r");
- delay(1000);
- ss.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- ss.print("AT+CMGS=\"+966544092008\"\r");//Replace this with your mobile number
- delay(1000);
- ss.print("[SOS] Please Help me I'm at this location: ");
- ss.print(Buf);
- ss.write(0x1A);
- delay(1000);
- stringMessage = "";
- ///////////////////////////////////////////////////////////////////////////////////////
- }
- void a9g_init() {
- ss.begin(GPSBaud);
- 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");
- }
- void a9g_loop() {
- if (millis() > 5000 && gps.charsProcessed() < 10)
- Serial.println(F("No GPS data received: check wiring"));
- send_gps_data();//Send SMS with Location Data
- }
Advertisement
Add Comment
Please, Sign In to add comment