Skip to content

Instantly share code, notes, and snippets.

@AgustinPelaez
Last active February 27, 2017 19:59
Show Gist options
  • Save AgustinPelaez/8463432 to your computer and use it in GitHub Desktop.
Save AgustinPelaez/8463432 to your computer and use it in GitHub Desktop.
Get the ISS position, calculate the distance to a specific place and post the value to Ubidots
from ubidots import ApiClient
import requests,time
from math import *
#Connect to Ubidots
api = ApiClient('a21ebaf64e14d195c0044fcxxb9f6dab9d653af3')
#Instantiate local variable from Ubidots
local_distance = api.get_variable('54ca7a2176254xxxfd4b9493f')
def main():
while(1):
#Get current ISS position
req_iss = requests.get('http://api.open-notify.org/iss-now.json')
dict = req_iss.json()
latlong = dict['iss_position'];
lat1 = latlong['latitude']
lon1 = latlong['longitude']
#Calculate Distance to Home
lat2 = 50.085305
lon2 = -5.315853
d = getDistance(lat1,lon1,lat2,lon2)
d = round(d,1)
#Send value to Ubidots
local_distance.save_value({'value':d,'context':{'lat':lat1,'lng':lon1}})
time.sleep(1)
def getDistance(lat1,lon1,lat2,lon2):
R = 6371; #Radius of the earth in km
dLat = deg2rad(lat2-lat1); # deg2rad below
dLon = deg2rad(lon2-lon1);
a = sin(dLat/2) * sin(dLat/2) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * sin(dLon/2) * sin(dLon/2)
c = 2 * atan2(sqrt(a), sqrt(1-a));
d = R * c; # Distance in km
return d;
def deg2rad(deg):
return deg * (pi/180)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment