Raspberry Pi 3 UART Communication with NEO 6mv2 Gps Module (Part B): Email Notification

Hi guys, Last tutorial, we got the data from the GPS module and did all the setup necessary to allow the PI communicate via Serial as we disabled the Mini UART and enabled the PL011 UART. For this second part of the tutorial, we will learn how to send data from the Pi to an email address and for this tutorial, we will be sending the GPS location data converted to a link, so the user can just click on the link and the location on google map pops up.

Required Components

For this tutorial we will need the same components used in part A but in case you missed it, here they are.

  1. Raspberry Pi 3
  2. Neo 6m v2 GPS Module
  3. Jumper Wires
  4. Breadboard

 

Schematics

Connect the components Listed above as shown in the Image below.

 GY-NEO6MV2light_bb

 

Other Preparations

Before we jump into writing the python script to send the data to our email (Gmail for this tutorial) we have to prepare both our email and the raspberry pi for the communication.

The first thing we need to do is to setup our gmail to allow access from not too secure apps. To do that follow the steps below.

  1. Go to the “Less secure apps” section on the my account section
  2. Turn on Access for less secure apps.

If you are a G-Suite User, this settings will be hidden if your administrator has locked less secure app account access.

With our Email setup, the next thing we will need to do before moving to the code is install the pynmea2 library.

The Pynmea2 library will be used to make sense of the GPS NMEA data and extract the longitude and latitude from the data that will be dumped via the serial port by the GPS module.

To install the pynmea2 library;

1. Make sure you are at the home directory using;

Cd ~

2. Then run;

Sudo pip install pynmea2

This will install the pynmea2 library for use by your python Idle.

With this all done, we are ready to move to the code.

Code

For the Last tutorial we already handled part of the code that had to do with the GPS. You can check it out here. Lets do some explanation of the code.

The first thing we do is include all the libraries needed to make the code work. The first library that was included is the time library which contains the sleep function that was used to set a kind of wait period during the code.

The next line includes the serial library which was used to communicate with the GPS module.
The next line includes the string library which enables string conversions. After string conversions, we include the pynmea2 library and the smtplib library which helps send the google map link created to an email.

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

With the libraries all added, the next things is for us to enter the email details.

from addr should be the email address from which it will appear the mail originated from.
pword is the password of that email address
To addr is the email address to which the location should be sent.

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

Most of the other part of the code have been discussed in the previous tutorial, but you can hit me up via the comment section if you have any questions.

Full code for this project is available below.

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

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


#setup the serial port to which gps is connected 
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
        print(newlat)
        newlong = newmsg.longitude
        print(newlong)
        lat  = str(newlat)
        lon = str(newlong)
        content = "http://maps.google.com/maps?q=" + lat + "," + lon
        Email = content
        msg.attach(MIMEText(Email, 'plain'))
        try:
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(fromaddr, pword)
            text = msg.as_string()
            server.sendmail(fromaddr, toaddr, text)
            server.quit()
            print ("mail sent!")
        except:
            print("error, couldnt send mail, be sure to enable non secure apps login on sender's email")

        time.sleep(3)
        
        

load it up on the pi and run.

you should get an output that looks like the image below in your mail box.

email-e

That’s it guys. Let me know if you have any questions.

Cheers!