How to pass output from a python program into a processing program
<p>I am reading the orientation a gyroscope <a href="https://www.raspberrypi.org/products/sense-hat/" rel="nofollow">(sense-hat)</a> through a python program that can output the position in strings. I am trying to use this data as an input in a Processing program to make it interactive depending on the orientation of the gyroscope. How would I get Processing to interface with the python program?</p>

<p>I've never used the Sense HAT before, but I'm guessing it's using I2C behind the scenes. In theory it should be possible to reimplement the code in Processing using it's <a href="https://processing.org/reference/libraries/io/I2C.html" rel="nofollow">I2C io library</a>, but in practice it may take quite a bit of effort, looking at the <a href="https://github.com/RPi-Distro/python-sense-hat/blob/458b55f5b8f32d855ba0e5850aed517b06e91f84/sense_hat/sense_hat.py#L78" rel="nofollow">sense-hat library uses RTIMU</a> and all the fancy filtering that does on it's own.</p>

<p>To get your Python program to talk to Processing you have at least two options:</p>

<ol>
<li><a href="https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html" rel="nofollow">pipe the output</a> from the python program into Processing's <code>stdin</code> and parse what's coming through</li>
<li>Use sockets.</li>
</ol>

<p>The second option should be simpler and I can think of mutiple options:</p>

<ol>
<li>raw UDP sockets</li>
<li>OSC using <a href="https://pypi.python.org/pypi/pyOSC" rel="nofollow">PyOSC</a> for the Python and <a href="http://www.sojamo.de/libraries/oscP5/" rel="nofollow">oscP5</a> for Processing</li>
<li>Using WebSockets</li>
</ol>

<p>I'd recommend the second option again: UDP is pretty fast and OSC on top of that makes it east to pass messages with arguments.</p>

<p>The Python script would:</p>

<ul>
<li>poll orientation data</li>
<li>share orientation values via a message like <code>/orientation</code></li>
</ul>

<p>The Processing sketch would:</p>

<ul>
<li>be an OSC Server server and wait for data</li>
<li>fetch the 3 float arguments from the <code>/orientation</code> message received and draw</li>
</ul>

<p>Here's an <strong>untested</strong> proof of concept sender script in Python:</p>

<pre><code>import time
from sense_hat import SenseHat
from OSC import OSCClient, OSCMessage

#update 60 times a second -&gt; feel free to adjust this what works best
delay = 1.0/60.0 
# sense hat
sense = SenseHat()
# OSC client -&gt; Processing
client = OSCClient()
client.connect( ("localhost", 12000) )


while True:
    # read sense hat
    orientation = sense.get_orientation_degrees()
    print("p: {pitch}, r: {roll}, y: {yaw}".format(**orientation))
    # send data to Processing
    client.send( OSCMessage("/orientation", [orientation["pitch"],orientation["roll"],orientation["yaw"] ] ) )
    # wait
    time.sleep(delay)
</code></pre>

<p>and the Processing receiver:</p>

<pre><code>import oscP5.*;
import netP5.*;

OscP5 oscP5;

float pitch,yaw,roll;

void setup() {
  size(400,400,P3D);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,12000);
}


void draw() {
  background(0);  
  text("pitch: " + pitch + "\nyaw: " + yaw + "\nroll: " + roll,10,15);
}

/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage message) {
  message.print();
  if(message.checkAddrPattern("/orientation")==true) {
    /* check if the typetag is the right one. -&gt; expecting float (pitch),float (yaw), float (roll)*/
    if(message.checkTypetag("fff")) {
      pitch = message.get(0).floatValue();
      yaw   = message.get(1).floatValue();
      roll  = message.get(2).floatValue();
    }
  }
}
</code></pre>

<p><strong>Note</strong> that you'll need to install PyOSC and run the Processing sketch before hand, otherwise you might get a Python error about the OSCClient not being able to connect. The idea is Processing becomes an OSC Server and the Python Script is an OSCClient and the server need to be available for the client to connect. (You can make the Python script an OSC Server if you want and the Processing sketch a client if that works better for you)</p>

<p>To install PyOSC try:</p>

<pre><code>sudo pip install pyosc
</code></pre>

<p>Otherwise:</p>

<pre><code>cd ~/Downloads
wget https://pypi.python.org/packages/7c/e4/6abb118cf110813a7922119ed0d53e5fe51c570296785ec2a39f37606d85/pyOSC-0.3.5b-5294.tar.gz
tar xvzf pyOSC-0.3.5b-5294.tar.gz
cd pyOSC-0.3.5b-5294
sudo python setup.py install
</code></pre>

<p>Again, the above is untested, but the idea is to:</p>

<ol>
<li>Download the library</li>
<li>Unzip it</li>
<li>Navigate to the unzipped folder</li>
<li>Install it via <code>sudo python setup.py install</code></li>
</ol>

python raspberry-pi processing gyroscope