Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. HardwareSerial & BTserial = Serial1; //SETTING SERIAL1
  2. // BTrx --> 18tx1 sending data to BT orange wire
  3. // BTtx --> 19rx1 recieving data from BT green wire
  4. int BTtest;
  5. char c = ' ';
  6. char lineBuffer[1000];
  7. char subBuffer[30];
  8. unsigned int index = 0;
  9. unsigned int index2 = 0;
  10. byte BTtotal;
  11. byte BTresult = 0;
  12. String send = "";
  13. void setup()
  14. {
  15. // ////BT PINS /////////////
  16. pinMode(8, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  17. digitalWrite(8, HIGH); // SET this to HIGH, unplug ppower, unplug USB > plug USB > plug power, to enter AT mode
  18. pinMode(45, OUTPUT); // give power to BT HC-05
  19. digitalWrite(45, HIGH); // give power to BT HC-05
  20. Serial.begin(9600);
  21. // setting up mega serial
  22. BTserial.begin(38400); // HC-05 default serial speed for AT mode is 38400
  23. // Wait for hardware to initialize
  24. delay(1000);
  25. // ////Starting BT /////////////
  26. initHC05ToInq(); // Set correct states for inq
  27. initMessage();
  28. BTserial.println("AT+INQ");
  29. // ////Starting BT /////////////
  30. }
  31.  
  32. void delayAndRead()
  33. {
  34. delay(50);
  35. while (BTserial.available())
  36. {
  37. char c = BTserial.read();
  38. Serial.print(c);
  39. }
  40. delay(800);
  41. }
  42.  
  43. void initHC05ToInq()
  44. {
  45. Serial.println(F("Starting BT module..."));
  46. BTserial.println("AT+CMODE=1"); // Enable connect to any device
  47. delayAndRead();
  48. BTserial.println("AT+ROLE=1"); // Set to master in order to enable scanning
  49. delayAndRead();
  50. BTserial.println("AT+INQM=1,100,12"); // RSSI, Max 10 devices, ~30s
  51. delayAndRead();
  52. BTserial.println("AT+CLASS=0"); // Disable COD filter
  53. delayAndRead();
  54. BTserial.println("AT+INIT"); // Init.
  55. delayAndRead();
  56. }
  57. Voind loop() {
  58. BTtotal = updateBTmodule();
  59. }
  60. void initMessage()
  61. {
  62. //send = "Bluetooth1;{\"state\":{\"reported\":{\"devices\":[";
  63. send = "B";
  64.  
  65. }
  66.  
  67. int updateBTmodule() {
  68. // Keep reading from HC-05 and send to Arduino Serial Monitor
  69. if (BTserial.available())
  70. {
  71. // Read character and append into buffer
  72. c = BTserial.read();
  73. lineBuffer[index] = c;
  74. index++;
  75. // When line ends
  76. if (c == '\n')
  77. {
  78. // Remove line end characters from buffer
  79. lineBuffer[index - 1] = 0; // \r\n
  80. // Reset buffer index for next line
  81. index = 0;
  82.  
  83. if (lineBuffer[0] == 'O' && lineBuffer[1] == 'K')
  84. {
  85. // Finish message
  86. //send += "]";
  87. // DEBUG / TODO actually send this message
  88. //if (BTresult > 0)
  89. //{
  90. //Serial.println(BTresult);
  91. //Serial.println(send);
  92. //}
  93. //else
  94. //{
  95. //Serial.println(BTresult);
  96. //Serial.println(send);
  97. //}
  98. // Restart INQ
  99. BTserial.println("AT+INQ");
  100. BTresult = 0;
  101. initMessage();
  102.  
  103. }
  104. else
  105. {
  106. capture = false;
  107. index2 = 0;
  108. for (index = 0; index < 30; index++)
  109. {
  110. if (!capture)
  111. {
  112. if (lineBuffer[index] == ':')
  113. {
  114. capture = true;
  115. }
  116. }
  117. else
  118. {
  119. subBuffer[index2] = lineBuffer[index];
  120. if (lineBuffer[index] == ',')
  121. {
  122. subBuffer[index2] = 0;
  123. break;
  124. }
  125. index2++;
  126. }
  127. }
  128. index = 0;
  129. // Add this line buffer
  130. String str((char *) subBuffer);
  131. if (send.indexOf(str) <= 0)
  132. {
  133. // If not first then add comma
  134. // if (BTresult > 0)
  135. //{
  136. // send += ",";
  137. //}
  138. //send += "\"";
  139. send += str;
  140. // send += "\"";
  141. // Keep count
  142. BTresult++;
  143. }
  144. }
  145. }
  146. }
  147.  
  148. return BTresult;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement