I am trying to communicate between HM-10 module (without any breadboard or soldering) and experience problem with reading data from serial.
Here is the code:
#define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i)
void afterSetup() {
Serial3.write("AT+NAME?");
delay(500);
}
void setup() {
// initialize both serial ports:
Serial.begin(9600);
/*Serial3.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(100); // Short delay, wait for the Mate to send back CMD
Serial3.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*/
Serial3.begin(9600); // Start bluetooth serial at 9600
Serial.write("\n\n\n==== START ====\n");
afterSetup();
}
unsigned char mode = 2;
void wr(HardwareSerial*, int);
void wr(HardwareSerial *target, int bt, unsigned char mode) {
//target->write(TO_HEX(((bt & 0xF000) >> 12 )));
//target->write(TO_HEX(((bt & 0x0F00) >> 8 )));
if(mode > 0) {
target->write("\t\t\t\t");
}
target->write(TO_HEX(((bt & 0x00F0) >> 4 )));
target->write(TO_HEX((bt & 0x000F)));
target->write(" (");
target->write(bt);
target->write(")\n");
return;
}
void loop() {
// read from port 1, send to port 0:
if (Serial3.available()) {
int inByte = Serial3.read();
if(mode != 1) {
mode = 1;
}
wr(&Serial, inByte, 1);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial3.print((char)inByte);
if(mode != 0) {
mode = 0;
}
wr(&Serial, inByte, 0);
}
}
Wiring is simple:
HM-10 | due
-------------------
RX | TX3
TX | RX3
VCC | 3V3
GND | GND
Basically, I am able to get data from serial, but it corrupted.
Here is an example:
- Command: AT+NAME?
- Expected: HMSoft
Response:
28 (() 25 (%) 2A (*) 35 (5) 15 () FE (�)
And another, simpler:
- Command: AT
- Expected: OK
- Response: F5
What am I doing wrong?
Serial3
before callingbegin()
on it?