Browse Source

added basic at commands

develop
Robin Thoni 7 years ago
parent
commit
5ab7bfcb09
2 changed files with 134 additions and 11 deletions
  1. 105
    9
      HC05.cpp
  2. 29
    2
      HC05.h

+ 105
- 9
HC05.cpp View File

63
     DEBUG_WRITE("\r\nNo connection\r\n");
63
     DEBUG_WRITE("\r\nNo connection\r\n");
64
     return(0);
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
     int recvd = 0;
76
     int recvd = 0;
76
     DEBUG_PRINTLN(cmd);
77
     DEBUG_PRINTLN(cmd);
77
 
78
 
94
         //            of a multiline response before the OK is received.
95
         //            of a multiline response before the OK is received.
95
         //            The return would incorrectly indicate an error (no
96
         //            The return would incorrectly indicate an error (no
96
         //            OK response).
97
         //            OK response).
97
-        recvd = _btSerial.readBytesUntil('\n',_buffer,_bufsize);
98
+        recvd = _btSerial.readBytesUntil('\n', _buffer, _bufsize);
98
         if (recvd > 0)
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
         else
107
         else
104
         {
108
         {
113
     // command mode. The appeared to be a baud rate dependency and with
117
     // command mode. The appeared to be a baud rate dependency and with
114
     // >100ms required at 9600 baud.
118
     // >100ms required at 9600 baud.
115
     delay(150);
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
         digitalWrite(_cmdPin, state);
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
+}

+ 29
- 2
HC05.h View File

71
 class HC05 : public Stream
71
 class HC05 : public Stream
72
 {
72
 {
73
 public:
73
 public:
74
+    enum Roles {
75
+        Slave = 0,
76
+        Master = 1,
77
+        SlaveLoop = 2
78
+    };
79
+
74
     HC05(int cmdPin, int statePin);
80
     HC05(int cmdPin, int statePin);
75
     HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
81
     HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
76
     unsigned long findBaud();
82
     unsigned long findBaud();
79
 
85
 
80
     // cmd(): 100ms default timeout covers simple commands, but commands
86
     // cmd(): 100ms default timeout covers simple commands, but commands
81
     // that manage the connection are likely to take much longer.
87
     // that manage the connection are likely to take much longer.
82
-    int cmd(String cmd, unsigned long timeout=100);
83
-    int cmd(const char* cmd, unsigned long timeout=100);
88
+    bool cmd(String cmd, unsigned long timeout=100);
89
+    bool cmd(const char* cmd, unsigned long timeout=100);
84
 
90
 
85
     // HC05 cmdMode2 forces 38400, no parity, one stop bit
91
     // HC05 cmdMode2 forces 38400, no parity, one stop bit
86
     // Entering cmdMode2 uses a pin to turn the HC05 power on and off
92
     // Entering cmdMode2 uses a pin to turn the HC05 power on and off
112
     int _bufsize;
118
     int _bufsize;
113
     char _buffer[32];
119
     char _buffer[32];
114
     void setCmdPin(bool state);
120
     void setCmdPin(bool state);
121
+
122
+    String _stringBuffer;
123
+    String _stringData;
124
+    int _lastError;
125
+
126
+public:
127
+    bool atGetCommand(String command, unsigned long timeout=100);
128
+    bool atGetCommand1(String command, String arg1, unsigned long timeout=100);
129
+
130
+    bool atTest(unsigned long timeout=100);
131
+    bool atReset(unsigned long timeout=100);
132
+    bool atVersion(unsigned long timeout=100);
133
+    bool atRestore(unsigned long timeout=100);
134
+    bool atGetAddress(unsigned long timeout=100);
135
+    bool atGetName(unsigned long timeout=100);
136
+    bool atSetName(String name, unsigned long timeout=100);
137
+    bool atGetRemoteName(String address, unsigned long timeout=100);
138
+    bool atGetRole(unsigned long timeout=100);
139
+    bool atSetRole(Roles role, unsigned long timeout=100);
140
+    bool atGetPassKey(unsigned long timeout=100);
141
+    bool atSetPassKey(String passKey, unsigned long timeout=100);
115
 };
142
 };
116
 
143
 
117
 extern HC05 btSerial;
144
 extern HC05 btSerial;

Loading…
Cancel
Save