I use Arduino UNO+ RN-42 BT module
I found that open serial monitor on IDE, my phone can connect to the arduino+BT and send/receive message between phone and arduino.
If I didn't open serial monitor, I often can't connect to BT from my phone, even if phone connected successfully, arduino can't receive message from my phone. The success rate is very low when I didn't open serial monitor.
So, anyone know that? what's the point I ignore?
Here is my code
#include <SoftwareSerial.h>
#define RLED 8
#define GLED 9
int bluetoothTx = 10; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 11; // RX-I pin of bluetooth mate, Arduino D3
char incomingByte;
char incomingByte2;
int BTN1 = 2;
int btn_val1 = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
//Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
pinMode(BTN1, INPUT);
digitalWrite(GLED, HIGH);
}
void loop()
{
btn_val1 = digitalRead(BTN1);
if(btn_val1 == HIGH)
{
bluetooth.println("111122223333");
digitalWrite(RLED, LOW);
}
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
incomingByte2 = bluetooth.read();
if(incomingByte2 == '0') {
digitalWrite(GLED, LOW); // if 1, switch LED Off
bluetooth.println("LED OFF. Press 1 to LED ON!");
}
if(incomingByte2 == '1') {
digitalWrite(GLED, HIGH); // if 0, switch LED on
bluetooth.println("LED ON. Press 0 to LED OFF!");
}
if(incomingByte2 == '3') {
digitalWrite(RLED, HIGH);
bluetooth.println("Stop music");
}
}
}