You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HC05.cpp 6.0KB

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