hello everyone!I'm new to Arduino.
I'm trying to make a code that should transmit a command between 2 esp 32 (transmitter and receiver)
When I press the momentary button connected to the ESP32 transmitter, a message is sent via Bluetooth
The receiver receives the message and sends a short LOW pulse (ex: 200ms) on the T pin of a bistable relay module.
The module switches between NO ↔ NC at each pulse
I tried this code but it doesn't want to work:
ESP32 – Transmitter (with button)
BluetoothSerial SerialBT;
const int buttonPin = 0; // Pin where the button is connected
bool lastState = HIGH;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
SerialBT.begin("ESP32_TX"); // Bluetooth device name
}
void loop() {
bool currentState = digitalRead(buttonPin);
if (lastState == HIGH && currentState == LOW) {
// Button was just pressed
SerialBT.println("TRIGGER"); // Send trigger command
delay(200); // Debounce delay
}
lastState = currentState;
}
**ESP32 – Receiver (controls the relay)**
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
const int relayTriggerPin = 26; // Pin connected to T on the relay module
void setup() {
pinMode(relayTriggerPin, OUTPUT);
digitalWrite(relayTriggerPin, HIGH); // Keep HIGH when idle
SerialBT.begin("ESP32_RX", true); // true = Bluetooth slave mode
}
void loop() {
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n');
command.trim();
if (command == "TRIGGER") {
// Send a short LOW pulse to trigger the relay
digitalWrite(relayTriggerPin, LOW);
delay(200); // Duration of pulse (adjust if needed)
digitalWrite(relayTriggerPin, HIGH);
}
}
}```
If anyone can help me, thank you in advance!
Please provide details about the hardware you are using.
You chose the Nano Family > Nano ESP32 forum category for your topic, which would imply you are using the Arduino Nano ESP32 board. However, I have doubts about that as newcomers to the forum who don't make a strong effort to choose the appropriate category often use this category even though they are not using the Nano ESP32 board.
I'm not sure I understood correctly what you mean by "doesn't want to work". Please provide a more detailed description of what you mean in a reply on this forum topic to help us to understand it.
Make sure to include the following information:
What did you do?
What were the results you expected from doing that thing?
What were the results you observed that did not match your expectations?
If you encountered any errors or warnings, please provide the full and exact text of those messages.
The Nano ESP32 does not support Classic Bluetooth, so you can't use BluetoothSerial.
You can only use BLE (Bluetooth Low Energy).
There are many Example programs under ESP32 BLE Arduino.
I suggest you start with the server and client examples.
Last time I was fiddling with BT on my Arduino Nano ESP32 was almost two years ago (trying to get stupidly fancy with my ceylon eye / kitt project) and I was having problems where the BLE examples I was trying could not maintain any semblance of a persistent connection from any of my phones to the ESP32.
For example, boot up ESP32, it starts advertising (yay), connect to ESP32 from phone, send some commands, all goes well. Quit the BT app on the phone, re-start it, hit head against wall because the ESP32 is no longer advertising. Reboot ESP32 and then I can reconnect from the phone apps. Not optimal.
I needed to conjure up some code to watch- for disconnects and re-start advertising but the coding for BT made my eyes water, then I got distracted and that was the end of that.
After lots of distraction and travel I’m getting ready to pull out all the breadboards again and I’m wondering if the ESP-NOW library is going to be less painful or more straightforward for this requirement?
Or if there’s some newer BLE code, lol.
Thanks for any guidance. I’ll start poking into ESP-NOW docs regardless.
Personally I think ESP-NOW is much more straightforward than BLE. But it won’t work for your previous application because it cannot connect to the phone. It’s a protocol for communication between ESPs. So, a little bit different…