-
Notifications
You must be signed in to change notification settings - Fork 1
/
mega_to_hm10.ino
76 lines (63 loc) · 1.44 KB
/
mega_to_hm10.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
USB(mega2560)-to-BLE sketch
Apploader project
http://www.apploader.info
Anton Smirnov
2015
Note:
HM-10 applies baud rate set in 'AT+BAUD' after switch off and on
*/
int led = 13;
int ledHIGH = 0;
long bauds[] = {
// major
9600, 57600, 115200,
// others
19200, 38400, 4800, 2400, 1200, 230400
};
bool detectBleBaudRate() {
Serial.println("Detecting BLE baud rate:");
for (int i=0; i<(sizeof(bauds)/sizeof(long)); i++) {
Serial.write("Checking ");
long cur_baud = bauds[i];
Serial.println(cur_baud, DEC);
Serial1.begin(cur_baud);
Serial1.write("AT");
Serial1.flush();
delay(50);
String response = Serial1.readString();
if (response == "OK") {
Serial.println("Detected");
return true;
} else {
Serial1.end();
}
}
return false;
}
void setup() {
// init
Serial.begin(115200); // USB (choose 115200 in terminal)
if (detectBleBaudRate())
Serial.write("Ready, type AT commands\n\n");
else
Serial.write("Not ready. Halt");
pinMode(led, OUTPUT);
}
void loop() {
// read from BLE (HM-10)
if (Serial1.available()) {
Serial.write("ble: ");
String str = Serial1.readString();
Serial.print(str);
Serial.write('\n');
}
// read from USB (Arduino Terminal)
if (Serial.available()) {
Serial.write("usb: ");
String str = Serial.readString();
Serial1.print(str);
Serial.print(str);
Serial.write('\n');
}
}