Listen to the serial port for ASCII commands and act upon them.
The following demo commands exist: 'on' : Turn onboard LED on. 'off' : Turn onboard LED off. set ##.##
: Print the passed value back to the terminal, interpreted as a float, integer and boolean.
#include <Arduino.h>
const uint8_t CMD_BUF_LEN = 16;
char cmd_buf[CMD_BUF_LEN]{'\0'};
void setup() {
Serial.begin(9600);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
}
void loop() {
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.print(" As integer: ");
Serial.print(" As boolean: ");
} 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:215
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:202
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:194
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:101
bool available()
Poll the stream for incoming characters and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:62