(JSON and the) Arduinaut

A pretty simple setup to read arduino pins and output to serial as JSON data.

Get the code here (and slightly more formal docs): https://github.com/karljacuncha/arduinaut

Quick set up guide

argh-blarg feckin-bastard-of-a what-the-hell -that's-just- argh

Y'know C? That's shite
And y'know Firmata? That might work for some people, but not me on my mac & uno - so that's shite too.
Y'know custom formats & byte streams and all that? They're especially shite.

The charming little phrase at the title of this section has been used by myself a number of times in the preceeding week.
Y'see, I had thought that it would be a splendid idea for me to get one of them fancy new accelerometer do-hickeys that all the cool kids are raving on about these days.
Eager as I was to get it making some beautiful noise (as a gesture based MIDI controller type yokeymebob) I hooked it up straight away and got me some data.
Some random looking data.
After a lot of swearing, debugging and research (in that oder), I found that this wuss of an IC can't handle a whole measly 5v, and that most likely I'd fried it with the wrong Vout.
Ok, so I'm an awful eejit, fair enough.
But what bugged me most was just trying to get a decent read on what was coming off the pins.

So yeah, there are things out there like Firmata and other libraries for handling communication with and monitoring an Arduino, but even if I could've gotten one of these working, why was there not just some simple set up that I could use to easily read the raw data coming off the board with no need for anything beyond the standard Auduino software?

Being a bit more of a high-level, functional language kind of guy, JSON is just one of those things I'm reading everywhere anyway.
It may not seem the most efficient for low level hardware, but it is easily both machine & human readable. It's ASCII and can be off the Arduino onto the serial port pretty easily then picked up by pretty much anything. And it provides some data validation due to its structure, which is always handy for serial comms.
In fact, as a good rule in general, everything's always fecked and broken and the best way to get it working is never trust anything to be ok and always keep it as simple and uncluttered as possible. The more stuff, the more stuff can go wrong.
So with that in mind, I went ahead and made my own bunch of scripts to suit what I wanted.

Reinventing the wheel...

The good thing about reinventing the wheel is that you can get a round one. - Douglas Crockford

... and then you get wooden one that's lighter and easier to build, then you get an iron one that can run on rails and support heavy loads, then you get a light aluminium one with spokes and tyres, then you get one that can roll about on the surface or Mars...

Yeah, I don't care of there's other stuff that I could try get working, I'm going back to basics here to keep it simple so that with just the Arduino software, you can quickly open the Serial Monitor and see the data straight away to confirm that much is actually working.

Next you'll probably want a client of some sort (or basic starting off point for an app) that will just fire up with little or no dependencies or extra configuration needed; just so you can see that the data is being received and it's responding as expected to whatever sensors/ICs you've got feeding into the Arduino.

Procssing tends to go hand in hand with the Arduino, so you'll likely have that already. If not, it's free and pretty simple to download and get up & running. The same goes for Python if you prefer the command line interface.

I kept the required extras down to just one in both cases, and in both cases that extra library is pretty common and easy to install.

The actual serial reading code in each case is basically just:

    // do stuff     
        internalBuffer = internalBuffer + everythingCurrentlyOnSerial
        if more than 1 complete line in internalBuffer:
            linesArray = internalBuffer.split("\n")     
            // the last line is probably incomplete, so stick it back on the buffer to be completed next time:
            internalBuffer = linesArray[lastItem]
            // it's the 2nd last on that should be the last complete read, so try parse that to a JSON object:
            latestData = json.loads(linesArray[secondlastItem])     
    catch Exception:
        log or handle if needed    
    // do more stuff, such as:
    send (latest_data) to doomsdayDevice

All scripts are reasonably well commented, and should be easy enough to reconfigure & extend.



The Arduino Sketch.
(Change the config to suit if needed - defualt is to 115200 baud, 100ms intervals, reading all pins. Monitor scripts default to this too)
Upload to your board.
Use the in-built serial monitor in the Arduino app to confirm the output:

{"A0":1023,"A1":0,"A2":674,"A3":0, ... "D13":0}
{"A0":999,"A1":0,"A2":674,"A3":0, ... "D13":1}

With that in place, try one of the monitor scripts for a nicer read out:



Most of this script is just the usual crap needed for creating a command line app (with optpArse), and some fomatting functions to present the data in a slightly more human-friendly way. (Those bits in themeselves may even be useful for other apps.)

A CLI script to see the arduino output.
Requires pySerial
Run as:

./ArduinautMonitor.py --port=/dev/tty.usbmodem3d11 --baud=115200



Most of this script is dealing with the GUI setup - setting up the select lists and handling choices on the port/baud lists, drawing the sliders & buttons and having them react to the data.
The more interesting bit is the Arduino class which should be relatively well self contained and reusable.

Requires controlP5
(Also uses org.json.* which I'm not sure is available in older versions of Processing)
Just load up the sketch & hit run...

Issues and Such...

Tested on OSX 10.6.81, with an Arduino Uno (original release).
Using Arduino 1.0.3, Processing 2.0b8 and Python 2.6

[1] Yeah yeah, I know I complain about IE users not updating while sticking with this old OS, but hey, I'm using a computer to do work on, not a feckin' phone for sharing youtube clips of cats and all that crap. And yeah, I probably should just switch away from Apple Computers at this stage, but it does look like every other OS is on that downward spiral too. </rant>