A simple Command Line Interface (CLI) to Arduino

Last Updated: April 7th, 2013 by Chris
Filed under: Physical Computing
Tags:

Note: a newer version of this sketch is available here.

Synopsis: Example code for creating a CLI that interacts with an Arduino-based microcontroller. Useful for playing with pins, turning them on or off, etc., in real-time, using a terminal program.

This example demonstrates how to create a CLI for an Arduino-based microcontroller. Connect to the Arduino with a terminal application (PuTTY is a good one) to send the commands (the serial monitor included with Arduino is not a good choice for this). The LED on pin 13 turns HIGH and LOW in response to the commands.

You can enter one of the following commands at the prompt:

  • on
  • off
  • blink

Blink is a toggle, in that it either starts the LED blinking if it is not currently blinking, or stops the LED from blinking if it currently is blinking.

Note that this sketch does not filter out BACKSPACE key presses.  If BACKSPACE is pressed during the input, then the input is invalid.

The terminal application should use the following settings (the default works with PuTTY):

  • The same COM port to which you connect the Arduino software for program downloads
  • Speed (baud): 9600
  • Data bits: 8
  • Stop bits: 1
  • Parity: None
  • Flow Controll: XON/XOFF

Please post a comment if you have questions (or comments!).

Screenshot

simplecli working through PuTTY

simplecli working through PuTTY

The Code

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* simplecli version 001
 * -----------------
 * C. Cosentino 2009 - http://digital-salvage.net/?p=36
 *  This software is licensed under the CC-GNU GPL version 2.0
 *     See: http://creativecommons.org/licenses/GPL/2.0/
 *     
 *  Simple CLI example that demonstrates how to use serial to perform
 *  interactive commands (turning a pin on or off) via a terminal application.
 */
 
#define ledPin 13           // define a pin for the LED
#define MCHARS 8            // max characters to accept
#define ENTER 13            // byte value of the Enter key (LF)
char inStr[MCHARS];         // the input string 
int inByte = 0;             // byte being read in
int strCount = 0;           // counter for the string
int blink = 0;              // toggle for blink routine
int value = LOW;            // Initial Pin value of LED for blink
 
void setup() {
        pinMode(ledPin, OUTPUT);  // sets ledPin as an OUTPUT pin
	Serial.begin(9600);	  // opens serial port, sets data rate to 9600 bps
        Serial.print(">");        // print  prompt when serial is ready
}
 
void loop() {
  if(Serial.available()) {                 // if bytes are available to be read
    inByte = Serial.read();                // read the byte
    Serial.print(char(inByte));            // echo byte entered to terminal as a char
    if(inByte != ENTER) {                  // if the byte is not a NEWLINE
      inStr[strCount] = char(inByte);      // add the byte to the "String" array
      strCount++;                          // increase the item string count
    } 
  }
  if(inByte == ENTER || strCount >= MCHARS) {  // if enter was pressed or max chars reached
    Serial.flush();                         // flush the serial data (overkill?)
    Serial.println("");                     // print a newline
    if (strcmp(inStr, "blink") == 0){       // string compare, does entered text == blink?
      if (blink == 1) {                     // if so, if blink is 1, pin was already blinking
        Serial.println("LED Off");          // print to the terminal
        digitalWrite(ledPin, LOW);          // make certain LED is in LOW state
        blink = 0;                          // toggle blink variable
      } else {                              // if it was not 1...
        Serial.println("LED Blinking");          // print to the terminal
        blink = 1;                          // ... then set to 1
      } 
    } else if (strcmp(inStr, "on") == 0){   // string compare, does entered text == on?
      digitalWrite(ledPin, HIGH);           // if so, then turn on ledPin
      Serial.println("LED On");              // print to the terminal
      blink = 0;
    } else if (strcmp(inStr, "off") == 0){  // string compare, does entered text == off?
      digitalWrite(ledPin, LOW);            // if so, then turn off ledPin
      Serial.println("LED Off");            // print to the terminal
      blink = 0;
    } else {                                // if the input text doesn't match any defined above
      Serial.println("Invalid.");           // echo back to the terminal
    }
    strCount = 0;                           // get ready for new input... reset strCount 
    inByte = 0;                             // reset the inByte variable
    for(int i = 0; inStr[i] != '\0'; i++) { // while the string does not have null
      inStr[i] = '\0';                      // fill it with null to erase it
    }
    Serial.println("");                     // print a newline
    Serial.print(">");                      // print the prompt
  }
  if(blink == 1) {                          // if blink
    delay(100);                             // wait for it! (you could use millis if/then here)
    if(value == LOW) {                      // if pin is LOW
      value = HIGH;                         // set it for HIGH
    } else {                                // otherwise it is HIGH
      value = LOW;                          // so set it for low
    }
    digitalWrite(ledPin, value);            // send the command to the pin
  }
}

Leave a Reply

Login with one of the following services:


(you must login to post comments. Otherwise we get lots of Spammers!