Close

Processing on the Pi - Networking

A project log for Networked Low Resolution DMD Projectors

WiFi networked electro-mechanical 7x7 pixel flip-dot displays as giant DMDs (Digital Micromirror Devices) for low resolution projection

michael-shaubMichael Shaub 02/24/2016 at 03:470 Comments

Date: 2/6/2016

For the Projectors to synchronize over WiFi I needed them to be able to communicate via the Processing sketches. Since I've used OSCp5 library in the past, that was an obvious thing to try. The library runs on the Raspberry Pi but not as I'd expected. All the sketches that use the loopback or local host address to test sending and receiving on the same device worked fine. But, when I tried to have another computer contact the Pi over the network it never worked properly. I asked for help on the Processing Forum here but didn't get help fast enough for this project.

So, Plan B.

I found some example sketches on the Processing website in an article called Network Tutorial. In that series there's an example called Shared Drawing Canvas that I decided to try and it worked! Even though the Pi wouldn't report its IP address properly, when I specified the address it worked just fine. The code is shown below:

// by Alexander R. Galloway
// from https://processing.org/tutorials/network/

// 2A: Shared drawing canvas (Server) - see Client Below

import processing.net.*;

Server s; 
Client c;
String input;
int data[];

void setup() { 
  size(450, 255);
  background(204);
  stroke(0);
  frameRate(5); // Slow it down a little
  s = new Server(this, 12345);  // Start a simple server on a port
} 
void draw() { 
  if (mousePressed == true) {
    // Draw our line
    stroke(255);
    line(pmouseX, pmouseY, mouseX, mouseY); 
    // Send mouse coords to other person
    s.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
  }
  
  // Receive data from client
  c = s.available();
  if (c != null) {
    input = c.readString(); 
    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ' '));  // Split values into an array
    // Draw line using received coords
    stroke(0);
    line(data[0], data[1], data[2], data[3]); 
  }
}

//---------------------(client commented out below)------------------------------//
/*
// 2B: Shared drawing canvas (Client)

import processing.net.*; 

Client c; 
String input;
int data[]; 

void setup() { 
  size(450, 255); 
  background(204);
  stroke(0);
  frameRate(5); // Slow it down a little
  // Connect to the server’s IP address and port­
  c = new Client(this, "192.168.86.106", 12345); // Replace with your server’s IP and port
} 

void draw() {         
  if (mousePressed == true) {
    // Draw our line
    stroke(255);
    line(pmouseX, pmouseY, mouseX, mouseY); 
    // Send mouse coords to other person
    c.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
  }

  // Receive data from server
  if (c.available() > 0) { 
    input = c.readString(); 
    input = input.substring(0,input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ' '));  // Split values into an array
    // Draw line using received coords
    stroke(0);
    line(data[0], data[1], data[2], data[3]); 
  } 
}
*/

Discussions