Jump to content

python proximity script stops connecting to isy994i


glacket

Recommended Posts

I am at a loss. I created the following proximity script in python that runs on raspbian jesse on an rpi. After a couple of hours it always just kind of fizzles out and stops working. From what I can see everything checks out. I am just not sure how to proceed. Any ideas on what I am missing. It is driving me crazy. The only thing I can think of is maybe the keep alive command is wrong.

#!/usr/bin/python

import bluetooth
import time
import httplib2

username=''
password=''
host="http://x.x.x.x"
firstRun = True
resultHolduser1 = None
resultHolduser2 = None
resultHolduser3 = None

def ISY(mhost,murl,muser,mpass):
    h = httplib2.Http(".cache")
    h.add_credentials(username, password)
    h.request(mhost + murl, "GET", headers={'content-type':'text/plain', 'Connection': 'keep-alive'} )

while True:

    result = bluetooth.lookup_name('AC:37:43:B6:E0:D2', timeout=3)
    if resultHolduser1 != result or firstRun == True:
        resultHolduser1 = result
        if (result != None): 
            ISY(host,"/rest/vars/set/2/2/1",username,password)
        else: 
            ISY(host,"/rest/vars/set/2/2/0",username,password)

    result = bluetooth.lookup_name('A4:84:31:ED:3D:BF', timeout=3)
    if resultHolduser2 != result or firstRun == True:
        resultHolduser2 = result
        if (result != None):
            ISY(host,"/rest/vars/set/2/1/1",username,password)
        else:
            ISY(host,"/rest/vars/set/2/1/0",username,password)

    result = bluetooth.lookup_name('A4:84:31:ED:3D:81', timeout=3)
    if resultHolduser3 != result or firstRun == True:
        resultHolduser3 = result
        if (result != None):
            ISY(host,"/rest/vars/set/2/8/1",username,password)
        else:
            ISY(host,"/rest/vars/set/2/8/0",username,password)

    firstRun = False
    time.sleep(3)

 

Link to comment

I am not familiar with python2 but some loose observations on the layout

- no return on functions to separate main from function? I know there are  lot of pythonisms that I avoid. :) 
- I found with my python3 multiple element  boolean logic needed parenthesis to get order of operations correct. I was never sure if this was a bug in my compiler or I needed to study this more.  It should work without them but I have had lots of trouble with them working bare..

 if (a == b ) or (c  !=  d):

 

Link to comment

I'd add a bunch of debug statements, then run it in the foreground and watch to see what exactly happens when it "fizzles out".

 

Does the program exit with an error?  Keep running, but stop talking to ISY?  etc.

If you can reduce the frequency it sends changes to the ISY, then turning off Keep-Alive (and instead doing  {'Connection': 'close'}) is worth trying.

Link to comment

It should only send changes to the isy when a recognized Bluetooth device is detected for the first time or no longer detected. I don't see any verbose on the isy nor do I see anything on the pi. I have script running at startup via cron.

Link to comment

As per KeviNH, I do a similar thing.

While writing code, I do a print('XXXX routine:', param1, param2) on the first line of every function. If there is problems you can read the list/log and determine what got sent where. Once  things are working reliably then comment out all the first line print(..... lines and leave them there for later debugging, if ever needed.

This is not involving ISY or the pi, but rather, only your program code.

Link to comment
5 hours ago, larryllix said:

As per KeviNH, I do a similar thing.

While writing code, I do a print('XXXX routine:', param1, param2) on the first line of every function. If there is problems you can read the list/log and determine what got sent where. Once  things are working reliably then comment out all the first line print(..... lines and leave them there for later debugging, if ever needed.

This is not involving ISY or the pi, but rather, only your program code.

Ill try adding some debug code and parethesis. I am not very good at python. I cobbled this together with alot of help. Thanks for the help!

Link to comment
3 hours ago, glacket said:

Ill try adding some debug code and parethesis. I am not very good at python. I cobbled this together with alot of help. Thanks for the help!

LOL.  I have been writing software since 1973 and python3 is a huge learning curve for me. The documentations is terrible and everybody offers guesses. LOL

 

Try the parenthesis around each logical comparison first. I don't think my python3 behaves correctly and I have never spent the time to investigate but I have had lots of problems with compound logical comparisons not function the way I thought they should.

Link to comment

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...