Trying to code a Arduino Bluetooth with relay to turn on/off automatically

Hi!
I am a beginner into HC-05 Bluetooth and Arduino field. I'm currently working on a Relay connect automatically when in range Arduino Bluetooth module project, using an Arduino Nano and the HC-05 Bluetooth module.
I would like something look like the Bluetooth speaker, whenever smartphone is nearby (in range) relay will "ON" automatically, smartphone is out off range relay will "OFF" automatically. is this possible to Mobil Bluetooth and hc-05 paired Bluetooth connection with or without android app.
How do I make the code and sketch it , any one have idea please send details or similar project link.
Please refer below image for more my project idea...

In your Arduino code often you need to set certain I/O to a specific state, depending on the Bluetooth connection status.

This can be done in many ways, here is the way I do it:

If you are using HC-05, you can do it like this:

hc05

  1. Define a I/O pin to monitor the connection status: const int BtStatus = 2;
  2. Set the BtStatus pin to: pinMode (BtStatus, INPUT);
  3. Connect the STATE pin from the HC-05 to BtStatus pin.
  4. Monitor the connection in the loop().

Example:

const int BtStatus = 2;

void setup() {
  Serial.begin(9600);

  pinMode (BtStatus, INPUT);
}


void loop() {

  if (digitalRead (BtStatus) == 1)
  {
    Serial.println ("Client Connected");
    // Turn the relay ON
  }
  else
  {
    Serial.println ("Client Disconnected");
    // Turn the relay OFF
  }

}

Hope that helps,
Mike.

2 Likes

Hi Bharathi

To do it with an App, the App would have to be always on, probably not what you want to do. An Android background service on the other hand is worth investigation. You can't code it with App Inventor, that is Java/Kotlin territory using Android Studio.

1 Like

Hi, I have posted here some Tutorials about AppInventor and Arduino, maybe can help you

1 Like

Hi @Nilton_Santos,
Can you send HC-05 Bluetooth and Arduino Connection details and example code.

Hi, this is the link for my playlist in my channel:

And the codes you can get from:
https://drive.google.com/drive/folders/1UO5DpqmZWLikAud0fdxDPTCpXYMCI61h?usp=sharing

1 Like