Using Processing 2.0

I wanted to use the computer language ‘Processing’ to read data from a serial port and record the data. Here are my collected notes on doing this.

Processing is an open-source programming language which has been designed to teach computer programming fundamentals through a visual environment. It is the basis of the language used for the Arduino development envrionment.

It is designed to be easy to use, but also powerful and have lots of features useful for students, artists and designers. It is also multi-platform and can easily create executable stand-alone code. The programs can easily be shared with others.

I had used it briefly for a hack-session with Active Ingredient, building a lamp which varies its brightness in response to changes in atmospheric CO2 levels.

Design idea

Lots of the power monitoring equipment I have been building recently has been streaming data out via a serial connection. I am implementing a universal protocol for the data streams, hence I can produce plug and play devices which work with all the equipment I produce.

I wanted to take this stream of data (generated approximately every second) and display it in an interesting format and also record the data to a spreadsheet file.

I also wanted the program to be multi-platform and easy to alter, adjust and hack for other people to use.

Data format

The data is sent in 12 byte strings with a ‘a’ at the start of each piece of data. Each device has a unique reference. The format from the Pedalog is as follows:

  • aXXV+++—–
  • aXXI++++—-
  • aXXP++++—-

At 115200 baud.

Where XX is the unique reference (this must be in capitals and can be from AA to ZZ). The data is within the ++++ part of the output and the 12 bytes are filled with ‘-‘ as padding to make up 12 bytes. Voltage (volts) and Current (amps) both have one decimal place, whereas Power is in Watts

For example:

aFFV123—–

This device reference ‘FF’ and the voltage is 12.3V

aABI1234—–

This is device reference ‘AB’ and the current is 123.4A

aCCP1234—-

This is device reference ‘CC’ and the power is 1234W

 We need to grab the data arriving at the serial port and parse it (strip out all the bits we dont need) and display those values.

Reading the Serial Port

 There is an example code in the processing examples. This works to read data from a serial port (found by listing all the serial ports). It then displays any data which arrives at the serial port.

This works well, but the data stream is the ‘int’ value, whereas we want to the ‘int’s converted into ‘char’ type. This was done using the (char)int conversion.

I also added code to check that the data started with an ‘a’ and hence put each 12 byte data stream into a seperate buffer.

The next thing to do was to strip out the voltage, current and power as data values. This next code reads the incomming serial port values, checks that they have come from the correct device ID, checks that they are the correct ‘aXXXXXXXXXXX’ format. It then displays them on the screen as strings. These are then converted into integers and then back into strings to be displayed again.

I need to add some more checks to ensure that strings which are not integers are never converted (as this crashes the program).

Here is a screen shot of the program running:

Graphing the data

The next thing to do was to plot the data on a graph. This is where Processing comes into its own – there are loads of examples of interesting ways to display information visually.

The first example displays a circle which gets larger for a higher generated power. This looked OK, but was not as good as I thought, mainly due to the ‘jerky’ motion caused by the data being streamed at 1 second samples.

The code to do this was simple, I just drew an ellipse, after displaying the data, with a diameter proportional to the powerInt:

There is lots more work that could be done here, but I’ve got something working and so will develop that. This blog post is a reminder of the steps I made to get going with drawing data.

Saving the data to a file

The last thing to do was to save the data stream to a file, preferably some kind of data file, like comma sperated variables (.csv).

This turns out to be pretty straight forward. The main command is saveStrings :

There is an example of saving data to a file within the Processign examples. This does pretty much exactly what I need – append data to a file.

So I’m now in the position or reading and parsing data from the serial port, displaying that data visually and saving the data to a text file. Now to work on the full code design.

Leave a Reply

Your email address will not be published. Required fields are marked *