Make Python and Pure Data communicate on the Raspberry Pi

When working with the Raspberry Pi, it may be useful to make Python and Pure Data communicate. You may want to use Python to setup a webserver for instance, and need to command Pd from within your Python script.

When you install Pd on the RPi (“sudo apt-get install puredata” if you’re running the Raspbian distro), additional programs are installed with the core. Two of these programs (included in puredata-utils) are there to help you interface between Pd and another program : pdsend and pdreceive.

Let’s look at a simple one-way communcation between Python and Pd, using pdsend. To use it, you’ll need to have a [netreceive] object in your patch. As you can see in the patch below, [netreceive] must be created with a specific port number (3000 for instance). Python will have to send messages to the same port. I used a [route] object immediatly after [netreceive] in order to handle different types of messages, identified by a number (0 for toggling DSP on and off, and 1 for volume).Using netreceive in Pd

In your Python script, you can create a specific function for communicating with Pd. In the script below, I have called it “send2Pd”. Other functions in your script can call it by asking it to send a specific message to Pd. “send2Pd” simulates the command line (pdsend was designed to be used with the command line) with the help of the ‘os’ module (please note that you need to import it in your script). The messages we send to Pd are all of the form “id value;”. We need the id because the Pd patch was made to handle more than one incoming message. Note the mandatory semicolon at the end of the message.

Python script to show how to send messages to Pd.

Note that you could also use pdreceive (and the [netsend] object in Pd) to get data from Pd into your Python script.

This entry was posted in Software and tagged , , . Bookmark the permalink.

8 Responses to Make Python and Pure Data communicate on the Raspberry Pi

  1. enrike says:

    have you tried py/pyext external for PD?
    http://puredata.info/Members/thomas/py/

    • Nope, but i’m aware of it. What i wanted to document is how one can work the other way around : use Pd inside Python, basically. I’ll be writting more about this in the future.

  2. Ben Jacobs says:

    Thank you Pierre, this was extremely helpful!

  3. j-b says:

    Un grand merci a toi, je débute avec le raspberry, linux et python et ton site m’a beaucoup aidé. j’en ai copié une bonne partie car je suis en train de programmer le raspberry pour une installation sonore avec capteurs de presence..par contre je n’arrive pas a faire communiquer python et pd. j’ai copié le script que tu as publié mais puredata ne reagit pas lorsque je le lance dans la console..est ce que le script est bien ecrit?
    faut il configurer puredata ou linux avant de lancer le script pour que le reseau soit connécté? peut tu m’aiguiller?

  4. Dalmae says:

    Bonjour,
    super ce post, par contre, j’essaie d’utiliser le ‘pdreceive’. Bien que Python voit les messages que j’envoie via le ‘netsend’ de PureData, je n’arrive pas à récupérer le message dans python pour le renvoyer vers une autre destination (notamment un Pi-Lite (et j’arrive à faire fonctionner le Pi-Lite seul évidemment))
    Je ne sais pas ce que je loupe au niveau de mon code, peut-être pourrais-tu m’éclaircir sur ce point ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s