Update: another function has been added: the next time ISS will fly over the location of the RPI.
The description of this function is added in Step 2.
First of all my apologies for the poor quality of the video. I couldn’t find a way to capture the output of the lcd matrix decently and tried a lot of filters. It’s meant for a short display of the script parts, but doesn’t come near the live experience . You might get the picture by observing it through your eye lashes. ;-0)
The python script applies all of the SenseHat sensors. It contains the following functionality, which I will explain later on:
It is a straight forward script, avoiding classes for example, to make it easy to follow for beginning programmers (like me).
The hardware I used :
Some upfront remarks:
So it looks like the main application use for the SenseHat is data logging. One can code some simple sprite based games as well. (Useful when grasping the basics of game development). Personally I’m not really interested in logging huge amounts of environmental data. The winning contribution of the kids of the Thirsk school inspired me more, so I enhanced their Idea towards a live tracker for the ISS.
You can find the full script at: Bitbucket
First of all the libraries needed:
The main section contains the statements to fetch the events (joystick movements) of the pygame queue. This part should always be in the main loop of the script. The joystick is a 5-way button (Up, Down, Left, Right, Press) that produces 10 different events (5 times KEYUP, 5 times KEYDOWN). When pushing the joystick, one produces 2 events (KEYUP and KEYDOWN) with the same keycode (Up, Down, Left, Right). I first cleaned the queue after reading a single event, but when I added the trackISS() function to the main loop, in some peculiar way, pygame handled both events before clearing the queue. For that reason the handle.event() function starts with a filter on event type. The rest speaks for itself.
trackISS() was added to the main loop to make the script a bit more lively: when no keys are used, the location of the ISS is displayed. The function is kept simple: get a json record from the API, enrich the data received through a call to a second API and display the results.
A description of the API’s and some examples on how to use them, can be found at: ISS API's
The data structure of the json records can also be easily retrieved by copying the url in a browser. (After all an API is just a ‘not-so-good-looking-url’.) A ‘try – except’ statement handles access to the API-url. This is done to prevent the script from crashing when the url is not accessible (caused by e.g. loss of wifi, a glitch from your provider or maintenance done at the API-server, etc.).
The first API returns a lot of data like solar coordinates, velocity, visibility and earth coordinates. To enrich this data with a country code and the current time zone, the latitude and longitude returned by the first API have to be added to the second URL by simple string concatenation.
The second API only returns data when the ISS is above land. Above sea the API returns the notorious ‘404 Page not found error’. So, after checking the availability of the url, the script checks for a HTTPError as well. When encountering a HTTPError, the location at sea is displayed. If data is returned, the country code is used to search the flag data.
The function showLevels() deals with the pitch and roll data. It contains a loop checking the orientation of the RPi and timing the idle position of the Raspberry. Every time the RPi is moved within the maximum idle time, the number of rows and collumns is calculated and displayed. The size of the lcd matrix (8x8) determines the resolution of the level (180 degr / 8). Since there’s no centre/middle of the matrix, the row/column to start with has to be determined every time, depending on the movement (nose or tail, left or right). It’s no sophisticated coding, just handling each requirement/constrain at a time.
The environmentals() function just calls for each sensor at a time. The coding for each sensor is taken straight out of the SenseHat examples (the folder is part of the install). Be aware that calling for the magnetometer disables the gyroscope and the accelerometer. You’ll have to switch those on every time using the ‘set_imu_config(bool, bool, bool)' statement.
So far for what I could get out of the SenseHat. Hopefully it helps people with the first steps using the board. I’m curious about other/new ideas for apllications. And of course feel free to push more flags into the IssFlag.py script.