Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. /*
  3. int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
  4. int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
  5.  
  6. char input;
  7.  
  8. SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
  9.  
  10. void setup()
  11. {
  12. Serial.begin(9600); // Begin the serial monitor at 9600bps
  13.  
  14. bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  15. delay(100); // Short delay, wait for the Mate to send back CMD
  16. bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  17. // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  18. bluetooth.begin(9600); // Start bluetooth serial at 9600
  19. }
  20.  
  21. void loop()
  22. {
  23. if(bluetooth.available()) // If the bluetooth sent any characters
  24. {
  25. // Send any characters the bluetooth prints to the serial monitor
  26. Serial.print((char)bluetooth.read());
  27. }
  28. if(Serial.available()) // If stuff was typed in the serial monitor
  29. {
  30. // Send any characters the Serial monitor prints to the bluetooth
  31. input = (char)Serial.read();
  32. bluetooth.print(input);
  33. Serial.print(input);
  34. }
  35. // and loop forever and ever!
  36. }
  37.  
  38. */
  39.  
  40.  
  41. /* Random key stuff to be tested and implemented */
  42. /* Random key stuff to be tested and implemented */
  43. /* Random key stuff to be tested and implemented */
  44. /* Random key stuff to be tested and implemented */
  45. /* Random key stuff to be tested and implemented */
  46. /* Random key stuff to be tested and implemented */
  47. /* Random key stuff to be tested and implemented */
  48. /* Random key stuff to be tested and implemented */
  49. /* Random key stuff to be tested and implemented */
  50. /* Random key stuff to be tested and implemented */
  51. /* Random key stuff to be tested and implemented */
  52. /* Random key stuff to be tested and implemented */
  53.  
  54.  
  55. ///* key stuff -- FOB will be client/master (Tries to connect)|| sever = slave, client = master.
  56. //TODO: 1. Pairing and unlock function, 2. Sleep mode?
  57.  
  58. int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
  59. int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
  60. char input;
  61. SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
  62. String *found[8];
  63. char inChar;
  64. int connected = 0;
  65. int authorized = 0;
  66.  
  67. void setup()
  68. {
  69. Serial.begin(9600); // Begin the serial monitor at 9600bps
  70.  
  71. bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  72. delay(100); // Short delay, wait for the Mate to send back CMD
  73. bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  74. // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  75. bluetooth.begin(9600); // Start bluetooth serial at 9600
  76. delay(2000);
  77. bluetooth.print("AT");
  78. delay(100);
  79. bluetooth.print("AT");
  80. delay(100);
  81. bluetooth.print("AT");
  82. delay(300);
  83. bluetooth.print("AT");
  84. delay(300);
  85. bluetooth.print("AT+IMME1");
  86. delay(50);
  87. bluetooth.print("AT+ROLE1");
  88. delay(50);
  89. bluetooth.print("AT+MODEL1");
  90. delay(50);
  91. bluetooth.print("AT+TYPE2");
  92. delay(50);
  93. bluetooth.print("AT+NOTI1");
  94. delay(50);
  95. bluetooth.print("AT+PASS123456");
  96. delay(50);
  97. bluetooth.print("AT+FILT0");
  98. delay(50);
  99. bluetooth.print("AT+ERASE");
  100. delay(50);
  101. bluetooth.print("AT+CLEAR");
  102. delay(50);
  103. bluetooth.print("AT+POWE0");
  104. delay(50);
  105. bluetooth.print("AT+NAMEKEY");
  106. delay(50);
  107. bluetooth.print("AT+RESET");
  108. delay(1300);
  109. bluetooth.print("AT+START");
  110. int i = 0;
  111. Serial.print("DONE THE FUCKIN SHIT");
  112. }
  113.  
  114. int authorize(int tries){
  115. if (tries < 5 && !authorized) {
  116. String inString = String("");
  117. delay(3000);
  118. bluetooth.print("ready.");
  119. delay(3000);
  120. while (bluetooth.available()) {
  121. inString += (char)bluetooth.read();
  122. }
  123. if (inString == "goahead") {
  124. delay(3000);
  125. bluetooth.print("unlock");
  126. delay(2000);
  127. inString = String("");
  128. delay(3000);
  129. while (bluetooth.available()) {
  130. inString += (char)bluetooth.read();
  131. }
  132. if (inString == "accepted") {
  133. authorized = 1;
  134. Serial.print("AUTHORIZED"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  135. return 1;
  136.  
  137. }
  138. else if (inString =="denied") {
  139. authorized = 0;
  140. authorize(tries+1);
  141. }
  142. }
  143. else {
  144. authorize(tries+1);
  145. }
  146. }
  147. return 1;
  148. }
  149.  
  150. int idle(){
  151. String inString = String("");
  152. if (authorized) {
  153. while (1) {
  154. if (bluetooth.available()) {
  155. inString += (char)bluetooth.read();
  156. }
  157. if (inString =="TooFar") {
  158. Serial.print("TooFar"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  159. bluetooth.print("AT+ERASE");
  160. delay(50);
  161. bluetooth.print("AT+CLEAR");
  162. delay(50);
  163. bluetooth.print("AT+RESET");
  164. delay(1000);
  165. connected = 0;
  166. authorized = 0;
  167. return 0;
  168. }
  169.  
  170. else if (inString =="OK+LOST") {
  171. Serial.print("LOST"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  172. connected = 0;
  173. authorized = 0;
  174. return 0;
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180.  
  181. int Connect(int Target, int attmpt) {
  182. String inString = String("");
  183. if (attmpt < 5) {
  184. authorized = 0;
  185. bluetooth.print("AT+CONN" + Target);
  186. int l = 0;
  187. inString = String(""); // now confirm that BT devices are connected
  188. while (l < 2000) {
  189. if (bluetooth.available()) {
  190. inString += (char)bluetooth.read();
  191. }
  192. l++;
  193. }
  194. if (inString == "OK+CONN"){
  195. connected = 1;
  196. authorize(0);
  197. Serial.print("Connected"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  198. idle();
  199. return 1;
  200. }
  201. if (inString == "OK+CONNN" || inString == "OK+CONNE") {
  202. connected = 0;
  203. authorized = 0;
  204. Connect(Target, attmpt+1);
  205. }
  206. else {
  207. connected = 0;
  208. authorized = 0;
  209. Connect(Target, attmpt+1);
  210. }
  211. }
  212. return 0;
  213. }
  214.  
  215.  
  216. void loop()
  217. {
  218. String inString = String("");
  219. int a = 0;
  220. while (a < 10 && !connected) {
  221. delay(4000);
  222. Serial.print("Loop, not connected ");
  223. authorized = 0;
  224. while (bluetooth.available()) {
  225. inChar = (char)bluetooth.read();
  226. if (inChar == '+'){
  227. //found[i] = &inString;
  228. inString = String("");
  229. }
  230. else {
  231. inString += inChar;
  232. }
  233. }
  234. if (inString == "CONN") {
  235. Serial.print("AuToConnected"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  236. connected = 1;
  237. authorize(0);
  238. idle();
  239. break;
  240. }
  241. a++;
  242. }
  243. if (!connected){
  244. bluetooth.print("AT+DISC?");
  245. bluetooth.flush();
  246. int i = 0;
  247. inString = String("");
  248. }
  249. else {
  250. idle();
  251. }
  252. int i = 0;
  253. inString = String("");
  254. while (!connected) { // forever scanning while loop until this thing times out.
  255. if (bluetooth.available()) {
  256. inChar = (char)bluetooth.read();
  257. if (inChar == '+'){
  258. found[i] = &inString;
  259. inString = String("");
  260. i++;
  261. }
  262. else {
  263. inString += inChar;
  264. }
  265. }
  266. if (inString == "DISCE"){ //end or timeout
  267. break;
  268. }
  269. }
  270. for (int j = 0; j++; j != i){
  271. // this part, we need to compare every found value (lock ID) with every stored lock ID.
  272. // if the lock IDs match, then send command to pair or connect.
  273. //NOTE: STORED ADDRESS VALUES MUST BE PREPENDED WITH "DISC:"
  274. if ((const char*)found[j] == "DISC:7C669D9A6180AT") {
  275. Serial.print("FOUNDFIRSTTIME"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
  276. Connect(j-2, 0); //since the first two results are garbage
  277. }
  278. }
  279. }
  280.  
  281.  
  282.  
  283. //*/
  284.  
  285. /* key stuff -- LOCK will be server (Broadcasts ID and waits for connections)|| sever = slave, client = master.
  286.  
  287. Remote: (ourfob) // CENTRAL (makes connections)
  288. AT+ROLE1
  289. AT+POWE3
  290. AT+MODE1
  291.  
  292. Fob: (ourlocks) // PERIFERAL (Takes connections)
  293. AT+ROLE0
  294. AT+POWE3
  295. AT+PIO11
  296. AT+MODE1
  297.  
  298. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement