#define LED_BUILTIN 18
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // define baudrate
pinMode(LED_BUILTIN, OUTPUT); // set the digital pin as output:
Serial.write(45); // write a byte with the value 45 => '-' character
Serial.write('\n'); // write a newline character
Serial.println("Hello, ESP32");
// check buffer size
int wlen = Serial.availableForWrite();
// prints the received data
Serial.print("TX buffer size: ");
Serial.println(wlen);
}
void loop() {
// put your main code here, to run repeatedly:
// listen serial data from PC
if(Serial.available()) {
// READ SERIAL DATA ONE BY ONE
// int incomingByte = Serial.read();
// // Serial.println(incomingByte); // real data get from pc (int)
// Serial.print("I received: ");
// Serial.println((char)incomingByte);
// READ DATA UNTIL SENDER PRESS ENTER
String command = Serial.readStringUntil('\n'); // read string until newline character
if (command == "ON") {
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
Serial.println("Turn LED ON");
} else if (command == "OFF") {
digitalWrite(LED_BUILTIN, LOW); // turn off LED
Serial.println("Turn LED OFF");
}
}
delay(10); // this speeds up the simulation
}