Sunday, 4 October 2015

PiAware - Aircraft Overhead Indicator LED

I've been using the PiAware software to track aircraft flying near me and I liked the idea of turning an LED on when a plane was overhead (or at least near by!).


The first step was creating a way of reading data from PiAware using Python 3, so I created a module called flightdata.py.

Once I had the data it was simply a case of looping through each of the aircraft signals found, calculating the distance between my gps co-ordinates and the gps position of the aircraft. If the distance was less than 10km I turned the led on!

I 'reused' the code to calculate the distance between 2 GPS co-ords from codecodex.

Setting up

You'll need PiAware installed on your Raspberry Pi, up and running and tracking aircraft.

You'll need an LED, appropriate resistor, breadboard and a couple of Male/Female jumper cables to connect it togther.

The LED is connected to ground and pin 17 with the resistor in between.

Install
cd ~
git clone https://github.com/martinohanlon/flightlight
Usage
Launch the flightlight program passing the latitude (lat) and longitude (lon) of your PiAware station (use www.whatsmygps.com to find your gps location) and the range that should be used to detect if an aircraft is overhead.
usage: flightlight.py [-h] lat lon range
e.g. using GPS coords of 52.4539, -1.7481 (Birmingham, UK Airport) with a range of 10km
cd ~/flightlight/flightlight
sudo python3 flightlight.py 52.4539 -1.7481 10
Code
import RPi.GPIO as GPIO
import argparse

from flightdata import FlightData
from haversine import points2distance
from time import sleep

#pin of the LED to light
LEDPIN = 17

class LED():
    def __init__(self, ledPin):
        self.ledPin = ledPin
        GPIO.setup(ledPin, GPIO.OUT)

    def on(self):
        GPIO.output(self.ledPin, True)

    def off(self):
        GPIO.output(self.ledPin, False)

#read command line options
parser = argparse.ArgumentParser(description="PiAware Flight Light")
parser.add_argument("lat", type=float, help="The latitude of the receiver")
parser.add_argument("lon", type=float, help="The longitude of the receiver")
parser.add_argument("range", type=int, help="The range in km for how close an aircraft should be to turn on the led")
args = parser.parse_args()

#get the flight data
myflights = FlightData()

#set GPIO mode
GPIO.setmode(GPIO.BCM)

try:

    #create LED
    led = LED(LEDPIN)

    #loop forever
    while True:
        
        plane_in_range = False

        #loop through the aircraft and see if one is in range
        for aircraft in myflights.aircraft:
            if aircraft.validposition == 1:
                startpos = ((args.lat, 0, 0), (args.lon, 0, 0))
                endpos = ((aircraft.lat, 0, 0), (aircraft.lon, 0, 0))
                distance = points2distance(startpos, endpos)
                #debug
                #print(distance)
                if distance <= args.range:
                    plane_in_range = True

        #turn the led on / off
        if plane_in_range:
            led.on()
            #print("on")
        else:
            led.off()
            #print("off")
            
        sleep(1)

        #refresh the data
        myflights.refresh()

finally:
    #tidy up GPIO
    GPIO.cleanup()

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Even though request.py exists, I get lots of errors when I run: flightlight.py
    Any idea's?

    ReplyDelete
    Replies
    1. Hi Teddy,

      Im a bit confused - there isnt a file in this project called "request.py"?

      What errors do you get when you try and run flightlight.py?

      Martin

      Delete
    2. When the code is run, I get a whole list of:
      /usr/lib/python3.2/urllib/request.py errors.

      Delete

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