Go Down

Topic: HC-05 State Pin (Read 540 times) previous topic - next topic

Netratt

Hi,
I'm new with arduino programming and all that stuff but managed to make a project in which i want to unlock a door with my android phone.So far everything worked perfectly until i wanted to add the auto lock/unlock feature.
I made the android app send the commands for lock and unlock(manually pressing buttons),they work perfectly.It reconnects automatically when the app is started to the bluetooth module(HC-05).

My main concern is that the STATE pin returns only '0'  value altho the bluetooth module is connected with my phone. I have no clue why or what my mistake is on the coding part involving arduino.
I want to use the STATE pin as a value that will be used by the Android app when i activate auto lock/unlock feature.

I used the " Serial.println(digitalRead(BTpin));" command to see what the pin STATE returns,that is the only reason it is included in the command without a specified statement attached to it.

I did a ton of searching on the internet about how to code it right and so on but nothing worked since STATE pin only returns the value '0'.Now i am confused and i have no clue what worked or what might have worked.

What i am trying to make arduino on a logical thinking: "If the bluetooth module is connects to the phone unlock the door, when the connection is lost lock the door." On a more detailed level i will make the app send a value/string(whatever) when in auto mode, which arduino will read it and unlock the door,but arduino will be the one checking if the connection is still available and if not it will lock the door.

Code: [Select]
#include <SoftwareSerial.h>
#define ledPin 7
#define autoPin 8
#define BTpin 12
SoftwareSerial BTserial(2, 3);


int state = 0;
void setup()
 {
  pinMode(BTpin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0)
    {
    state = Serial.read();
    }
 
          if (state == '0')
          {
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin, LOW);
          Serial.println("Locked");
          Serial.println(digitalRead(BTpin));
          state = 0;
         
          }
          else if (state == '1')
          {
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin, LOW);
          Serial.println("Unlocked");
          Serial.println(digitalRead(BTpin));
          state = 0;
          }
}

PaulS

Quote
My main concern is that the STATE pin returns only '0'  value
The state variable is not valued by reading a pin, so I have to wonder what you are talking about.
The art of getting good answers lies in asking good questions.

Netratt

#2
Feb 11, 2019, 10:17 pm Last Edit: Feb 11, 2019, 10:18 pm by Netratt
The state variable is not valued by reading a pin, so I have to wonder what you are talking about.
My mistake that i didn't mention the fact that the android app while pressing the lock/unlock button sends the bytes 48 or 49(ASCII) and 'state' is used to store those two values.Altho they might look the same for unlock statement arduino will have a longer delay than the lock statement.
The ledPin 7 is actually controling a relay.

The thing is that yes i can and know how to read that STATE Pin from the bluetooth module but it returns only '0'/LOW so basically i can't use it.It never changes to HIGH state when  my phone connects to the module.The question is why it doesn't change,do i need to tell it to change from LOW to HIGH from the Android app? I doubt that,it should return that by itself when connected or disconnected from the phone/app.

PaulS

Post a link to the device you are using. It is not clear what the STATE pin is trying to tell you, but, I doubt that the STATE refers to connected vs. disconnected.
The art of getting good answers lies in asking good questions.

cattledog

Good job using code tags with your first post.

Quote
I used the " Serial.println(digitalRead(BTpin));" command to see what the pin STATE returns
Are you saying that digitalRead(BTpin) returns a value of 0 when the 2x flash (not the unconnected rapid flash) of led on the module indicates the phone is connected to the module.

Code: [Select]
void setup() {
  Serial.begin(115200);
  pinMode(12,INPUT);//connnected to State pin
}
void loop() {
  Serial.println(digitalRead(12));
  delay(1000);
}


Quote
"If the bluetooth module is connects to the phone unlock the door, when the connection is lost lock the door."
You need to make the lock/unlock code dependent upon the digitalRead() of the state pin, and not dependent upon some entry from Serial.

cattledog

Quote
The thing is that yes i can and know how to read that STATE Pin from the bluetooth module but it returns only '0'/LOW so basically i can't use it.It never changes to HIGH state when  my phone connects to the module.The question is why it doesn't change,do i need to tell it to change from LOW to HIGH from the Android app? I doubt that,it should return that by itself when connected or disconnected from the phone/app.
On my HC05 the state pin indeed indicates the connection state.

PaulS

You need to make the lock/unlock code dependent upon the digitalRead() of the state pin, and not dependent upon some entry from Serial.
But, the entry from Serial defines whether the phone wants the door locked or unlocked.

What OP is trying to do, as I understand it, is to lock the door when the phone drops the connection by going out of range.
The art of getting good answers lies in asking good questions.

cattledog

#7
Feb 11, 2019, 10:35 pm Last Edit: Feb 11, 2019, 10:36 pm by cattledog
With my phone and module, the digitalRead() of the state pin indeed transitions from 1 to 0 when the phone goes out of range and the connection is dropped.

Netratt

So, this is the new code and i changed the int state to int command to get rid of the confusion regarding the STATE pin.
Code: [Select]
#include <SoftwareSerial.h>
#define ledPin 7
#define BTpin 12
SoftwareSerial BTserial(2, 3);


int command = 0;
void setup() {

  pinMode(12, INPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0)
    {
    command = Serial.read();
    }
    Serial.println(digitalRead(12));
    delay(1000);
 
          if (command == '0')
          {
          digitalWrite(7, HIGH);
          delay(500);
          digitalWrite(7, LOW);
          Serial.println("Locked");
          command = 0;
         
          }
          else if (command == '1')
          {
          digitalWrite(7, HIGH);
          delay(500);
          digitalWrite(7, LOW);
          Serial.println("Unlocked");
          command = 0;
          }

}


This indeed returns the values 1 and 0 finally.
I am tho confused why just changing addressing the pins directly with the numers of the pins instead of their defined names made a difference in returning the 1 and 0 correctly.
I'm tired right now after 7-8 hours struggling with this,maybe after a good sleep tomorrow i'll manage to get a grip on my thinking and fully understand it.Now i can sleep with my mind at peace for today hehe.
Either way i couldn't have done it without you guys,many many thanks for your time and help.Highly appreciated !!!

PaulS

Quote
I am tho confused why just changing addressing the pins directly with the numers of the pins instead of their defined names made a difference in returning the 1 and 0 correctly.
It didn't. You also change WHEN you read the pin. Reading the pin only when there is serial data to read, as you were doing, means that the device must have been connected.

Anonymous printing sucks. Print something before every value, so you know what the value means.
The art of getting good answers lies in asking good questions.

cattledog

#10
Feb 12, 2019, 12:28 am Last Edit: Feb 12, 2019, 12:29 am by cattledog
Quote
Reading the pin only when there is serial data to read, as you were doing, means that the device must have been connected.
Agreed. But then the OP should have seen 1's.
This is what I got running his sketch 
Code: [Select]
Unlocked
1
Locked
1
Unlocked
1
Locked
1


Quote
My main concern is that the STATE pin returns only '0' 
The fact that he was seeing 0's makes me think he somehow corrected a wiring problem. When he wakes up in the morning, perhaps he'll figure out what was going on. :)


Netratt

Agreed. But then the OP should have seen 1's.
This is what I got running his sketch 
Code: [Select]
Unlocked
1
Locked
1
Unlocked
1
Locked
1


The fact that he was seeing 0's makes me think he somehow corrected a wiring problem. When he wakes up in the morning, perhaps he'll figure out what was going on. :)


It was printing
Code: [Select]
Unlocked
0
Locked
0
Unlocked
0
Locked
0
:)

wiring didn't change at all,i don't even remember what i did yesterday in all that chaos :))
At least i learned a lot so can't complain at all :>

Go Up