// Benjamin Emereau 08/03/2024
bool powerstate = false; // "Bool variable to store power state (ON/OFF)"
String inputString; // "Temporary variable to read user input"
int ledPin = 8; // "Pin number for the LED (D1)"
void setup() {
Serial.begin(115200); // "Serial communication setup"
// "User instructions for controlling the LED via terminal"
Serial.println("Welcome! This ESP32 controls an LED.");
Serial.println("Type 'ON' in the terminal to turn the LED on.");
Serial.println("Type 'OFF' in the terminal to turn the LED off.");
pinMode(ledPin, OUTPUT); // "Set the LED pin as output"
}
void loop() {
if (Serial.available()) { // "Check if serial input is available"
inputString = Serial.readStringUntil('\n'); // "Read user input"
Serial.println(inputString); // "Echo user input"
if (inputString == "ON") {
powerstate = true; // "Turn LED on"
Serial.println("LED turned ON");
} else if (inputString == "OFF") {
powerstate = false; // "Turn LED off"
Serial.println("LED turned OFF");
}
if (powerstate) {
digitalWrite(ledPin, HIGH); // "Set LED pin high"
} else {
digitalWrite(ledPin, LOW); // "Set LED pin low"
}
}
}