Parcourir la source

added basic at commands

develop
Robin Thoni il y a 8 ans
Parent
révision
5ab7bfcb09
2 fichiers modifiés avec 134 ajouts et 11 suppressions
  1. 105
    9
      HC05.cpp
  2. 29
    2
      HC05.h

+ 105
- 9
HC05.cpp Voir le fichier

@@ -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
+}

+ 29
- 2
HC05.h Voir le fichier

@@ -71,6 +71,12 @@
71 71
 class HC05 : public Stream
72 72
 {
73 73
 public:
74
+    enum Roles {
75
+        Slave = 0,
76
+        Master = 1,
77
+        SlaveLoop = 2
78
+    };
79
+
74 80
     HC05(int cmdPin, int statePin);
75 81
     HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
76 82
     unsigned long findBaud();
@@ -79,8 +85,8 @@ public:
79 85
 
80 86
     // cmd(): 100ms default timeout covers simple commands, but commands
81 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 91
     // HC05 cmdMode2 forces 38400, no parity, one stop bit
86 92
     // Entering cmdMode2 uses a pin to turn the HC05 power on and off
@@ -112,6 +118,27 @@ private:
112 118
     int _bufsize;
113 119
     char _buffer[32];
114 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 144
 extern HC05 btSerial;

Chargement…
Annuler
Enregistrer