NLTK > Learning > Tutorials > Processing

Processing

You can use the Processing language with the Hub (version 0.9 or later) to gain access to everything it supports, including the Arduino Controller (running Firmata), the XBee wireless sensor, the Make Controller, and the DMX 512a and MIDI features of MediaControl.

Here is an example Processing sketch that retrieves analog values from an Arduino. For this to work, you must first upload the Firmata library to your Arduino:

  • In the Arduino software, open the standard firmata:
  • File –> Sketchbook –> Examples –> Library-Firmata –> StandardFirmata
  • Upload the software to your Arduino
  • Leave the Arduino attached to your computer by USB.
// Simple communication with NETLab Hub server
//    Establishes socket connection
//    Sends init command to talk to Arduino running Firmata
//    Sends command to start reading from analog input port 0
//    Waits for value strings to be sent by Hub server
//    Parses value strings and calls handleInput() function
//    handleInput() draws a rect with a X position of analog input value/4

import processing.net.*;
Client myClient;
byte nullByte = 0; // the null character is at the end of each message sent by the NETLab Hub server

void setup() {
  size (300, 100);
  background(0);
  rect(0, 33, 33, 33);
  // connect to the NETLab Hub, which listens on port 51000
  myClient = new Client(this, "127.0.0.1", 51000);
  // send initialization to connect the Hub to Arduino, must be terminated by newline "\n"
  // assumes a serial connection on the mac that begins with usbserial, "*" is a wildcard
  // if using a PC, change this to the COM port the Arduino is connected on, e.g. "COM5"
  // the last parameter is the baud rate, which for Firmata 2.1/Arduino 18 should be 57600
  myClient.write("/service/arduino/reader-writer/nlhubconfig/connect /dev/cu.usbserial* 57600\n");
  // send command to get values from analoginput port 0 of Arduino
  myClient.write("/service/arduino/reader-writer/analogin/0/value\n");
} 

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);
}

// ClientEvent message is generated when the server
// sends data to an existing client.
void clientEvent(Client theClient) {
  int theLength;
  String inputString;
  int inputValue;

  theLength = theClient.available();
  if (theLength > 0) { 

    // get the string from the Hub, each message is delimited by a null character at end
    inputString = theClient.readStringUntil(nullByte);
    // make sure we found a nullbyte and read some characters
    if (inputString != null) {
      // NETLab Hub sends an OSC style string like this: /analogin/0/value 169
      //println(inputString);
      inputString = split(inputString, ' ')[1]; // separate the analog value at end of string
      inputValue = int(split(inputString,char(nullByte))[0]); // get rid of null at end of string
      //println(inputValue);
      handleInput(inputValue);
    }
  }
}

Last modified July 19th, 2010