def get_gps(): if gps.get_satellites()[0] <1: print("satellite disconnect") return None # 卫星数量不足,无法定位 longtitude = round(gps.get_location()[1],6) latitue = round(gps.get_location()[0],6) return latitue,longtitude# tuple,纬度,经度
def delay(ms): global interrupt f = open("t/gps.log", "a") machine.set_idle(True) step = 1000 prev_gps_state = location.gps_state if ms > 5 * 60 * 1000: location.set_gps_state(False) while ms > 0: if interrupt: interrupt = False f.close() raise OSError(4) #EINTR if ms < step: step = ms rec = gps.nmea_data()[0] coords = gps.get_location() if rec[1]: f.write(str(rec) + " " + str(coords) + "\n") utime.sleep_ms(step) ms -= step machine.watchdog_reset() location.set_gps_state(prev_gps_state) machine.set_idle(False) f.close()
def get_coordinates(): global gps_state if gps_state and has_gps_fix(): coords = gps.get_location() return (coords[0], coords[1], True) return get_lbs_location()
def loop(): global beep_off_if_no_location_timestamp location = get_location() if location: timestamp = get_millis() lines = [] lines.append("%s:%d" % (get_status(location), location.hacc)) lines.append("TIME: %d" % (location.time)) lines.append("LAT: %f" % (location.lat)) lines.append("LON: %f" % (location.lon)) if (location.hacc <= 100): beep_off_if_no_location_timestamp = get_millis() nearest_station = get_nearest_station(location, stations) if nearest_station: distance = get_distance(location, nearest_station) lines.append("%.1fkm in %s" % (distance, nearest_station.name)) lines.append("%.1fkm/h" % (location.speed)) if (distance <= 1.0 and not leds.is_on()): leds.set(True) if (distance > 1.0 and leds.is_on()): leds.set(False) if (distance <= 0.3 and not pwm.is_beep()): pwm.set_beep(True) leds.set_sleep_timeout(400) if (distance > 0.3 and pwm.is_beep()): pwm.set_beep(False) leds.set_sleep_timeout() else: print("ERROR: no nearest station?") print("duration %d" % (millis_passed(timestamp))) else: if pwm.is_beep(): pwm.set_beep(False) if leds.is_on(): leds.set(False) oled.display_lines(lines) if beep_off_if_no_location_timestamp != 0 and millis_passed( beep_off_if_no_location_timestamp) > 10000: beep_off_if_no_location_timestamp = 0 print("No location in 10 seconds, turning off") if pwm.is_beep(): pwm.set_beep(False) if leds.is_on(): leds.set(False)
# Micropython a9g example # Source: https://github.com/pulkin/micropython # Author: pulkin # Demonstrates how to retrieve GPS coordinates from the built-in GPS module import gps gps.on() print("Location", gps.get_location()) print("Satellites (tracked, visible)", gps.get_satellites()) gps.off()
print("================") import gps gps.on() fw = gps.get_firmware_version() print("GPS firmware:", fw) assert len(fw) > 3 vis, tracked = gps.get_satellites() print("GPS sats:", vis, tracked) assert vis == int(vis) assert tracked == int(tracked) assert 0 <= tracked <= vis lat, lng = gps.get_location() print("GPS location:", lat, lng) assert -90 <= lat <= 90 assert -180 <= lng <= 180 gps.off() _lat, _lng = gps.get_last_location() print("GPS last location:", lat, lng) assert lat == _lat assert lng == _lng try: gps.on(0) raise ValueError("No GPS exception was raised") except gps.GPSError as e: print("GPS error OK:", e)
# Source: https://github.com/pulkin/micropython # Author: sebi5361 # Demonstrates how to interface with traccar # Get online import cellular import gps import socket ID = 76548 # Same arbitrary number as in Traccar url = "demo.traccar.org" port = 5055 cellular.gprs("internet", "", "") gps.on() loc = gps.get_location() s = socket.socket() s.connect((url, port)) s.send( bytes( 'POST /?id={}&lat={}&lon={} HTTP/1.1\r\nHost: {}:{}\r\n\r\n'.format( ID, loc[0], loc[1], url, port), 'utf8')) rsp = s.recv(50) s.close() gps.off() cellular.gprs(False) assert rsp == b'HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n'