In this tutorial we will learn to track the in International Space Station's (ISS) GPS location to using Python and Ubidots. We'll be polling this service made possible by Nathan Bergey which will update Ubidots' cloud with the position of the ISS position in real-time. Then, with the raw position data provided, we'll explore how to measure distances and triangulate locations with Ubidots cloud software. 

Send date to Ubidots' cloud and create a map on your Dashboard to track the International Space Station in real-time!

From your Computer's Terminal

Requests: HTTP for Humans

Requests is a popular Python library that simplifies making HTTP requests. To begin we will install the Python packages using pip:

$ pip install requests

Sending data to Ubidots for visualization

Create and run a Python script in your computer's terminal:

$ nano

Then copy and paste the below code in your terminal; assigning your Ubidots TOKEN and location coordinates where indicated in the code. For more information, please see the Ubidots REST API Reference.   

NOTE: This script is just tracking the ISS position and using a standard formula to calculate its distance to a specific point in earth. It doesn't reflect real sighting opportunities as this depends on more on weather and time variables than simply proximity (i.e. location relative to you).

import requests
import time
from math import *

TOKEN = 'xxxxxxxx'  # Assign your Ubidots TOKEN
DEVICE_LABEL = 'iss'  # Assign the device label desired
VARIABLE_LABEL = "distance"


# Assign your home coordinates
LAT_HOME = 42.361145
LNG_HOME = -71.057083

def get_iss_position():
    # Get current ISS position
    req_iss = requests.get('')
    dict = req_iss.json()
    lat_lng = dict['iss_position']
    # save the current ISS  possition in new variables
    lat_iss = float(lat_lng['latitude'])
    lng_iss = float(lat_lng['longitude'])
    return lat_iss, lng_iss

def deg2rad(deg):
    return deg * (pi/180)

def getDistance(lat_iss, lng_iss, lat_home, lng_home):
    R = 6371  # Radius of the earth in km
    dLat = deg2rad(lat_home-lat_iss)  # deg2rad below
    dLng = deg2rad(lng_home-lng_iss)
    a = sin(dLat/2) * sin(dLat/2) + cos(deg2rad(lat_iss)) * \
        cos(deg2rad(lat_home)) * sin(dLng/2) * sin(dLng/2)
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    d = R * c  # Distance in km to Home
    return d

def build_payload(variable_label, value, lat_iss, lng_iss):
    # Build the payload to be sent
    payload = {variable_label: value, "position": {
        "value": 1, "context": {"lat": lat_iss, "lng": lng_iss}}}
    return payload

def send_ubidots(device_label, payload):
    # Make the HTTP request to Ubidots
    url = "{0}{1}/?token={2}".format(BASE_URL, device_label, TOKEN)
    status = 400
    attempts = 0

    while status >= 400 and attempts <= 5:
        req =, json=payload)
        status = req.status_code
        attempts += 1

    response = req.json()

    return response

def main(device_label, variable_label, lat_home, lng_home):
    # Get the current ISS position
    lat_iss, lng_iss = get_iss_position()
    # Caculate the distance in KM to Home
    distance = getDistance(lat_iss, lng_iss, lat_home, lng_home)
    distance = round(distance, 1)
    # Build the payload to be sent
    payload = build_payload(variable_label, distance, lat_iss, lng_iss)
    # Send the HTTP request to Ubidots
    response = send_ubidots(device_label, payload)

    return response

if __name__ == '__main__':
    while True:
            response = main(DEVICE_LABEL, VARIABLE_LABEL, LAT_HOME, LNG_HOME)
            print("response json from server: \n{0}".format(response))


Once the code has been completed with your Ubidots TOKEN and coordinates, press CONTROL + O and ENTER to save. To exit the Python editor, press CONTROL + X and return to the computer's terminal. 

NOTE: This script is tracking the ISS position and using a standard formula to calculate its distance to a specific point on earth. This will not reflect real sighting opportunities as this depends on more than simply proximity (i.e. time of the day and weather).

Now back in the computer's terminal, let's run the script and confirm our work. Paste and run the below script in your computer's terminal. 


If successful, your terminal will immediate begin reporting server responses.

To visualize your work in Ubidots, go to the Device Management tab of your account and visualize the device created :)

Did this answer your question?