Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- /*
- int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
- int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
- char input;
- SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
- void setup()
- {
- Serial.begin(9600); // Begin the serial monitor at 9600bps
- bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
- delay(100); // Short delay, wait for the Mate to send back CMD
- bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
- // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
- bluetooth.begin(9600); // Start bluetooth serial at 9600
- }
- void loop()
- {
- if(bluetooth.available()) // If the bluetooth sent any characters
- {
- // Send any characters the bluetooth prints to the serial monitor
- Serial.print((char)bluetooth.read());
- }
- if(Serial.available()) // If stuff was typed in the serial monitor
- {
- // Send any characters the Serial monitor prints to the bluetooth
- input = (char)Serial.read();
- bluetooth.print(input);
- Serial.print(input);
- }
- // and loop forever and ever!
- }
- */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- /* Random key stuff to be tested and implemented */
- ///* key stuff -- FOB will be client/master (Tries to connect)|| sever = slave, client = master.
- //TODO: 1. Pairing and unlock function, 2. Sleep mode?
- int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
- int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
- char input;
- SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
- String *found[8];
- char inChar;
- int connected = 0;
- int authorized = 0;
- void setup()
- {
- Serial.begin(9600); // Begin the serial monitor at 9600bps
- bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
- delay(100); // Short delay, wait for the Mate to send back CMD
- bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
- // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
- bluetooth.begin(9600); // Start bluetooth serial at 9600
- delay(2000);
- bluetooth.print("AT");
- delay(100);
- bluetooth.print("AT");
- delay(100);
- bluetooth.print("AT");
- delay(300);
- bluetooth.print("AT");
- delay(300);
- bluetooth.print("AT+IMME1");
- delay(50);
- bluetooth.print("AT+ROLE1");
- delay(50);
- bluetooth.print("AT+MODEL1");
- delay(50);
- bluetooth.print("AT+TYPE2");
- delay(50);
- bluetooth.print("AT+NOTI1");
- delay(50);
- bluetooth.print("AT+PASS123456");
- delay(50);
- bluetooth.print("AT+FILT0");
- delay(50);
- bluetooth.print("AT+ERASE");
- delay(50);
- bluetooth.print("AT+CLEAR");
- delay(50);
- bluetooth.print("AT+POWE0");
- delay(50);
- bluetooth.print("AT+NAMEKEY");
- delay(50);
- bluetooth.print("AT+RESET");
- delay(1300);
- bluetooth.print("AT+START");
- int i = 0;
- Serial.print("DONE THE FUCKIN SHIT");
- }
- int authorize(int tries){
- if (tries < 5 && !authorized) {
- String inString = String("");
- delay(3000);
- bluetooth.print("ready.");
- delay(3000);
- while (bluetooth.available()) {
- inString += (char)bluetooth.read();
- }
- if (inString == "goahead") {
- delay(3000);
- bluetooth.print("unlock");
- delay(2000);
- inString = String("");
- delay(3000);
- while (bluetooth.available()) {
- inString += (char)bluetooth.read();
- }
- if (inString == "accepted") {
- authorized = 1;
- Serial.print("AUTHORIZED"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- return 1;
- }
- else if (inString =="denied") {
- authorized = 0;
- authorize(tries+1);
- }
- }
- else {
- authorize(tries+1);
- }
- }
- return 1;
- }
- int idle(){
- String inString = String("");
- if (authorized) {
- while (1) {
- if (bluetooth.available()) {
- inString += (char)bluetooth.read();
- }
- if (inString =="TooFar") {
- Serial.print("TooFar"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- bluetooth.print("AT+ERASE");
- delay(50);
- bluetooth.print("AT+CLEAR");
- delay(50);
- bluetooth.print("AT+RESET");
- delay(1000);
- connected = 0;
- authorized = 0;
- return 0;
- }
- else if (inString =="OK+LOST") {
- Serial.print("LOST"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- connected = 0;
- authorized = 0;
- return 0;
- }
- }
- }
- return 0;
- }
- int Connect(int Target, int attmpt) {
- String inString = String("");
- if (attmpt < 5) {
- authorized = 0;
- bluetooth.print("AT+CONN" + Target);
- int l = 0;
- inString = String(""); // now confirm that BT devices are connected
- while (l < 2000) {
- if (bluetooth.available()) {
- inString += (char)bluetooth.read();
- }
- l++;
- }
- if (inString == "OK+CONN"){
- connected = 1;
- authorize(0);
- Serial.print("Connected"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- idle();
- return 1;
- }
- if (inString == "OK+CONNN" || inString == "OK+CONNE") {
- connected = 0;
- authorized = 0;
- Connect(Target, attmpt+1);
- }
- else {
- connected = 0;
- authorized = 0;
- Connect(Target, attmpt+1);
- }
- }
- return 0;
- }
- void loop()
- {
- String inString = String("");
- int a = 0;
- while (a < 10 && !connected) {
- delay(4000);
- Serial.print("Loop, not connected ");
- authorized = 0;
- while (bluetooth.available()) {
- inChar = (char)bluetooth.read();
- if (inChar == '+'){
- //found[i] = &inString;
- inString = String("");
- }
- else {
- inString += inChar;
- }
- }
- if (inString == "CONN") {
- Serial.print("AuToConnected"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- connected = 1;
- authorize(0);
- idle();
- break;
- }
- a++;
- }
- if (!connected){
- bluetooth.print("AT+DISC?");
- bluetooth.flush();
- int i = 0;
- inString = String("");
- }
- else {
- idle();
- }
- int i = 0;
- inString = String("");
- while (!connected) { // forever scanning while loop until this thing times out.
- if (bluetooth.available()) {
- inChar = (char)bluetooth.read();
- if (inChar == '+'){
- found[i] = &inString;
- inString = String("");
- i++;
- }
- else {
- inString += inChar;
- }
- }
- if (inString == "DISCE"){ //end or timeout
- break;
- }
- }
- for (int j = 0; j++; j != i){
- // this part, we need to compare every found value (lock ID) with every stored lock ID.
- // if the lock IDs match, then send command to pair or connect.
- //NOTE: STORED ADDRESS VALUES MUST BE PREPENDED WITH "DISC:"
- if ((const char*)found[j] == "DISC:7C669D9A6180AT") {
- Serial.print("FOUNDFIRSTTIME"); //DEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGINGDEBUGGING
- Connect(j-2, 0); //since the first two results are garbage
- }
- }
- }
- //*/
- /* key stuff -- LOCK will be server (Broadcasts ID and waits for connections)|| sever = slave, client = master.
- Remote: (ourfob) // CENTRAL (makes connections)
- AT+ROLE1
- AT+POWE3
- AT+MODE1
- Fob: (ourlocks) // PERIFERAL (Takes connections)
- AT+ROLE0
- AT+POWE3
- AT+PIO11
- AT+MODE1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement