You Reply: Code:
char* mac[] = {"0x50658397CDC6", "FakeMac2", "FakeMac1"}; //6 is the correct number, 5 is not. It should not connect on 2nd or 3rd element
unsigned long previousMillis = 0;
const long interval = 5000; // interval at which to awitch the connection (milliseconds), at least 10 seconds
int linkTo = 0; //MAC address variable
int LEDno = 0; //LED output variable
char Data[100];
char RAW[3]; // RSSI array
int INDEX;
char Value = '-'; //RSSI beginning character
int count = 1; //initialize cycle counter.
void setup() {
Serial.begin(115200);
pinMode(2,OUTPUT); //set port as OUTPUT
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(13, OUTPUT);
delay(100);
}
void loop() {
LEDno = linkTo **;
unsigned long currentMillis = millis(); // get current time
if (currentMillis - previousMillis >= interval) { //Do this every 10 seconds
previousMillis = currentMillis; //Set last ttime ran
Serial.print("Connection Attempt No:"); // show cycle no.
Serial.println(count);
delay(50);
Bind(mac[linkTo++], LEDno);
delay(50);
if(linkTo > 3) linkTo = 0;
}
}
void Bind(char* Mac, int LEDno){
digitalWrite(13, HIGH);
delay(100);
Serial.println("AT+EXIT"); // Make sure we're out of AT MODE
delay(100);
Serial.println("Begin AT Bind");
Serial.println();
Serial.print("Attempting to connect to MAC: ");
Serial.println(Mac);
delay(100);
Serial.print("+++");// Enter AT mode of itself
delay(1000);
Serial.print("AT+BIND=");
Serial.println(Mac); // BIND to specific MAC
delay(1000);
Serial.println("AT+RSSI=?"); // Ask about the RSSI, Check connection to specified MAC
for(int x=0 ; Serial.available() > 0 ; x++ ){ // get the Enter AT mode words
delay(100); // Slow down for accuracy
Data[x] = Serial.read(); // Read and store Data Byte by Byte
delay(50);
if (Data[x] == Value ) // Look for the elemnt of the array that have "-" that's the start of the RSSI value
{
delay(50);
INDEX=x;
delay(50);
}
RAW[0] = Data[INDEX**]; // Copy the RSSI value to RAW Char array
delay(50);
RAW[1] = Data[INDEX**];
delay(50);
RAW[2] = Data[INDEX+3];
//RAW[3] = Data[INDEX+3];
}
delay(100);
Serial.println("AT+EXIT");
delay(100);
digitalWrite(13, LOW); //Get out of AT Mode
delay(100);
int RSSI = atoi(RAW); //Convert the Array to an integer
Serial.print("RSSI Value="); //Display RSSI
Serial.println(RSSI);
Serial.println("Begin LED Output");
if(RSSI != 0){ // If not connected it should be 0, If connected its non zero. 1 LED per MAC. Should blink LED 1, not LED 2 or 3.
digitalWrite(LEDno, HIGH);
delay(500);
digitalWrite(LEDno, LOW);
delay(500);
digitalWrite(LEDno, HIGH);
delay(500);
digitalWrite(LEDno, LOW);
delay(50);
RSSI = 0; //Reset RSSI just in case although as it restarts it should be rewritten anyway.
}
else{ // If RSSI is 0 aka no connection, rapid blink LED 1 to show no connection
digitalWrite(LEDno, HIGH);
delay(100);
digitalWrite(LEDno, LOW);
delay(100);
digitalWrite(LEDno, HIGH);
delay(100);
digitalWrite(LEDno, LOW);
}
delay(50);
Serial.println("End LED Output"); //Display showing end of function was reached.
Serial.println();
digitalWrite(13, LOW);
count++; //count inc
delay(1000);
}