Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
@ehiosud
Latest commit 69d1c1b Aug 1, 2024 History
3 contributors

Users who have contributed to this file

@ehiosud @olayinka2 @ndigwee
import json
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/ZZ/#"
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")
#initialize zoom level
zoom_level = 15
# Function to update the map with the latest location
def update_map():
if latest_location:
map_widget.set_position(latest_location[0], latest_location[1])
map_widget.set_zoom(zoom_level)
map_widget.set_marker(latest_location[0], latest_location[1], text="Current Location")
window.after(1000, update_map)
# Functions to zoom in and out
def zoom_in():
global zoom_level
if zoom_level < 19: # Maximum zoom level for TkinterMapView
zoom_level += 1
map_widget.set_zoom(zoom_level)
def zoom_out():
global zoom_level
if zoom_level > 1: # Minimum zoom level for TkinterMapView
zoom_level -= 1
map_widget.set_zoom(zoom_level)
# Zoom buttons
button_zoom_in = Button(window, text="Zoom In", command=zoom_in)
button_zoom_in.place(relx=0.9, rely=0.1, anchor="center")
button_zoom_out = Button(window, text="Zoom Out", command=zoom_out)
button_zoom_out.place(relx=0.9, rely=0.2, anchor="center")
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker
client.connect(MQTT_BROKER, MQTT_PORT)
client.loop_start()
# Start updating the map
update_map()
try:
window.mainloop()
except KeyboardInterrupt:
print("Exiting...")
# Clean up
client.loop_stop()
client.disconnect()