People in Space Indicator

Use the Python requests module to access the People in Space API, and create an indicator to show the number of people currently in space.

On 15th December 2015, British ESA Astronaut Tim Peake set off for the International Space Station, where he will be living and working for six months. As well as the scientific research Tim will be performing while in space, he's also going to be running experiments using Python code written by school students on two Raspberry Pis, as part of the Astro Pi project. By working through this resource, you can keep count of Tim and his fellow astronauts on the ISS.

Browse the People in Space API

API stands for Application Programming Interface. One form of API is a web API, which can be used to extract data from a service provided online. Some APIs are open to the public and provide data free of charge; other APIs require a login to access them, which can be obtained with an application, or can be provided as a service to customers for a fee.

The purpose of providing data via an API is to allow people to access it for their own use or to build programs with it. Some APIs provide business-critical data sets or otherwise important data about things like the weather, financial activity, or even providing access to things like a Twitter stream or Flickr photographs from around the world. Other APIs are just for fun, like the Pokemon API, the Star Wars API or the Adventure Time API. While these APIs were made for fun, they can still be used in interesting and useful ways. You could build a Pokemon game in Minecraft, or build a quiz game about the characters in Adventure Time!

The format of this data is called JSON (pronounced "Jason"), which stands for JavaScript Object Notation. While the name refers to the programming language JavaScript, it's a standard data interchange format which is made to be portable between languages and applications. If you're familiar with Python's dictionary data structure, you'll see the similarities. In other languages, a nested array could be used in the same way.

Note that the API provides three pieces of data: message, number, and people.

message is a single string containing the word success

number is the integer value 6

people is an array of astronauts

Within the people array, there are six objects. Each object contains two pieces of data:

craft is the spacecraft on which the astronaut is present

name is the astronaut's full name

Using Python's requests module

Now you'll use the requests module in Python to access the API.

Open Python 3 from the main menu:

In the Python shell, type the following line and press Enter:

>>> import requests

(note the >>> is not typed, but denotes shell input)

You're using the Python shell's REPL (Read-Eval-Print Loop), which means each line is executed immediately, rather than writing a file, saving it and running all commands in one go

Now create a variable containing the URL of the API as a string:

url = "http://api.open-notify.org/astros.json"

Make a request to the API by entering:

r = requests.get(url)

Now simply type r and press Enter; this is the same as typing print(r) in a file. The REPL allows you to quickly inspect objects this way. You should now see the following:

<Response [200]>

This states that r contains a Response type object, and shows 200. This is the status code of the request, which means "Success". Other status codes include 404, which means "File not found", and 500 - "Server error".

Now enter help(r). This shows the docstring (documentation string) for the Response object. You should see:

Help on Response in module requests.models object:
class Response(builtins.object)
| The :class:`Response <Response>` object, which contains a
| server's response to an HTTP request.
...

Observe the section of the docstring headed Data descriptors defined here. You'll see a list of available properties and their descriptions, including:

apparent_encoding

content

is_permanent_redirect

is_redirect

links

ok

text

These properties are essentially variables accessible within an object. For example, r.ok is a variable containing the success status of the request (True or False).

Adding LEDs

Next, you'll connect some LEDs to the Pi's GPIO pins and use each of them to represent a person in space.

Start by using a male-to-female jumper wire to connect one of the Pi's ground pins to the breadboard, and add a resistor to connect it to the ground rail:

Now connect a single LED to your breadboard with the shorter leg (cathode) to the ground rail and place the longer leg (anode) in the middle of the breadboard. Then use a male-to-female jumper wire to connect the LED's row to GPIO pin 2 on the Pi:

Now, rather than just printing n, you can use it to determine how many LEDs should be lit. Consider the following loop:

for led in leds:
led.on()

This allows you to access each LED in turn. However, we need to know at what point we should stop turning them on.

You'll need to be able to compare the number in n to each LED number (the order in the sequence, not the GPIO pin number). You'll need to use the enumerate function to assign a number to each LED in sequence. See how enumerate works by trying it out in the shell (not the file):

As you can see, this provides a list of the LEDs associated with index numbers, starting from 0.

Since enumerate returns two values, the index number and the LED object, you can loop over it and access both values by using for i, led in enumerate(leds). Add the following loop to the code in your file:

for i, led in enumerate(leds):
if n > i:
led.on()
else:
led.off()

Save and run your code. Assuming the API returns 6, you should have 6 LEDs light up!

You'll want to check the API for changes periodically. It's important to make sure you're not making too many requests to the API. Start by importing sleep by adding the import at the top of the file with the others:

from time import sleep

Now wrap the requests and LED code in a while loop to make it run continuously and keep updating the LEDs:

Ensure the sleep is added at the end, so it waits 60 seconds between API calls to check the result every minute.

Run the code and it should always show the current number of people in space. Leave it running and it should update in the future, as astronauts are delivered to and from the International Space Station.

Try making your own indicator unit out of wood or cardboard:

Astronaut names

Finally, learn to get the astronaut names from the API call.

When you originally inspected the JSON object, it showed the names of all the people in space as well as just the number. Return to the Python shell and inspect j['people'] from the dictionary: