The Arduino Command Line Interface, aka cmdArduino, is a simple shell that can be run on an Arduino. It’s a small library to parse commands from the serial port on Arduino compatible boards.It’s nothing fancy and its main purpose is to allow users to easily call their functions on a running Arduino via a simple serial terminal. It also allows users to pass in arguments from the command line into the functions they wrote so they can easily toggle pins, set blinking speed, set pwm duty cycles, or whatever else might need command line user input.
The direct link to the github repository is here: Github Link
Alternatively, you can install it automatically using the Arduino library manager and searching for cmdArduino.
Or you can install it manually using the direct download link from github here.
To use it create your command functions, then wire them up in your setup function. All command functions must take int arg_cnt, char ** args
as it’s parameters and return void
. Here’s an example from a simple robot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | void setup() { // initialize the command line cmd.begin(57600); // create commands in the command table cmd.add('hello', cmdHello); cmd.add('blink',cmdBlink); } void loop() { // poll command line for any new commands cmd.poll(); } /* Commands go here */ void cmdHello(int arg_cnt, char **args) { Serial.println("Hello world!"); } void cmdBlink(int arg_cnt, char **args) { int blinkDelay = cmd.conv(args[1]); pinMode(13, OUTPUT); digitalWrite(13, HIGH); delay(blinkDelay); digtialwrite(13, LOW); delay(blinkDelay); } |
Call cmd.begin(baudrate) to initialize the command line. Call cmd.add(<cmdName>, <cmdFunction>) to attach a command name to a command function. Open the Serial Monitor in the Arduino IDE. Make sure the baudrate is set correctly to what you have in the code, ie: in the above example, it’s 57600. Also make sure the line endings are set to “Carriage Return”. Then type <enter> and you should see the command line. You can now enter your commands.
If you type ‘hello’ at the command line, you get ‘Hello world!’ back.
If you type ‘blink 1000’ at the command line, the built-in LED on the board will blink with a duty cycle of 1 second, ie: 1 second ON, 1 second OFF.