172 lines
4.9 KiB
Python
172 lines
4.9 KiB
Python
print("\nchaos minimap!")
|
|
import os
|
|
import wifi
|
|
import adafruit_connection_manager
|
|
import adafruit_requests
|
|
import json
|
|
import time
|
|
import board
|
|
import busio
|
|
import displayio
|
|
import terminalio
|
|
from adafruit_display_text import label
|
|
|
|
# Import the SSD1306 module.
|
|
import adafruit_ssd1306
|
|
|
|
from secrets import hauk_endpoint,hauk_session,hauk_slug,hauk_name
|
|
|
|
# Create the I2C interface.
|
|
i2c = busio.I2C(board.D7, board.D8)
|
|
|
|
# Create the SSD1306 OLED class.
|
|
# The first two parameters are the pixel width and pixel height. Change these
|
|
# to the right size for your display!
|
|
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
|
|
|
|
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
|
|
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
|
|
requests = adafruit_requests.Session(pool, ssl_context)
|
|
|
|
locavg = []
|
|
def location_avg():
|
|
global locavg
|
|
if len(locavg) > 10:
|
|
locavg = locavg[1:]
|
|
lavg = list(zip(*locavg))
|
|
print(lavg[0], lavg[1])
|
|
lavg = (sum(lavg[0])/len(lavg[0]), sum(lavg[1])/len(lavg[1]))
|
|
return lavg
|
|
|
|
def update_location():
|
|
global locavg
|
|
print('scanning',)
|
|
wifi.radio.stop_scanning_networks()
|
|
time.sleep(2)
|
|
ns = wifi.radio.start_scanning_networks()
|
|
wifi_peers = []
|
|
for n in ns:
|
|
#print(n.ssid, n.bssid.hex(':'), n.rssi, n.channel)
|
|
wifi_peers.append({"bssid": n.bssid.hex(':'), "ssid": n.ssid, "rssi": n.rssi})
|
|
|
|
#print(wifi_peers)
|
|
wifi.radio.stop_scanning_networks()
|
|
print('loc request')
|
|
|
|
data = json.dumps(wifi_peers)
|
|
print(data)
|
|
lat,lon=0,0
|
|
with requests.post(f"{hauk_endpoint}/location", data=data) as response:
|
|
#print(response.text)
|
|
j = response.json()
|
|
if 'triangulation' in j:
|
|
print(j['triangulation'])
|
|
lat = j['triangulation']['Position']['Lat']
|
|
lon = j['triangulation']['Position']['Lon']
|
|
else:
|
|
print('no location received')
|
|
|
|
locavg.append((lat,lon))
|
|
lavg = location_avg()
|
|
print(lavg)
|
|
form_data={"sid":hauk_session, "lat":lavg[0],"lon":lavg[1], "time":0.0, "pwd":""}
|
|
print(form_data)
|
|
with requests.post(f"{hauk_endpoint}/api/post.php", data=form_data) as r:
|
|
print('post.php', r.text)
|
|
|
|
import math
|
|
|
|
def haversine(coord1, coord2):
|
|
"""
|
|
Calculate the great-circle distance between two points on the Earth
|
|
specified in decimal degrees (latitude and longitude).
|
|
|
|
Parameters:
|
|
coord1 (tuple): (latitude, longitude) for point 1
|
|
coord2 (tuple): (latitude, longitude) for point 2
|
|
|
|
Returns:
|
|
float: distance in kilometers between the two points
|
|
"""
|
|
|
|
# Convert latitude and longitude from degrees to radians
|
|
lat1, lon1 = map(math.radians, coord1)
|
|
lat2, lon2 = map(math.radians, coord2)
|
|
|
|
# Haversine formula
|
|
dlon = lon2 - lon1
|
|
dlat = lat2 - lat1
|
|
a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2
|
|
c = 2 * math.asin(math.sqrt(a))
|
|
|
|
# Radius of Earth in kilometers (mean radius)
|
|
r = 6371.0
|
|
|
|
return c * r
|
|
|
|
def haver_dist(coord1, coord2):
|
|
lati1 = math.radians(coord1[0])
|
|
long1 = math.radians(coord1[1])
|
|
lati2 = math.radians(coord2[0])
|
|
long2 = math.radians(coord2[1])
|
|
dlong = long2 - long1
|
|
dlati = lati2 - lati1
|
|
a = (
|
|
math.sin(dlati / 2) ** 2
|
|
+ math.cos(lati1) * math.cos(lati2) * math.sin(dlon / 2) ** 2
|
|
)
|
|
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
|
distance = R * c
|
|
return distance*1000
|
|
|
|
def bearing(coord1, coord2):
|
|
lati1 = math.radians(coord1[0])
|
|
long1 = math.radians(coord1[1])
|
|
lati2 = math.radians(coord2[0])
|
|
long2 = math.radians(coord2[1])
|
|
y = math.sin(long2 - long1) * math.cos(lati2)
|
|
x = math.cos(lati1) * math.sin(lati2) - math.sin(lati1) * math.cos(
|
|
lati2
|
|
) * math.cos(long2 - long1)
|
|
intbearing = math.atan2(y, x)
|
|
intbearing = math.degrees(intbearing)
|
|
intbearing = (intbearing + 360) % 360
|
|
return intbearing
|
|
|
|
def update_display():
|
|
display.fill(0)
|
|
display.text("freeside minimap!", 0, 0, 1)
|
|
l = {}
|
|
with requests.get(f"{hauk_endpoint}/api/fetch.php?id={hauk_slug}&since=0") as r:
|
|
#print('fetch.php', r.text)
|
|
l = r.json()
|
|
|
|
print(l)
|
|
|
|
d = location_avg()
|
|
for (name,val), i in zip(l['points'].items(), range(len(l['points']))):
|
|
if len(val) > 0:
|
|
d = haver_dist(d, (val[0][0],val[0][1]))
|
|
b = bearing(d, (val[0][0],val[0][1]))
|
|
else:
|
|
d = float('inf')
|
|
b = float('NaN')
|
|
print(name, ":", d, b)
|
|
display.text(f"{name}: {d} {b}", 0, 10*(i+1), 1)
|
|
display.show()
|
|
|
|
|
|
def main():
|
|
display.fill(0)
|
|
display.text("freeside minimap!", 0, 0, 1)
|
|
display.show()
|
|
|
|
while True:
|
|
try:
|
|
update_location()
|
|
update_display()
|
|
except Exception as e:
|
|
import traceback
|
|
traceback.print_exception(e)
|
|
|