Sending data from python to processing

Hi
I’m trying to transmit sensor data from a python script to a processing sketch via sockets.

On the python side I have:

import socket
import random

HOST = ''                     # Symbolic name meaning all available interfaces
PORT = 12000              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

while True:
    #s.send("test\n".encode());
    data = random.random()        #simulated data
    data = str(data).encode()
    s.send(data)

and in Processing:

import processing.net.*;

Server s;

void setup() {
  s = new Server(this, 12000);
}

void draw() {
  
  println("new loop");
  
  // check if a client sent something
  Client c = s.available();
  if (c != null) {
    String input = c.readString();
    println(input);
  }
  delay(1000);
}

The random numbers print just fine in the Processing sketch, but while the Python script is running, the if statement in Processing never exits and so I get an endless stream of data, and the draw loop never repeats.

How can I have the Processing sketch sample exactly one value from the python script per draw loop?

Thanks!

Hi,

I think your issue comes from the fact that in your Python script, you are making an endless loop in the while and the rate of execution is really high compared to the delay you put inside the draw() function in Processing.

So in one second (1000 ms), your Python script is filling the socket buffer with random values but when you read them in Processing, it’s reading thousands of values.

I have the following solution :

  • In your Python file, change this line to send a new line each time :

    data = (str(data) + "\n").encode()
    
  • In your Processing code, use the readStringUntil() function to read from the buffer until it found that newline (which is converted to a byte) :

    String input = c.readStringUntil(byte('\n'));
    

Now it should read only one random value. I don’t know if it’s the best way to do this but it works!

Hope it helps :wink:

1 Like

Indeed it does help!

Thanks!

1 Like

That does work, but now I’m wondering if ideally would the processing sketch poll the python script when it wants an update rather than having python bombard it with data? Would I need to switch around the server / client for that?

Yeah you are right, is this your entire code? Because otherwise this example is not really optimized and it would make no sense to just send a random value to Processing when you can do the same in Java.

So if not what do you ideally want to send to Processing?

This is just my attempt to work out the protocol to slot it into a much larger program. The reason to combine python and processing, is that I have a gyro sensor that comes with a python lib (for raspberry pi), and I want it to control the gravity of a simulation I have that runs in processing. If I could find a lib to interface with the sensor directly from Processing I would, but haven’t found one.

1 Like

Nice video! :slight_smile:

So yes ideally it’s not a good idea to spam your computer with sockets so fast.

I thought about something : in python in your while loop, only send data if the buffer is empty (if reading it return no bytes) and in your Processing sketch, if the buffer is not empty, read it. You can do this at every frame instead of waiting a delay I think.

This is going to synchronize both the Processing and Python sketch. I’m not an expert of network and sockets but it makes me think of synchronization mechanism with semaphores.

Anyway you can look at this thread to dig into this :

Thanks, will look into it!