Hello! I am having a lot of trouble trying to find a solution for this problem, if anybody knows what to do, please help.
I am trying to create an app that can send a phone number to arduino and so make arduino turn this received text into a variable that will be used in another moment in the code. I don`t know, however, how to make arduino receive and turn this text into a variable.
The code I have tried in Arduino is the following:
#include <SoftwareSerial.h>
// Defina os pinos RX e TX para o módulo Bluetooth
SoftwareSerial bluetoothSerial(2, 3); // RX, TX
String PhoneNumber; // Alterado para o tipo de dados String para armazenar um número de telefone
void setup() {
// Inicialize a comunicação serial com o módulo Bluetooth
Serial.begin(9600);
bluetoothSerial.begin(9600);
Serial.println("Configurando o módulo Bluetooth...");
delay(1000);
// Configurar o módulo para modo mestre
bluetoothSerial.println("AT+ROLE=0");
delay(500);
// Configurar o módulo para nome e código de acesso
bluetoothSerial.println("AT+NAME=MeuBluetooth"); // Nome desejado
delay(500);
bluetoothSerial.println("AT+PSWD=1234"); // Código de acesso desejado
delay(500);
Serial.println("Módulo Bluetooth configurado com sucesso!");
}
void loop() **THIS PART IS NOT WORKING AS IT SHOULD, PLEASE HELP**
// Check if there are available data for reading
if (bluetoothSerial.available() > 0) {
// Read the sequence of characters received from Bluetooth
String PhoneNumber = bluetoothSerial.readString();
Serial.println(PhoneNumber);
// Was supposed to display received data on Serial Monitor
Serial.println("Cellphone number received: " + PhoneNumber);
}
else {
Serial.println (-1);
}
}
I have tried multiple options in MIT App Inventor and none of them seem to work, I have tried building my own code and I was capable of connecting to the Bluetooth Module (HC-06) but haven't been able to make arduino "understand" the text that is being sent.
Hi,
in addition to what @ABG has already suggested (please do what he said), which Arduino board are you using ? Please be aware that the Mega board maps one of its 4 HW serial lines on pins 2,3 therefore you'd better map the SoftwareSerial pins on 10,11.
Second: why you need to configure the HC06 ? the HC06 is always a "slave" (only the HC05 can be configured as a master) therefore you don't need to set the code : bluetoothSerial.println("AT+ROLE=0");
Moreover if you set the pincode as "1234" this means that the pairing between AI2 and the HC06 shall succeed only after the pincode has been successfully exchanged. And I don't see this behaviour in your .aia.
Make things easier.
Or, since you want to trasmit a phone number, transmit it with a "termnator character", like a '$' for example. So in your Arduino code you can receive all characters until the '$' is received, then you can convert the received string in a number, or you can leave it as a string, depending on your needs.
The following Arduino code is a trivial example of what you can do, just to test the communication.
//=========================================================================
void setup() {
Serial.begin(9600);
BTSerial.begin(9600); // be sure that your HC06 baudrate is effectively set @9600
// in some cases the factory set baudrate is 38400
timeout = 0;
}
//========================================================================
void loop() {
if(SerialBT.available()) // any character on BT ?
while(SerialBT.available()) // reads characters until any
Serial.print(BTSerial.read()); // displays on serial monitor anything received
else // no (more) characters
{
delay(1);
if(timeout++ >= 1000) // if no characters for 1 second, prints a message to serial monitor
{Serial.println("Waiting BT"); timeout = 0; }
}
}
I suppose that your HW connection between Arduino CPU board and the HC06 shield is correct: in other words like this: Tx pin of Arduino to Rx pin of HC06 (and Rx pin of Arduino to Tx pin of HC06) .
Like the following:
Don't care on the Arduino Nano board: the same applies to any Arduino CPU board, just remember that if you use the SoftwareSerial library it's better to map the pins on 10,11.
Best wishes.
For what I've seen, I need to configure the HC06 in order to be able to pair with it in the first time, in order for it to be "visible" to other devices (Such as my cellphone);
Regarding the pincode "1234", this is used only in the first time that you pair with the bluetooth module (in the cellphone configuration). If you've paired with it at least once then you won't need to put the pincode afterwards when trying to pair with it again through the MIT App Inventor. Thats why there is no such part for a pincode in my .aia. (I've also seen that even if I don't include the password part, the first time you try to pair with the module it will always ask for a pincode, that will probably be 0000 or 1234, at least in the modules I've tested);
I'll take a look at the "terminator character" you mentioned and the code as well. I'll bring you an answer if it works.
Dear Jonas,
honestly I never had the need to set a pincode on the Arduino BT shields but, anyway, things are evolving.... To this purpose I attach a couple of information that gave me a big help a long time ago, when I needed to interface some HC06 that weren't working until I programmed them accordingly.
A UNO board is fine with pins 2,3 (remember that 0,1 are dedicated to the HW serial line. i.e. the USB interface toward the PC).
Another hint is to try with a BT terminal emulator on your phone so to try to communicate with your Arduino board without the uncertainty of your AI2 app. (on Google playstore).