DvG_StreamCommand
StreamCommand.ino

Listen to the serial port for ASCII commands and act upon them.

An ASCII command is any string ending with a newline '\n' character.

The following demo commands exist:

#include <Arduino.h>
// Instantiate serial port listener for receiving ASCII commands
const uint8_t CMD_BUF_LEN = 16; // Length of the ASCII command buffer
char cmd_buf[CMD_BUF_LEN]{'\0'}; // The ASCII command buffer
DvG_StreamCommand sc(Serial, cmd_buf, CMD_BUF_LEN);
void setup() {
Serial.begin(9600);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
}
void loop() {
// Poll the Serial stream for incoming characters and check if a new
// completely received command is available
if (sc.available()) {
// A new command is available --> Get it and act upon it
char *str_cmd = sc.getCommand();
Serial.print("Received: ");
Serial.println(str_cmd);
if (strcmp(str_cmd, "on") == 0) {
Serial.println(" -> LED ON");
digitalWrite(PIN_LED, HIGH);
} else if (strcmp(str_cmd, "off") == 0) {
Serial.println(" -> LED OFF");
digitalWrite(PIN_LED, LOW);
} else if (strncmp(str_cmd, "set", 3) == 0) {
Serial.print(" As float : ");
Serial.println(parseFloatInString(str_cmd, 3));
Serial.print(" As integer: ");
Serial.println(parseIntInString(str_cmd, 3));
Serial.print(" As boolean: ");
Serial.println(parseBoolInString(str_cmd, 3) ? "true" : "false");
} else {
Serial.println(" Unknown command");
}
Serial.println("");
}
}
int parseIntInString(const char *str_in, uint16_t pos)
Safely parse an integer value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:206
bool parseBoolInString(const char *str_in, uint16_t pos)
Safely parse a boolean value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:193
float parseFloatInString(const char *str_in, uint16_t pos)
Safely parse a float value in C-string str_in from of position pos.
Definition: DvG_StreamCommand.cpp:185
A lightweight Arduino library to listen to a stream, such as Serial or Wire, for incoming commands (o...
Class to manage listening to a stream, such as Serial or Wire, for incoming ASCII commands (or ASCII ...
Definition: DvG_StreamCommand.h:32
char * getCommand()
Return the reference to the command buffer only when a complete command has been received....
Definition: DvG_StreamCommand.cpp:92
bool available()
Poll the stream for incoming characters and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:53