Two way information in python script

Hello List,
I continue on my quest to control a YUN via my ipad/iphone with the TouchOSC app. I am making good progress with help from the forum and the web and can now do this well but the information flow between the python script and the YUN is one way and I would to make it two way. A screen shot of the controller is attached and it is all fine exept I want to show when the pulse generator is ready to run with a ready light and running with a busy light.

Python deals with the interaction with the Ipad and YUN using the following structure:

from OSC import OSCServer,OSCClient, OSCMessage
import sys
from time import sleep
import types
sys.path.insert(0, '/usr/lib/python2.7/bridge/') # make sure python can see the tcp module

from tcp import TCPJSONClient
													
#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)
server = OSCServer( ("192.168.0.21", 8000) )	#this is the address of the YUN
client = OSCClient()
client.connect( ("192.168.0.19", 9000) )	#this is the address of the phone

In order to send information to the YUN, I use the following code defined within a python function:

        global faderDefault
	faderDefault = 0
        json.send({'command':'put', 'key':'F01', 'value':'%.2f' % (faderDefault)})

Then, in the arduino sketch:

   Bridge.get("F01",F01value,3);  // read the state of the fader
   int F01int = atoi(F01value);

where F01value is defined as a global

   char F01value[3];

So far so good but I want to be able to send information from the Arduino sketch to the python script and then adjust the control on the ipad accordingly.

     Bridge.put("LD1", "0");    // sends a key of "LD1" with a value of 0

What I don't know is how to read this from within the python script and have failed to find anything on the internet that deals with this precise issue.

Any suggestions?
Thanks,
Nick

take a look here

I don't know how elegant it would be, but you could use REST in Python to communicate with the Arduino sketch.

I have asked the same question and have gotten pointed to the TemperatureWebPanel example. Not much help. The one thing I thought might be my answer was the bridge mailbox, but that does not work and was reported as a bug in github 2 weeks ago and is still not fixed.

This question has been posted numerous times in different threads and is never answered directly.

I really wish that the Yun gurus would just post a simple sketch and associated python script that provides simple shared memory type communications between the 2 processors without having any web interface. If that were provided, a lot of us would just go away and build some amazing things. :slight_smile:

Maybe this can be of help: I once wrote a simple sketch and python script to 'test' the bridge. It uses 2 way communication.
You can find this example code on Yafa! How to make a fermentation controller using an Arduino Yun.: Bridge communication - Part3

@newline:
Thanks. I am going to have a look. This really looks promising.

@newline - Thanks for the code - you did great work. I tried it and it will help me to make a start. I have a few questions, and perhaps you have investigated enough to know the answers:

  1. Why is 60 a magic number in the Yun code before it fails? Seems like an awfully small packet.
  2. Performance is horrible. I removed all of the delays, both on the arduino side and the python side (with the exception of waiting for the reset to complete). If I measured things correctly, it takes any where from .120 - .220 seconds for a packet round trip. I modified the packet size at its smallest to be 15 characters and at the largest to be 60 characters and the round trip times seem pretty constant, so I am guessing that whatever mechanism is being used internally to do the transfer is causing the delay and not the number of characters.

I have seen some threads about doing serial transfers between the 2 processors directly, and maybe that is the route I should go, but I am sure it will be opening up a second Pandora's box. :slight_smile:

Glad it was of some use.
About your questions:

  1. There was a length limit, and some other bridge issues. But I think they did 2 important fixes:
    Yafa! How to make a fermentation controller using an Arduino Yun.: Bridge Communication - Part 4
    http://myyafa.blogspot.be/2013/12/bridge-communication-part-5.html

  2. I do find the bridge slow, and I think it should be possible to speed things up. For my application it is more than fast enough, so I haven't looked deep into it. I think speeding up the library should be high on some priority list. I once did a look and the library looked to be build from a large stack of other code.

Just to close this out on my end and to say thanks for helpful input, I was able to get things working following this thread:
http://forum.arduino.cc/index.php?topic=191820.0

I was able to load PyMata on the linux side, and the slightly modified StandardFirmata on the arduino side was able to get data to flow bidirectionally using Serial1 as the above thread instructs. I set my transmission speeds to the standard firmata 57600 and it worked very well. And the thread also talks about disabling the serial console on serial1. Without this modification things did not work. I just needed to comment out "ttyATH0::askfirst:/bin/ash --login" in /etc/inittab.

I was a little concerned that I would not be able to rsh into the Yun, but it still works, so for the time being I am happy with the result.

Next step - connecting to the Yun via WiFi from a host and sending commands that will be interpreted on the Yun and sent to PyMata to control the arduino I/O. I hope this is less painful then the adventure of getting data flowing between the 2 processors. :slight_smile: