This example will help you to learn how to send GPS location to Ubidots using Python. We'll poll this service made possible by Nathan Bergey which updates the ISS position in real-time. By reading its live data we are able to measure the distance between the ISS and Boston, and then post the whole data to Ubidots.

Once the data is there, you can create a Map widget in your Dashboard to see the ISS moving in real-time!

You can also create a Ubidots Event to trigger an alert whenever the station is nearby. To do this, enter and update source code below with your Raspberry Pi's text editor. Be sure to update your credentials with your own API key and variable id, plus changing the coordinates to match your location. To learn how to send a context to the Ubidots API, chttps://ubidots.com/docs/devices/index.html#raspberry-piheck out our documentation.

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).

from ubidots import ApiClient
import requests,time
from math import *

#Connect to Ubidots

api = ApiClient(token='xxxxxxxxxxxxxxxxxxxxxxxxxx')

#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()

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).

Did this answer your question?