Polling data from bridgeclient in python

Hello:
I have been following the various threads about two way flow of data between the arduino side of the Yun and the linino part.

There is a lot of confusion, at least on my part, as the best way forward but i have finally got something working although not very well.

I have tried using the Serial method i.e.

arduino = serial.Serial('/dev/ttyATH0', 115200)

but this has not worked for me because when I try to comment out the relevant line in /etc/inittab that is necessary for this method to work, my python code stops with an error. I have not figured out why and might to this in the future.

However, the method that is working is to use

from bridgeclient import BridgeClient as bridgeclient	# this compiles without problem
													
#set up the json client and the osc client and server. The server should have the ip of your Yun.
#The client.connect should have the ip address of you phone.
json = TCPJSONClient('127.0.0.1', 5700)	# this IP address is local host
jserver = TCPJSONServer('0.0.0.0', 5900)	#jserver = TCPJSONServer('0.0.0.0', 5700)
server = OSCServer( ("192.168.0.20", 8000) )	#this is the address of the YUN and the port should match outgoing port on TouchOSC
client = OSCClient()
client.connect( ("192.168.0.21", 9000) )	#this is the address of the phone and the port should match the incomming port on TouchOSC

SketchRead = bridgeclient()		# this is probably not the best way forward
SketchRead.begin

So I can read values from the input pins of an arduino sketch

Arduino Sketch contains this:

Bridge.put("Ai0", String(analogRead(0)));
delay(20);
Bridge.put("Ai1", String(analogRead(1)));
delay(20);

And the python script calls a function that includes this:

def readAIs():
myval = SketchRead.get ('Ai0')
print myval
myval = SketchRead.get ('Ai1')
print myval

In order to poll the arduino, I call the readAIs() function in a timeout function. This code is taken from various examples on this forum. Apologies for not providing links but this project has gone on for months and I have lost track of where various bits have come from.

def handle_timeout(self):
	print ("Awaiting Input....")
	readAIs()
	
server.handle_timeout = types.MethodType(handle_timeout, server)

What I would like to do is to not use the time out function to call my function in python but use something like a While True: loop

However, I don't know where to put this in the python code and what I can use to be TRUE in order to make sure the polling does not interfere with the rest of the code which is sending information from the python script (linked to a tablet running touchOSC) to the arduino.

As discussed elsewhere, this method is painfully slow updating every second or so but I was hoping that by removing it from the timeout function which itself only updates every 1-2 seconds, then I might at least improve the speed a little.

Hope someone can help.

B
NickH

NickH:
What I would like to do is to not use the time out function to call my function in python but use something like a While True: loop

However, I don't know where to put this in the python code and what I can use to be TRUE in order to make sure the polling does not interfere with the rest of the code which is sending information from the python script (linked to a tablet running touchOSC) to the arduino.

Hello, can you post the entire code? It is hard to help you without having an idea of the code structure. As far as I understand you want to poll the data with a while loop but remember that using a while will block your code until you exit from the loop, and using the timeout function like you did does not stop the code.

What about using this method python - Executing periodic actions - Stack Overflow ?