NADBYTE

How to read Location from GPS module and send Location with SMS using GSM module

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

Schematic Diagram

Connections

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”]
Exit mobile version