Files

74 lines
1.9 KiB
C++

/*
* ESP32 Single Relay Test - Minimal Example
*
* This is a simplified version for testing just ONE relay
* Perfect for beginners or initial hardware testing
*
* Hardware Setup:
* - Connect ESP32 5V to Relay Module VCC
* - Connect ESP32 GND to Relay Module GND
* - Connect ESP32 GPIO 13 to Relay Module IN1
*
* Commands via Bluetooth:
* - ON: Turn relay on
* - OFF: Turn relay off
* - STATUS: Check relay state
*/
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
// Relay configuration
const int RELAY_PIN = 13; // Change this to your GPIO pin
bool relayState = false;
void setup() {
// Start serial for debugging
Serial.begin(115200);
Serial.println("ESP32 Single Relay Test");
// Configure relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Start with relay OFF (active LOW)
// Start Bluetooth
SerialBT.begin("ESP32-Test-Relay");
Serial.println("Bluetooth ready! Device: ESP32-Test-Relay");
Serial.println("Commands: ON, OFF, STATUS");
}
void loop() {
// Check for Bluetooth commands
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n');
command.trim();
command.toUpperCase();
Serial.println("Received: " + command);
if (command == "ON") {
relayState = true;
digitalWrite(RELAY_PIN, LOW); // Active LOW relay
SerialBT.println("Relay is ON");
Serial.println("Relay is ON");
}
else if (command == "OFF") {
relayState = false;
digitalWrite(RELAY_PIN, HIGH); // Active LOW relay
SerialBT.println("Relay is OFF");
Serial.println("Relay is OFF");
}
else if (command == "STATUS") {
String status = relayState ? "ON" : "OFF";
SerialBT.println("Relay: " + status);
Serial.println("Relay: " + status);
}
else {
SerialBT.println("Unknown command. Use: ON, OFF, or STATUS");
}
}
delay(10);
}