Browse Source

init

develop
Robin THONI 3 years ago
commit
3304009137

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+# Files git should ignore
2
+*.swp
3
+.idea

+ 104
- 0
Examples/changeName/changeName.ino View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * Change the BT device name
3
+ *
4
+ * This works with a Bluetooth terminal. The user is prompted for a new
5
+ * name which is then written to HC-05.
6
+ *
7
+ */
8
+#include <Arduino.h>
9
+#include "HC05.h"
10
+
11
+/*
12
+ * Configure this sketch to work with either a software or a hardware
13
+ * serial port, as configured in HC05.h
14
+ */
15
+#ifdef HC05_SOFTWARE_SERIAL
16
+#include <SoftwareSerial.h>
17
+HC05 btSerial = HC05(A2, A5, A3, A4);  // cmd, state, rx, tx
18
+#else
19
+HC05 btSerial = HC05(3, 2);  // cmd, state
20
+#endif
21
+
22
+/*
23
+ * See the ITeadStudio HC-05 datasheet for a full list of commands.
24
+ */
25
+String NewNameCmd("AT+NAME=");
26
+
27
+
28
+void setup()
29
+{
30
+  btSerial.findBaud();
31
+}
32
+
33
+
34
+void loop(){
35
+  char buffer[32];
36
+  size_t recvd = 0;
37
+  bool waiting = true;
38
+  String newName;
39
+
40
+  btSerial.println("");
41
+  btSerial.print("New name? ");
42
+
43
+  // Use a timeout that will give reasonablly quick response to the user.
44
+  btSerial.setTimeout(100);
45
+  while (waiting)
46
+  {
47
+    if (btSerial.available())
48
+    {
49
+      recvd = btSerial.readBytes(buffer, 32);
50
+      for (size_t i = 0; i < recvd; i++)
51
+      {
52
+        if (buffer[i] != '\n')
53
+        {
54
+          newName += buffer[i];
55
+          btSerial.print(buffer[i]);
56
+        }
57
+        else
58
+        {
59
+          btSerial.println(" ");
60
+          waiting = false;
61
+          break;
62
+        }
63
+      }
64
+    }
65
+    delay(100);
66
+  }
67
+
68
+  newName.toCharArray(buffer, 32);
69
+  newName = NewNameCmd + newName;
70
+  newName.toCharArray(buffer, 32);
71
+
72
+  // make sure there is no pending output to interfere with commands
73
+  btSerial.flush();
74
+
75
+  // The name change command takes extra time.
76
+  // 1000ms is large enough, but arbitrary.
77
+  if (btSerial.cmd(buffer,1000))
78
+  {
79
+    btSerial.println("Name changed.");
80
+    btSerial.println("Reconnect or rescan to see the result.");
81
+    btSerial.println("Disconnecting...");
82
+    btSerial.flush();
83
+    btSerial.cmd("AT+DISC", 1000);
84
+  }
85
+  else
86
+  {
87
+    btSerial.println("Name NOT changed.");
88
+  }
89
+  
90
+  // Send a count to the port just to indicate activity.
91
+  // This will appear after the connection is re-established, or
92
+  // immediately if the name change command fails.
93
+  for (uint8_t i = 0; true; i++)
94
+  {
95
+    if (i == 0)
96
+    {
97
+      btSerial.println("");
98
+    }
99
+    btSerial.print(i);
100
+    btSerial.print('\r');
101
+    delay(1000);
102
+  }
103
+}
104
+

+ 36
- 0
Examples/echo/echo.ino View File

@@ -0,0 +1,36 @@
1
+/*
2
+ * echo - echo characters back to bluetooth device
3
+ *
4
+ * Waits for a connection and then echos each charater received.
5
+ *
6
+ * Debugging is enabled, so if you open the 'Serial Monitor' you can see
7
+ * the search for the HC05 baud and the wait for the BT connection.
8
+ */
9
+#include <Arduino.h>
10
+#include "HC05.h"
11
+
12
+#ifdef HC05_SOFTWARE_SERIAL
13
+#include <SoftwareSerial.h>
14
+HC05 btSerial = HC05(A2, A5, A3, A4);  // cmd, state, rx, tx
15
+#else
16
+HC05 btSerial = HC05(3, 2);  // cmd, state
17
+#endif
18
+
19
+void setup()
20
+{
21
+  DEBUG_BEGIN(57600);
22
+  btSerial.findBaud();
23
+}
24
+
25
+void loop()
26
+{
27
+  btSerial.println("Echo Server- type something");
28
+
29
+  while (btSerial.connected())
30
+  {
31
+    if (btSerial.available())
32
+    {
33
+      btSerial.write(btSerial.read());
34
+    }
35
+  }
36
+}

+ 83
- 0
Examples/findBaudTest/findBaudTest.ino View File

@@ -0,0 +1,83 @@
1
+/*
2
+ * findBaudTest - Test all supported baud settings
3
+ *
4
+ * The progress and results are printed to Serial, so open the 'Serial
5
+ * Montitor'.
6
+ *
7
+ * The progress and results will be easier to read if you disable the
8
+ * debugging (comment out or delete the "#define DEBUG_HC05" line in
9
+ * HC05.h.
10
+ */
11
+#include <Arduino.h>
12
+#include "HC05.h"
13
+
14
+#ifdef HC05_SOFTWARE_SERIAL
15
+#include <SoftwareSerial.h>
16
+HC05 btSerial = HC05(A2, A5, A3, A4);  // cmd, state, rx, tx
17
+#else
18
+HC05 btSerial = HC05(3, 2);  // cmd, state
19
+#endif
20
+
21
+void setup()
22
+{
23
+  DEBUG_BEGIN(57600);
24
+  Serial.begin(57600);
25
+  Serial.println("---------- Setup ----------");
26
+  btSerial.findBaud();
27
+  btSerial.setBaud(4800);
28
+  Serial.println("---------- Starting test ----------");
29
+}
30
+
31
+void loop()
32
+{
33
+  int numTests = 0;
34
+  int failed = 0;
35
+  unsigned long rate = 0;
36
+  unsigned long rates[] = {4800,9600,19200,38400,57600,115200};
37
+
38
+  for (int i = 0; i < 6; i++)
39
+  {
40
+    for (int j = 0; j < 6; j++)
41
+    {
42
+      numTests++;
43
+      Serial.print(rates[i]);
44
+      btSerial.setBaud(rates[i]);
45
+      rate = btSerial.findBaud();
46
+      if (rate != rates[i])
47
+      {
48
+        Serial.print(" FAILED: found rate ");
49
+        Serial.println(rate);
50
+        failed++;
51
+      }
52
+      else
53
+      {
54
+        Serial.print("->");
55
+        Serial.print(rates[j]);
56
+        btSerial.setBaud(rates[j]);
57
+        rate = btSerial.findBaud();
58
+        if (rate != rates[j])
59
+        {
60
+          Serial.print("FAILED: found rate ");
61
+          Serial.println(rate);
62
+          failed++;
63
+        }
64
+        else
65
+        {
66
+          Serial.println(" PASSED");
67
+        }
68
+      }
69
+    }
70
+  }
71
+
72
+  Serial.println("--------- Tests Complete ----------");
73
+  Serial.print("Results: ");
74
+  Serial.print(failed);
75
+  Serial.print(" of ");
76
+  Serial.print(numTests);
77
+  Serial.println(" tests failed.");
78
+
79
+  while (true)
80
+  {
81
+     ;
82
+  }
83
+}

+ 46
- 0
Examples/hc05_test/hc05_test.ino View File

@@ -0,0 +1,46 @@
1
+/*
2
+ * hc05_test - Test the disconnect command
3
+ *
4
+ * When the main loop detects that a bluetooth device is connected it
5
+ * disconnects the device.
6
+ *
7
+ * Montor the progress using the debug port.
8
+ */
9
+#include <Arduino.h>
10
+#include "HC05.h"
11
+
12
+#ifdef HC05_SOFTWARE_SERIAL
13
+#include <SoftwareSerial.h>
14
+HC05 btSerial = HC05(A2, A5, A3, A4);  // cmd, state, rx, tx
15
+#else
16
+HC05 btSerial = HC05(3, 2);  // cmd, state
17
+#endif
18
+
19
+#ifdef DEBUG_HC05
20
+#ifdef DEBUG_SW_PORT
21
+  extern SoftwareSerial DEBUG_PORT;
22
+#endif
23
+#endif
24
+
25
+void setup()
26
+{
27
+  DEBUG_BEGIN(57600);
28
+  DEBUG_PRINTLN("Setup");
29
+  delay(3000);  // this delay is for debugging convenience only
30
+  DEBUG_PRINTLN("DelayComplete");
31
+  btSerial.println(btSerial.findBaud());
32
+  
33
+}
34
+
35
+void loop()
36
+{
37
+  if (btSerial.connected()){
38
+    DEBUG_PRINTLN("Connected");
39
+    btSerial.cmd("AT+DISC", 1000);
40
+  }
41
+  else
42
+  {
43
+    DEBUG_PRINTLN("Disconnected");
44
+  }
45
+  delay(1000);
46
+}

+ 72
- 0
Examples/recover/recover.ino View File

@@ -0,0 +1,72 @@
1
+/*
2
+ * recover - recover from forgotten serial settings
3
+ *
4
+ * Uses cmdMode2 to set the HC05 to 19200 baud, no parity, ones stop bit
5
+ * (19200N1). 19200N1 is supported by both the hardware and software
6
+ * serial ports on the Arduino.
7
+ *
8
+ * Debugging is enabled, so if you open the 'Serial Monitor' you can see
9
+ * the process. Note that if you don't get the 'Serial Monitor' open
10
+ * before the 'AT+UART=' command is issued, you will not really see the
11
+ * old settings. There is a three second delay at the beginning to give
12
+ * you a chance to get the serial monitor running.
13
+ *
14
+ * The `Serial Monitor` should be set to 57600 baud.
15
+ *
16
+ */
17
+#include <Arduino.h>
18
+#include "HC05.h"
19
+
20
+#ifdef HC05_SOFTWARE_SERIAL
21
+#include <SoftwareSerial.h>
22
+HC05 btSerial = HC05(A2, A5, A3, A4);  // cmd, state, rx, tx
23
+#else
24
+HC05 btSerial = HC05(3, 2);  // cmd, state
25
+#endif
26
+
27
+int powerPin = 7;
28
+
29
+void setup()
30
+{
31
+  DEBUG_BEGIN(57600);
32
+  DEBUG_PRINTLN("Starting recovery in 3 seconds.");
33
+  btSerial.cmdMode2Start(powerPin);
34
+
35
+  // Provide some time for the user to start the serial monitor
36
+  delay(3000);
37
+
38
+  // For curiosity's sake, ask for the old settings
39
+  btSerial.cmd("AT+UART?");
40
+
41
+  // Now set the baud to 19200N1
42
+  btSerial.cmd("AT+UART=19200,0,0");
43
+
44
+  // Exit command mode and switch to the new baud setting
45
+  btSerial.cmd("AT+RESET");
46
+  btSerial.cmdMode2End();
47
+}
48
+
49
+
50
+void loop()
51
+{
52
+  int c;
53
+  unsigned long rate;
54
+  rate = btSerial.findBaud();
55
+
56
+  DEBUG_PRINTLN("");
57
+
58
+  if (rate == 19200)
59
+  {
60
+    DEBUG_PRINTLN("Recovery successful. HC05 serial settings = 19200N1");
61
+  }
62
+  else
63
+  {
64
+    DEBUG_PRINT("Recovery failed. Did you connect HC05 power to pin ");
65
+    DEBUG_PRINT(powerPin);
66
+    DEBUG_PRINTLN("?");
67
+  }
68
+  while (true)
69
+  {
70
+    ;
71
+  }
72
+}

+ 260
- 0
HC05.cpp View File

@@ -0,0 +1,260 @@
1
+#include <Arduino.h>
2
+#include <HC05.h>
3
+
4
+#ifdef DEBUG_SW_PORT
5
+SoftwareSerial DEBUG_SW_PORT;
6
+#endif
7
+
8
+#ifdef HC05_SOFTWARE_SERIAL
9
+HC05::HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx):_btSerial(rx,tx,0)
10
+#else
11
+#define _btSerial HC05_HW_SERIAL_PORT
12
+HC05::HC05(int cmdPin, int statePin)
13
+#endif
14
+{
15
+    pinMode(cmdPin, OUTPUT);
16
+    _cmdPin = cmdPin;
17
+    cmdMode = false;
18
+#ifdef HC05_STATE_PIN
19
+    pinMode(statePin, INPUT);
20
+    _statePin = statePin;
21
+#endif
22
+    _bufsize = sizeof(_buffer)/sizeof(char);
23
+}
24
+
25
+static const unsigned long rates[] =
26
+    {4800,9600,19200,38400,57600,115200};
27
+
28
+unsigned long HC05::findBaud()
29
+{
30
+    const int bt_rx = 4;
31
+    const int bt_tx = 5;
32
+    int numRates = sizeof(rates)/sizeof(unsigned long);
33
+    int response = false;
34
+    int recvd = 0;
35
+    //char _buffer[128];
36
+
37
+    DEBUG_PRINTLN("findBaud");
38
+    setCmdPin(HIGH);
39
+    delay(100);
40
+    for(int rn = 0; rn < numRates; rn++)
41
+    {
42
+        _btSerial.begin(rates[rn]);
43
+        _btSerial.setTimeout(100);
44
+        _btSerial.flush();
45
+        DEBUG_WRITE("Trying ");
46
+        DEBUG_PRINT(rates[rn]);
47
+        DEBUG_WRITE("... ");
48
+        _btSerial.write("AT\r\n");
49
+        recvd = _btSerial.readBytes(_buffer,_bufsize);
50
+        if (recvd > 0)
51
+        {
52
+            DEBUG_PRINTLN("Found.");
53
+            // FIXME: refactor to a single return
54
+            setCmdPin(LOW);
55
+            return(rates[rn]);
56
+        }
57
+        else
58
+        {
59
+            DEBUG_PRINTLN("x");
60
+        }
61
+    }
62
+    setCmdPin(LOW);
63
+    DEBUG_WRITE("\r\nNo connection\r\n");
64
+    return(0);
65
+}
66
+int HC05::cmd(String cmd, unsigned long timeout)
67
+{
68
+    char raw[cmd.length() + 1];
69
+    cmd.toCharArray(raw, sizeof(raw));
70
+    return this->cmd(raw, timeout);
71
+}
72
+
73
+int HC05::cmd(const char* cmd, unsigned long timeout)
74
+{
75
+    int recvd = 0;
76
+    DEBUG_PRINTLN(cmd);
77
+
78
+    setCmdPin(HIGH);
79
+    // No spec for how long it takes to enter command mode, but 100ms
80
+    // seems to work- assuming the output has been drained.
81
+    delay(100);
82
+    _btSerial.write(cmd);
83
+    _btSerial.write("\r\n");
84
+    _btSerial.setTimeout(timeout);
85
+    do
86
+    {
87
+        // ATTENTION: At least through Arduino v1.0.3, it is not possible
88
+        //            to tell the difference between a timeout and
89
+        //            receiving only the termination character (NL in this
90
+        //            case), because the termination character is not
91
+        //            returned and timeout is not returned as a unique
92
+        //            indication.
93
+        //            In this case the result would be an early return
94
+        //            of a multiline response before the OK is received.
95
+        //            The return would incorrectly indicate an error (no
96
+        //            OK response).
97
+        recvd = _btSerial.readBytesUntil('\n',_buffer,_bufsize);
98
+        if (recvd > 0)
99
+        {
100
+            DEBUG_WRITE((uint8_t *)_buffer,recvd);
101
+            DEBUG_WRITE('\n');
102
+        }
103
+        else
104
+        {
105
+            DEBUG_PRINTLN("timeout 1");
106
+        }
107
+    }
108
+    while ((recvd > 0) && (_buffer[0] != 'O' || _buffer[1] != 'K'));
109
+
110
+    setCmdPin(LOW);
111
+
112
+    // Empirically determined that it takes some time to reliably exit
113
+    // command mode. The appeared to be a baud rate dependency and with
114
+    // >100ms required at 9600 baud.
115
+    delay(150);
116
+    return((_buffer[0] == 'O' && _buffer[1] == 'K'));
117
+}
118
+
119
+
120
+/*
121
+ * If setBaud() is called while the HC-05 is connected, then
122
+ * it will be disconnected when AT+RESET command is issued, and
123
+ * it may take 2 (or more?) connection attempts to reconnect. The extra
124
+ * connect attempts may be a host side issue and not specific to the
125
+ * HC-05 module.
126
+ */
127
+void HC05::setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity)
128
+{
129
+    int recvd = 0;
130
+    setCmdPin(HIGH);
131
+    delay(200);
132
+    DEBUG_WRITE("AT+UART=");
133
+    _btSerial.write("AT+UART=");
134
+    DEBUG_PRINT(baud);
135
+    _btSerial.print(baud);
136
+
137
+    DEBUG_PRINT(",");
138
+    _btSerial.print(",");
139
+
140
+    DEBUG_PRINT(stopbits);
141
+    _btSerial.print(stopbits);
142
+
143
+    DEBUG_PRINT(",");
144
+    _btSerial.print(",");
145
+
146
+    DEBUG_PRINT(parity);
147
+    _btSerial.print(parity);
148
+
149
+    DEBUG_WRITE("\r\n");
150
+    _btSerial.write("\r\n");
151
+
152
+    recvd = _btSerial.readBytes(_buffer,_bufsize);
153
+    if (recvd > 0)
154
+    {
155
+        DEBUG_WRITE((uint8_t *)_buffer,recvd);
156
+    }
157
+    else
158
+    {
159
+        DEBUG_PRINTLN("timeout 2");
160
+    }
161
+    cmd("AT+RESET");
162
+    setCmdPin(LOW);
163
+    _btSerial.begin(baud);
164
+    delay(1000);
165
+}
166
+
167
+// Usually parity is none, and there is only one stop bit, so this
168
+// simpler call will do the job.
169
+void HC05::setBaud(unsigned long baud)
170
+{
171
+    setBaud(baud, 0, 0);
172
+}
173
+
174
+
175
+int HC05::available()
176
+{
177
+    _btSerial.available();
178
+}
179
+
180
+int HC05::peek()
181
+{
182
+    _btSerial.peek();
183
+}
184
+
185
+void HC05::flush()
186
+{
187
+    _btSerial.flush();
188
+}
189
+
190
+int HC05::read()
191
+{
192
+    _btSerial.read();
193
+}
194
+
195
+void HC05::begin(unsigned long baud)
196
+{
197
+    _btSerial.begin(baud);
198
+}
199
+
200
+#ifndef HC05_SOFTWARE_SERIAL
201
+// only hardware serial ports support parity/stop bit configuration
202
+void HC05::begin(unsigned long baud, uint8_t config)
203
+{
204
+    _btSerial.begin(baud, config);
205
+}
206
+#endif
207
+
208
+#ifdef HC05_STATE_PIN
209
+bool HC05::connected()
210
+{
211
+    return(digitalRead(_statePin)?true:false);
212
+}
213
+#endif
214
+
215
+size_t HC05::write(uint8_t byte)
216
+{
217
+#ifdef HC05_STATE_PIN
218
+    // The down side of this check is that the status gets checked for
219
+    // every byte written out. That doesn't seem efficient.
220
+    if (digitalRead(_statePin) != HIGH)
221
+    {
222
+        DEBUG_PRINT("No Connection, waiting...");
223
+        while (digitalRead(_statePin) == LOW)
224
+        {
225
+            delay(100);
226
+        }
227
+        DEBUG_PRINTLN("OK");
228
+    }
229
+#endif
230
+    _btSerial.write(byte);
231
+}
232
+
233
+
234
+void HC05::cmdMode2Start(int pwrPin)
235
+{
236
+    pinMode(pwrPin, OUTPUT);
237
+    digitalWrite(pwrPin, LOW);
238
+    delay(250);  // off or reset time
239
+    digitalWrite(_cmdPin, HIGH);
240
+    digitalWrite(pwrPin, HIGH);
241
+    cmdMode = true;
242
+    _btSerial.begin(38400);
243
+    delay(1500);  // time for the HC05 to initialize
244
+}
245
+
246
+
247
+void HC05::cmdMode2End(void)
248
+{
249
+    digitalWrite(_cmdPin, LOW);
250
+    cmdMode = false;
251
+    delay(1000);
252
+}
253
+
254
+void HC05::setCmdPin(bool state)
255
+{
256
+    if (cmdMode == false)
257
+    {
258
+        digitalWrite(_cmdPin, state);
259
+    }
260
+}

+ 118
- 0
HC05.h View File

@@ -0,0 +1,118 @@
1
+/*
2
+ * HC05.h - interface definitions for the HC05 library
3
+ *
4
+ * Select hardware or software serial port:
5
+ *   Define HC05_SOFTWARE_SERIAL to select a SoftwareSerial port, then
6
+ *   initialize the HC05 class with either two arguments, for a hardware port,
7
+ *   or four arguments for a software serial port:
8
+ *
9
+ *     HC05(cmdPin, statePin)
10
+ *     or
11
+ *     HC05(cmdPin, statePin, rxPin, txPin)
12
+ *
13
+ * Specify an alternate hardware serial port by changing the
14
+ * HC05_HW_SERIAL_PORT define.
15
+ *
16
+ * Define DEBUG_HC05 to enable debugging messages and use the
17
+ * DEBUG_BEGIN() macro in the sketch setup() function.
18
+ * By default, debugging messages go to the harware serial port, Serial.
19
+ * Change that by defining DEBUG_SW_PORT.
20
+ *
21
+ */
22
+#ifndef HC05_h
23
+#define HC05_h
24
+
25
+#include <inttypes.h>
26
+#include <Stream.h>
27
+#include <SoftwareSerial.h>
28
+
29
+/*
30
+ * Comment the following define line if you aren't using the State pin or
31
+ * if your HC05 does not have such a pin.  You still must specify a
32
+ * statePin to initialize the HC05 class, but the pin will not be used.
33
+ */
34
+#define HC05_STATE_PIN
35
+
36
+/*
37
+ * This macro must be defined even if you are using a software serial
38
+ * port. You can change this to any serial port supported by your
39
+ * Arduino (i.e, Serial1, Serial2, etc.)
40
+ */
41
+#define HC05_HW_SERIAL_PORT Serial
42
+
43
+
44
+/*
45
+ * Optional macros, define as needed
46
+ */
47
+#define HC05_SOFTWARE_SERIAL
48
+#define DEBUG_HC05
49
+//#define DEBUG_SW_PORT swserial(4,5)
50
+
51
+#ifdef DEBUG_HC05
52
+#ifdef DEBUG_SW_PORT
53
+#define DEBUG_PORT swserial
54
+#else
55
+#define DEBUG_PORT Serial
56
+#endif
57
+
58
+#define DEBUG_BEGIN(baud) DEBUG_PORT.begin(baud)
59
+#define DEBUG_WRITE(...) DEBUG_PORT.write(__VA_ARGS__)
60
+#define DEBUG_PRINT(...) DEBUG_PORT.print(__VA_ARGS__)
61
+#define DEBUG_PRINTLN(...) DEBUG_PORT.println(__VA_ARGS__)
62
+
63
+#else
64
+#define DEBUG_BEGIN(baud)
65
+#define DEBUG_WRITE(...)
66
+#define DEBUG_PRINT(...)
67
+#define DEBUG_PRINTLN(...)
68
+
69
+#endif  // DEBUG_HC05
70
+
71
+class HC05 : public Stream
72
+{
73
+public:
74
+    HC05(int cmdPin, int statePin);
75
+    HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
76
+    unsigned long findBaud();
77
+    void setBaud(unsigned long baud);  // always no parity, one stop bit
78
+    void setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity);
79
+
80
+    // cmd(): 100ms default timeout covers simple commands, but commands
81
+    // 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);
84
+
85
+    // HC05 cmdMode2 forces 38400, no parity, one stop bit
86
+    // Entering cmdMode2 uses a pin to turn the HC05 power on and off
87
+    // with the cmdPin high. cmdPin must remain high to stay in
88
+    // cmdMode2, so use cmdMode2End() to exit.
89
+    void cmdMode2Start(int pwrPin);
90
+    void cmdMode2End(void);
91
+
92
+#ifdef HC05_STATE_PIN
93
+    bool connected(void);
94
+#endif
95
+    virtual int available(void);
96
+    virtual void begin(unsigned long);
97
+    virtual int peek(void);
98
+    virtual int read(void);
99
+    virtual void flush(void);
100
+    virtual size_t write(uint8_t);
101
+    using Print::write;
102
+#ifdef HC05_SOFTWARE_SERIAL
103
+    SoftwareSerial _btSerial;
104
+#endif
105
+
106
+private:
107
+    bool cmdMode;
108
+    int _cmdPin;
109
+#ifdef HC05_STATE_PIN
110
+    int _statePin;
111
+#endif
112
+    int _bufsize;
113
+    char _buffer[32];
114
+    void setCmdPin(bool state);
115
+};
116
+
117
+extern HC05 btSerial;
118
+#endif  // HC05_h

BIN
HardwarSerial.fzz View File


BIN
ImprovedPowerCtrl.fzz View File


BIN
ImprovedPowerCtrl_bb.png View File


+ 21
- 0
LICENSE View File

@@ -0,0 +1,21 @@
1
+Copyright (c) 2013 J.Dunmire
2
+
3
+Permission is hereby granted, free of charge, to any person obtaining a
4
+copy of this software and associated documentation files (the
5
+"Software"), to deal in the Software without restriction, including
6
+without limitation the rights to use, copy, modify, merge, publish,
7
+distribute, sublicense, and/or sell copies of the Software, and to
8
+permit persons to whom the Software is furnished to do so, subject to
9
+the following conditions:
10
+
11
+The above copyright notice and this permission notice shall be included
12
+in all copies or substantial portions of the Software.
13
+
14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+

BIN
LibDevelopment.fzz View File


BIN
LibDevelopment_bb.png View File


+ 155
- 0
README.md View File

@@ -0,0 +1,155 @@
1
+HC05
2
+====
3
+Forked from https://github.com/jdunmire/HC05/
4
+
5
+An Arduino library for the HC-05 Bluetooth ITead Studio HC-05 Serial
6
+Port Module.
7
+
8
+See the `LICENSE` file for copyright and license information.
9
+
10
+The serial port can be configured as any supported Serial port or
11
+a SoftwareSerial port.
12
+
13
+Includes a demonstration program that can be uses to change the name
14
+reported by an HC-05 module.
15
+
16
+Additional information is available as an
17
+[exercise](http://rockingdlabs.dunmire.org/exercises-experiments/hc05-bluetooth)
18
+at [RockingD Labs](http://rockingdlabs.dunmire.org).
19
+
20
+
21
+Components
22
+----------
23
+`HC05`
24
+    A class for controlling and communicating through an ITead Studio
25
+    HC-05 Serial Port Module. This class inherits from the Stream class.
26
+
27
+
28
+###Methods:
29
+The Stream class is extended with the following methods.
30
+
31
+`findBaud()`
32
+    Determine HC-05 communications speed. Make this call in setup()
33
+    instead of `begin()`. `begin()` is still avaialble and can be used
34
+    inplace of `findBaud()` if you know the HC-05 communications speed.
35
+
36
+`setBaud(unsigned long rate)`
37
+    Specify the HC-05 communications speed. The speed is non-volatile so
38
+    call this only when the rate returned by findBaud() is not the one
39
+    you require.
40
+
41
+`setBaud(unsigned long rate, unsigned long parity, unsigned long stopbits)`
42
+    Use this method when you need something besides the default no
43
+    parity, one stop bit settings that are the default.
44
+    __CAUTION!__ The HC-05 supports many serial configurations that are not
45
+    compatible with an Arduino. For example, the Arduino software serial
46
+    port port supports only no parity, one stop bit settings.
47
+
48
+`cmd()`
49
+    Send a command to the module. The 'key' (cmdPin) pin is activated to
50
+    put the module in command mode where 'AT' commands are recognized.
51
+
52
+`cmdMode2Start(int pwrPin)`
53
+   This is an alternate command mode. This 2nd command mode has the
54
+   advantage forcing the HC-05 into a know communications speed: 38400.
55
+   However, entering this 2nd command mode requires switching the power
56
+   to the HC-05.
57
+
58
+`cmdMode2End()`
59
+   Exits the alternate command mode, leaving the power to the HC-05 on.
60
+
61
+`connected()` (Only if HC05_STATE_PIN is defined in `HC05.h`)
62
+    Returns true when a BT connection has been established.
63
+
64
+`write()`
65
+`print*()`
66
+    The write(), and print*(), methods block until there is a BT
67
+    connection.
68
+
69
+Example Programs
70
+----------------
71
+The default library configuration uses a software serial port. The
72
+example programs will work with either a hardware or a software serial
73
+port. The configuration is changed by modifying the `HC05.h` file.
74
+
75
+See the `SoftwareSerial.fzz` file for the proper default connections.
76
+(`.fzz` files can be read by the free program available from
77
+[Fritzing](http://fritzing.org/home/))
78
+
79
+`changeName`
80
+    This application is one of the reasons I wrote this library. I
81
+    wanted to be able to change the name reported by the HC-05 because I
82
+    have multiple HC-05 modules that I kept mixing up. With this program
83
+    you can set the name of the HC-05 module to reflect something
84
+    physically identifying (or anything else that helps you tell your
85
+    modules apart).
86
+
87
+`echo`
88
+    Echo characters as they are received.
89
+
90
+`hc05_test`
91
+    Tests the disconnect command (AT+DISC). This was something I used
92
+    during development and probably is not of general interest.
93
+
94
+`recover`
95
+    This example used the 2nd command mode to _recover_ the HC-05 when
96
+    its serial port settings are incompatible with the Arduino serial
97
+    ports. Power to HC-05 must be controlled by an Arduino pin.
98
+    See the `Recovery.fzz` diagram for suitable connections.
99
+
100
+`findBaudTest`
101
+    Tests both setBaud() and findBaud() by trying every combination of
102
+    supported rates. The output from this example looks best if
103
+    DEBUG_HC05 is not defined. (Simply comment out that line in HC05.h).
104
+
105
+
106
+Installation
107
+------------
108
+###Option 1: Git (Recommended)
109
+* Follow this [GitHub repository](https://github.com/jdunmire/HC05)
110
+  and use `git` to track your own changes by cloning:
111
+
112
+    $ cd ~/sketchbook/libraries
113
+    $ git clone https://github.com/jdunmire/HC05.git
114
+
115
+* Start the Arduino IDE and you should find `HC05` in the
116
+  libraries section.
117
+
118
+###Option 2: Source only
119
+* Download a ZIP file. The ZIP button at
120
+  [GitHub](https://github.com/jdunmire/HC05) will always get the
121
+  latest version, but you may prefer one of the
122
+  [tagged](https://github.com/jdunmire/HC05/tags) versions.
123
+
124
+* Unpack the zip file into your sketchbook library directory
125
+  (`~/sketchbook/libraries` on Linux).
126
+
127
+* Rename the resulting directory (or create a symlink) to
128
+  ~/sketchbook/libraries/HC05
129
+
130
+* Start the Arduino IDE and you should find `HC05` in the libraries
131
+  section.
132
+
133
+### Configuration
134
+By default the library is configured for a software serial port and
135
+debugging output to the hardware serial port (Serial) is turned on. You
136
+will need to edit the HC05.h file if you want to change those settings.
137
+
138
+See the `SoftwareSerial.fzz` file for the proper default connections.
139
+The `HardwareSerial.fzz` shows the hardware port alternative. The files
140
+can be read by the free program available from
141
+[Fritzing](http://fritzing.org/home/)
142
+
143
+The Bluetooth port is `btSerial` and must be setup as shown at the top
144
+of the Example sketches. If debugging output is enabled in HC05.h (it is
145
+by default) then your sketch must include a DEBUG_BEGIN(baud) command to
146
+initialize the debug output port and set it's baud rate.
147
+
148
+#### Hardware Serial Port Issues for UNO
149
+Using the hardware serial port on the UNO comes with some caveats:
150
+
151
+  * You will have to disconnect the HC-05 module to upload a sketch.
152
+  * If you use the Arduino `Serial Monitor` you will see the traffic to
153
+    and from the HC-05 serial port. If you type in the `Serial Monitor`
154
+    it will interfere with the HC-05 traffic.
155
+

BIN
Recommended_bb.png View File


BIN
Recovery.fzz View File


BIN
Recovery_bb.png View File


BIN
SoftwareSerial.fzz View File


+ 21
- 0
keywords.txt View File

@@ -0,0 +1,21 @@
1
+#
2
+# Syntax coloring for HC05 Library
3
+#
4
+
5
+# Datatypes (Classes)
6
+HC05	KEYWORD1
7
+
8
+# Methods and functions
9
+cmd	KEYWORD2
10
+findBaud	KEYWORD2
11
+setBaud	KEYWORD2
12
+connected	KEYWORD2
13
+available	KEYWORD2
14
+peek	KEYWORD2
15
+read	KEYWORD2
16
+flush	KEYWORD2
17
+write	KEYWORD2
18
+
19
+# Constants (Macros in this case)
20
+DEBUG_HC05	LITERAL1
21
+HC05_SOFTWARE_SERIAL	LITERAL1

Loading…
Cancel
Save