Go Down

Topic: Arduino + HC-05 + App Inventor (Read 588 times) previous topic - next topic

TorbenO

Mar 01, 2019, 05:33 pm Last Edit: Mar 01, 2019, 05:36 pm by TorbenO
Hello guys,

I have some problems with my Android phone and HC-05. I am working on a arduino project and theirefor I need to connect my phone to my arduino (I am using an Arduino Uno). I found multiple tutorials on the web where they make a simple ON/OFF led controll with arduino using a HC-05 and App Inventor. Since it's exactly what I need, I tried it out.

I made the app with App Inventor. I wrote a program for the arduino. When I run the app, I can connect to my bluetooth HC-05 and I can press the button, but my led doesn't go OFF or ON. My led is even burning constantly.

What am I doing wrong? Hope you'll help me, Thank you.

Here is the arduino code:
Code: [Select]
int data;
int led = 13;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);  
}

void loop() {
  if(Serial.available()>0);{
    data = Serial.read();
    if(data = '1');{
      digitalWrite(led, HIGH);
    }
    if(data = '0');{
      digitalWrite(led, LOW);
    }
  }
}

cattledog

#1
Mar 01, 2019, 07:25 pm Last Edit: Mar 01, 2019, 07:27 pm by cattledog
Code: [Select]
if(data = '1');
if(data = '0');


Double error.  

== not = for comparison

No semicolon after conditional statement

Code: [Select]
if(data == '1')
 if(data == '0')



Loose this semicolon as well
Code: [Select]
if(Serial.available()>0);

Nick_Pyner

#2
Mar 02, 2019, 02:23 am Last Edit: Mar 02, 2019, 03:19 pm by Nick_Pyner
When I run the app, I can connect to my bluetooth HC-05
This merely means that you have done what you have said you have done, and it guarantees nothing as far as Arduino is concerned - and not your app inventor stuff either.. Maybe the real intention of your project is the app inventor stuff but, either way, you don't actually need it, and you would be better off using an ordinary bluetooth terminal to start with, thereby removing a potential problem from the game.

If, after going through reply#1, you still have no joy, you might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Note that getting Bluetooth going is a much a matter of procedure as programming.


TorbenO

#3
Mar 02, 2019, 09:21 am Last Edit: Mar 02, 2019, 10:30 am by TorbenO
== not = for comparison
No semicolon after conditional statement
Loose this semicolon as well
Thank you for responding, I changed all you said, but when I try with the app, the led stays off.

Maybe the real intention of your project is theapp inventor stuff but, either way, you don't actually need it, and you would be better off using an ordinary bluetooth terminal to start with, thereby removing a potential problem from the game.
http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino
I see what you mean, so I installed bluetooth terminal on my android phone. I took the program from the second link. When I connect with the bluetooth terminal to my HC-05, I can everything I write in the serial monitor on my computer see in the app. As far as I understand, I also need to be able to see the text from the serial monitor on the bluetooth terminal app. (in this case is it: OK then, you first, say something.....). But I don't see any text, only the text I type in the serial monitor.
When I type 1 or 0 in the serial monitor, the led goes on and off so then it works, but when I typ 1 or 0 in the bluetooth terminal, nothing happens.

Grumpy_Mike

Quote
but when I typ 1 or 0 in the bluetooth terminal, nothing happens.
Your code only looks at the Serial input. How have you wired up your Bluetooth adaptor?
The normal way is to use software serial, so you need to use that in the Arduino's code.

Nick_Pyner

#5
Mar 02, 2019, 03:35 pm Last Edit: Mar 02, 2019, 03:41 pm by Nick_Pyner
I took the program from the second link. When I connect with the bluetooth terminal to my HC-05, I can everything I write in the serial monitor on my computer see in the app. As far as I understand, I also need to be able to see the text from the serial monitor on the bluetooth terminal app. (in this case is it: OK then, you first, say something.....). But I don't see any text, only the text I type in the serial monitor.
When I type 1 or 0 in the serial monitor, the led goes on and off so then it works, but when I typ 1 or 0 in the bluetooth terminal, nothing happens.
I think you might be mixing code - or trying to think about it. IF you use the code in the second link, the objective is only to connect Arduino <> Android, and send text two-way. No amount of typing 0 or 1 will flash any LEDs. The serial monitor is just a passenger and need not be connected at all. You should see "OK, you first..." on Android and you say something there, using the standard terminal. If you don't see anything on Android, you have a problem, probably Bluetooth wired wrong way round.   

Read the notes, and look at the pictures.

Note particularly that you can run the programme first using the serial monitor, ensure that all is kosher, then connect bluetooth with no change to the code.  If no joy, you know where the problem isn't, and it isn't too hard to find where it is.


TorbenO

I think you might be mixing code - or trying to think about it.
I indeed mixed the test code and the actual code for my led.
If you don't see anything on Android, you have a problem, probably Bluetooth wired wrong way round.  
How have you wired up your Bluetooth adaptor?
I didn't use sofware serial since I didn't think it was necessary, so I used the standard serial connection: RX from the HC-05 to RX (port 0) form the arduino, idem for the TX (port 1). But since that didn't work I started to doubt on my wiring like you said. So I tried to change TX with RX, now is RX connected to port 1 en TX connected to port 2. How strange it sounds, when I connect the HC-05 like that I now see the text "OK, you first.." on my Android.
I tried also the led program and even that works. I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?


 ;D Already thank you very much to everyone for helping me out. I can now start on my actual project. When I need more help I'll not doubt to ask it. :D

cattledog

Quote
tried also the led program and even that works. I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?
You have discovered that they need to be cross connected Rx>Tx and Tx>Rx.

The Rx/Tx designators relate to the device they connect to. Think about it and you'll see that output Tx from the module has to connect to input Rx of the Arduino and vica versa..

Bringamosa

R stands for receiver T for transmitter.
So you need Rx connected to Tx so you have the Recever "listening" to the Transmitter, and vica versa Transmitter "talking" to the Receiver

Grumpy_Mike

Quote
I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?
Because you should always connect an input to an output, so connecting RX to RX on the face of it is the stupid thing to do. But by an odd quirk this is sometimes what you have to do.

This means that the RX pin can be an input or an output depending on how the piece of equipment is classed. The same goes for the TX pin.

Equipment can be classed as either DTE (Data Terminal Equipment) or CTE (Computer Terminal Equipment)
https://en.wikipedia.org/wiki/Data_terminal_equipment

Nick_Pyner

#10
Mar 03, 2019, 12:10 am Last Edit: Mar 03, 2019, 02:26 pm by Nick_Pyner
I didn't use sofware serial since I didn't think it was necessary, so I used the standard serial connection:
That's fine, it isn't. Indeed, it can be a liability. Just remember that the pins are indeed clearly named, and transmitters (Tx) transmit to receivers, while receivers (Rx) receive from transmitters.


TorbenO

It actualy makes sence now. Receiver to transmitter and transmitter to recveier. I didn't think about it like that.

Thanks a lot for explaining guys. Now I know what I did wrong, so I definitly won't do it wrong the next time!

Thanks again!

Nick_Pyner

While you are at it, you might note that a 1k/2k voltage divider in the Arduino Tx line is a good idea.

Grumpy_Mike

Quote
Now I know what I did wrong, so I definitly won't do it wrong the next time!
Remember in reply #9 you might have to do things wrong. It is always worth a check with an LED to see what any particular piece of equipment has as its output. I always do this with a new piece of equipment with an oscilloscope.

So when connecting a CTE to a CTE or connecting a DTE to a DTE then you need to do it "wrong", that is RX to RX and TX to TX.

Go Up