Sending a GPS location by SMS

Now that we have the location part working correctly, we can start building our GPS tracking projects for secret agents. The first project will simply use the location and send it via SMS to the phone number of your choice.

As the sketch is quite similar to previous one, I'll only show which parts changed compared to the location test sketch. We first need to define the phone number where you want to send the tracking data to:

char * sendToNumber = "123456789";

After getting the location just as in the previous section, we can now build the message that we will send via SMS:

char messageToSend[140];
String message = "Current GPS location: " + latitude + "," + longitude;
message.toCharArray(messageToSend, message.length());

Using the FONA instance, it's very easy to actually send the SMS to the number we defined earlier:

if (!fona.sendSMS(sendToNumber, messageToSend)) {
  Serial.println(F("Failed to send SMS"));
} else {
  Serial.println(F("Sent location data!"));
}

Finally, we wait before each message. I used 10 seconds for testing purposes, but you can enter your own delay in milliseconds:

Serial.println(F("Waiting until next message..."));
delay(10000);

It's now time to test the project. You can grab the whole code from the GitHub repository of the book at https://github.com/marcoschwartz/arduino-secret-agents.

Then, make sure you change the phone number and your GPRS settings inside the code and upload it to the board. This is what you should see:

Sending a GPS location by SMS

If you can see the last line, it means that an SMS actually has been sent. I quickly received the message after this:

Sending a GPS location by SMS

This was followed by a series of messages with the current location of the project:

Sending a GPS location by SMS

If you can see this message on your phone, congratulations, you have built your first GPS tracker using Arduino!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163