SparkFun will be closed on Tuesday, November 5th to support our team in being able to go out and vote! Orders placed after 2 pm MT Monday, November 4th will ship on Wednesday, November 6th. Thanks for your patience and understanding.
One of the most powerful tools available to tinkerers and prototypers is the Application Program Interface. An API is the structure by which two computer programs talk to each other – think of it as a traffic cop for information traveling between two applications.
In a workshop at SXSW EDU last year, I encountered NASA's open data platform. My mind was blown by the fact that a good percentage of NASA's projects reveal their data and code through an open API, and it's all fairly well supported and reasonably easy to get started with.
To begin, I navigated to the cover page for the Open Data Portal. From there I selected the data catalog; I'm pretty into the asteroid thing these days, so I used the Near Earth Object database.
From here I clicked on the API button:
This brought me to the opening page for the NeoWs.
If you go to the Getting Started documents, it will ask you to sign up for a development key. You don't necessarily need one; you can browse the API catalog and pull out example URLs and use them at will without a key. I chose object 2018GG – there's a nifty visual of this house-sized space rock here.
After pulling out the URL for the data on 2018GG, I put together a simple Python script that I ran in Trinket.
import urllib.request
contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3542519?api_key=DEMO_KEY").read()
print(contents)
When I ran this, I actually got a return in the console matching all the data available for 2018GG:
I called in the urllib library and used the request method. Next, I created a variable and stored the return coming from the URL connected to 2018GG. Finally, I printed out what's stored in the variable. That’s a lot of data for this simple call. We can simplify things by loading everything into a Json object using the code below.
import urllib.request
import json
contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read()
varab = json.loads(contents)
print(varab)
This gave me a value and an object notation connected to the value, all comma-separated.
I wanted to find out what the key titles associated with each element of data are, so I wrote a short script that separates out and prints just the keys.
import urllib.request
import json
contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read()
varab = json.loads(contents)
print(varab.keys())
The printout now shows a whole list of the keys I can work with.
Continuing to narrow things down, I picked out a single element from my list of keys and added some text to it and built a small, very specific printout. I separated out the element line I really wanted by a set of asterisks for readability.
import urllib.request
import json
contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read()
varab = json.loads(contents)
print(varab.keys())
print('*' * 79)
print(('object 3802394 is: ')+varab['close_approach_data'][0]['miss_distance']['miles']+(" miles away!!!"))
print('*' * 79)
The fun part of all this is that not only can this be implemented in the Trinket environment like I've used here, but it's a great project for Python on the Raspberry Pi. Further steps might include a warning light or buzzer if an object got too close for comfort. This example would employ the GPIO pins on the Raspberry Pi and give a great overview of connected data through an API and physical computing. Many thanks to Cameron here at SFE for wisdom and help in getting this all going!
I hope you have fun with this and happy hacking.
For more fun, check out NASA openMCT https://nasa.github.io/openmct/
I'd be fun to see an AVC project use NASA mission control software to visualize performance!
Oh man, this is great!!! I am digging into this, Thanks !!!
Thanks, this is awesome.
If you use it, let us know how it goes!!
The link to www.trinket.io above that's broken due to an extra space character probably didn't help out Clark either.
<a href="http://www.Trinket%20.io">Trinket</a>
Fixed! Many Thanks and good catch.
Maybe I shouldn't read blog posts before I've even finished my first cup of coffee in the morning... it took me a bit to figure out that you weren't referring to the Adafruit Trinket...