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.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <inttypes.h>
  25. #include <Stream.h>
  26. #include <SoftwareSerial.h>
  27. /*
  28. * Comment the following define line if you aren't using the State pin or
  29. * if your HC05 does not have such a pin. You still must specify a
  30. * statePin to initialize the HC05 class, but the pin will not be used.
  31. */
  32. #define HC05_STATE_PIN
  33. /*
  34. * This macro must be defined even if you are using a software serial
  35. * port. You can change this to any serial port supported by your
  36. * Arduino (i.e, Serial1, Serial2, etc.)
  37. */
  38. #define HC05_HW_SERIAL_PORT Serial
  39. /*
  40. * Optional macros, define as needed
  41. */
  42. #define HC05_SOFTWARE_SERIAL
  43. #define DEBUG_HC05
  44. //#define DEBUG_SW_PORT swserial(4,5)
  45. #ifdef DEBUG_HC05
  46. #ifdef DEBUG_SW_PORT
  47. #define DEBUG_PORT swserial
  48. #else
  49. #define DEBUG_PORT Serial
  50. #endif
  51. #define DEBUG_BEGIN(baud) DEBUG_PORT.begin(baud)
  52. #define DEBUG_WRITE(...) DEBUG_PORT.write(__VA_ARGS__)
  53. #define DEBUG_PRINT(...) DEBUG_PORT.print(__VA_ARGS__)
  54. #define DEBUG_PRINTLN(...) DEBUG_PORT.println(__VA_ARGS__)
  55. #else
  56. #define DEBUG_BEGIN(baud)
  57. #define DEBUG_WRITE(...)
  58. #define DEBUG_PRINT(...)
  59. #define DEBUG_PRINTLN(...)
  60. #endif // DEBUG_HC05
  61. class HC05 : public Stream
  62. {
  63. public:
  64. enum Roles {
  65. Slave = 0,
  66. Master = 1,
  67. SlaveLoop = 2
  68. };
  69. HC05(int cmdPin, int statePin);
  70. HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
  71. unsigned long findBaud();
  72. void setBaud(unsigned long baud); // always no parity, one stop bit
  73. void setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity);
  74. // cmd(): 100ms default timeout covers simple commands, but commands
  75. // that manage the connection are likely to take much longer.
  76. bool cmd(String cmd, unsigned long timeout=100);
  77. bool cmd(const char* cmd, unsigned long timeout=100);
  78. // HC05 cmdMode2 forces 38400, no parity, one stop bit
  79. // Entering cmdMode2 uses a pin to turn the HC05 power on and off
  80. // with the cmdPin high. cmdPin must remain high to stay in
  81. // cmdMode2, so use cmdMode2End() to exit.
  82. void cmdMode2Start(int pwrPin);
  83. void cmdMode2End(void);
  84. #ifdef HC05_STATE_PIN
  85. bool connected(void);
  86. #endif
  87. virtual int available(void);
  88. virtual void begin(unsigned long);
  89. virtual int peek(void);
  90. virtual int read(void);
  91. virtual void flush(void);
  92. virtual size_t write(uint8_t);
  93. using Print::write;
  94. #ifdef HC05_SOFTWARE_SERIAL
  95. SoftwareSerial _btSerial;
  96. #endif
  97. private:
  98. bool cmdMode;
  99. int _cmdPin;
  100. #ifdef HC05_STATE_PIN
  101. int _statePin;
  102. #endif
  103. int _bufsize;
  104. char _buffer[32];
  105. void setCmdPin(bool state);
  106. String _stringBuffer;
  107. String _stringData;
  108. int _lastError;
  109. public:
  110. bool atGetCommand(String command, unsigned long timeout=100);
  111. bool atGetCommand1(String command, String arg1, unsigned long timeout=100);
  112. bool atTest(unsigned long timeout=100);
  113. bool atReset(unsigned long timeout=100);
  114. bool atVersion(unsigned long timeout=100);
  115. bool atRestore(unsigned long timeout=100);
  116. bool atGetAddress(unsigned long timeout=100);
  117. bool atGetName(unsigned long timeout=100);
  118. bool atSetName(String name, unsigned long timeout=100);
  119. bool atGetRemoteName(String address, unsigned long timeout=100);
  120. bool atGetRole(unsigned long timeout=100);
  121. bool atSetRole(Roles role, unsigned long timeout=100);
  122. bool atGetPassKey(unsigned long timeout=100);
  123. bool atSetPassKey(String passKey, unsigned long timeout=100);
  124. };
  125. extern HC05 btSerial;
  126. #endif // HC05_h