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