Email Alert System on Location Change with Raspberry Pi and GPS Module

In this tutorial you’ll learn how to build an email alert system on location change with a Raspberry Pi. In summary, you’ll send GPS data to your email address using the Raspberry Pi and the NEO-6M GPS module. This project can be extended to act as a GPS tracker that would log the coordinates into a file that you can access later.

This project was written by Emmanuel Odunlade and edited by Rui Santos.


Having a device that notifies you via email when there is a change in the location of an object sounds great. You can do that and much more if you learn how to use the NEO-6M GPS module with the Raspberry Pi.

Here’s what you’re going to achieve with this tutorial:

  • Using the NEO-6M GPS module with Raspbery Pi;
  • Sending an email with the Raspberry Pi;
  • This tutorial is also a good reference for setting up UART on the Raspberry Pi.

Here’s what Emmanuel says about this project: “I personally decided to write this tutorial after spending about three days trying to connect a component that could talk to the Raspberry Pi 3 via UART to work.


If you like home automation and you want to learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino. I recommend that you download my course: Build a Home Automation System for $100.

Parts required

Here’s a complete list of the components you need for this project:

You can use the preceding links or go directly to to find all the parts for your projects at the best price!

About the NEO-6M V2 GPS Module

The NEO-6M GPS module, shown in the figure below, can be a bit tricky to use as it communicates with the Raspberry Pi via UART. So, you need to follow some steps to enable UART communication.

In this tutorial we’re assuming you know how to setup your Raspberry Pi, and access it via SSH using putty. We also assume you are using the Raspbian Jessie operating system, but it should also work on Raspbian Stretch.

Enabling UART on the Raspberry Pi

The SoC used on the raspberry pi has two built in UARTs: PL011 and another one which we can refer to as the mini UART. Accordingly to Raspberry Pi’s official website, the two UARTS were implemented using different hardware blocks and thus, they have different characteristics. However, both operate on the 3.3v logic level.

In the Raspberry Pi 3 and Pi Zero (Raspberry Pi boards with Wireless and Bluetooth modules), the PL011 UART is connected to the Bluetooth module, while the mini UART is used for the linux console output. This makes the PL011 unavailable for serial communication with external components.

To solve this you need to install an overlay which is available in the current version of Raspbian, and transfer linux console output to the PL011. Why can’t we use the mini UART? I will define the PL011 as the best of the two UART due to its implementation level.

So to dive in: the first thing you should do before starting any Raspberry Pi project is updating the Pi. For that, enter the following command:

pi@raspberrypi:~ $ sudo apt update && sudo apt upgrade

Then, you need to edit the config.txt file. Run this next command:

pi@raspberrypi:~ $ sudo nano /boot/config.txt

At the bottom of the text file, add the following lines to disable the Bluetooth and allow increase in core frequency (each parameter should be in a different line).


Ensure there are no typos. Press CTRL+X, followed by Y and press Enter to save.

The next thing you need to do is editing the cmdline.txt file:

pi@raspberrypi:~ $ sudo nano /boot/cmdline.txt

Replace the content in that file with the following (make sure everything is kept in the same line):

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait quiet splash plymouth.ignore-serial-consoles

Press CTRL+X, followed by Y and to save press Enter.

Next, you need to disable the RPi serial getty service on the mini UART by entering the following commands:

pi@raspberrypi:~ $ sudo systemctl stop [email protected]
pi@raspberrypi:~ $ sudo systemctl disable [email protected]

Note: if you want to enable the serial getty service and start it later,  you need to run the following commands (DO NOT RUN the next commands now):

pi@raspberrypi:~ $ sudo systemctl enable [email protected]
pi@raspberrypi:~ $ sudo systemctl start [email protected]

Reboot the system with:

pi@raspberrypi:~ $ sudo reboot

Wait for the Pi to come back on, and establish an SSH communication.

The final step is to enable the ttyAMA0 which is the PL011 UART using the command;

pi@raspberrypi:~ $ sudo systemctl enable [email protected]


With the Raspberry Pi UART setup done, you’re ready to build your circuit. Just follow the next schematic diagram.

Testing the setup

At this stage it is important to test your GPS module and see if the UART setup works as desired. Power up the system and run the following command:

pi@raspberrypi:~ $ sudo cat /dev/ttyAMA0

You should see a bunch of NMEA data streaming as seen in the image below. This shows that the UART setup is up and running, as well as your GPS.

Preparing the script

With the Raspberry Pi setup complete and the components connected, the next step is writing the script that collects the NMEA data from the GPS module. Then, you need to parse it to extract latitude and longitude, and then converting it into a google map link before sending via email.

Before writing the script, you need to download the Python library we’re using to parse the NMEA data. You also need to setup your Gmail account (or other email account) to allow access by less secure apps.

Installing the pynmea2 Python library

To parse the GPS information, we’ll be using the pynmea2 Python library. You can install it on the Raspberry Pi with the following:

pi@raspberrypi:~ $ sudo apt install python-pip
pi@raspberrypi:~ $ sudo pip install pynmea2

More information on how this works can be found on pynmea2 GitHub page.

If you’re using Raspbian Lite, you might also need to install pyserial:

pi@raspberrypi:~ $ sudo pip install pyserial

Allow access for less secure apps

In your email account, you need to allow login by less secure apps. Here’s how it works on Gmail:

Note: G Suite users have this setting hidden if their administrator has locked less secure app account access.

Creating a new script

With this done you are ready for the script. Its well commented so that you understand the purpose of each section, but you can still post any comments below.

Create a new python file called by entering the following command:

pi@raspberrypi:~ $ sudo nano

Copy the following code to the newly created file.

# Author: Emmanuel Odunlade
# Complete Project Details

import time
import serial
import string
import pynmea2
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

#setting up mail information
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Location change alert"

#setup the serial port to which GPS is connected to
port = "/dev/ttyAMA0"
ser = serial.Serial(port, baudrate=9600, timeout=0.5)
dataout  = pynmea2.NMEAStreamReader()

while True:
    newdata = ser.readline()
    print ("getting new lat")
    if newdata[0:6] == '$GPGGA':
        newmsg = pynmea2.parse(newdata)
        newlat = newmsg.latitude
        newlong = newmsg.longitude
        lat  = str(newlat)
        lon = str(newlong)
        content = "" + lat + "," + lon
        Email = content
        msg.attach(MIMEText(Email, 'plain'))
            server = smtplib.SMTP('', 587)
            server.login(fromaddr, pword)
            text = msg.as_string()
            server.sendmail(fromaddr, toaddr, text)
            print ("mail sent!")
            print("error, couldnt send mail, be sure to enable non secure apps login on sender's email")


View raw code

Note: you need to edit the code with your own email information.

Press CTRL+X, followed by Y and press Enter to save.

Running the script

Finally, run your script:

pi@raspberrypi:~ $ sudo python


This script sends the GPS coordinates via email every three seconds.

The idea is that you edit this code to suit your own project.

Note: for your Raspberry Pi to send emails it needs an internet connection. You can connect your Raspberry Pi to your smartphone as long as it has data connection and the ability to act as an hotspot. If you’re not able to do this, you can log the data into a file and access it later.

Wrapping up

In this project we’ve shown you how to send send GPS location data to your email every three seconds. You can edit it with a little bit of python knowledge to report location whenever there is a change.

This could open up a whole lot of possibilities from gaming to tracking valuables, etc.

We hope you’ve found this tutorial useful.

Thanks for reading.

Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…

Enjoyed this project? Stay updated by subscribing our newsletter!

8 thoughts on “Email Alert System on Location Change with Raspberry Pi and GPS Module”

  1. Why would you want this? I can’t think of a single reasonable use for it – particularly in the suggested format of being powered by a mains adaptor.

    Los are quite heavy on current usage, just on their own, so even a battery powered version might not be that practical.

    Nice technique though and it must be useful for something…

  2. Works perfectly, thank you!
    Is it possible to run the GPS and detect lat and long using a switch or a sensor? If yes, could you please tell me how to?

    • Hi.
      You just need to add an if statement to check whether a button was pressed or not. If the button was pressed, run the GPS code that sends the location.
      To control a button using python with the Raspberry Pi, you can use the gpiozero library. It is very easy to use. Take a look at the documentation on the following link:

  3. Hi.
    Is it possible to get the values from the gps in this format in the email?

    MMSI: 123456789 (fixed number)
    TIMESTAMP=2020-10-18 08:24



Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.