#include "Adafruit_FONA.h" #include // what's the name of the hardware serial port? //#define GPSSerial Serial #define GPS_RX 4 #define GPS_TX 5 SoftwareSerial GPSSerial = SoftwareSerial(GPS_TX, GPS_RX); // Connect to the GPS on the hardware port Adafruit_GPS GPS(&GPSSerial); #define FONA_RX 2 #define FONA_TX 3 #define FONA_RST 7 // this is a large buffer for replies char replybuffer[255]; // We default to using software serial. If you want to use hardware serial // (because softserial isnt supported) comment out the following three lines // and uncomment the HardwareSerial line #include SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); SoftwareSerial *fonaSerial = &fonaSS; // Hardware serial is also possible! // HardwareSerial *fonaSerial = &Serial1; // Use this for FONA 800 and 808s Adafruit_FONA fona = Adafruit_FONA(FONA_RST); uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); void setup() { GPS.begin(9600); GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); GPS.sendCommand(PGCMD_ANTENNA); delay(1000); GPSSerial.println(PMTK_Q_RELEASE); pinMode(5, OUTPUT); digitalWrite(5, LOW); delay(1000); // make it slow so its easy to read! fonaSerial->begin(9600); if (! fona.begin(*fonaSerial)) { while(1){ digitalWrite(6, LOW); delay(1000); digitalWrite(6, HIGH); delay(1000); } } //unlock sim //fona.unlockSIM(""); fonaSerial->print("AT+CNMI=2,1\r\n"); //set up the FONA to send a +CMTI notification when an SMS is received digitalWrite(6, HIGH); delay(1000); } char fonaNotificationBuffer[64]; //for notifications from the FONA char smsBuffer[250]; void loop() { char* bufPtr = fonaNotificationBuffer; //handy buffer pointer if (fona.available()) //any data available from the FONA? { int slot = 0; //this will be the slot number of the SMS int charCount = 0; //Read the notification into fonaInBuffer do { *bufPtr = fona.read(); delay(1); } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaNotificationBuffer)-1))); //Add a terminal NULL to the notification string *bufPtr = 0; //Scan the notification string for an SMS received notification. // If it's an SMS message, we'll get the slot number in 'slot' if (1 == sscanf(fonaNotificationBuffer, "+CMTI: " FONA_PREF_SMS_STORAGE ",%d", &slot)) { digitalWrite(5, LOW); delay(1000); char callerIDbuffer[32]; //we'll store the SMS sender number in here // Retrieve SMS sender address/phone number. fona.getSMSSender(slot, callerIDbuffer, 31); // Retrieve SMS value. uint16_t smslen; fona.readSMS(slot, smsBuffer, 250, &smslen); //Read GPS bool updated = false; while(!updated){ if (GPS.newNMEAreceived()) { if (GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false if (GPS.fix){ updated = true; } } delay(1000); } String lat = String(GPS.latitude, 4); //something like "5046.7294" String lon = String(GPS.longitude, 4); //something like "00603.5655" String response = "https://www.google.com/maps/search/" + GPS.lat + lat.substring( 0, lat.length()-7 ) + "%C2%B0+" + lat.substring( lat.length()-7 ) + "',+" + GPS.lon + lon.substring( 0, lon.length()-7 ) + "%C2%B0+" + lon.substring( lon.length()-7 ) + "'"; char charBuf[100]; response.toCharArray(charBuf, 100); //Send back an automatic response fona.sendSMS(callerIDbuffer, charBuf); digitalWrite(5, HIGH); delay(1000); // delete the original msg after it is processed // otherwise, we will fill up all the slots // and then we won't be able to receive SMS anymore fona.deleteSMS(slot); }/**/ } }