Generic command interface allows serial and network scripting for other applications

From the Blog


Building a generic command line server for Arduino

Posted by ajfisher on July 24th, 2011 at 5:24 pm

I’ve been playing around a lot with the idea of networked arduino devices that can respond to events by having messages passed to them to do different things (more on this in another post). I noticed I was duplicating a lot of code in set up for things like a server to take the message and process it so got to thinking – what if you could build a generic server that will take a message much like FTP and then the arduino does an action.

And thus several evenings have been spent creating such a thing.

At the moment this is a standalone sketch which I will attempt to bust out into a library shortly. The main thrust of the design is this:

  • Set up a command class which has a command code, a pointer to a function and a help message
  • Write a server that will take inbound messages, parse them and the call the functions
  • Write some generic helper commands that can be used to do simple tasks (analog and digital read and write etc).

The beauty of this design is that it works across Serial connections as well as Ethernet ones so you can try it out without a networked arduino, similarly it works using a protocol style inspired by FTP where you send a message and receive a response, this makes it infinitely scriptable. Finally because the command structure is abstract you can implement your own commands easily that do your own function.

For example I have a function that pulses an LED for a period defined by the argument. This function uses PWM and ramps up from 0 to full brightness, then holds for the period specified then ramps down until it’s at zero again. The function is called pulse and takes care of determining what the parameters passed into it mean and what errors mean etc.

My server just understands the call PULS and then passes anything after the space to the function pulse() which then does the work.

It’s early days with this project as yet and more updates will follow. You can follow, fork  or just download the code at the github project.