FlightAware Discussions

How to determine bearings of all planes in tracking range?

I’m trying to build an LED compass using the aircraft.json, so compare all the planes lat/lon with the receivers to give a number between 1 and 360 to give the bearing, which could then be picked up by the Arduino to interpret. Ideally, I’d also add altitude to the data, too change the LED colour.

I’ve seen a few code calculations for working out bearings, but most return just one aircraft.
How could I get an array of bearings for the planes around, in a format that could be fed to an Arduino?

The aircraft’s heading is transmitted as part of the ADS-B message, so you only need to ‘read’ it.

Not sure I understand what value there is in combining multiple headings though.

Bearing isn’t the same as heading - heading is the direction the aircraft is pointing, whereas bearing is the direction from some point (eg a receiver) to the aircraft. That isn’t something that is transmitted, and would need to be calculated from the position of the aircraft and receiver.

The aircraft positions are contained within the aircraft.json file generated by dump1090. There are many ways to read and process data from jsons so it really depends on how you want to implement it, and what your arduino code is expecting as an input. I doubt you will find something pre-written that does exactly what you want, but you might be able to adapt something to generate the information you need. For example, my heatmap scripts use jq to read jsons and awk to do calculations (including bearing) on the data. You could definitely do everything you want in python which would probably be somewhat neater and more flexible.

Yes, the question was how to work out the calculations for multiple bearings from the central receiver position.
I found some code that Websanity created - Add Audio Cue to Piaware - #13 by websanity - that worked out the closest aircraft and then worked out the bearing from there.
I adapted the foreach loop:

// Fetch local data file
$json = file_get_contents('http://xxx.xxx.xxx.xxx:8080/data/aircraft.json');
$j_aircraft_data = json_decode($json,true);

// Home location and a constant
$p = 0.017453292519943295;    // Math.PI / 180

foreach ($j_aircraft_data['aircraft'] as $index=>$aircraft) {

        if (isset($aircraft['hex']) && isset($aircraft['lat']) && isset($aircra$

                //$lat1=50.339734;      // TEST hardcode

                // Bearing
                $x = cos($r_lat_home)*sin($r_lat1) - sin($r_lat_home)*cos($r_la$
                $y = sin($r_dLon) * cos($r_lat1);
                $brng=(atan2($y, $x)) / $p;     // i.e.  * 180 / PI;
                $nearest_bearing1=round(($brng + 360) % 360);

                if ($alt1 >= 0 && $alt1 < 2000){
                        $col = "rgb(255,0,0)";
                 }elseif ($alt1 >= 2001 && $alt1 < 4000){
                        $col = "rgb(255,125,0)";
                }elseif ($alt1 >= 4001 && $alt1 < 6000){
                        $col = "rgb(255,215,0)";
                }elseif ($alt1 >= 6001 && $alt1 < 10000){
                        $col = "rgb(0,255,0)";
                }elseif ($alt1 >= 10001 && $alt1 < 20000){
                        $col = "rgb(0,0,255)";
                }elseif ($alt1 >= 20001 && $alt1 < 40000){
                        $col = "rgb(255,0,255)";
                }elseif ($alt1 >= 40001 && $alt1 < 60000){
                        $col = "rgb(255,255,255)";

                        echo $col;
                        echo "<br>";

        }       // aircraft data set
}       // Loop all aircraft entries


And now the result is:


I’ve also asked the question on the Arduino forum about what’s the best format to send to it, but so far I’ve not heard much useful back.

Hi folks,

Hope that code is working for you, good adaptation [feyipuk]

I haven’t touched my code for ages; I don’t know if I said but I have PHP and MySQL on the Pi and instead of driving LEDS - which sounds great! - driving a webpage just on local IP address so I can see on phone.

You could always go to somewhere like: https://www.noip.com to create a real web address, setup port forwarding on your router, then on the PI put dynamic no-IP client then make your PI accessible on a real web page (which I’ve done for another project for a friend)
Maybe I should do that and use that to fetch the data from the Pi for processing on a proper web server although the whole point is to see what you can hear from your house.

I kind of lost heart with doing further work when all Aviation “paused”; maybe when it picks up I’ll do some more - glad to see others still working away on their projects!