I have made simple Python program that I want to use on my phone, When I host the server on my laptop I am able to acces it on my laptop but when I try to acces it on my phone it doesn’t work
I am pretty new to python so I don’t know of I am missing something or whatever.
This is the code I use
<!DOCTYPE html>
<html>
<head>
<title>GPS Tracker</title>
</head>
<body>
<h1>GPS Tracker</h1>
<button onclick="startTracking()">Start Tracking</button>
<p id="speed">Speed: N/A</p>
<script>
let watchId;
function startTracking() {
if (navigator.geolocation) {
watchId = navigator.geolocation.watchPosition(sendPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function sendPosition(position) {
fetch('http://<my- IPv4-Address>:5000/update_location', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
})
.then(response => response.json())
.then(data => {
document.getElementById('speed').innerText = 'Speed: ' + data.speed.toFixed(2) + ' m/s';
})
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>
and the python file
from flask import Flask, request, jsonify
from geopy.distance import geodesic
import time
app = Flask(__name__)
previous_data = {
'latitude': None,
'longitude': None,
'timestamp': None
}
@app.route('/update_location', methods=['POST'])
def update_location():
global previous_data
data = request.json
latitude = data['latitude']
longitude = data['longitude']
timestamp = time.time()
if previous_data['latitude'] is not None and previous_data['longitude'] is not None:
prev_coords = (previous_data['latitude'], previous_data['longitude'])
curr_coords = (latitude, longitude)
distance = geodesic(prev_coords, curr_coords).meters
time_diff = timestamp - previous_data['timestamp']
if time_diff > 0:
speed = distance / time_diff
else:
speed = 0
else:
speed = 0
previous_data['latitude'] = latitude
previous_data['longitude'] = longitude
previous_data['timestamp'] = timestamp
return jsonify({'speed': speed})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
The site does work on my laptop but just not on my phone,sometimes it does seem to work but just unable to load and after that getting a time out “this website took to long to load”
I have tried the following things
- Used a different port
- Using the terminal outside of VSC