Sunday, 27 September 2015

Read PiAware Flight Data with Python

I have been using Piaware for a couple of weeks and I like the idea of being able to read the signals from aircraft just using simple equipment.


I wanted to use this information to do interesting 'stuff', I'm imagining home built radars and led's which flash, but before I could do anything cool I needed to find a way of getting to this data using Python.

Its possible to read data directly from the dump1090 program using tcp sockets, but the data is a raw stream and it seemed like too much work (I'm all for simplicity)!

The Piaware install also comes with a web front end so you can see the data you are receiving [http://localhost:8080], you can also get to the json data [http://localhost:8080/data.json] which is feeding this web page and this looked like a much easier way of getting the data out.

I create a small Python 3 class called FlightData [link to github flightdata.py] which reads the data from the web page and parses it into an object.

You can install it by cloning the flightdata github repository and copying the flightdata.py file to your project (if there is sufficient interest I'll make it into an 'installable' module):
git clone https://github.com/martinohanlon/flightdata
cp ./flightdata/fightdata.py ./myprojectpath
You can test it by running the flightdata.py program file:
python3 flightdata.py
Below is a sample program which shows how to use it:
from flightdata import FlightData
from time import sleep

myflights = FlightData()
while True:
    #loop through each aircraft found
    for aircraft in myflights.aircraft:
   
        #read the aircraft data
        print(aircraft.hex)
        print(aircraft.squawk)
        print(aircraft.flight)
        print(aircraft.lat)
        print(aircraft.lon)
        print(aircraft.validposition)
        print(aircraft.altitude)
        print(aircraft.vert_rate)
        print(aircraft.track)
        print(aircraft.validtrack)
        print(aircraft.speed)
        print(aircraft.messages)
        print(aircraft.seen)
        print(aircraft.mlat)
   
   sleep(1)

   #refresh the flight data
   myflights.refresh()

4 comments:

  1. Hi Martin... thanks for this...it's what I was looking for. I have updated the URL to the new PiAware V3 location for the json file (localhost/dump1090-fa/data/aircraft.json). It threw an error on the decode(). Said it couldn't be None. I changed the line to set it to utf8, which is the json default I think. Got past that and hung up on line 66 in flightdata.py where it tries to parse the array. Error is TypeError: String indicies must be integers. I will admit to zero python experience, so that's likely the problem! Any thoughts? thanks. John

    ReplyDelete
    Replies
    1. I suspect you are using the wrong file location (hence getting None back) I just had a quick look on my piaware machine and "http://localhost:8080/data.json" still takes you to a valid location. Do you know the file location has changed? I'll have a look at piaware v3 at somepoint mine is still 2.1-5

      Delete
  2. It moved with V3. you can access mine Pi via a proxy on my main site, if you'd like to do so for testing. (the proxy uses "adsb" but Piaware is /dump1090-fa/data/aircraft. the 8080 port will still work on the Piaware, but it simply forwards to port 80 where lighttpd is running.

    andersononline.net/adsb/data/aircraft.json

    ReplyDelete
  3. Hi again. I inserted "print(json.dumps(json_data,sort_keys=
    True,indent=5))" just before the "for aircraft in json_data:" and it dumped the current json file OK. So we seem to be connecting.

    Come to think of it, when I ran this the very first time, I got a 404 error, which is what made me change the URL in the first place.

    Thanks!
    ......John

    ReplyDelete