FlightAware Discussions

Add Audio Cue to Piaware

Does anyone know what sort of process would be required for me to add a function onto Piaware that would trigger an audio queue if a plane was within a given distance and below a certain altitude in relation to my ADS-B’s location?

I was hoping to add a mini speaker onto the Raspberry Pi board to notify me if there was an aircraft nearby without me having to look directly at the online visual map.

1 Like

I assume you mean an audio cue.

You could write something to periodically monitor /run/dump1090-fa/aircraft.json and trigger actions from there.

1 Like

That’s what I was thinking but I’m not entirely sure how to access that file to add it in. Is there a github page with the master files somewhere? Or do I have to unpack the image file and repack? (Also yes cue! Thank you.)

Best way is to use python to decode the json.
You’ll need SSH access to the system first, then understand python.

This is a python script that for example determines the range of an aircraft:
https://github.com/wiedehopf/graphs1090/blob/master/dump1090.py

It’s meant for use with collectd, but maybe you can figure out how to make a standalone script to query the json.

Programming can be learned by anyone i’d say, but you’ll have to invest some time.
Once you understand the python script i linked, it shouldn’t be too hard to build sth that can do sth when a plane comes closer than xyz nmi.

I have python experience, I guess at this point I’m just looking for how I would go about accessing the main files to edit.

The piaware sdcard image is essentially just a Raspbian image + extras; you can create /boot/ssh and ssh in for access. See https://flightaware.com/adsb/piaware/build/optional

The piaware & dump1090-fa source code is on github, but I wouldn’t expect you’d be modifying that, your script would be standalone.

Awesome! Thanks so much for the help!

I am writing something to point to nearest plane on a web page served by the pi so I have all the code to look through relevant file and pick out closest (and tell you it’s bearing!) Still work in progress but I can share, if I can figure how to do so on the forum. Only problem is that it is in PHP. No, the advantage is that it is in PHP - for me anyway:-).

If I could get this pathetic Android tablet to screenshot o clipboard I’ll post a screenshot. Standby…

Anybody know best way to share code here? I’d post a file i guess but need to be on a proper computer, will try later. Btw the above page self refreshes so it animates, but you could run PHP on a cron job, or translate to python to trigger a noise or send an email.

1 Like

That looks ace – I’ll sometimes just have the map zoomed out to 20 NMs and if something wanders in I can likely go outside and see it. Or if something buzzes past I can check the map to see what it is. Your mod looks great for those!

Some people use github and should be able to guide you if that’s suitable. I’ve posted someone’s code recently to Google Drive, shared it as read-only (the default when shared) and posted the link. That shows it as code in a text window with a download link for the file. It still needs an installer or install instructions though (on github that takes the form of a README.md file). If it’s relatively short you could post it as text and use 3 backticks above the first line to turn it into code-formatted text in your post.

```
First line
Second line

gets turned into

First line
Second line

Cool, I’ll post when I have Nas drive turned on. abcd567 helpfully gave me this advice on getting PHP to run alongside Piaware on a Pi)

sudo apt install php7.3-cgi
sudo lighty-enable-mod fastcgi-php
sudo /etc/init.d/lighttpd force-reload

Because none of official Pi documentation worked for me :frowning:

Here is some code - I think we should open up a specific topic for this perhaps. Will try to do later and post here. *based on fixed IP)

// Fetch local data file
$json = file_get_contents('http://192.168.0.XXX/dump1090-fa/data/aircraft.json');
$j_aircraft_data = json_decode($json,true);


// Home location and a constant
$lat_home=52.339734;
$lon_home=-0.156381;
$p = 0.017453292519943295;    // Math.PI / 180


// Fallbacks
$nearest_index=-1;
$nearest_x=99999;
foreach ($j_aircraft_data['aircraft'] as $index=>$aircraft) {

	if (isset($aircraft['hex']) && isset($aircraft['lat']) && isset($aircraft['lon'])  && isset($aircraft["flight"]) ) {
	
		$lat1=$aircraft["lat"];
		$lon1=$aircraft["lon"];
		//$lat1=50.339734;	// TEST hardcode
		//$lon1=-0.156381;


		// DISTANCE d is odd unti: don't need to convert to nm until we find NEAREST
		$a = 0.5 - cos(($lat_home - $lat1) * $p)/2 + 
			cos($lat1 * $p) * cos($lat_home * $p) * 
			(1 - cos(($lon_home - $lon1) * $p))/2;
		$d=asin(sqrt($a)); // Just work out relative distance - clean up when found nearest
		if ($d < $nearest_x) {
			$nearest_x=$d;
			$nearest_index=$index;
		}
	}	// aircraft data set
}	// Loop all aircraft entries


$j_nearest_aircraft_data=$j_aircraft_data['aircraft'][$nearest_index];
$nearest_icao=$j_nearest_aircraft_data['hex'];
//$nearest_x=12742*$nearest_x;
//$nearest_nm=round($nearest_x*5.399565)/10;
$nearest_nm=round($nearest_x*68801.25723)/10;	// See above for calc in full
$lat1=$j_nearest_aircraft_data['lat'];
$lon1=$j_nearest_aircraft_data['lon'];


// Bearing
$dLon = $lon_home-$lon1;
$dLat = $lat_home-$lat1;
$brng=atan2($dLon, $dLat) / $p;
$nearest_bearing2=round(($brng + 180) % 360);	//Convert polar to "degrees"

FWIW, this won’t produce the correct bearing (it’s approximately correct at the equator for small distances, but gets worse as you move towards the poles). From https://www.movable-type.co.uk/scripts/latlong.html you want:

θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
where φ1 , λ1 is the start point, φ2 , λ2 the end point ( Δλ is the difference in longitude)

(isn’t spherical geometry fun)

Now that is an interesting topic to discuss! I know what you mean BUT I had the following in place and ran it against the naive/fast atan2 approach and for where I am I found the latter produced a result that if I looked in the sky it gave a better answer! Maybe magnetic deviation? Maybe because we’re looking at very short ranges here so curvature of earth actually is not the right way to go???

Would love to understand! At ranges of 20nm or less I was seeing deviations between the two of 10 or so degrees but agreement at compass “quarters” N E S W.

???

// Bearing
$dLon = ($lon1-$lon_home);
$y = sin($dLon) * cos($lat1);
$x = cos($lat_home)*sin($lat1) - sin($lat_home)*cos($lat1)*cos($dLon);
$brng=(atan2($y, $x)) / $p;	// i.e.  * 180 / PI;
$nearest_bearing1=round(360 - (($brng + 360) % 360));
echo("Bearing 1: ".$nearest_bearing1."&deg;<br/>");

You need to convert your lat/lon values to radians. (Doesn’t matter for the simple atan2 case, since it’s just a scaling factor, but it does matter as soon as you start calling sin/cos)

ooo, thank you very much Sir that had confused the heck out of me - what a numpty :frowning:

I’ll post some updated code when I have the time, Good there’s somebody who knows their left hand from their right on here :slight_smile:

New and improved bearing calculation snippet in PHP thanks to obj putting me straight

// Bearing
$r_lat1=deg2rad($lat1);
$r_lon1=deg2rad($lon1);
$r_lat_home=deg2rad($lat_home);
$r_lon_home=deg2rad($lon_home);
$r_dLon=$r_lon1-$r_lon_home;
$x = cos($r_lat_home)*sin($r_lat1) - sin($r_lat_home)*cos($r_lat1)*cos($r_dLon);
$y = sin($r_dLon) * cos($r_lat1);
$brng=(atan2($y, $x)) / $p;	// i.e.  * 180 / PI;
$nearest_bearing1=round(($brng + 360) % 360);
echo("Bearing 1: ".$nearest_bearing1."&deg;<br/>");