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()

11 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. Have you been able to go around the string indices error? I validated the JSON to be good, which I expected to be, so not sure what the python code is doing wrong. I'm running python 3.4.2

      Delete
    3. yes I did. It was a while ago though. i'm not sure I could tell you what I did. I now get an email when flights of interest are picked up my by PiAware receiver.

      Delete
    4. I see. If we were able to send the python file it would be great, or anything that could help me figure out how to get it to run. Otherwise, I'll need to figure out how to install older version of raspbian and pi aware back to when it worked. As you can imagine, I would like to avoid this if possible.

      Delete
    5. here is a link to the program I created. it reads the json file periodically and sends me an email if an aircraft of interest has been picked up. Mostly I use this to help me get pictures of some WWII aircraft from the Hamilton Warplane Museum that often pass overhead.
      I tried to post the code, but was told my post was too long. Here's a link to it:
      https://www.dropbox.com/s/2awljgkict5jjas/flights.py?dl=0

      Delete
    6. Thanks John, much appreciated. I will see if I can figure out how to get the radar working. Cheers!

      Delete
    7. @Jay - I was able to solve it by assigning the aircraft list a separate variable and then iterating through that variable in the loop...

      def parse_flightdata_json(json_data):
      aircraft_list = []
      ac_identified = json_data['aircraft']
      for each_ac in ac_identified:
      aircraftdata = AirCraftData(
      ..
      ..

      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
  4. Hi there,

    I am planing to create the same.. i am new in rasberry pi.. i would like to see, if there is anyway to track the vessels.. what is the range of your tracks and what equipment do i need to do all this.

    I have rasberry pi3, usb gps, usb antenna and also stratux software.. can you advise please

    ReplyDelete

Note: only a member of this blog may post a comment.