Hi!
Sorry if this might be a too easy question, but I don't have so much Arduino experience.
I'm trying to receive a String from my HC-06 Bluetooth module. When I google it I mostly just get about how to make it with one integer, except these which shows how to do it with strings:
But it doesn't make any sense to me. I'm just looking a code snippet which has the code I need.
Can someone explain or just give me a working code snippet that can read serial data, please?
Thanks!
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. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
Imim:
I'm trying to receive a String from my HC-06 Bluetooth module.
Can someone explain or just give me a working code snippet that can read serial data, please?
Are you trying to receive an ASCII coded string like "Hello!" in the Serial Monitor of your Arduino UNO from your Android Smart Phone via Blue Tooth Module?
GolamMostafa:
Are you trying to receive an ASCII coded string like "Hello!" in the Serial Monitor of your Arduino UNO from your Android Smart Phone via Blue Tooth Module?
Figure-1: Connection between NANO and HC06 using software UART Port
2. Upload the following sketch in the UNO/NANO
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);//SRX-pin/STX-pin of UNO
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
byte n = SUART.available(); //checking if a character has arrived from BT
if(n != 0)
{
char x = SUART.read(); //read the character from BT and save
Serial.print(x); //show the character in Serial Monitor of NANO
}
//---read from InputBox of Serial Monitor and then send to BT
byte n1 = Serial.available(); //check if a character has come from Serial Monitor of NANO
if(n1 !=0)
{
char x1 = Serial.read();
SUART.print(x1);
}
}
3. Pair the NANO and Android Phone.
4. Open the Serial Monitor at 9600 Bd.
Figure-2: Details of Serial Monitor
5. Send hello from Android i n ASCII Mode. Check that the string has appeared on the OutputBox of Serial Monitor.
6. Send ok! from the InputBox of Serial Monitor. Check that the message has appeared on the BT Screen of the Android Phone.