Hello All,
I use Nano v3 (At328p / pb) and a JDY-31 for Bluetooth serial communication, attached to hardware serial (RX0 TX1, see drawing). The JDY-31, when nothing is connected via Bluetooth, is in command mode where it accepts AT commands; if there is an active BT connection, it acts as a serial port.
My plan is to set up the BT parameters (name, baudrate) by the Nano via sending AT commands to JDY-31. In theory, it should work as it is in command mode.
- I started with basic testing. I connected JDY-31 to software serial (RX=A0, TX=D8), then connected the Nano to my PC via the USB; then I used the below sketch that simply copies the terminal into softwareserial and vice versa, it works like a charm:
#include <SoftwareSerial.h>
SoftwareSerial btSerial(A0, 8); // RX, TX
char b;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
btSerial.begin(9600);
Serial.println("Ready.");
}
void loop() {
if(btSerial.available()) {
b = btSerial.read();
Serial.print(b);
}
if(Serial.available()) {
b = Serial.read();
btSerial.print(b);
Serial.print(b);
}
delay(5);
}
-
I connected JDY-31 to hwserial (TXD to RX0, RXD to TX1) , and the AT commands did not work anymore, there is no reply to them. Note that in this setup there is nothing connected to the USB serial - I understand the consequences so I power the board from VIN + GND and let the USB port intact.
-
With the same hardware setup, If I connect to the Bluetooth module with my BT client, the communication is flawless, the Nano gets everything via SPP and it can also sends data without a problem and my BT client receives it well. So it shouldn't be a wiring problem.
Here is the sketch that does not work, the Serial.available() never true:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
btWrite("AT+VERSION\r\n");
delay(1000);
if(Serial.available()){
while(Serial.available()){Serial.read();} // flush
btWrite("AT+BAUD7\r\n"); // 57600
}else {
while(1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void btWrite(const char *msg){
const char *p = msg;
while(*p){
Serial.write(*p++);
delay(20);
}
}
There is no baud rate difference; since the JDY-31 board does not get the AT command, it stays at 9600 bps baud rate.
The question is that, why is this difference? If I send the same commands via RX0-TX1, the Bluetooth module does not send a reply to AT commands, if I use it via Swserial, it sends the replies for AT commands. I need to set up the JDY-31 via AT commands, using the hw serial setup.
Thank you!