Listen to the serial port for ASCII commands and act upon them.
An ASCII command is any string ending with a newline '\n' character.
#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:203
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:190
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:182
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:91
bool available()
Poll the stream for incoming characters and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:52