Skip to content

Commit

Permalink
xyz
Browse files Browse the repository at this point in the history
  • Loading branch information
MAC authored and MAC committed Jul 27, 2024
1 parent 0d6b555 commit 055134e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 50 deletions.
Binary file modified .DS_Store
Binary file not shown.
51 changes: 1 addition & 50 deletions location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,4 @@
import time
import paho.mqtt.client as mqtt
from tkinter import Tk, Button
from tkintermapview import TkinterMapView

# MQTT settings
MQTT_BROKER = "broker.hivemq.com"
MQTT_PORT = 1883
MQTT_TOPIC_LOCATION = "owntracks/DE/#"
MQTT_TOPIC_UPDATE = "home/location_updates"

# Initialize MQTT client
client = mqtt.Client()

# Variable to store the latest location
latest_location = None

def on_connect(client, userdata, flags, rc):
print(f"Connected to MQTT broker with result code {rc}")
client.subscribe(MQTT_TOPIC_LOCATION)

def on_message(client, userdata, msg):
global latest_location
try:
payload = json.loads(msg.payload)
if 'lat' in payload and 'lon' in payload:
latest_location = (payload['lat'], payload['lon'])
print(f"Updated location: {latest_location}")
publish_location()
except Exception as e:
print(f"Failed to parse MQTT message: {e}")

def publish_location():
global coordinates
if latest_location:
location_data = {
"latitude": latest_location[0],
"longitude": latest_location[1],
"timestamp": int(time.time())
}
client.publish(MQTT_TOPIC_UPDATE, json.dumps(location_data))
coordinates = location_data

# Initialize coordinates with a default value
coordinates = {"latitude": 0.0, "longitude": 0.0}

# Tkinter setup
window = Tk()
window.title("Map Viewer")
window.config(width=800, height=600)

map_widget = TkinterMapView(window, width=800, height=600, corner_radius=0)
map_widget.place(relx=0.5, rely=0.5, anchor="center")
from tkintermapview import TkinterMapView

0 comments on commit 055134e

Please sign in to comment.