Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. /*
  2. * Bluetooth LE device scanner using HM-11
  3. * Richard Hawthorn
  4. * 6th June 2015
  5. */
  6.  
  7. #include <SoftwareSerial.h>
  8. #include <Time.h>
  9.  
  10. //define software serial pins, for the HM-11 device
  11. SoftwareSerial mySerial(2, 3); // RX, TX
  12.  
  13. //define string to collect serial data
  14. String inputString = ""; // a string to hold incoming data
  15. boolean stringComplete = false; // whether the string is complete
  16.  
  17. //used for timing
  18. int lastMinute = -1;
  19.  
  20. void setup()
  21. {
  22. //setup the serial ports
  23. Serial.begin(9600);
  24. mySerial.begin(9600);
  25.  
  26. //reserve space for the incoming string data
  27. inputString.reserve(200);
  28.  
  29. //let the user know the program has started
  30. Serial.println("Starting");
  31. //send setup bluetooth commands
  32. at("AT"); // check if working, always returns OK
  33. at("AT+ROLE1"); // select master = central
  34. at("AT+RESET"); // actually more a restart than a reset .. needed after ROLE
  35. at("AT+SHOW1"); // include Bluetooth name in response
  36. //at("AT+IMME1"); // "work immediately", not sure what this does
  37. //at("AT+FILT0"); // show all BLE devices, not only HM ones
  38. delay(1000); // wait a bit, NECESSARY!!
  39. at("AT+IMME1");
  40. at("AT+DISC?");
  41.  
  42.  
  43. }
  44.  
  45. //scan for devices
  46. void scan(){
  47. Serial.println("Scanning");
  48. at("AT+DISC?");
  49. }
  50.  
  51. //sending commands
  52. void at(char* cmd) {
  53. mySerial.write(cmd);
  54. Serial.print(cmd);
  55. while(!mySerial.find("OK"))Serial.print("."); //probably shouldn't wait here forever
  56. Serial.println(" .. OK");
  57. }
  58.  
  59. //process the incoming serial string
  60. void processString(String string){
  61.  
  62. String deviceId = "";
  63. String deviceName = "";
  64.  
  65. int startId = string.indexOf("+DISI:") + 6;
  66. int endId = string.indexOf("+NAME:");
  67. int startName = endId + 6;
  68. int endName = string.indexOf("\n");
  69.  
  70. deviceId = string.substring(startId, endId);
  71. deviceName = string.substring(startName, endName);
  72.  
  73.  
  74. Serial.println(deviceId);
  75. Serial.println(deviceName);
  76. }
  77.  
  78. //check if we have any response from the bluetooth device
  79. void checkBtResponse(){
  80.  
  81. while (mySerial.available()) {
  82. // get the new byte:
  83. char inChar = (char)mySerial.read();
  84. // add it to the inputString:
  85. inputString += inChar;
  86. // if the incoming character is a newline, set a flag
  87. // so the main loop can do something about it:
  88. if (inChar == '\n') {
  89. stringComplete = true;
  90. }
  91. }
  92.  
  93. //if we have received a serial string
  94. if (stringComplete == true){
  95. processString(inputString);
  96. stringComplete = false;
  97. inputString = "";
  98.  
  99.  
  100. }
  101. }
  102.  
  103. //run the scan every minute
  104. void timer(){
  105. int minute();
  106. return millis()/1000;
  107. int minuteNow = minute();
  108. if (minuteNow != lastMinute){
  109. lastMinute = minuteNow;
  110. scan();
  111. }
  112. }
  113.  
  114. void loop(){
  115. timer();
  116. checkBtResponse();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement