Linux Format

Space station detector

Les Pounder shows us how to create an Internatio­nal Space Station alert system, so that we never miss it going by overhead.

- Les Pounder is a freelance maker who works with organisati­ons such as the Raspberry Pi Foundation to promote maker skills.

Les Pounder shows us how to create an Internatio­nal Space Station alert system so we never miss it going overhead!

Here’s an interestin­g little project in which we’ll learn how to get data from the web and use it to tell us when the Internatio­nal Space Station will fly overhead, with the help of Pimoroni’s Unicorn HAT LED board (£25.50 from http://bit.ly/ lxf251unic­orn).

Ground control

With the Raspberry Pi unpowered, connect the Unicorn HAT so that it fits neatly on top of the Pi, connecting to all 40 GPIO pins. Connect your keyboard, mouse, HDMI and power to the Pi and boot to the desktop. You will need the Pi connected to the internet for this.

In a terminal, type the following and press Return to install the Unicorn HAT Python library.

$ curl https://get.pimoroni.com/unicornhat | bash

Follow the install instructio­ns and keep the terminal open, as we need to install more libraries.

>>Installing the Python Libraries<<

For the project we need two extra Python 3 libraries. The first is called ‘requests’ and is used to work with data from the web. The second is called ‘geopy’ and provides us with a location based on our IP address. In the terminal, type the following to install these:

$ sudo pip3 install requests geopy

After a few moments the libraries will be installed and we can close the terminal.

Using your favourite Python 3 editor on the Raspberry Pi, create a new file and then save the project as Iss-alert.py. We start by importing the libraries for the project, ‘requests’ and ‘unicornhat’.

import requests import unicornhat

Then we import two functions from two more libraries. From Geopy we import Nominatim , which is a function to enable our code to look up a location using Openstreet­map. The second is the sleep function, to control the speed of our project.

from geopy.geocoders import Nominatim from time import sleep

To use the Nominatim location function we shall create an object called geolocator :

geolocator = Nominatim()

To access the JSON data we need to provide the URL of where it is located. ISS = “http://api.open-notify.org/iss-now.json”

How do we find our location? Using geopy, we create an object called home and in it we store our geolocatio­n data. home = geolocator.geocode(“your TOWN/CITY HERE”)

To store the latitude data from home we need to create a variable called home_lat . We then ensure that the data stored is a float data type, as we shall be doing a little maths with it later. home_lat = float(home.latitude)

We then do the same with the longitude data. home_lon = float(home.longitude)

We then print the location data to the shell to check that it is correct. print(“home”) print(home_lat, home_lon)

The main loop of our code is where we use while True: to run constantly. The first action in the loop is to get the latest location of the ISS using the JSON file. So we get that data and store it in a dictionary called r. while True: r = requests.get(iss)

JSON data is contained in { } brackets and uses a key: value structure to store data. In Python this is called a dictionary, and we can retrieve data from a dictionary by giving the name of the dictionary and

passing the key that we wish to see the value of. So for the latitude of the ISS we create a variable called lat and use that to store the JSON data stored in r, specifical­ly the data for [‘iss_position’][‘latitude’] . We also wrap the data in a float() helper function that will ensure that our data is a decimal number (floating point number), as we need to use the data later.

lat = float(r.json()[‘iss_position’][‘latitude’])

We do the same for the ISS longitude.

lon = float(r.json()[‘iss_position’][‘longitude’])

Now print the ISS latitude and longitude to the Python shell for debug.

print(lat, lon)

Here we have a big line of code, but to break it down think of it as a question: “Is the position of the ISS near to my home position?” For this we use an if conditiona­l test that checks to see if the latitude and longitude of the ISS is greater than or equal to our home latitude and less than or equal to our home longitude. We subtract 1 from our home latitude and longitude as this will give us approximat­ely 5-10 minutes to get outside and see the ISS.

if lat >= (home_lat -1) and lat <= (home_lat) and lon >= (home_lon -1) and lon <= (home_lon):

Sitting in a tin can

So what happens if the ISS happens to be above us? Well, we first print to the Python shell that it is indeed overhead. Then we use two for loops, one for x and another for y . These relate to the x and y axis of our Unicorn HAT board.

PRINT(“WARNING: ISS IS ALMOST OVERHEAD”) for x in range(8):

for y in range(8):

Using these two for loops we instruct the Unicorn HAT to set the LED (pixel) at position x,y to red (255,0,0 in RGB colour codes) and then we tell Python to wait for 0.01 seconds before showing the change on the Unicorn HAT.

unicornhat.set_pixel(x,y,255,0,0) sleep(0.01) unicornhat.show()

Breaking out of the two for loops we now tell Python to wait for 15 seconds before turning off the LEDS, show the change to the user and than wait for a further 45 seconds before repeating the ISS location check process.

sleep(15) unicornhat.clear() unicornhat.show() sleep(45)

If the ISS is not nearby, our else condition is activated and we use the exact same for loop structure to turn the LEDS green for 15 seconds and then turn them off for 45 seconds.

else: print(“iss NOT NEAR”) for x in range(8): for y in range(8): unicornhat.set_pixel(x,y,0,255,0) sleep(0.01) unicornhat.show() sleep(15) unicornhat.clear() unicornhat.show() sleep(45)

Now save the code and open a terminal. The Unicorn HAT can only be used with sudo, so to run our code we need to type:

$ sudo python3 Iss-alert.py

You should see the Unicorn HAT light up to show the status. If it goes red, run outside and look to the skies for the Internatio­nal Space Station!

 ??  ?? Alert yourself to the location of the Internatio­nal Space Station using a few lines of Python and 64 ultrabrigh­t neopixel LEDS.
Alert yourself to the location of the Internatio­nal Space Station using a few lines of Python and 64 ultrabrigh­t neopixel LEDS.
 ??  ??
 ??  ?? Even the humble Pi Zero W can run this project. In fact the Unicorn HAT board is twice the size of the Pi Zero W!
Even the humble Pi Zero W can run this project. In fact the Unicorn HAT board is twice the size of the Pi Zero W!

Newspapers in English

Newspapers from Australia