thatiotguy
Published © LGPL

Tracking the International Space Station (ISS) Using Python

"Track the ISS and its crew with Python! Real-time data on the International Space Station and its astronauts. 🚀🛰️ #Python #ISSTracking"

BeginnerFull instructions provided30 minutes15
Tracking the International Space Station (ISS) Using Python

Story

Read more

Code

ISS Tracker Code

Python
import requests
import json
from math import radians, sin, cos, sqrt, atan2

# Function to calculate the distance between two sets of coordinates
def calculate_distance(lat1, lon1, lat2, lon2):
    # Radius of the Earth in kilometers
    earth_radius = 6371.0

    # Convert latitude and longitude from strings to floats and then to radians
    lat1 = radians(float(lat1))
    lon1 = radians(float(lon1))
    lat2 = radians(float(lat2))
    lon2 = radians(float(lon2))

    # Haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    distance = earth_radius * c

    return distance

# Function to get ISS data
def get_iss_data():
    url = "http://api.open-notify.org/iss-now.json"
    response = requests.get(url)

    if response.status_code == 200:
        data = json.loads(response.text)
        return data
    else:
        return None

# Function to display ISS details
def display_iss_data(data):
    if data:
        timestamp = data["timestamp"]
        latitude = data["iss_position"]["latitude"]
        longitude = data["iss_position"]["longitude"]

        print(f"ISS details as of {timestamp}:")
        print(f"Latitude: {latitude}")
        print(f"Longitude: {longitude}")

        # Coordinates of Bangalore
        bangalore_lat = 12.9716
        bangalore_lon = 77.5946

        # Calculate the distance and altitude
        distance = calculate_distance(bangalore_lat, bangalore_lon, latitude, longitude)
        print(f"Distance from Bangalore to ISS: {distance} km")
    else:
        print("Failed to retrieve ISS data. Please check your internet connection.")

# Function to get information about astronauts on the ISS
def get_iss_astronauts():
    url = "http://api.open-notify.org/astros.json"
    response = requests.get(url)

    if response.status_code == 200:
        data = json.loads(response.text)
        return data
    else:
        return None

# Function to display information about astronauts on the ISS
def display_iss_astronauts(astronauts_data):
    if astronauts_data:
        print("Astronauts currently on the ISS:")
        for astronaut in astronauts_data["people"]:
            name = astronaut["name"]
            craft = astronaut["craft"]
            print(f"{name} on board {craft}")
    else:
        print("Failed to retrieve ISS astronaut data. Please check your internet connection.")

if __name__ == "__main__":
    iss_data = get_iss_data()
    display_iss_data(iss_data)

    # Example: Get ISS pass times for New York City
    #pass_times_data = get_iss_pass_times(40.7128, -74.0060)
    #display_iss_pass_times(pass_times_data)

    # Get information about astronauts on the ISS
    astronauts_data = get_iss_astronauts()
    display_iss_astronauts(astronauts_data)

Credits

thatiotguy
15 projects • 47 followers
World around Embedded System and IoT
Contact

Comments

Please log in or sign up to comment.