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.

changeName.ino 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * Configure this sketch to work with either a software or a hardware
  12. * serial port, as configured in 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. * See the ITeadStudio HC-05 datasheet for a full list of commands.
  22. */
  23. String NewNameCmd("AT+NAME=");
  24. void setup()
  25. {
  26. btSerial.findBaud();
  27. }
  28. void loop(){
  29. char buffer[32];
  30. size_t recvd = 0;
  31. bool waiting = true;
  32. String newName;
  33. btSerial.println("");
  34. btSerial.print("New name? ");
  35. // Use a timeout that will give reasonablly quick response to the user.
  36. btSerial.setTimeout(100);
  37. while (waiting)
  38. {
  39. if (btSerial.available())
  40. {
  41. recvd = btSerial.readBytes(buffer, 32);
  42. for (size_t i = 0; i < recvd; i++)
  43. {
  44. if (buffer[i] != '\n')
  45. {
  46. newName += buffer[i];
  47. btSerial.print(buffer[i]);
  48. }
  49. else
  50. {
  51. btSerial.println(" ");
  52. waiting = false;
  53. break;
  54. }
  55. }
  56. }
  57. delay(100);
  58. }
  59. newName.toCharArray(buffer, 32);
  60. newName = NewNameCmd + newName;
  61. newName.toCharArray(buffer, 32);
  62. // make sure there is no pending output to interfere with commands
  63. btSerial.flush();
  64. // The name change command takes extra time.
  65. // 1000ms is large enough, but arbitrary.
  66. if (btSerial.cmd(buffer,1000))
  67. {
  68. btSerial.println("Name changed.");
  69. btSerial.println("Reconnect or rescan to see the result.");
  70. btSerial.println("Disconnecting...");
  71. btSerial.flush();
  72. btSerial.cmd("AT+DISC", 1000);
  73. }
  74. else
  75. {
  76. btSerial.println("Name NOT changed.");
  77. }
  78. // Send a count to the port just to indicate activity.
  79. // This will appear after the connection is re-established, or
  80. // immediately if the name change command fails.
  81. for (uint8_t i = 0; true; i++)
  82. {
  83. if (i == 0)
  84. {
  85. btSerial.println("");
  86. }
  87. btSerial.print(i);
  88. btSerial.print('\r');
  89. delay(1000);
  90. }
  91. }