Processing

You can use the Processing (as well as any other language that supports sockets) with the Hub to gain access to everything it supports, including the Arduino Controller (running Firmata), the XBee wireless sensor, and the DMX 512a and MIDI features of MediaControl.

As of the 2.0 version of the Hub (4.0 NETLab Toolkit release), a Processing library is included to making communicating with the Hub easier. This library is contained in a folder of the  release called “NETLab Hub Processing library”. Place the library folder “Put into your Processing libraries folder”  in your Processing folder in your documents folder.

Below is a simple example sketch that moves a small square back and forth based on the values returned by the Arduino Analog Input 0. Be sure your Arduino has the Firmata Software loaded on it from the Arduino software release (File –> Examples –> Firmata –> StandardFirmata).

Download the Processing Sketch: Hub_usage_demo

import processing.net.*;
import netlab.hub.processing.Hub;

Hub hub;
int arduinoPort = 0;

void setup() {
  // set up the display
  size (300, 100);
  background(0);
  rect(0, 33, 33, 33);

  // set up the Hub
  frameRate(30); // Poll the Hub at 30 fps
  hub = new Hub(this);
  // Connect to a Hub running on the local machine on the default port
  hub.write("/service/arduino/reader-writer/connect /dev/cu.usb*");
}

void draw() { 
  // do nothing, because everthing happens in response to data coming from Hub
}

void handleInput(int value) {
  // insert your code here to do something with values from Arduino

  // this code redraws a rectangle with an X position of the value/4
  background(0);
  value = value/4;
  rect(value, 33, 33, 33);
}

void hubDataReceived(String command, String[] values) {
  String arduinoSerialPort;
  // If we received an Ardunio connect confirmation then
  // read the full name of the serial port use it to request polling on the serial port
  if ("/service/arduino/reader-writer/connect".equals(command) && "OK".equals(values[0])) {
     arduinoSerialPort = values[1];
     // set up automatic polling in the Hub
     hub.write("/service/arduino/reader-writer/poll /{" + arduinoSerialPort + "}/analogin/" + arduinoPort);
  } else if (values.length > 0) {
    //Hub.print(command, values); // Print the incoming message to the Processing console
    handleInput(int(values[0]));
  }
}

Last modified October 31st, 2012