Hi, after couple of days reading instructions and visiting forums I am still unable to properly get information from PC/Android to Arduino Mega using a HC-06 Bluetooth module.
Wiring is pretty much what one finds all over the internet.
BT Rx connected at the junction of two resistors, a 2k2 going to ground and a 1k5 going to Arduino Mega pin14 (TX3) .
BT Tx connected straight to Arduino Mega pin15 (RX3).
A LED is connected from Arduino pin 10 to ground via a 1K resistor.
The code is as follows:
String readString;
char c;
int LedPin =10;
int flag=0;
void setup() {
Serial.begin(9600);
Serial.println("OK then, you first, say something.....");
Serial.println("Go on, type something and hit Send,");
Serial.println("or just hit the Enter key,");
Serial.println("then I will repeat it!");
Serial.println("");
pinMode(LedPin, OUTPUT);
Serial3.begin(9600);
}
void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++) {
Serial3.write(stringData*); // Push each char 1 by 1 on each loop pass*
// Nothing is printed at the android terminal software*
}* } void loop() { while (Serial3.available()) {
delay(3);*
c = Serial3.read();*
readString += c;* }
if (readString.length() >0) {
if (flag==0){ *
digitalWrite(LedPin, HIGH); //works fine*
flag = 1;*
} else {*
digitalWrite(LedPin, LOW); //works fine*
flag = 0;*
}*
writeString(readString);*
Serial.println(readString); //nothing (sometimes garbage) on monitor*
Serial3.write("Response OK"); // nothing shows up at the android device*
readString="";* } } 1- Sending something from Android, anything, will turn led on and off 2- If I try to detect what was sent from Android, it does not work, nothing is printable, I just cannot see what is in readString. 3- Except from the opening text at the setup() nothing but a few garbage is sent to the monitor. 4- nothing arrives at the Android device. 5- Tried external power supply with no results. 6- Pairing works with no problems. Any assistance will be welcome. Thanks Paulo
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Dear Robin2, thanks for the Serial Input Basics, I had a quick read and it seems it adresseses many of my problems.
How about serial data from the Arduino to the Android device, I have no control on that end, how to format the data? Should I use Serial.write or Serial.print?
pcborges:
I also testes several comm speeds for Serial3.begin(...) and with 38400 it behaves like below (In the case below I typed: A B C (one at a time)):
This just in ...
This just in ... A
This just in ...
This just in ... B
This just in ...
This just in ... C
What do you think?
Paulo
That looks correct. My guess is that the blank lines are linefeed characters (try the trick with (byte) and see what happens).
I'm not sure if you are looking for more advice. If so please post the complete program you have been trying and explain what you need help with.
I think you are just working with my first example which receives single characters. You will probably find that my second example that expects an end-marker (in the example it is a linefeed) will be more reliable if the device sending data is sending a linefeed character or some other terminator.
Hi Robin, excuse me for not been clear enough.
Yes, I am still using your first exemple with some minor modifications and still looking for advice.
But you are right, Yesterday I was Reading your Serial Input Basics again and saw your additional exemples to send multiple characters and I am experimenting with them today.
In my Project I need to send data to the Aduino and get some other information back, so I need to be able to handle data flowing on both directions.
I am not new to programing but new to Arduino and C.
Still getting used to C's particularities on treating with strings.
I will try your examples and adapt them to my requirements:
1- need to send data that I can identify as text and values from Android to Arduino.
2- need to send formated data back (text and numbers)
I am using your 3rd example as the base.
That is the best so far to send data from Android to Arduino but I am having problems sending data back to Android.
I am not writing an Android app, just using one of the many available at the internet, just an Androis BT terminal, so I can just type and send for instance with easy, Works fine.
It is working now.
I had to replace Serial3.write by Serial3.println but also use another BT terminal app.
I can now have reliable BT communication and connection for na extended period of time.