def get_lat_lon_time_from_nmea(nmea_file, local_time=True): ''' Read location and time stamps from a track in a NMEA file. Returns a list of tuples (time, lat, lon). GPX stores time in UTC, by default we assume your camera used the local time and convert accordingly. ''' with open(nmea_file, "r") as f: lines = f.readlines() lines = [l.rstrip("\n\r") for l in lines] points = [] for l in lines: if "GPRMC" in l: data = pynmea2.parse(l) date = data.datetime.date() if "$GPGGA" in l: data = pynmea2.parse(l) timestamp = datetime.datetime.combine(date, data.timestamp) lat, lon, alt = data.latitude, data.longitude, data.altitude points.append((timestamp, lat, lon, alt)) points.sort() return points
def test_proprietary_with_comma(): # class with no extra comma class TNLDG(pynmea2.tnl.TNL): fields = () # raise Exception(TNL.sentence_types) # raise Exception(pynmea2.ProprietarySentence.sentence_types) data = "$PTNLDG,44.0,33.0,287.0,100,0,4,1,0,,,*3E" msg = pynmea2.parse(data) assert isinstance(msg, TNLDG) assert msg.data == ['DG', '44.0', '33.0', '287.0', '100', '0', '4', '1', '0', '', '', ''] assert str(msg) == data # type with extra comma data = '$PTNL,PJT,NAD83(Conus),CaliforniaZone 4 0404*51' msg = pynmea2.parse(data) assert type(msg) == pynmea2.tnl.TNLPJT assert msg.manufacturer == 'TNL' assert msg.sentence_type == 'PJT' assert msg.coord_name == 'NAD83(Conus)' assert msg.project_name == 'CaliforniaZone 4 0404' assert str(msg) == data
def test_srf(): # implemented sentence data = '$PSRF100,0,1200,8,1,1' msg = pynmea2.parse(data) assert type(msg) == pynmea2.srf.SRF100 # unimplemented sentence data = '$PSRF999,0,1200,8,1,1' msg = pynmea2.parse(data) assert type(msg) == pynmea2.srf.SRF
def parse_data(self, datastring): self.log_gps(datastring) try: data = pynmea2.parse(datastring) except AttributeError as er: log(er.message) return except pynmea2.SentenceTypeError as er: log(er.message) return except pynmea2.ParseError as er: log(er.message) return except pynmea2.ChecksumError as er: log(er.message) return mappings = {"RMC": self.extract_rmc, "GGA": self.extract_gga, "GSV": self.extract_gsv, "VTG": self.extract_vtg, "GSA": self.extract_gsa} try: mappings[data.sentence_type](data) self.gpsStateChanged(self.info) except KeyError: return except AttributeError: return
def parse_sentence(self): """ parse GPS nmea0183 sentences, one shot, currently only support RMC,""" sentence_type = "" while sentence_type is not "RMC": sentence = SC16IS750.read_sentence(self) try: parsed_sentence = pynmea2.parse(sentence) except: continue try: sentence_type = parsed_sentence.sentence_type except AttributeError: # "sentence not supported" # print "#attribute error" continue latitude = parsed_sentence.latitude longitude = parsed_sentence.longitude lat_dir = parsed_sentence.lat_dir lon_dir = parsed_sentence.lon_dir timestamp = parsed_sentence.timestamp datestamp = parsed_sentence.datestamp spd_over_grnd = parsed_sentence.spd_over_grnd true_course = parsed_sentence.true_course fix = parsed_sentence.is_valid # print fix return({"latitude": latitude, "longitude": longitude, "lat_dir": lat_dir, "lon_dir": lon_dir, "datetime": datestamp.strftime("%Y-%m-%d ") + timestamp.strftime("%H:%M:%S"), "spd_over_grnd": spd_over_grnd, "true_course": true_course, "fix": fix})
def test_query(): data = 'CCGPQ,GGA' msg = pynmea2.parse(data) assert isinstance(msg, pynmea2.QuerySentence) assert msg.talker == 'CC' assert msg.listener == 'GP' assert msg.sentence_type == 'GGA'
def test_unknown_sentence(): data = 'PZZZABC,1,2,3' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ProprietarySentence assert msg.manufacturer == 'ZZZ' assert msg.data == ['ABC', '1', '2', '3'] assert msg.render(checksum=False, dollar=False) == data
def test_srf(): # implemented sentence data = '$PSRF100,0,1200,8,1,1' msg = pynmea2.parse(data) assert type(msg) == pynmea2.srf.SRF100 assert msg.sentence_type == 'SRF100' assert msg.protocol == '0' assert msg.baud == '1200' assert msg.databits == '8' assert msg.stopbits == '1' assert msg.parity == '1' # unimplemented sentence data = '$PSRF999,0,1200,8,1,1' msg = pynmea2.parse(data) assert type(msg) == pynmea2.srf.SRF
def test_extra_comma(): # extra comma after name data = "$PTNL,AVR,212604.30,+52.1800,Yaw,,,-0.0807,Roll,12.579,3,1.4,16*21" msg = pynmea2.parse(data) assert msg.manufacturer == 'TNL' assert msg.data == ['', 'AVR','212604.30','+52.1800','Yaw','','','-0.0807','Roll','12.579','3','1.4','16'] assert msg.render() == data
def test_GGA(): data = "$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D" msg = pynmea2.parse(data) assert msg.talker == 'GP' assert msg.type == 'GGA' # Timestamp assert msg.timestamp == datetime.time(18, 43, 53) # Latitude assert msg.lat == '1929.045' # Latitude Direction assert msg.lat_dir == 'S' # Longitude assert msg.lon == '02410.506' # Longitude Direction assert msg.lon_dir == 'E' # GPS Quality Indicator assert msg.gps_qual == '1' # Number of Satellites in use assert msg.num_sats == '04' # Horizontal Dilution of Precision assert msg.horizontal_dil == '2.6' # Antenna Alt above sea level (mean) assert msg.altitude == 100.0 # Units of altitude (meters) assert msg.altitude_units == 'M' # Geoidal Separation assert msg.geo_sep == '-33.9' # Units of Geoidal Separation (meters) assert msg.geo_sep_units == 'M' # Age of Differential GPS Data (secs) assert msg.age_gps_data == '' # Differential Reference Station ID assert msg.ref_station_id == '0000'
def get_location(gps): gps_data = "" print ("reading in location") gps.setblocking(True) while True: try: d = gps.recv(8096) gps.setblocking(False) except IOError: break gps_data += d print d if not d: break gps_data = "\n".join(gps_data.split("\n")[1:-1]) # :( nmea_stream_reader = pynmea2.NMEAStreamReader() gps_loc_msg = None for msg in gps_data.split("\n"): if len(str(msg)) > 50 and "GPGGA" in msg: gps_loc_msg = pynmea2.parse(msg[:-1]) if gps_loc_msg == None: return (0, 0, 0) return (gps_loc_msg.latitude, gps_loc_msg.longitude, gps_loc_msg.horizontal_dil)
def test_proprietary_implemented(): # Ensure a proprietary sentence that is explicitly implemented isn't # returned as a generic proprietary sentence. data = "$PGRME,15.0,M,45.0,M,25.0,M*1C" msg = pynmea2.parse(data) assert repr(msg) == "<RME(hpe='15.0', hpe_unit='M', vpe='45.0', vpe_unit='M', osepe='25.0', osepe_unit='M')>"
def test_XDR(): data = "$YXXDR,A,-64.437,M,N,A,054.454,D,E,C,17.09,C,T-N1052*46" msg = pynmea2.parse(data) assert isinstance(msg, pynmea2.XDR) assert msg.talker == 'YX' assert msg.type == 'A' assert msg.value == '-64.437' assert msg.units == 'M' assert msg.id == 'N' assert msg.num_transducers == 3 t0 = msg.get_transducer(0) assert t0.type == 'A' assert t0.value == '-64.437' assert t0.units == 'M' assert t0.id == 'N' t1 = msg.get_transducer(1) assert t1.type == 'A' assert t1.value == '054.454' assert t1.units == 'D' assert t1.id == 'E' t2 = msg.get_transducer(2) assert t2.type == 'C' assert t2.value == '17.09' assert t2.units == 'C' assert t2.id == 'T-N1052'
def main(): time_string = datetime.utcnow().strftime('%Y%m%d%H%M%S%f') filename = '/media/sdcard/gps{0}.csv'.format(time_string) raw_filename = '/media/sdcard/rawgps{0}.csv'.format(time_string) print 'logging to the following files' print filename print raw_filename ser = serial.Serial('/dev/ttyMFD1', 9600) while(True): s = ser.readline() if(s.startswith("$GP")): append_to_file(raw_filename, s) try: msg = pynmea2.parse(s) except pynmea2.nmea.ParseError: continue if msg.sentence_type == 'RMC': s = '{0},{1},{2}\n'.format( msg.datetime, msg.latitude, msg.longitude) append_to_file(filename, s)
def push_latlon_NMEA_data(self,stream, deque): """ """ while True: time.sleep(0.1) while(len(deque) > 0): data = deque.pop() #print data # Interprete the data try: data_nmea = pynmea2.parse(data['nmea']) data_time = data['time'] # Merge the date of the unix time sent and the # HHMMSS of the GPGGA string together ts = time.gmtime(data_time) tstr = time.strftime("%Y%m%d",ts) # if(data_nmea.identifier().rfind('GPGGA')>=0): #print('lat/lon!') nmea_time = data_nmea.timestamp.strftime("%H%M%S") tstr_unix = tstr + nmea_time #print('unix time',tstr_unix) tfloat_unix = calendar.timegm(time.strptime(tstr_unix,"%Y%m%d%H%M%S")) lat = data_nmea.latitude lon = data_nmea.longitude #print(nmea_time) #print(tfloat_unix,time.gmtime(tfloat_unix)) #print(data_nmea.latitude) #print(data_nmea.longitude) stream.pub_data([[nmea_time,tfloat_unix,lon,lat],]) except Exception as e: print(str(e) + ' Bad data ')
def test_tnl(): data = '$PTNL,BPQ,224445.06,021207,3723.09383914,N,12200.32620132,W,EHT-5.923,M,5*60' msg = pynmea2.parse(data) assert type(msg) == pynmea2.tnl.TNLBPQ assert msg.datestamp == datetime.date(2007,12,2) assert msg.latitude == 37.384897319 assert msg.longitude == -122.00543668866666
def run(self): print "Reading GPS data..." while(True): line = ser.readline() try: msg = pynmea2.parse(line) # print type(msg) if isinstance(msg, pynmea2.types.GGA): if not hasattr(msg, 'latitude') or not hasattr(msg, 'longitude'): print "no satellite signal..." time.sleep(5) continue gps.horizontal_dil = msg.horizontal_dil gps.lat = msg.latitude gps.lng = msg.longitude # print "horizontal_dil:", gps.horizontal_dil # print "lat: ", gps.lat , "\nlng: ", gps.lng if isinstance(msg, pynmea2.types.GSA): gps.num_sv_in_use = gps.count_sv(msg) # print "num_sv_in_use: ", gps.num_sv_in_use if isinstance(msg, pynmea2.types.GSV): gps.num_sv_in_view = msg.num_sv_in_view # print "num_sv_in_view: ", gps.num_sv_in_view except ValueError: pass
def test_ubx00(): data = '$PUBX,00,074440.00,4703.74203,N,00736.82976,E,576.991,D3,2.0,2.0,0.091,0.00,-0.032,,0.76,1.05,0.65,14,0,0*70' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ubx.UBX00 assert msg.timestamp == datetime.time(7, 44, 40) assert msg.latitude == 47.06236716666667 assert msg.lat_dir == 'N'
def test_ubx04(): data = '$PUBX,04,073824.00,131014,113903.99,1814,16,495176,342.504,21*18' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ubx.UBX04 assert msg.date == datetime.date(2014, 10, 13) assert msg.time == datetime.time(7, 38, 24) assert msg.clk_bias == 495176
def run(self): while (self.connected): c = self.socket.recv(1) if (c=='$' or c=='!'): # beginning self.sentence = c else: self.sentence += c if c=='\n': # end of sentence # should check checksum... if (self.sentence[0]=='$'): self.logger.debug("Received sentence: %s" % self.sentence) sentence_header, comma, sentence_body = self.sentence.partition(',') sentence_header = sentence_header.lstrip('$') self.sentences[sentence_header] = self.sentence try: msg = pynmea2.parse(self.sentence) self.logger.debug("Sentence type: %s" % msg.sentence_types) if not NmeaDataSource.lock.acquire(False): self.logger.error("Lock failed writing watchField") else: try: for watchField in self.watch_fields: watchField.update_value_from_message(msg) finally: NmeaDataSource.lock.release() except ValueError as e: self.logger.debug("Unknown message type received") except: self.logger.error("Something shitty has happened. Offender is:") self.logger.error(self.sentence) raise self.socket.close()
def convert_nmea_to_json(nmea_str, filename, GMT_OFFSET=0): json_list = [] filename = filename.strip('.LOG').strip('N') year = 2000 + int(filename[0:2]) month = int(filename[2:4]) day = int(filename[4:6]) print(year, month, day) for line in nmea_str.split('\n'): line = line.strip() if '@' in line or 'GPRMC' in line or len(line) == 0: continue record = pynmea2.parse(line) dt = record.timestamp dt = datetime(year, month, day, dt.hour, dt.minute, dt.second) # Gather values posix = int(dt.strftime("%s")) posix += (60 * 60 * GMT_OFFSET) lat = float(record.latitude) lon = float(record.longitude) json_list.append({ 'time': posix, 'lat': lat, 'lon': lon, }) return json.dumps({ "track": json_list })
def parseGPS(self, str): if str.find('GGA') > 0: msg = pynmea2.parse(str) if len(msg.lat) > 2: if self.environment != None: self.environment.gpsConnected = True self.gpsPos = (msg.lat + msg.lat_dir, msg.lon + msg.lon_dir) self.convertToDecimalDegrees() # Set time according to GPS time if not self.timeSet: print 'Setting system time to GPS time...' gpsTime = "%s:%s:%s" % (msg.timestamp.hour, msg.timestamp.minute, msg.timestamp.second) if gpsTime != None: os.system('sudo date -u --set="%s"' % gpsTime) self.timeSet = True print 'System time set.' else: self.gpsPos = (None, None) if self.environment != None: self.environment.gpsConnected = False if self.environment != None: self.environment.gpsPos = self.gpsPos
def test_proprietary_3(): # A sample proprietary sentence from a Magellan device # (via <http://www.gpsinformation.org/dale/nmea.htm#proprietary>). data = "$PMGNST,02.12,3,T,534,05.0,+03327,00*40" msg = pynmea2.parse(data) assert msg.manufacturer == 'MGN' assert msg.data == ['ST','02.12','3','T','534','05.0','+03327','00'] assert msg.render() == data
def test_mixin(): msg = pynmea2.parse(data) assert msg.latitude == -19.484083333333334 assert msg.longitude == 24.175100000000000 assert msg.latitude_minutes == 29.045000000000073 assert msg.longitude_minutes == 10.506000000000085 assert msg.latitude_seconds == 2.6999999999970896 assert msg.longitude_seconds == 30.360000000000582
def test_ashrltn(): data = '$PASHR,LTN,3*3D' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ash.ASHRLTN assert msg.manufacturer == 'ASH' assert msg.subtype == 'LTN' assert msg.latency == 3 assert msg.render() == data
def test_proprietary_1(): # A sample proprietary sentence from a LCJ Capteurs # anemometer. data = "$PLCJ,5F01,66FC,AA,9390,6373" msg = pynmea2.parse(data) assert msg.manufacturer == "LCJ" assert msg.data == ['','5F01','66FC','AA','9390','6373'] assert msg.render(checksum=False) == data
def handle(self): global gpsData # self.request is the TCP socket connected to the client self.data = self.request.recv(1024).strip() rawdata = self.data # uncomment following lines to debug # print self.data # print "{} wrote:".format(self.client_address[0]) if "GET /gps.json HTTP/1.1" in rawdata: response_proto = "HTTP/1.1" response_status = "200" response_status_text = "OK" response_headers = { "Access-Control-Allow-Origin": "*", "Content-Length": len(json.dumps(gpsData)), "Content-Type": "text/html; encoding=utf8", "Connection": "close", } response_headers_raw = "".join("%s: %s\n" % (k, v) for k, v in response_headers.iteritems()) self.request.send("%s %s %s" % (response_proto, response_status, response_status_text)) self.request.send("\n") self.request.send(response_headers_raw) self.request.send("\n") # to separate headers from body self.request.send(json.dumps(gpsData)) elif "HTTP/1." in rawdata: response_proto = "HTTP/1.1" response_status = "404" response_status_text = "Not Found" self.request.send("%s %s %s" % (response_proto, response_status, response_status_text)) else: for line in rawdata.splitlines(): # cradelpoint devices append the device ID to the front of the NEMA sentence gpsId = line.split(",")[0] # store the rest of the sentence as the valid NEMA sentence rawNema = line.split(",", 1)[-1] # parse the message using pynema2 parsedNema = pynmea2.parse(rawNema) if isinstance(parsedNema, pynmea2.types.talker.GGA): # GGA sentence detected, collect position data updateTime = datetime.datetime.combine(datetime.datetime.utcnow().date(), parsedNema.timestamp) ggaData = { "info": gpsId, "lat": parsedNema.latitude, "lng": parsedNema.longitude, "updt": updateTime.isoformat(), } gpsData.setdefault(gpsId, ggaData) gpsData[gpsId].update(ggaData) elif isinstance(parsedNema, pynmea2.types.talker.VTG): # VTG sentence detected, collect speed data vtgData = {"info": gpsId, "spd": parsedNema.spd_over_grnd_kmph} gpsData.setdefault(gpsId, vtgData) gpsData[gpsId].update(vtgData)
def test_r00(): data = "$GPR00,A,B,C*29" msg = pynmea2.parse(data) assert msg.talker == 'GP' assert msg.sentence_type == 'R00' assert msg.waypoint_list == ['A','B','C'] msg.waypoint_list = ['ABC','DEF'] assert str(msg) == "$GPR00,ABC,DEF*42"
def streamFromSerial(self): while not self.stopped(): if self.openSerialConnection(): data = self.serial_reference.readline() if data[0:6] == '$GPGGA': msg = pynmea2.parse(data) print (msg) self.newGPSPoint(msg.latitude, msg.longitude, msg.altitude) eventlet.sleep()
def test_corrupt_message(): # data is corrupt starting here ------------------------------v data = '$GPRMC,172142.00,A,4805.30256324,N,11629.09084774,W,0.D' # fails with strict parsing with pytest.raises(pynmea2.ChecksumError): msg = pynmea2.parse(data, check=True) # lazy parsing succeeds msg = pynmea2.parse(data, check=False) assert isinstance(msg, pynmea2.types.RMC) # corrupt data assert msg.spd_over_grnd == '0.D' # missing data assert msg.true_course == None # renders unchanged assert msg.render(checksum=False) == data
def shodan_search_worker(fk, query, search_type, category, country=None, coordinates=None, all_results=False): results = True page = 1 SHODAN_API_KEY = keys['keys']['shodan'] pages = 0 screenshot = "" print(query) while results: if pages == page: results = False break # Shodan sometimes fails with no reason, sleeping when it happens and it prevents rate limitation search = Search.objects.get(id=fk) api = Shodan(SHODAN_API_KEY) fail = 0 try: time.sleep(5) if coordinates: results = api.search("geo:" + coordinates + ",20 " + query, page) if country: results = api.search("country:" + country + " " + query, page) except: fail = 1 print('fail1, sleeping...') if fail == 1: try: time.sleep(10) if coordinates: results = api.search("geo:" + coordinates + ",20 " + query, page) if country: results = api.search("country:" + country + " " + query, page) except Exception as e: print(e) if fail == 1: try: time.sleep(10) if coordinates: results = api.search("geo:" + coordinates + ",20 " + query, page) if country: results = api.search("country:" + country + " " + query, page) except Exception as e: print(e) if fail == 1: try: time.sleep(10) if coordinates: results = api.search("geo:" + coordinates + ",20 " + query, page) if country: results = api.search("country:" + country + " " + query, page) except Exception as e: results = False print(e) try: total = results['total'] if total == 0: print("no results") break except Exception as e: print(e) break pages = math.ceil(total / 100) + 1 print(pages) for counter, result in enumerate(results['matches']): lat = str(result['location']['latitude']) lon = str(result['location']['longitude']) city = "" indicator = [] # print(counter) # time.sleep(20) try: product = result['product'] except: product = "" if 'vulns' in result: vulns = [*result['vulns']] else: vulns = "" if result['location']['city'] != None: city = result['location']['city'] hostnames = "" try: if 'hostnames' in result: hostnames = result['hostnames'][0] except: pass try: if 'SAILOR' in result['http']['title']: html = result['http']['html'] soup = BeautifulSoup(html) for gps in soup.find_all("span", {"id": "gnss_position"}): coordinates = gps.contents[0] space = coordinates.split(' ') if "W" in space: lon = "-" + space[2][:-1] else: lon = space[2][:-1] lat = space[0][:-1] except Exception as e: pass if 'opts' in result: try: screenshot = result['opts']['screenshot']['data'] with open( "app_kamerka/static/images/screens/" + result['ip_str'] + ".jpg", "wb") as fh: fh.write(base64.b64decode(screenshot)) fh.close() for i in result['opts']['screenshot']['labels']: indicator.append(i) except Exception as e: pass if query == "Niagara Web Server": try: soup = BeautifulSoup(result['http']['html'], features="html.parser") nws = soup.find("div", {"class": "top"}) indicator.append(nws.contents[0]) except: pass # get indicator from niagara fox if result['port'] == 1911 or result['port'] == 4911: try: fox_data_splitted = result['data'].split("\n") for i in fox_data_splitted: if "station.name" in i: splitted = i.split(":") indicator.append(splitted[1]) except: pass # get indicator from tank if result['port'] == 10001: try: tank_info = result['data'].split("\r\n\r\n") indicator.append(tank_info[1]) except: pass if result['port'] == 2000: try: ta_data = result['data'].split("\\n") indicator.append(ta_data[1][:-3]) except Exception as e: pass if result['port'] == 502: try: sch_el = result['data'].split('\n') if sch_el[4].startswith("-- Project"): indicator.append(sch_el[4].split(": ")[1]) except: pass if "GPGGA" in result['data']: try: splitted_data = result['data'].split('\n') for i in splitted_data: if "GPGGA" in i: msg = pynmea2.parse(i) lat = msg.latitude lon = msg.longitude break except Exception as e: print(e) if result['port'] == 102: try: s7_data = result['data'].split("\n") for i in s7_data: if i.startswith("Plant"): indicator.append(i.split(":")[1]) if i.startswith("PLC"): indicator.append(i.split(":")[1]) if i.startswith("Module name"): indicator.append(i.split(":")[1]) except: pass # get indicator from bacnet if result['port'] == 47808: try: bacnet_data_splitted = result['data'].split("\n") for i in bacnet_data_splitted: if "Description" in i: splitted1 = i.split(":") indicator.append(splitted1[1]) if "Object Name" in i: splitted2 = i.split(":") indicator.append(splitted2[1]) if "Location" in i: splitted3 = i.split(":") indicator.append(splitted3[1]) except: pass device = Device(search=search, ip=result['ip_str'], product=product, org=result['org'], data=result['data'], port=str(result['port']), type=search_type, city=city, lat=lat, lon=lon, country_code=result['location']['country_code'], query=search_type, category=category, vulns=vulns, indicator=indicator, hostnames=hostnames, screenshot=screenshot) device.save() page = page + 1 if not all_results: results = False
def test_BOD(): data = "XXBOD,045.,T,023.,M,DEST,START" msg = pynmea2.parse(data) assert isinstance(msg, pynmea2.BOD) assert msg.talker == 'XX'
import serial import pynmea2 import time port = "/dev/ttyACM1" serialPort = serial.Serial(port, baudrate=9600, timeout=0.01) # port2 = "/dev/ttyACM1" # serialPort2 = serial.Serial(port2, baudrate = 9600, timeout = 0.01) while True: gps_data = serialPort.readline() gps_str = gps_data.decode("utf-8") # print(gps_str) if gps_str.find('GGA') > 0: # print('test1') msg = pynmea2.parse(gps_str) #__________ # print(msg) # print(msg.lon) # print("Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Satellites: %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units,msg.num_sats)) #___________ lat_string = msg.lat lon_string = msg.lon if (lat_string.find(".") > 0 and lon_string.find(".") > 0): lat_DD = int(float(lat_string) / 100) lon_DD = int(float(lon_string) / 100) lat_SS = float(lat_string) - lat_DD * 100 lon_SS = float(lon_string) - lon_DD * 100 lat = lat_DD + lat_SS / 60 lon = lon_DD + lon_SS / 60 if (msg.lat_dir == "S"):
parser = argparse.ArgumentParser() parser.add_argument('--input-path', type=str, help='Enter the NMEA.txt directory.') parser.add_argument('--output-path', type=str, help='Enter the PATH.kml directory.') args = parser.parse_args() print("parse args complete") return args if __name__ == '__main__': my_args = parseArgs() # read nmea file and save the coordinates output_list = [] nmea_file = open(my_args.input_path, 'r') for nmea_line in nmea_file: if nmea_line.startswith( '$GNGGA'): # this line contains the information we need record = NmeaHandle.parse(nmea_line) s = str(record.longitude) + ',' + str( record.latitude) + ',' + '1' + '\n' output_list.append(s) nmea_file.close() # write all the coordinates into the .kml kml_file = open(my_args.output_path, 'w') kml_file.writelines(output_list) kml_file.close() print('finished all.')
# an error occurs while True: # Establish connection with client. c, addr = s.accept() print('Got connection from', addr) #now=datetime.utcnow() #ora = '%02d-%02d-%02d %02d:%02d:%02d'%(now.year, now.month, now.day, now.hour, now.minute, now.second) data = c.recv(1024) #dati=data.split() #print(dati) #latitude(deg) longitude(deg) height(m) Q ns sdn(m) sde(m) sdu(m) sdne(m) sdeu(m) sdun(m) age(s) ratio # print('Data:{}'.format(dati[0].decode('utf-8'))) try: dati=data.split() #print(dati msg=pynmea2.parse(dati[0].decode('utf-8')) now=datetime.utcnow() ora = '%02d-%02d-%02d %02d:%02d:%02d'%(now.year, now.month, now.day, now.hour, now.minute, now.second) print('ora UTC {}'.format(ora)) print('latitudine {}'.format(msg.latitude)) print('longitudine {}'.format(msg.longitude)) print('altitude {}'.format(msg.altitude)) print('qualita fissaggio {}'.format(msg.gps_qual)) except: print('messaggio da ref station') #print(type(dati[0].decode('utf-8'))) #print('Ora:{}'.format(dati[1].decode('utf-8'))) #print('Lat:{}'.format(float(dati[2]))) #print('Lon:{}'.format(float(dati[3])))
def gps_imu_2020(netCDFfiles): ncOut = Dataset("gps.nc", 'w', format='NETCDF4') ncOut.instrument = "CSIRO ; GPS/IMU Logger" ncOut.instrument_model = "SOFS-7.5" ncOut.instrument_serial_number = "2018" compress = False # uncompressed day 608MB, 667MB compressed, RAW data is 178 MB, uncompressed is smaller than compressed # add time tDim = ncOut.createDimension("TIME") ncTimesOut = ncOut.createVariable("TIME", "d", ("TIME", ), zlib=compress) ncTimesOut.long_name = "time" ncTimesOut.units = "days since 1950-01-01 00:00:00 UTC" ncTimesOut.calendar = "gregorian" ncTimesOut.axis = "T" ncOut.createDimension("VECTOR", 3) # add variables accel_out_var = ncOut.createVariable("ACCEL", "f4", ("TIME", "VECTOR"), fill_value=np.nan, zlib=compress) gyro_out_var = ncOut.createVariable("GYRO", "f4", ("TIME", "VECTOR"), fill_value=np.nan, zlib=compress) compass_out_var = ncOut.createVariable("COMPASS", "f4", ("TIME", "VECTOR"), fill_value=np.nan, zlib=compress) typeCount = [0, 0, 0, 0, 0, 0, 0, 0, 0] imu_timedelta = timedelta(seconds=1 / 40.033) imu_ts = None ts_start = None gps_gga_ts = None gps_gga_timedelta = timedelta(seconds=0.2) print("file to process", len(netCDFfiles[1:])) imu_n = 0 imu_total = 0 last_ts = None parser = core.Parser([ACK_CLS, NAV_CLS, RXM]) for fn in netCDFfiles[1:]: print(fn) with open(fn, "rb") as f: errorCount = 0 while True: byte = f.read(7) if not byte: break (magic, type, N, seq) = struct.unpack("<HBHH", byte) print('magic 0x%04x type %d N %d seq %d' % (magic, type, N, seq)) pos = f.tell() if magic != 0xAA55: f.seek(-6, 1) print('skipping', pos) continue if (type < 10) & (N < 2048): print("type ", types[type]) pkt = f.read(N) check = f.read(1) if not check: break checkB = ord(check) s = 0 for b in pkt: s = s + b s = s & 0xff if (checkB == s): typeCount[type] = typeCount[type] + 1 try: if type == 4: # print("pkt " , ord(pkt[0]), ord(pkt[1]), ord(pkt[2]), ord(pkt[3]), ord(pkt[4]), ord(pkt[5]), ord(pkt[6]), ord(pkt[7])) time = struct.unpack("<BBBBBBH", pkt) ts = datetime(time[6], time[5], time[4], time[2], time[1], time[0]) if last_ts: time_d = ts - last_ts print("imu sample/sec", imu_n / time_d.total_seconds()) last_ts = ts print( "time %4d-%02d-%02d %02d:%02d:%02d : %s imu samples %d" % (time[6], time[5], time[4], time[2], time[1], time[0], ts, imu_n)) imu_ts = ts if not ts_start: ts_start = ts imu_n = 0 elif type == 8: nmea = pkt.decode('utf-8') print("%d pos NMEA %s" % (pos, nmea[:-2])) try: msg = pynmea2.parse(nmea[:-2]) if msg.sentence_type == 'RMC': print(msg.timestamp, msg.latitude, msg.longitude, msg.datestamp) gps_gga_ts = datetime.combine( msg.datestamp, msg.timestamp) else: print(gps_gga_ts, msg.timestamp, msg.latitude, msg.longitude, msg.altitude) if gps_gga_ts: gps_gga_ts = gps_gga_ts + gps_gga_timedelta except pynmea2.nmea.ParseError: pass elif type == 1: ubx = struct.unpack("<BBBBH", pkt[0:6]) print("ubx ", ubx) if (ubx[0] == 181) & (ubx[1] == 98): cls_name, msg_name, payload = parser.receive_from( io.BytesIO(pkt)) print(cls_name, msg_name) #, payload) elif type == 7: text = pkt.decode('utf-8') text = text[:-1] print("text : %s" % text) elif type == 5: # print("pkt " , ord(pkt[0]), ord(pkt[1]), ord(pkt[2]), ord(pkt[3])) adc = struct.unpack("<i", pkt) #print "adc ", adc[0] elif type == 6: # print("pkt " , ord(pkt[0]), ord(pkt[1]), ord(pkt[2]), ord(pkt[3])) print("len ", len(pkt)) count = struct.unpack("<HHHHHHHHH", pkt) print("count ", count) print("found ", typeCount) elif type == 3: # there are 2440 of these in 1 min, or 40.67 / sec imu = struct.unpack("<hhhhhhhhhhlllll", pkt) #print ("imu " , imu) print( "%s : imu %d : compass %f %f %f, gyro %f %f %f, accel %f %f %f" % (imu_ts, imu_n, imu[0] * 4915.0 / 32768, imu[1] * 4915.0 / 32768, imu[2] * 4915.0 / 32768, imu[3] * 2000.0 / 32768, imu[4] * 2000.0 / 32768, imu[5] * 2000.0 / 32768, imu[6] * 4.0 / 32768, imu[7] * 4.0 / 32768, imu[8] * 4.0 / 32768)) imu_n += 1 if imu_ts: #print("imu ts", imu_ts) ncTimesOut[imu_total] = date2num( imu_ts, calendar=ncTimesOut.calendar, units=ncTimesOut.units) accel_out_var[imu_total] = [ imu[6] * 4.0 / 32768, imu[7] * 4.0 / 32768, imu[8] * 4.0 / 32768 ] gyro_out_var[imu_total] = [ imu[3] * 2000.0 / 32768, imu[4] * 2000.0 / 32768, imu[5] * 2000.0 / 32768 ] compass_out_var[imu_total] = [ imu[0] * 4915.0 / 32768, imu[1] * 4915.0 / 32768, imu[2] * 4915.0 / 32768 ] imu_ts += imu_timedelta imu_total += 1 elif type == 2: imu = struct.unpack("<hhhhhhhhhhlllll", pkt) print("imu", imu_n, imu) imu_n += 1 #print imu[0], imu[1], imu[2], imu[3], imu[4], imu[5], imu[6], imu[7], imu[8] else: print("Unknown type ", type) except (UnicodeDecodeError): print(pos, " Error : ", pkt, "type ", type) errorCount = errorCount + 1 else: print(pos, " sum error ", checkB, s) f.seek(pos) print("error count", errorCount) # add some summary metadata ncTimeFormat = "%Y-%m-%dT%H:%M:%SZ" ncOut.setncattr("time_coverage_start", ts_start.strftime(ncTimeFormat)) ncOut.setncattr("time_coverage_end", imu_ts.strftime(ncTimeFormat)) # add creating and history entry ncOut.setncattr("date_created", datetime.utcnow().strftime(ncTimeFormat)) ncOut.setncattr( "history", datetime.utcnow().strftime("%Y-%m-%d") + " created from file " + netCDFfiles[1]) ncOut.close()
def main(): global iotHubClient, serialStream, averageCounter, currentCounter, averageCounterMoving, averageLat, averageLon, averageAlt, speedAvgCycles, s speedAvg = 0.0 # in theory, we get a new trip name every time we boot tripName = 'Trip' + datetime.now().strftime('%Y-%m-%d_%H:%M:%S') speed = 0.0 sequence = 1 try: print('starting main.py') iotHubClient = iothub_client_init() while True: sentence = serialStream.readline() if sentence.find('VTG') > 0: # we are working with accelerometer data speed = sentence.split(',') if len(speed[7]) > 0: speed = float(speed[7]) * .62137 speedAvg = ((speedAvg * speedAvgCycles) + round(speed, 0)) / (speedAvgCycles + 1) else: print('No Acceleeromoter data') if sentence.find('GGA') > 0: validData = True # we are working with lat long data try: data = pynmea2.parse(sentence) except: print('Exception in pynmea2.parse(' + sentence + ')') validData = False if (validData and (len(data.num_sats) > 0) and (data.num_sats > 4)): averageLat = ((averageLat * currentCounter) + float(data.latitude)) / (currentCounter + 1) averageLon = ((averageLon * currentCounter) + float(data.longitude)) / (currentCounter + 1) averageAlt = ((averageAlt * currentCounter) + float(data.altitude)) / (currentCounter + 1) latlong = {'lat':averageLat, 'long':averageLon, 'speed':round(speed, 0)} s.sendto(json.dumps(latlong), ('192.168.1.255', 2000)) currentCounter = currentCounter + 1 if averageCounter/(1+speed*.05) < currentCounter: cDateTime = str(datetime.now()) gpsmessage = {'event': 'gpsMessage', 'sequence':sequence, 'speedMPH': round(speed, 0), 'tripName': tripName, 'latitude': round(averageLat, 6), 'longitude': round(averageLon, 6), 'altitude': round(averageAlt, 1), 'satelliteCount': data.num_sats, 'averageCount': averageCounter, 'submissionTime': cDateTime} sequence = sequence + 1 # first we write to a local file, in case we can't write to IoT Hub file = open('/opt/gps/gps.json','a') file.write(json.dumps(gpsmessage) + '\r') print(json.dumps(gpsmessage)) try: iotHubClient = iothub_client_init() sendMessage(json.dumps(gpsmessage)) except: print ("exception") currentCounter = 0 else: print('Not enough satellites') except IoTHubError as e: print("Unexpected error %s from IoTHub" % e) except KeyboardInterrupt: print("IoTHubClient stopped") except: print("Unknown Error") print_last_message_time(iotHubClient)
def test_ubx03(): data = '$PUBX,03,20,3,e,281,72,36,062,5,e,034,10,23,000,8,U,328,11,44,064,9,-,323,-2,,000,13,-,341,01,,000,16,U,307,45,49,064,18,e,144,18,,000,21,U,150,74,35,037,25,e,134,06,,000,27,U,271,20,52,064,29,U,074,36,36,063,31,U,209,26,37,040,120,e,210,31,,000,126,-,157,33,,000,66,U,036,19,34,015,67,e,090,20,22,000,68,-,136,00,,000,73,e,273,60,47,064,74,U,330,24,44,064,80,U,193,36,36,023*33' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ubx.UBX03 assert msg.num_sv == 20
def test_TXT(): data = '$GNTXT,01,01,02,ROM BASE 2.01 (75331) Oct 29 2013 13:28:17*44' msg = pynmea2.parse(data) assert type(msg) == pynmea2.talker.TXT assert msg.text == 'ROM BASE 2.01 (75331) Oct 29 2013 13:28:17'
import pynmea2 import RPi.GPIO as gpio gpio.setmode(gpio.BCM) port = "/dev/ttyAMA0" ser = serial.Serial(port, baudrate=9600, timeout=0.5) try: while 1: try: data = ser.readline() except: print("Loading") if data[0:6] == '$GPGGA': msg = pynmea2.parse(data) latval = msg.lat concatlat = "Lat: " + str(latval) print(concatlat) longval = msg.lon concatlong = "Long: " + str(longval) print(concatlong) time.sleep(5) except KeyboardInterrupt: time.sleep(2) # gps connection: # VCC-> 5v/3.3v GND->GND
PORT = "/dev/ttyUSB1" ser = serial.Serial(PORT, baudrate=9600, timeout=1) time.sleep(0.5) ##Firebase DB setup fdb = FirebaseDB() model = fdb.new_model("NMEA") reply = ser.read(ser.inWaiting()) reply = reply.decode(errors='replace').split("\n") gp = "" for line in reply: if "$GPGGA" in line: gp = line # print("GP:", gp) if gp: nmeaobj = pynmea2.parse(gp) data = { nmeaobj.fields[i][0]: nmeaobj.data[i] for i in range(len(nmeaobj.fields)) } data = json.dumps(data) fdb.push_new(model, data) print(data) else: print("No data recevied") ser.close()
def to_kafka_png(): # 儲存原始gps訊號資料 gps = open("gps"+name+".nmea","ab") # 累計型資料初始化 distance = 0 avg_pace = 0 count = 0 # 繪製紀錄圖座標初始化 pace_list = [] time_list = [] distance_list = [] while True: # 本次資料傳輸時間 t = datetime.datetime.now() line = ser.readline() gps.write(line) # 將二位元資料轉為字串並用套件解析 record = pynmea2.parse(str(line)[2:-5]) # 取得緯度、精度及海拔 if str(line)[5:8] == "GGA": Latitude = record.latitude Longitude = record.longitude Altitude = record.altitude # 製作傳送到kafka資料 if Latitude != "" or Longitude != "" or Altitude != "": payload1 = {"records": [{"value": {"device_id": id, "timestamp": t, "Latitude": Latitude, "Longitude": Longitude, "Altitude": Altitude}}]} # 傳送資料到kafka r1 = requests.post(gps1, data=json.dumps(payload1), headers=headers) if r1.status_code != 200: print(r1.text) # 取得有關速度、距離資料 elif str(line)[5:8] == "VTG": # 計算運動總時間、總距離 now = datetime.datetime.now() diff = (now - start).seconds h = diff // 3600 m = (diff % 3600) // 60 s = diff % 60 time_total = "{0:02d}:{1:02d}:{2:02d}".format(h, m, s) speed = float(str(line).split(",")[7]) pace = round(60 / float(speed), 1) distance += round(speed * diff / 3600, 1) if speed != "": payload2 = {"records": [ {"value": {"device_id": id, "timestamp": t, "start": start.strftime("%Y-%m-%d %H:%M:%S"), "pace": pace, "time_total": time_total, "diff": diff, "distance": distance}}]} r2 = requests.post(gps2, data=json.dumps(payload2), headers=headers) if r2.status_code != 200: print(r2.text) count += 1 avg_pace = (pace + avg_pace) / count # 繪製運動紀錄圖 pace_list.append(pace) distance_list.append(distance) time_list.append(diff * 10) plt.subplot(2, 1, 1) plt.plot(time_list, pace_list) plt.ylabel("Pace") plt.subplot(2, 1, 2) plt.plot(time_list, distance_list) plt.ylabel("Distance") plt.xlabel("Time") plt.savefig('runarea' + name + '.png') print(time_list)
params = {} params['device_number'] = dev_num gps_data = gps_ser.readline() # print gps_data # while gps_data[0:6] != '$GPGGA' : # gps_data = gps_ser.readline() for i in range(0, 7): gps_data = gps_ser.readline() if gps_data[0:6] == '$GPGGA': break if gps_data[0:6] == '$GPGGA': msg = pynmea2.parse(gps_data) if msg.lat == "": msg.lat = "0.0" b = eval(msg.lat) / 100 a = str(b).split(".") clat = a[0] + "." + str(eval(a[1]) / 60) if msg.lon == "": msg.lon = "0.0" z = eval(msg.lon) / 100 x = str(z).split(".") clon = x[0] + "." + str(eval(x[1]) / 60) params['tra_lat'] = str(clat) params['tra_lon'] = str(clon) if params['tra_lat'] == "0.0": params['tra_lat'] = "*****" if params['tra_lon'] == "0.0":
print('compressed: ' + str(messageSize) + ' uncompressed: ' + str(len(''.join(str(e) for e in messageToSend)))) addVehicleInfo() # send the message! sendMessage(str(messageToSend)) messageToSend = [] sentence = serialStream.readline() if sentence.find('VTG') > 0: speed = sentence.split(',') if len(speed[7]) > 0: speed = float(speed[7]) * .62137 if sentence.find('GGA') > 0: gpsdata = pynmea2.parse(sentence) #print(gpsdata) if (gpsdata.num_sats > 4): currentTime = str(datetime.now()) message = { 'type': 'location', 'lat': float(gpsdata.latitude), 'lon': float(gpsdata.longitude), 'alt': float(gpsdata.altitude), 'sats': int(gpsdata.num_sats), 'speed': int(speed), 'currentTime': currentTime, 'bootTime': bootTime, 'sequence': sequence } #print(message)
print ('\nPress Ctrl + C to quit collecting information\n') try: with serial.Serial(USBPORT, BAUD_RATE) as ser: count = 0 while(True): gpsStartTime = timeit.default_timer() # Throw out old GPS data ser.read(ser.inWaiting()) # Read new GPS data. gpsString = ser.readline() # If the string is a GPS message, capture data. if(gpsString[0:6] == '$GPGGA'): count += 1 completeStartTime = timeit.default_timer() # Parse the GPS message. msg = pynmea2.parse(gpsString, BAUD_RATE) print str(msg.timestamp) print "latitude: " + str(msg.latitude) + " " + msg.lat_dir \ + ", longitude: " + str(msg.longitude) + ", " + msg.lon_dir \ + ", HAE: " + str(msg.altitude) + ", " + msg.altitude_units.lower() \ + ", # sats: " + msg.num_sats gpsStopTime = timeit.default_timer() - gpsStartTime print 'GPS time: ' + str(gpsStopTime) + ' s' # TODO: Check that the number of sats is > 4. # If not, sound an error beep? # Get the system date as YYYY-MM-DD sysDate = datetime.datetime.now().strftime('%Y-%m-%d') # Get the GPS time as HH-MM-SS and append to the system date sysTime = sysDate + " " + msg.timestamp.strftime('%H-%M-%S-%f')
def parseGPS(mystr): if mystr.find('GGA') > 0: msg = pynmea2.parse(mystr) return msg else : return None
ggaList = [None] * int(numLines / 3) rmcList = [None] * int(numLines / 3) ppsList = np.zeros(int(numLines / 3)) serList = np.zeros(int(numLines / 3)) numSats = np.zeros(int(numLines / 3)) fix = np.zeros(int(numLines / 3)) file = open("../../results/" + fileList[num], 'r') # Extract data from file. Format is $GPGGA...\n$GPRMC...\nt<SERIAL MILLIS><PPS MILLIS>\n print("Extracting data...") for i, line in enumerate(file): j = math.floor(i / 3) if line.startswith("$GPGGA"): ggaList[j] = pynmea2.parse(line) numSats[j] = ggaList[j].num_sats fix[j] = ggaList[j].gps_qual elif line.startswith("$GPRMC"): rmcList[j] = pynmea2.parse(line) elif line.startswith("t"): lineTemp = line[1:] lineTemp = lineTemp.split(",") serList[j] = float(lineTemp[0]) ppsList[j] = float(lineTemp[1]) print("Data extracted.") print("Processing data...") #ggaList = ggaList[:1000] #serList = serList[:1000]
ssl._create_default_https_context = ssl._create_unverified_context cj=cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) r=opener.open('https://192.168.179.116/login.cgi') login_data=urllib.urlencode({'username':user, 'password':pswd,'action':'login'}) r=opener.open('https://192.168.179.116/login.cgi',login_data) data=dict() print "login success" while(1): a=1 while a: rcv = port.readline() # print rcv[0:6] if rcv[0:6] == '$GPGGA': msg=pynmea2.parse(rcv) lat=msg.lat print lat lon=msg.lon print lon a=0 GPIO.output(17,GPIO.LOW) GPIO.output(27,GPIO.LOW) print "next set" status_page=opener.open('https://192.168.179.116/status.cgi') status=status_page.read() json_status=json.loads(status) signal=json_status['wireless']['signal'] noise=json_status['wireless']['noisef'] ccq=json_status['wireless']['ccq'] distance=json_status['wireless']['distance']
def GNSS(self): #self.text_log_feld.insert('end','GNSS is Running 1 !!!\n') print('GNSS: hat die ProzessID ') print(os.getpid()) self.text_log_feld.insert('end', '\nStart GNSS-Procces \n') i = 1 serialPort = serial.Serial(port, baudrate=9600, timeout=0.5) print("warm up GPS") while True: try: str = serialPort.readline() str = str.decode("utf-8") if str.find('GGA') > 0: msg = pynmea2.parse(str) print(i, msg) values = str.split(",") gps_time_utc = float(values[1]) y = values[2].replace(".", "") x = values[4].replace(".", "") print('test') altitude = float(values[9]) satellites = int(msg.num_sats) deg_lat = float(y[0:2]) min_lat = float(y[2:]) / 10E4 deg_lon = float(x[1:3]) min_lon = float(x[3:]) / 10E4 lat = round(deg_lat + min_lat / 60, 5) lon = round(deg_lon + min_lon / 60, 5) if i >= 5: self.gps["lat"].append(lat) self.gps["lon"].append(lon) self.gps["satellites"].append(satellites) self.gps["altitude"].append(altitude) if i == 15: self.mean_gps = { "lat": round(np.mean(self.gps["lat"]), 6), "lon": round(np.mean(self.gps["lon"]), 6), "altitude": round(np.mean(self.gps["altitude"]), 1), "satellites": round(np.mean(self.gps["satellites"]), 1) } print(self.gps) sql = '''INSERT INTO public.gps (lat, lon, altitude, satellite) VALUES (%s, %s, %s, %s);''' vals = [ self.mean_gps['lat'], self.mean_gps['lon'], self.mean_gps['altitude'], self.mean_gps['satellites'] ] db.execute((sql, vals)) print('in db geschreieben') self.text_log_feld.insert( 'end', 'Average measurements saved to database! \n') print(os.getpid()) return True i += 1 except Exception as e: print(e) print("no gpsinfo") self.text_log_feld.insert('end', 'No Gps data could be received! \n') return False
def test_ash(): data = '$PASHR,LTN,3*3D' msg = pynmea2.parse(data) assert type(msg) == pynmea2.ash.ASHRLTN assert msg.sentence_type == 'LTN' assert msg.latency == 3
import time import serial import pynmea2 import string ser = serial.Serial('./dev/ttyTHS1', 115200, timeout=0.2) while True: line = ser.readline() line = str(line, encoding='utf-8') if line.startswith('$GPRMC'): rmc = pynmea2.parse(line) print('Latitude:', rmc.latitude) print('Longitude:', rmc.longitude) print('Speed:', rmc.spd_over_grnd * 1.85) if line.startswith('$GPGGA'): rmc2 = pynmea2.parse(line) print('Sats:', rmc2.num_sats) with open('getGps.txt', "a+") as file: file.write(line.strip()) file.write("\n") file.close()
import pandas as pd # Try loop that allows for a graceful exit if needed try: while True: # Sets the address of the NEO 6M GPS Sensor and delcares # variables for simplification later on port="/dev/ttyAMA0" ser=serial.Serial(port, baudrate=9600, timeout=0.5) dataout = pynmea2.NMEAStreamReader() newdata=ser.readline() if newdata[0:6] == "$GPRMC": newmsg=pynmea2.parse(newdata) # Reads the sensor data lat = newmsg.latitude # Reads the data, assigns the Latitude to a variable long = newmsg.longitude # Reads the data, assigns the Longitude to a variable time = newmsg.timestamp # Reads the data, assigns the timestamp to a variable gps = "Latitude= " + str(lat) + " and Longitude= " + str(long) # Sets this string as a variable, gps print(gps) # Prints out the above string # Writes said string to a .csv file csvfile = "raw_gps_data.csv" with open(csvfile, "a") as fp: wr = csv.writer(fp, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) wr.writerow([lat, long, time]) # Formats the .csv file it just wrote to, adds column names, saves as new .csv df = pd.read_csv('raw_gps_data.csv', header=None) df.rename(columns={0: 'Latitude', 1: 'Longitude', 2: 'Timestamp'}, inplace=True) df.to_csv('gps_mapping_data.csv', index=False)
import serial import pynmea2 import pdb import string ser = serial.Serial('/dev/ttyUSB0', 4800, timeout=1) line = ser.readline() while True: line = ser.readline() try: msg = pynmea2.parse(line) if msg.sentence_type == 'MWD': print("True Wind (knots): " + str(msg).split(",")[5] + " True Wind (m/s): " + str(msg).split(",")[7]) if msg.sentence_type == 'MWV': print("Wind Speed: " + msg.wind_speed) #if msg.sentence_type == 'GGA': # print "lat: " + str(msg.latitude) + " long: " + str(msg.longitude) ''' if msg.sentence_type == 'XDR' and msg.type == 'A': print str(msg).split(",")[6]''' except: "Error!"
def serial_def(): ser = serial.Serial() ser.port = "/dev/ttyUSB2" ser.baudrate = 115200 ser.bytesize = serial.EIGHTBITS #number of bits per bytes ser.parity = serial.PARITY_NONE #set parity check: no parity ser.stopbits = serial.STOPBITS_ONE #number of stop bits ser.timeout = 2 #timeout block read ser.xonxoff = False #disable software flow control ser.rtscts = False #disable hardware (RTS/CTS) flow control ser.dsrdtr = False #disable hardware (DSR/DTR) flow control ser.writeTimeout = 2 ser.open() if ser.isOpen(): print(ser.name + ' is open...') ser.write("AT+CGPSINFO=0" + "\r\n") ser.write("ATE1" + "\r\n") ser.write("ATI" + "\r\n") ser.write("AT+CGPS=1,1" + "\r\n") ser.write("AT+CGPSOUT=8" + "\r\n") ser.write("AT+CGPSINFO=5" + "\r\n") time.sleep(1) out = '' while True: out += ser.read(1) out2 = ser.readline() #print("out1: " + out) if re.match(r"CGPSINFO:", out2): print("out2: " + out2) print(out2.split(",")) out3 = ((out2.split(",")[0].split(": ")[1:] + out2.split(",")[1:-1] + out2.split(",")[-1].split("\r")[0:]))[0:-1] msg = pynmea2.GGA('GP', 'GSV', out3) lat = int(str(float(out3[0].strip()[:-1]) / 100).split(".")[0]) lng = int(str(float(out3[2].strip()[:-1]) / 100).split(".")[0]) latdec = float("." + str(float(out3[0].strip()[:-1]) / 100).split(".")[1]) * 60 lngdec = float("." + str(float(out3[2].strip()[:-1]) / 100).split(".")[1]) * 60 latmin = int(str(latdec).split(".")[0]) lngmin = int(str(lngdec).split(".")[0]) latsec = float("." + str(latdec).split(".")[1]) * 60 lngsec = float("." + str(lngdec).split(".")[1]) * 60 print(pynmea2.parse(str(msg))) print("Latitude: " + str(lat) + " Degrees " + str(latmin) + " Minutes " + str(latsec) + " Seconds ") #str(msg.lat)) print("Longitude: " + str(lng) + " Degrees " + str(lngmin) + " Minutes " + str(lngsec) + " Seconds ") #str(msg.lon)) print("Alt: " + str(msg.altitude) + " Meters") #print(pynmea2.parse(out)) ser.close()
def parseGPS(str): if str.find('GGA') > 0: msg = pynmea2.parse(str) print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s" % ( msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units)
# find GGA lines and sort into arrays for GGA lines and times linesGGA = [] timeD = [] # Delta time (length of GPS second) for i in range(len(lines)): if (len(lines[i]) > (6 + 14)): # check whether line is long enough if (lines[i][:6] == "$GPGGA"): # check that the format is GPGGA contents = lines[ i] # want to remove the additional time to get into GGA commaLoc = -1 # store location of commas (want to find last entry) while (',' in contents[max(0, commaLoc) + 1:]): # check whether there is another comma commaLoc = contents.index(',', max(0, commaLoc) + 1) linesGGA.append(pynmea2.parse(contents[:commaLoc])) timeD.append(int(contents[commaLoc + 1:])) timeAvg = sum(timeD) / len(timeD) print(timeAvg) timeV = [0 for i in range(len(linesGGA)) ] # Variation in absolute time (since start) according to GPS # uses avg. GPS second as time unit qCon = [0 for i in range(len(linesGGA))] # quality of connection for i in range(len(linesGGA)): # get information for timeV and qCon qCon[i] = linesGGA[i].gps_qual if (i > 0): timeV[i] = timeD[i] + timeV[i - 1] - timeAvg else:
def readdzg(fi, frmt, spu, traces, verbose=False): ''' a parser to extract gps data from DZG file format DZG contains raw NMEA sentences, which should include RMC and GGA fi = file containing gps information frmt = format ('dzg' = DZG file containing gps sentence strings (see below); 'csv' = comma separated file with) spu = samples per unit (second or meter) traces = the number of traces in the file Reading DZG We need to relate gpstime to scan number then interpolate for each scan between gps measurements. NMEA GGA sentence string format: $GPGGA,UTC hhmmss.s,lat DDmm.sss,lon DDDmm.sss,fix qual,numsats,hdop,mamsl,wgs84 geoid ht,fix age,dgps sta.,checksum *xx if we want the date, we have to read RMC sentences as well: $GPRMC,UTC hhmmss,status,lat DDmm.sss,lon DDDmm.sss,SOG,COG,date ddmmyy,checksum *xx RMC strings may also be useful for SOG and COG, ...but let's keep it simple for now. ''' trace = 0 # the elapsed number of traces iterated through tracenum = 0 # the sequential increase in trace number rownp = 0 # array row number rowrmc = 0 # rmc record iterated through (gps file) rowgga = 0 # gga record timestamp = False prevtime = False td = False prevtrace = False rmc = False antname = False lathem = 'north' lonhem = 'east' x0, x1, y0, y1, z0, z1 = False, False, False, False, False, False # coordinates with open(fi, 'r') as gf: if frmt == 'dzg': # if we're working with DZG format for ln in gf: # loop through the first few sentences, check for RMC if ln.startswith( '$GPRMC' ): # check to see if RMC sentence (should occur before GGA) rmc = True if rowrmc == 10: # we'll assume that 10 samples is a safe distance into the file to measure frequency msg = pynmea2.parse(ln.rstrip( )) # convert gps sentence to pynmea2 named tuple ts0 = datetime.combine( msg.datestamp, msg.timestamp) # row 0's timestamp if rowrmc == 11: msg = pynmea2.parse(ln.rstrip()) ts1 = datetime.combine( msg.datestamp, msg.timestamp) # row 1's timestamp td = ts1 - ts0 # timedelta = datetime1 - datetime0 rowrmc += 1 if ln.startswith('$GPGGA'): if rowgga == 10: msg = pynmea2.parse(ln.rstrip( )) # convert gps sentence to pynmea2 named tuple ts0 = datetime.combine( datetime(1980, 1, 1), msg.timestamp) # row 0's timestamp (not ideal) if rowgga == 11: msg = pynmea2.parse(ln.rstrip()) ts1 = datetime.combine( datetime(1980, 1, 1), msg.timestamp) # row 1's timestamp (not ideal) td = ts1 - ts0 # timedelta = datetime1 - datetime0 rowgga += 1 if rowgga != rowrmc: print( "WARNING: GGA and RMC sentences are not recorded at the same rate! This could cause unforseen problems!" ) print('rmc records: %i' % rowrmc) print('gga records: %i' % rowgga) gpssps = 1 / td.total_seconds() # GPS samples per second if verbose: print('found %i GPS epochs at rate of %.1f Hz' % (rowrmc, gpssps)) dt = [('tracenum', 'float32'), ('lat', 'float32'), ('lon', 'float32'), ('altitude', 'float32'), ('geoid_ht', 'float32'), ('qual', 'uint8'), ('num_sats', 'uint8'), ('hdop', 'float32'), ('gps_sec', 'float32'), ('timestamp', 'datetime64[us]')] # array columns arr = np.zeros( (int(traces) + 1000), dt ) # numpy array with num rows = num gpr traces, and columns defined above if verbose: print('creating array of %i interpolated gps locations...' % (traces)) gf.seek(0) # back to beginning of file for ln in gf: # loop over file line by line if rmc == True: # if there is RMC, we can use the full datestamp if ln.startswith('$GPRMC'): msg = pynmea2.parse(ln.rstrip()) timestamp = TZ.localize( datetime.combine( msg.datestamp, msg.timestamp)) # set t1 for this loop else: # if no RMC, we best hope there is no UTC 0:00:00 in the file......... if ln.startswith('$GPGGA'): msg = pynmea2.parse(ln.rstrip()) timestamp = TZ.localize( msg.timestamp) # set t1 for this loop if ln.startswith('$GPGGA'): sec1 = float(ln.split(',')[1]) msg = pynmea2.parse(ln.rstrip()) x1, y1, z1, gh, q, sats, dil = float(msg.lon), float( msg.lat), float(msg.altitude), float(msg.geo_sep), int( msg.gps_qual), int(msg.num_sats), float( msg.horizontal_dil) if msg.lon_dir in 'W': lonhem = 'west' x1 = -x1 if msg.lat_dir in 'S': lathem = 'south' y1 = -y1 if prevtime: # if this is our second or more GPS epoch, calculate delta trace and current trace elapsedelta = timestamp - prevtime # t1 - t0 in timedelta format elapsed = float( (elapsedelta).total_seconds()) # seconds elapsed if elapsed > 3600.0: print( "WARNING: Time jumps by more than an hour in this GPS dataset and there are no RMC sentences to anchor the datestamp!" ) print( "This dataset may cross over the UTC midnight dateline!\nprevious timestamp: %s\ncurrent timestamp: %s" % (prevtime, timestamp)) print("trace number: %s" % trace) tracenum = round( elapsed * spu, 8 ) # calculate the increase in trace number, rounded to 5 decimals to eliminate machine error trace += tracenum # increment to reflect current trace resamp = np.arange( math.ceil(prevtrace), math.ceil(trace), 1 ) # make an array of integer values between t0 and t1 for t in resamp: x = (x1 - x0) / (elapsed) * ( t - prevtrace) + x0 # interpolate latitude y = (y1 - y0) / (elapsed) * ( t - prevtrace) + y0 # interpolate longitude z = (z1 - z0) / (elapsed) * ( t - prevtrace) + z0 # interpolate altitude sec = (sec1 - sec0) / (elapsed) * ( t - prevtrace) + sec0 # interpolate gps seconds tracetime = prevtime + timedelta( seconds=elapsedelta.total_seconds() * (t - prevtrace)) tup = (t, x, y, z, gh, q, sats, dil, sec, tracetime) arr[rownp] = tup rownp += 1 else: # we're on the very first row if verbose: print('using %s and %s hemispheres' % (lonhem, lathem)) x0, y0, z0, sec0 = x1, y1, z1, sec1 # set xyzs0 for next loop prevtime = timestamp # set t0 for next loop prevtrace = trace if verbose: print('processed %i gps locations' % rownp) diff = rownp - traces shift, endshift = 0, 0 if diff > 0: shift = diff / 2 if diff / 2 == diff / 2.: endshift = shift else: endshift = shift - 1 arrend = traces + endshift arr = arr[shift:arrend:1] if verbose: print( 'cut %i rows from beginning and %s from end of gps array, new size %s' % (shift, endshift, arr.shape[0])) # if there's no need to use pandas, we shouldn't (library load speed mostly, also this line is old): #array = pd.DataFrame({ 'ts' : arr['ts'], 'lat' : arr['lat'], 'lon' : arr['lon'] }, index=arr['tracenum']) elif frmt == 'csv': arr = '' return arr
def getGPSData(self): if self.newdata[0:6] == "$GPGGA": newmsg = pynmea2.parse(self.newdata) return newmsg
#https://sparklers-the-makers.github.io/blog/robotics/use-neo-6m-module-with-raspberry-pi/ #https://github.com/mikechan0731/GY-91_and_PiCamera_RaspberryPi #Chequear en internet para printear también la Velocidad y la Altitud. import serial import time import string import pynmea2 while True: port = "/dev/ttyAMA0" ser = serial.Serial(port, baudrate=9600, timeout=0.5) dataout = pynmea2.NMEAStreamReader() newdata = ser.readline() if newdata[0:6] == "$GPRMC": newmsg = pynmea2.parse(newdata) lat = newmsg.latitude long = newmsg.longitude gps = "Lat: " + str(lat) + " Long: " + str(long) print(gps) #Output: #Lat: XX.XXXXXXX Long: XX.XXXXXXX #Lat: XX.XXXXXXX Long: XX.XXXXXXX ''' GY-213 HDC1080 - Humedad - Temperatura ''' #Configurar desde Rasp | Posibles enlaces con info:
# This code is written to parse the NMEA messages # and store Lat/Long/Alt data to a csv file #************* USBPORT = 'COM4' # go to Device Manager to check for comm port number BAUD_RATE = 4800 #FILENAME_INPUT = raw_input('Enter the file name: ') #FILENAME = FILENAME_INPUT + '.csv' FILENAME = 'GPStest.csv' print ("Press Ctrl + C to quit collecting information\n") with open(FILENAME, 'wb') as fp: a = csv.writer(fp, delimiter=',') try: while(1): with serial.Serial(USBPORT, BAUD_RATE) as ser: s0 = ser.read() #print s0 if(s0 == '$'): s1 = ser.readline() #print s1 msg = pynmea2.parse(s1, BAUD_RATE) #print msg if (s1[0:5] == 'GPGGA'): cols = [str(msg.timestamp), msg.latitude, msg.lat_dir, msg.longitude, msg.lon_dir, str(msg.altitude), msg.altitude_units, str(msg.num_sats)] a.writerow(cols) print cols except KeyboardInterrupt: pass