Airplanes and Domoticz :-)

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Airplanes and Domoticz :-)

Postby freijn » Tuesday 21 March 2017 0:18

More then a year ago I "build" my ADS-B receiver on my Raspberry ver. 1 to monitor airplanes flying in my country.
https://flightaware.com/adsb/piaware/build Recently (dec last year)
I started with Domoticz in my house, running on a Rasp ver. 3.
https://domoticz.com/

Now I wrote a Python script to bind the two together. Counting airplanes flying less than 10 km from my house.
Usefull NO! Fun YES !! :-)


Image

krizzz
Posts: 150
Joined: Wednesday 20 November 2013 21:36
Target OS: Linux
Domoticz version:
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby krizzz » Wednesday 22 March 2017 7:45

Looks like Fun! I see in your counter that on the top is is mentioning 2 planes and in the text within the sensor 5 planes. Is the number 5 the previous count?


Verzonden vanaf mijn iPhone met Tapatalk

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Monday 27 March 2017 20:58

No :-)

Its total airplanes seen and total airplanes today :-)

Still am testing the script to have it robust.
It is now signalling airplanes in a circle of 20km around my house.
The aim is to count the lifeliner ( Yellow resque Heli ) and the ZXPxx Police heli are flying around above my hometown.
Of course I must get a trigger on my mobile when the do :-) But that is only the next step....

Let me know if you are interested in the (simple ) phyton script. I think I have a good working version soon :-)

User avatar
phoenixblue
Posts: 55
Joined: Friday 25 November 2016 12:20
Target OS: Raspberry Pi
Domoticz version: 3.7392
Contact:

Re: Airplanes and Domoticz :-)

Postby phoenixblue » Monday 27 March 2017 23:36

Do your receive the ADS-B info from the Internet or with an RTL-SDR stick?
I like to integrate it in my Domoticz system.
scripts on github: Link

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Tuesday 28 March 2017 8:32

Hi,

I'm interested in the script actually.

Kind regards.

freijn wrote:No :-)

Its total airplanes seen and total airplanes today :-)

Still am testing the script to have it robust.
It is now signalling airplanes in a circle of 20km around my house.
The aim is to count the lifeliner ( Yellow resque Heli ) and the ZXPxx Police heli are flying around above my hometown.
Of course I must get a trigger on my mobile when the do :-) But that is only the next step....

Let me know if you are interested in the (simple ) phyton script. I think I have a good working version soon :-)
Moved recently to Emmen.

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Tuesday 28 March 2017 23:17

phoenixblue wrote:Do your receive the ADS-B info from the Internet or with an RTL-SDR stick?
I like to integrate it in my Domoticz system.


I had an old Rasp 1 ( slow ) , so build the receiver with that one and indeed a RTL-SDR stick :
https://nl.flightaware.com/adsb/piaware/build

I can recommend to build a tuned antenna for it as it helps a lot in receiving longer distances.

So my Domoticz is on a SEPARATE Rasp 3 !!! Just to keep things nice, clear and separated :-)

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Tuesday 28 March 2017 23:19

assenzuid wrote:Hi,

I'm interested in the script actually.

Kind regards.



I will share it here after the weekend. Still need to correct 1 error.

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Tuesday 28 March 2017 23:20

Great,
Thanks

Verstuurd vanaf mijn E6653 met Tapatalk
Moved recently to Emmen.

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Saturday 01 April 2017 22:18

As promished, here the first working version of my airplane python script.

Copy the code and name it plane.py

The following additional libraries in python are required : json, yaml,math,time,requests
The install command is depending on the OS. Please use google for HowTo.

The following line need modifications !! :
http://192.168.1.155:8080/json.htm?type ... 7&svalue=1
The ip address of your Domoticz and the idx number used for counter.
Please update your Geocoordinates Lonhome and lathome easy to find using google maps.

The # means python comment. As you can see you can filter on plane name and distance, or no filter at all :-).

Any questions please let me know. Any ( positive or negative ) results please share !

Have fun.

Frank



Code: Select all

#####################################################
#Frank Reijn's airplane to Domoticz Pyhton script.
# This script is loading the flights json file and print all the objects with the key "flight"
# Additional of  the following libraries in python are required : json, yaml,math,time,requests
# The install command is depending on the OS. Please use google for HowTo.
# Many thanks to Ilaria for helping out here.
#####################################################

import json
import yaml
import math
import time
import requests
#Switch on debug statements
debug =0
Planes=[]
RemovePlanes=[]
planenumber=0
planealreadyfound=0
Dist2Plane=15
lonhome = 4.9402058
lathome = 52.4955744
CurrentList=[]
PreviousList=[]
MyNewPlanes=[]
difference=[]


def distance(lat1, lng1, lat2, lng2):
    #return distance as meter if you want km distance, remove "* 1000"
    radius = 6371 * 1000

    dLat = (lat2-lat1) * math.pi / 180
    dLng = (lng2-lng1) * math.pi / 180

    lat1 = lat1 * math.pi / 180
    lat2 = lat2 * math.pi / 180

    val = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLng/2) * math.sin(dLng/2) * math.cos(lat1) * math.cos(lat2)   
    ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1-val))
    return radius * ang

###########
#Start main routine
##########

#going into endless loop
while True:
   #Open the file
   #json_data = open('/run/dump1090-fa/aircraft.json').read()
   with open('/run/dump1090-fa/aircraft.json') as f:
      json_data = f.read()

   #Loads the json file
   #json.loads(json_data)
   #Loads the file uisng yaml, that clean up of unecode stuff
   data = yaml.safe_load(json_data)

   list=data["aircraft"]

   #Just print the whole list of dictionaries
   #print(list)


   #search for the planes
   for item in list:
      if 'flight' in item.keys():
         #print "      "
         #print(item['flight'])
         flightname = item['flight']
         #if  flightname.find("KLM") <> -1:
         #print "KLM Gevonden!!!"
         #print(item['flight'])
         if 'lon' in item.keys():
            lon6=(item['lon'])
            lat6=(item['lat'])
            #print lon6
            #print lat6
            dist= int ( distance(lonhome,lathome,lon6,lat6)/1000)
            if debug:
               print str(dist)+ " Km"
            if dist < Dist2Plane:   
               #print str(round(dist/1000))+ " Km"
               # if airplane is in my distance, I put it in CurrentList
               CurrentList.append(flightname)      


   if debug:
      print " ####Planes################## "
      print "CurrentList", CurrentList
      print "PreviousList", PreviousList
   difference=set(CurrentList).difference(PreviousList)
   if debug:
      print "difference", difference
   for item in difference:
      if item not in PreviousList:
         MyNewPlanes.append(item)
         if debug:
            print "New Plane found = " + item
         r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=87&svalue=1')
      #else:
         #MyNewPlanes.remove(item)
   
   if debug:
      print "Planes in list= " + str(len(CurrentList))
   #print" Lets sleep for 1 sec....."
   #time.sleep(1)
   if debug:
      print "MyNewPlanes=" , MyNewPlanes
   PreviousList=CurrentList
   CurrentList=[]
   MyNewPlanes=[]
   f.close()
   #sleep 5 sec
   time.sleep(5)




Image
Last edited by freijn on Sunday 02 April 2017 17:37, edited 1 time in total.

User avatar
HansieNL
Posts: 302
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby HansieNL » Sunday 02 April 2017 2:57

Great fun project. I got a spare Raspberry Pi, so for a few Euro's I can have a lot of fun.
Thanks for sharing.
Domoticz on Raspberry Pi 2, Homey, Hue lights, Toon thermostat, Logitech Harmony Hub, Fibaro Wall Plug, Fibaro Push Buttons, LightWaveRF Mood Controller, Venz M9 running LibreELEC, Synology NAS

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Sunday 02 April 2017 11:48

Thanks,

I will try implementing it.

Regards

freijn wrote:As promished, here the first working version of my airplane python script.

Copy the code and name it plane.py

The following additional libraries in python are required : json, yaml,math,time,requests
The install command is depending on the OS. Please use google for HowTo.

The following line need modifications !! :
http://192.168.1.155:8080/json.htm?type ... 7&svalue=1
The ip address of your Domoticz and the idx number used for counter.

The # means python comment. As you can see you can filter on plane name and distance, or no filter at all :-).

Any questions please let me know. Any ( positive or negative ) results please share !

Have fun.

Frank



Code: Select all

#####################################################
#Frank Reijn's airplane to Domoticz Pyhton script.
# This script is loading the flights json file and print all the objects with the key "flight"
# Additional of  the following libraries in python are required : json, yaml,math,time,requests
# The install command is depending on the OS. Please use google for HowTo.
# Many thanks to Ilaria for helping out here.
#####################################################

import json
import yaml
import math
import time
import requests
#Switch on debug statements
debug =0
Planes=[]
RemovePlanes=[]
planenumber=0
planealreadyfound=0
Dist2Plane=15
lonhome = 4.9402058
lathome = 52.4955744
CurrentList=[]
PreviousList=[]
MyNewPlanes=[]
difference=[]


def distance(lat1, lng1, lat2, lng2):
    #return distance as meter if you want km distance, remove "* 1000"
    radius = 6371 * 1000

    dLat = (lat2-lat1) * math.pi / 180
    dLng = (lng2-lng1) * math.pi / 180

    lat1 = lat1 * math.pi / 180
    lat2 = lat2 * math.pi / 180

    val = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLng/2) * math.sin(dLng/2) * math.cos(lat1) * math.cos(lat2)   
    ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1-val))
    return radius * ang

###########
#Start main routine
##########

#going into endless loop
while True:
   #Open the file
   #json_data = open('/run/dump1090-fa/aircraft.json').read()
   with open('/run/dump1090-fa/aircraft.json') as f:
      json_data = f.read()

   #Loads the json file
   #json.loads(json_data)
   #Loads the file uisng yaml, that clean up of unecode stuff
   data = yaml.safe_load(json_data)

   list=data["aircraft"]

   #Just print the whole list of dictionaries
   #print(list)


   #search for the planes
   for item in list:
      if 'flight' in item.keys():
         #print "      "
         #print(item['flight'])
         flightname = item['flight']
         #if  flightname.find("KLM") <> -1:
         #print "KLM Gevonden!!!"
         #print(item['flight'])
         if 'lon' in item.keys():
            lon6=(item['lon'])
            lat6=(item['lat'])
            #print lon6
            #print lat6
            dist= int ( distance(lonhome,lathome,lon6,lat6)/1000)
            if debug:
               print str(dist)+ " Km"
            if dist < Dist2Plane:   
               #print str(round(dist/1000))+ " Km"
               # if airplane is in my distance, I put it in CurrentList
               CurrentList.append(flightname)      


   if debug:
      print " ####Planes################## "
      print "CurrentList", CurrentList
      print "PreviousList", PreviousList
   difference=set(CurrentList).difference(PreviousList)
   if debug:
      print "difference", difference
   for item in difference:
      if item not in PreviousList:
         MyNewPlanes.append(item)
         if debug:
            print "New Plane found = " + item
         r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=87&svalue=1')
      #else:
         #MyNewPlanes.remove(item)
   
   if debug:
      print "Planes in list= " + str(len(CurrentList))
   #print" Lets sleep for 1 sec....."
   #time.sleep(1)
   if debug:
      print "MyNewPlanes=" , MyNewPlanes
   PreviousList=CurrentList
   CurrentList=[]
   MyNewPlanes=[]
   f.close()
   #sleep 5 sec
   time.sleep(5)




Image
Moved recently to Emmen.

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Sunday 02 April 2017 11:50

One remark, Also update the lonhome & lathome settings.
Moved recently to Emmen.

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Sunday 02 April 2017 17:38

Have fun and please let me know any suggestions of questions ?
If complete and interesting I could make a wiki for it.

I have updated lon and lat home in the previous post. Thanks Assen :-)

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Sunday 02 April 2017 21:25

One question, how do you get "vliegtuigen" in the counter?

Image

Image
Moved recently to Emmen.

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Sunday 02 April 2017 22:25

For some reason my /etc/rc.local is not running after I reboot my Raspberry PI with PiAware installed on it....

What's the best way to stat this Python script after a reboot automatically?
Moved recently to Emmen.

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Monday 03 April 2017 9:18

assenzuid wrote:One question, how do you get "vliegtuigen" in the counter?

Image

Image


Assen

Goto the 'utility' or 'overige' page. Select 'modify' or 'aanpassen' on the counter image.
You will find the parameters as the picture below :

Image

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Monday 03 April 2017 20:55

That's very easy :roll:

Only thing to fix is that /etc/rc.local is not running after I reboot my Raspberry PI with PiAware
Last edited by assenzuid on Monday 03 April 2017 21:00, edited 1 time in total.
Moved recently to Emmen.

freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Airplanes and Domoticz :-)

Postby freijn » Monday 03 April 2017 20:56

So I have added a Resque and Police Heli counter. Fortunately both are not flying around my town every hour. Unfortunately the both counters are declared dead if not heard from in 60 min....
So I have added some additional code as a keep alive task. Calling the same url but now with value ZERO.


Image



new added code only :

Code: Select all

#defines
#keep alive timer
tick1=0
#refresh in seconds
refreshtimer=1500

#just after the start of the while loop
if (time.time() - tick1) > refreshtimer:
      if debug:
         print ">>>Refresh URL called<<<"
      # call counter with dummy zero for keep alive ( Sensor red )
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=87&svalue=0')
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=98&svalue=0')
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=99&svalue=0')
      tick1 = time.time()
      



The full new script

Code: Select all

#####################################################
#Frank Reijn's airplane to Domoticz Pyhton script. V1.1
# This script is loading the flights json file and print all the objects with the key "flight"
# Additional of  the following libraries in python are required : json, yaml,math,time,requests
# The install command is depending on the OS. Please use google for HowTo.
# Many thanks to Ilaria for helping out here.
#####################################################

import json
import yaml
import math
import time
import requests
#Switch on debug statements
debug =0
Planes=[]
RemovePlanes=[]
planenumber=0
planealreadyfound=0
#distance around your location
Dist2Plane=10
#your location erasy to find with google maps
lonhome = 4.9402058
lathome = 52.4955744
CurrentList=[]
PreviousList=[]
MyNewPlanes=[]
difference=[]
#keep alive timer
tick1=0
#refresh in seconds
refreshtimer=60

#distance calculator
def distance(lat1, lng1, lat2, lng2):
    #return distance as meter if you want km distance, remove "* 1000"
    radius = 6371 * 1000

    dLat = (lat2-lat1) * math.pi / 180
    dLng = (lng2-lng1) * math.pi / 180

    lat1 = lat1 * math.pi / 180
    lat2 = lat2 * math.pi / 180

    val = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLng/2) * math.sin(dLng/2) * math.cos(lat1) * math.cos(lat2)   
    ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1-val))
    return radius * ang

###########
#Start main routine
##########
#get start time in ticks
ticks1=time.time()

#going into endless loop
while True:
   if (time.time() - tick1) > refreshtimer:
      if debug:
         print ">>>Refresh URL called<<<"
      # call counter with dummy zero for keep alive ( Sensor red )
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=87&svalue=0')
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=98&svalue=0')
      r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=99&svalue=0')
      tick1 = time.time()
      
   #Open the file
   #json_data = open('/run/dump1090-fa/aircraft.json').read()
   with open('/run/dump1090-fa/aircraft.json') as f:
      json_data = f.read()

   #Loads the json file
   #json.loads(json_data)
   #Loads the file uisng yaml, that clean up of unecode stuff
   data = yaml.safe_load(json_data)

   list=data["aircraft"]

   #Just print the whole list of dictionaries
   #print(list)


   #search for the planes
   for item in list:
      if 'flight' in item.keys():
         #print "      "
         #print(item['flight'])
         flightname = item['flight']
         #if  flightname.find("KLM") <> -1:
         #print "KLM Gevonden!!!"
         #print(item['flight'])
         if 'lon' in item.keys():
            lon6=(item['lon'])
            lat6=(item['lat'])
            #print lon6
            #print lat6
            dist= int ( distance(lonhome,lathome,lon6,lat6)/1000)
            if debug:
               print str(dist)+ " Km"
            if dist < Dist2Plane:   
               #print str(round(dist/1000))+ " Km"
               # if airplane is in my distance, I put it in CurrentList
               CurrentList.append(flightname)      


   if debug:
      print " ####Planes################## "
      print "CurrentList", CurrentList
      print "PreviousList", PreviousList
   difference=set(CurrentList).difference(PreviousList)
   if debug:
      print "difference", difference
   for item in difference:
      if item not in PreviousList:
         MyNewPlanes.append(item)
         if debug:
            print "New Plane found = " + item
         r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=87&svalue=1')
         if item.find("Life") <> -1:
            #add to lifeliner counter
            r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=98&svalue=1')
         if item.find("ZXP") <> -1:
            #add to lifeliner counter
            r=requests.get('http://192.168.1.155:8080/json.htm?type=command&param=udevice&idx=99&svalue=1')   
      #else:
         #MyNewPlanes.remove(item)
   
   if debug:
      print "Planes in list= " + str(len(CurrentList))
   #print" Lets sleep for 1 sec....."
   #time.sleep(1)
   if debug:
      print "MyNewPlanes=" , MyNewPlanes
   PreviousList=CurrentList
   CurrentList=[]
   MyNewPlanes=[]
   f.close()
   #sleep 5 sec
   time.sleep(5)


assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Monday 03 April 2017 21:18

Nice modification / Add-On.
Moved recently to Emmen.

assenzuid
Posts: 93
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Airplanes and Domoticz :-)

Postby assenzuid » Sunday 09 April 2017 14:51

Everytime Domoticz update (running beta) the feed from PiAwares stops working (I have to restart the script or reboot PiAware).
Is there a solutions to check if the feed is running, if not to restart it agian?
Moved recently to Emmen.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest