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 ./myprojectpathYou can test it by running the flightdata.py program file:
python3 flightdata.pyBelow 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()
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
ReplyDeleteI 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
DeleteHave 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
Deleteyes 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.
DeleteI 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.
Deletehere 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.
DeleteI 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
Thanks John, much appreciated. I will see if I can figure out how to get the radar working. Cheers!
Delete@Jay - I was able to solve it by assigning the aircraft list a separate variable and then iterating through that variable in the loop...
Deletedef parse_flightdata_json(json_data):
aircraft_list = []
ac_identified = json_data['aircraft']
for each_ac in ac_identified:
aircraftdata = AirCraftData(
..
..
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.
ReplyDeleteandersononline.net/adsb/data/aircraft.json
Hi again. I inserted "print(json.dumps(json_data,sort_keys=
ReplyDeleteTrue,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
Hi there,
ReplyDeleteI 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