Skip to content

Instantly share code, notes, and snippets.

@Shivamy2
Created March 3, 2021 05:42
Show Gist options
  • Save Shivamy2/46013d3806a2d417c61bad331483beb2 to your computer and use it in GitHub Desktop.
Save Shivamy2/46013d3806a2d417c61bad331483beb2 to your computer and use it in GitHub Desktop.
import serial #import serial pacakge
from time import sleep
import webbrowser #import package for opening link in browser
import sys
import requests#import system package
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
Webhost = 'http://dcs.glaitm.org:7080'
App_Key = '153eb8d5-a4eb-42d4-b2f4-d781ede74393'
ThingName = 'ECE_Geoloc'
Property1 = 'lat_in_degree'
Property2 = 'long_in_degree'
headers = { 'Content-Type': 'application/json', 'appKey': App_Key }
def GPS_Info():
global NMEA_buff
global lat_in_degree
global long_in_degree
nmea_time = []
nmea_latitude = []
nmea_longitude = []
nmea_time = NMEA_buff[0] #extract time from GPGGA string
nmea_latitude = NMEA_buff[1] #extract latitude from GPGGA string
nmea_longitude = NMEA_buff[3] #extract longitude from GPGGA string
print("NMEA Time: ", nmea_time,'\n')
print ("NMEA Latitude:", nmea_latitude,"NMEA Longitude:", nmea_longitude,'\n')
lat = float(nmea_latitude) #convert string into float for calculation
longi = float(nmea_longitude) #convertr string into float for calculation
lat_in_degree = convert_to_degrees(lat) #get latitude in degree decimal format
long_in_degree = convert_to_degrees(longi) #get longitude in degree decimal format
#convert raw NMEA string into degree decimal format
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
gpgga_info = "$GPGGA,"
ser = serial.Serial ("/dev/ttyS0") #Open port with baud rate
GPGGA_buffer = 0
NMEA_buff = 0
lat_in_degree = 0
long_in_degree = 0
try:
while True:
received_data = (str)(ser.readline()) #read NMEA string received
GPGGA_data_available = received_data.find(gpgga_info) #check for NMEA GPGGA string
if (GPGGA_data_available>0):
GPGGA_buffer = received_data.split("$GPGGA,",1)[1] #store data coming after "$GPGGA," string
NMEA_buff = (GPGGA_buffer.split(',')) #store comma separated data in buffer
GPS_Info() #get time, latitude, longitude
print("lat_in_degree:", lat_in_degree," long_in_degree: ", long_in_degree, '\n')
map_link = 'http://maps.google.com/?q=' + lat_in_degree + ',' + long_in_degree #create link to plot location on Google map
print("<<<<<<<<press ctrl+c to plot location on google maps>>>>>>\n") #press ctrl+c to plot on map and exit
print("------------------------------------------------------------\n")
payload = { Property1: lat_in_degree, Property2: long_in_degree}
response = requests.put(Webhost + '/Thingworx/Things/' + ThingName + '/Properties/*', headers=headers, json=payload, verify=False)
except KeyboardInterrupt:
webbrowser.open(map_link) #open current position information in google map
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment