ESP32 with 4 Port Relay - Bluetooth Control

Good evening,
I am a newbie working on a project to control a 12V 4-port relay with an ESP32 controller. I have the wiring and majority of the code complete and functioning, however, I am having issues with the sending/receiving data through bluetooth.

Since I can send the commands through serial monitor i(using USB) in the arduino program and can get the relays to turn on/off., I feel like this is something simple to get the bluetooth functioning as well (a missing command in void_loop?). I can connect to the device with my phone and serial monitor, so connecting to the device does not seem to be an issue.

I have searched the forums and internet for an answer to this, but have not had been able to track down a solution.

I'm using an ESP DEV KIT V1 with HiLetGo and a HiLetGo 12V 4 Channel Relay Module.

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

const int Relay1 = 25; //PIN 25 FOR RELAY #1
const int Relay2 = 26; //PIN 26 FOR RELAY #2
const int Relay3 = 27; //PIN 27 FOR RELAY #3
const int Relay4 = 33; //PIN 33 FOR RELAY #4
byte serialA;

void setup(){

  Serial.begin(115200);
  SerialBT.begin("ESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

  pinMode(Relay1,OUTPUT); //SETTING THE OUTPUT FOR RELAY #1
  pinMode(Relay2,OUTPUT); //SETTING THE OUTPUT FOR RELAY #2
  pinMode(Relay3,OUTPUT); //SETTING THE OUTPUT FOR RELAY #3
  pinMode(Relay4,OUTPUT); //SETTING THE OUTPUT FOR RELAY #4
}


void loop() {

    if (Serial.available()>0) {serialA =Serial.read(); Serial.println(serialA);} switch(serialA){;    
  delay(20); 
}
    case 49:
    digitalWrite(Relay1, !digitalRead(Relay1));
    delay(100);
    break;
    //if receiving a 49(ascii for 1) turn on Relay #1
    
    case 50:
    digitalWrite(Relay2, !digitalRead(Relay2));
    delay(100);
    break;
    //if receiving a 50(ascii for 2) turn on Relay #2
    
    case 51:
    digitalWrite(Relay3, !digitalRead(Relay3));
    delay(100);
    break;
    //if receiving a 51(ascii for 3) turn on Relay #3
    
    case 52:
    digitalWrite(Relay4,!digitalRead(Relay4));
    delay(100);
    break;
    //if receiving a 52(ascii for 4) turn on Relay #4
    
    case 53:
    digitalWrite(Relay1, HIGH);
    digitalWrite(Relay2, HIGH);
    digitalWrite(Relay3, HIGH);
    digitalWrite(Relay4, HIGH);
    break;
    //if receiving a 53(ascii for 5) turn on ALL Relays
    
    case 54:
    digitalWrite(Relay1, LOW);
    digitalWrite(Relay2, LOW);
    digitalWrite(Relay3, LOW);
    digitalWrite(Relay4, LOW);
    break;
    //if receiving a 54(ascii for 6) turn OFF ALL Relays
  }

}

I would appreciate any help that the group could provide!

Thanks,
Jerry

Hi Jerry,
welcome to the forum.
well done to post code as a code-section.

your code does nothing more than include and define a bluetooth-object.

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;

But there is nothing that even tries to receive something over bluetooth

So I recommend looking into the example that comes with the ESP32-board installation
SerialToSerialBT.ino

as a general search strategy: searching with quite a lot keywords as the very first attempt is always worth a try
www.google.de/search?as_q=ESP32+bluetooth+relay

if this doesn't lead to good ressource reduce the keywords to become more general
www.google.de/search?as_q=esp32+blue+tooth

best regards Stefan

Welcome to the forum

Here are a few extra tips that might help you and us in the future. :slight_smile:

Try the Tools -> Auto Format function in the IDE to make sure your code looks nice and is easy to read. You can configure the style to your liking.

Arduino-IDE-Auto-Format-configuration

Learn how to avoid the delay() functions. It is evil in most cases and cause for many questions/problems posted in the forum. Especially communication stacks dislike, not getting enough attention from you. Have a look at the following example to learn about timing your application without delay().

File -> Examples -> 02.Digital -> BlinkWithoutDelay

It is a good goal to have your loop function run as often as possible and have each task do only a little bit at a time. This way everything seem to work in parallel and you can extend your sketch over time without breaking what you already have.

If you use a variable only in one function declare it locally (inside the function). e.g. your serialA
If you need this variable to remember the value between function calls use the "static" keyword.

The following line

const int Relay1 = 25; //PIN 25 FOR RELAY #1

can be changed to this

#define RELAY_1_PIN 25

works very similar but I prefer it because

  • it is now a constant no RAM needed for the variable
  • the naming convention of CAPTIAL_UNDERSCORE is used by many for constant values (see OUTPUT, HIGH, ...)
  • it is clear from the name it is used for a pin
  • no comment needed anymore
  • the code is very clear

pinMode( RELAY_1_PIN, OUTPUT );
digitalWrite( RELAY_1_PIN, HIGH );

Avoid comments that are not necessary. e.g.

pinMode(Relay1,OUTPUT); //SETTING THE OUTPUT FOR RELAY #1

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.