Code 7: //The demo code is Bluetooth You can copy the code to your Arduino IDE.
Code:
#include "BluetoothSerial.h"
// init Class:
BluetoothSerial ESP_BT;
// init PINs: assign any pin on ESP32
int Relay1 = 2;
int Relay2 = 15;
int Relay3 = 5;
int Relay4 = 4;
// Parameters for Bluetooth interface
int incoming;
void setup() {
ESP_BT.begin("KC868-A4-BlueTooth"); //Name of your Bluetooth interface -> will show up on your phone
pinMode (Relay1, OUTPUT);
pinMode (Relay2, OUTPUT);
pinMode (Relay3, OUTPUT);
pinMode (Relay4, OUTPUT);
}
void loop() {
// -------------------- Receive Bluetooth signal ----------------------
if (ESP_BT.available())
{
incoming = ESP_BT.read(); //Read what we receive
// separate button ID from button value -> button ID is 10, 20, 30,40 etc, value is 1 or 0
//Relay1 ON:11 OFF:10
//Relay2 ON:21 OFF:20
//Relay3 ON:31 OFF:30
//Relay4 ON:41 OFF:40
int button = floor(incoming / 10);
int value = incoming % 10;
switch (button) {
case 1:
digitalWrite(Relay1, value);
break;
case 2:
digitalWrite(Relay2, value);
break;
case 3:
digitalWrite(Relay3, value);
break;
case 4:
digitalWrite(Relay4, value);
break;
}
}
}