Utilizzo arduino MEGA e un modulo Bluetooth.
per configurare il modulo ho utilizzato questo programma:
#define ROBOT_NAME "RobotVirgilio"
// If you haven't configured your device before use this
#define BLUETOOTH_SPEED 9600
// If you are modifying your existing configuration, use this:
// #define BLUETOOTH_SPEED 57600
/*
The posible baudrates are:
AT+BAUD1-------1200
AT+BAUD2-------2400
AT+BAUD3-------4800
AT+BAUD4-------9600 - Default for hc-06
AT+BAUD5------19200
AT+BAUD6------38400
AT+BAUD7------57600 - Johnny-five speed
AT+BAUD8-----115200
AT+BAUD9-----230400
AT+BAUDA-----460800
AT+BAUDB-----921600
AT+BAUDC----1382400
*/
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting config");
Serial3.begin(BLUETOOTH_SPEED);
delay(1000);
// Should respond with OK
Serial3.print("AT");
waitForResponse();
// Should respond with its version
Serial3.print("AT+VERSION");
waitForResponse();
// Set pin to 0000
Serial3.print("AT+PIN0000");
waitForResponse();
// Set the name to ROBOT_NAME
Serial3.print("AT+NAME");
Serial3.print(ROBOT_NAME);
waitForResponse();
// Set baudrate to 57600
Serial3.print("AT+BAUD4");
waitForResponse();
Serial.println("Done!");
}
void waitForResponse() {
delay(1000);
while (Serial3.available()) {
Serial.write(Serial3.read());
}
Serial.write("\n");
delay(10);
}
void loop() {}
La Configurazione sembra effettuata correttamente, infatti dal table android con installato il software BlueTerm, vedo il dispositivo e dopo aver digitato il codice "0000" si connette (il led sul modulo che lampeggia diventa fisso).
per provare la comunicazione ho caricato questo programma:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial3.begin(9600);
Serial.println("------------SETUP--------------");
}
void loop() {
// put your main code here, to run repeatedly:
Serial3.println("-----> Ciclo loop <--------------");
delay(100);
ReadDati();
Serial.println("----------------------------------");
}
void ReadDati(){
String str = "";
while (Serial3.available()>0) {
str += (char)Serial3.read();
}
Serial.println(str);
}
Riesco a scrive nel terminale del Tablet (Le stringhe inviate vengono correttamente visualizzate) mentre non riesco a ricevere i caratteri dal tablet.
Infatti, nella funzione ReadDati quando accedo alla Serial3.read() ottengo dei caratteri casuali.
Mi sapete dare qualche aiuto ???