Introduction

Hi guys, in this we will see how to read the present location from the GPS module and this location to the user from the GSM module as SMS.

Components Required

  • Raspberry Pi – 1 (Raspbian OS installed)
  • GSM Module – 1
  • GPS Module – 1
  • TTL Converter – 2

Schematic Diagram

Connections

  • GSM is connected to USB to TTL converter. GSM Tx pin is connected to Rx pin of TTL module, GSM Rx is connected to Tx pin of TTL module, VCC is connected to 5V, and GND is connected to GND.
  • GPS is connected to another USB to TTL converter. GPS Tx is connected to the Rx pin of the TTL module, VCC is connected to 5V, and GND is connected to GND.
  • GPS TTL module is connected to USB0 and GSM TTL module is connected to USB1.

Code

import serial               
from time import sleep
import sys

def GPS_Track():
    print("Getting GPS")
    global NMEA_buff
    global lat_in_degrees
    global long_in_degrees
    nmea_time = []
    nmea_latitude = []
    nmea_longitude = []
    nmea_time = NMEA_buff[0]                    
    nmea_latitude = NMEA_buff[1]                
    nmea_longitude = NMEA_buff[3]               
    
    latitude = float(nmea_latitude)                  
    longitude = float(nmea_longitude)               
    
    lat_in_degrees = convert_to_degrees(latitude)    
    long_in_degrees = convert_to_degrees(longitude)
    
def convert_to_degrees(raw_value):
    decimal_value = raw_value/100.00
    degrees = int(decimal_value)
    mm_mmmm = (decimal_value - int(decimal_value))/0.6
    position = degrees + mm_mmmm
    position = "%.4f" %(position)
    return position

def sendSms(lat,lon):
    print("Sending SMS\n")
    gsm.write(b'AT+CMGF=1\r\n')
    sleep(0.5)
    gsm.write(b'AT+CMGS=\"')
    serialcmd = args["mobile"]
    gsm.write(serialcmd.encode())
    gsm.write(b'\"\r\n')
    sleep(0.5)
    serialcmd = lat,lon
    gsm.write(serialcmd.encode())
    gsm.write(b'\x1A')
    sleep(3)
    
gpgga_info = "$GPGGA,"
ser = serial.Serial ("/dev/ttyUSB0")              
GPGGA_buffer = 0
NMEA_buff = 0
lat_in_degrees = 0
long_in_degrees = 0
gsm = serial.Serial("/dev/ttyUSB1",9600, timeout=0.5)
gsm.flush()


try:
    while True:
        received_data = (str)(ser.readline())                   
        GPGGA_data_available = received_data.find(gpgga_info)                   
        #print(received_data)
        if (GPGGA_data_available>=0):
            GPGGA_buffer = received_data.split("$GPGGA,",1)[1]   
            NMEA_buff = (GPGGA_buffer.split(','))               
            GPS_Track()                                          
            print("lat in degrees:", lat_in_degrees," long in degree: ", long_in_degrees, '\n')
            sendSms(lat_in_degrees,long_in_degrees)
            sleep(30)
            
                        
except KeyboardInterrupt:
    sys.exit(0)
    

Output

Thanks for reading this article for any assistance or doubts comment below.

[jetpack_subscription_form show_subscribers_total=”false” button_on_newline=”false” custom_font_size=”16″ custom_border_radius=”0″ custom_border_weight=”1″ custom_padding=”15″ custom_spacing=”10″ submit_button_classes=”” email_field_classes=”” show_only_email_and_button=”true”]

Leave a Reply

Your email address will not be published. Required fields are marked *