|
@@ -63,15 +63,16 @@ unsigned long HC05::findBaud()
|
63
|
63
|
DEBUG_WRITE("\r\nNo connection\r\n");
|
64
|
64
|
return(0);
|
65
|
65
|
}
|
66
|
|
-int HC05::cmd(String cmd, unsigned long timeout)
|
|
66
|
+bool HC05::cmd(String cmd, unsigned long timeout)
|
67
|
67
|
{
|
68
|
|
- char raw[cmd.length() + 1];
|
69
|
|
- cmd.toCharArray(raw, sizeof(raw));
|
70
|
|
- return this->cmd(raw, timeout);
|
|
68
|
+ return this->cmd(cmd.c_str(), timeout);
|
71
|
69
|
}
|
72
|
70
|
|
73
|
|
-int HC05::cmd(const char* cmd, unsigned long timeout)
|
|
71
|
+bool HC05::cmd(const char* cmd, unsigned long timeout)
|
74
|
72
|
{
|
|
73
|
+ _lastError = -1;
|
|
74
|
+ _stringBuffer = "";
|
|
75
|
+ _stringData = "";
|
75
|
76
|
int recvd = 0;
|
76
|
77
|
DEBUG_PRINTLN(cmd);
|
77
|
78
|
|
|
@@ -94,11 +95,14 @@ int HC05::cmd(const char* cmd, unsigned long timeout)
|
94
|
95
|
// of a multiline response before the OK is received.
|
95
|
96
|
// The return would incorrectly indicate an error (no
|
96
|
97
|
// OK response).
|
97
|
|
- recvd = _btSerial.readBytesUntil('\n',_buffer,_bufsize);
|
|
98
|
+ recvd = _btSerial.readBytesUntil('\n', _buffer, _bufsize);
|
98
|
99
|
if (recvd > 0)
|
99
|
100
|
{
|
100
|
|
- DEBUG_WRITE((uint8_t *)_buffer,recvd);
|
101
|
|
- DEBUG_WRITE('\n');
|
|
101
|
+ _buffer[recvd] = 0;
|
|
102
|
+ _stringBuffer += _buffer;
|
|
103
|
+ _stringBuffer += "\n";
|
|
104
|
+// DEBUG_WRITE((uint8_t *)_buffer,recvd);
|
|
105
|
+// DEBUG_WRITE('\n');
|
102
|
106
|
}
|
103
|
107
|
else
|
104
|
108
|
{
|
|
@@ -113,7 +117,22 @@ int HC05::cmd(const char* cmd, unsigned long timeout)
|
113
|
117
|
// command mode. The appeared to be a baud rate dependency and with
|
114
|
118
|
// >100ms required at 9600 baud.
|
115
|
119
|
delay(150);
|
116
|
|
- return((_buffer[0] == 'O' && _buffer[1] == 'K'));
|
|
120
|
+ DEBUG_WRITE(_stringBuffer.c_str());
|
|
121
|
+
|
|
122
|
+ if (_buffer[0] == 'O' && _buffer[1] == 'K') {
|
|
123
|
+ return true;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ if (_stringBuffer.startsWith("ERROR:(")) {
|
|
127
|
+ String errorCode = _stringBuffer.substring(7, _stringBuffer.length() - 3);
|
|
128
|
+ _lastError = errorCode.toInt();
|
|
129
|
+ DEBUG_WRITE("Error: ");
|
|
130
|
+ DEBUG_PRINTLN(_lastError);
|
|
131
|
+ } else {
|
|
132
|
+ _lastError = -1;
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ return false;
|
117
|
136
|
}
|
118
|
137
|
|
119
|
138
|
|
|
@@ -258,3 +277,80 @@ void HC05::setCmdPin(bool state)
|
258
|
277
|
digitalWrite(_cmdPin, state);
|
259
|
278
|
}
|
260
|
279
|
}
|
|
280
|
+
|
|
281
|
+bool HC05::atGetCommand(String command, unsigned long timeout)
|
|
282
|
+{
|
|
283
|
+ return this->atGetCommand1(command, "", timeout);
|
|
284
|
+}
|
|
285
|
+
|
|
286
|
+bool HC05::atGetCommand1(String command, String arg1, unsigned long timeout)
|
|
287
|
+{
|
|
288
|
+ if (this->cmd("AT+" + command + "?" + arg1, timeout)) {
|
|
289
|
+ if (_stringBuffer.startsWith("+" + command + ":")) {
|
|
290
|
+ _stringData = _stringBuffer.substring(command.length() + 2, _stringBuffer.length() - 6);
|
|
291
|
+ DEBUG_PRINTLN(_stringData);
|
|
292
|
+ }
|
|
293
|
+ return true;
|
|
294
|
+ }
|
|
295
|
+ return false;
|
|
296
|
+}
|
|
297
|
+
|
|
298
|
+bool HC05::atTest(unsigned long timeout)
|
|
299
|
+{
|
|
300
|
+ return this->cmd("AT", timeout);
|
|
301
|
+}
|
|
302
|
+
|
|
303
|
+bool HC05::atReset(unsigned long timeout)
|
|
304
|
+{
|
|
305
|
+ return this->cmd("AT+RESET", timeout);
|
|
306
|
+}
|
|
307
|
+
|
|
308
|
+bool HC05::atVersion(unsigned long timeout)
|
|
309
|
+{
|
|
310
|
+ return this->atGetCommand("VERSION", timeout);
|
|
311
|
+}
|
|
312
|
+
|
|
313
|
+bool HC05::atRestore(unsigned long timeout)
|
|
314
|
+{
|
|
315
|
+ return this->cmd("AT+ORGL", timeout);
|
|
316
|
+}
|
|
317
|
+
|
|
318
|
+bool HC05::atGetAddress(unsigned long timeout)
|
|
319
|
+{
|
|
320
|
+ return this->atGetCommand("ADDR", timeout);
|
|
321
|
+}
|
|
322
|
+
|
|
323
|
+bool HC05::atGetName(unsigned long timeout)
|
|
324
|
+{
|
|
325
|
+ return this->atGetCommand("NAME", timeout);
|
|
326
|
+}
|
|
327
|
+
|
|
328
|
+bool HC05::atSetName(String name, unsigned long timeout)
|
|
329
|
+{
|
|
330
|
+ return this->cmd("AT+NAME=" + name, timeout);
|
|
331
|
+}
|
|
332
|
+
|
|
333
|
+bool HC05::atGetRemoteName(String address, unsigned long timeout)
|
|
334
|
+{
|
|
335
|
+ return this->atGetCommand1("RNAME", address, timeout);
|
|
336
|
+}
|
|
337
|
+
|
|
338
|
+bool HC05::atGetRole(unsigned long timeout)
|
|
339
|
+{
|
|
340
|
+ return this->atGetCommand("ROLE", timeout);
|
|
341
|
+}
|
|
342
|
+
|
|
343
|
+bool HC05::atSetRole(Roles role, unsigned long timeout)
|
|
344
|
+{
|
|
345
|
+ return this->cmd("AT+ROLE=" + role, timeout);
|
|
346
|
+}
|
|
347
|
+
|
|
348
|
+bool HC05::atGetPassKey(unsigned long timeout)
|
|
349
|
+{
|
|
350
|
+ return this->atGetCommand("PSWD", timeout);
|
|
351
|
+}
|
|
352
|
+
|
|
353
|
+bool HC05::atSetPassKey(String passKey, unsigned long timeout)
|
|
354
|
+{
|
|
355
|
+ return this->cmd("AT+PSWD=" + passKey, timeout);
|
|
356
|
+}
|