Last active
June 29, 2020 21:06
-
-
Save dennisheitmann/4c3e56f5438c6aa8b54bcaa858b89730 to your computer and use it in GitHub Desktop.
Banggood H02 GPS Tracker (Product ID: 1031247) on own server (Change server IP to your server's IP and port to 5000 via SMS on the tracker before)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import socket | |
import datetime | |
import sys | |
import geopy | |
import gpxpy | |
import gpxpy.gpx | |
import matplotlib | |
matplotlib.use('agg') | |
import matplotlib.pyplot as plt | |
import smopy | |
mapfile = 'track.png' | |
my_lat = 47.85 | |
my_lon = 12.55 | |
plusminus = 0.004 | |
zoom_lvl = 15 | |
filename = 'track.gpx' | |
gpx = gpxpy.gpx.GPX() | |
gpx_track = gpxpy.gpx.GPXTrack() | |
gpx.tracks.append(gpx_track) | |
gpx_segment = gpxpy.gpx.GPXTrackSegment() | |
gpx_track.segments.append(gpx_segment) | |
HOST = '' | |
PORT = 5000 | |
socket.setdefaulttimeout(5.0) | |
old_x = 0 | |
old_y = 0 | |
try: | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
while True: | |
all_data = [] | |
try: | |
conn, addr = s.accept() | |
# print('MSG', end=': ') | |
# print(datetime.datetime.utcnow(), end=' ') | |
# print(addr, end=' ') | |
with conn: | |
while True: | |
data = conn.recv(32) | |
# print(data) | |
all_data.append(data.decode()) | |
if not data: | |
break | |
except socket.timeout: | |
try: | |
conn.close() | |
except: | |
pass | |
pass | |
p = geopy.point.Point() | |
if len(all_data) > 0: | |
# print(all_data) | |
gps_list = ''.join(all_data).split(',') | |
if gps_list[0] != '*hq': | |
del gps_list[0] | |
if len(gps_list) > 7: | |
if (gps_list[0] == '*hq' and gps_list[2] == 'VP1' and gps_list[3] == 'B'): | |
geo_str = gps_list[4][:2] + ' ' + gps_list[4][2:] + "' " + gps_list[5] + ' ' + gps_list[6][:3]+ ' ' + gps_list[6][3:] + "' " + gps_list[7] | |
p = geopy.point.Point(geo_str) | |
print(datetime.datetime.utcnow(), end=';') | |
print(addr[0], end=';') | |
print(gps_list[1], end=';') | |
print(p.latitude, end=';') | |
print(p.longitude, end=';') | |
print() | |
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(latitude=p.latitude, longitude=p.longitude, time=datetime.datetime.utcnow())) | |
my_lat = p.latitude | |
my_lon = p.longitude | |
gps_list.clear() | |
sys.stdout.flush() | |
try: | |
map = smopy.Map(smopy.correct_box((my_lat+plusminus, my_lon+plusminus, my_lat-plusminus, my_lon-plusminus), z=zoom_lvl), z=zoom_lvl) | |
ax = map.show_mpl(figsize=(10, 15), dpi=256) | |
if p.latitude != 0 and p.longitude != 0: | |
x, y = map.to_pixels(lat=p.latitude, lon=p.longitude) | |
# x, y = map.to_pixels(48.0, 12.5) | |
# print(x) | |
# print(y) | |
old_x = x | |
old_y = y | |
ax.plot(x, y, 'or') | |
else: | |
ax.plot(old_x, old_y, 'or') | |
plt.show() | |
plt.savefig(mapfile) | |
plt.close() | |
# print('plotted') | |
except: | |
# raise | |
pass | |
except KeyboardInterrupt: | |
fh = open(filename, 'w') | |
fh.write(gpx.to_xml()) | |
fh.close() | |
print('Shutdown by CTRL-C...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, what sms commands did you use to change the server of the device ?