BLUNO距离监测项目

时间:2014-11-15 14:49:07

标签: bluetooth-lowenergy

我试图做一个BLUNO(Arduino UNO + BLE)将连接到iBeacon并利用检测到的RSSI的项目。

我已经通过AT命令在BLUNO和iBeacon之间建立了联系。当我用AT命令ping它时,我可以在Arduino IDE串行监视器中获得RSSI结果。 我现在的问题是通过Arduino草图发送这些AT命令。我知道我使用串行通信,但我的Serial.Available函数永远不会返回超过0。

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  Serial.print("+++\r\n");
  Serial.print("AT+RSSI=?\r\n");
 }
void loop(){
  if(Serial.available()){
  digitalWrite(13, HIGH);
  delay(5000);
  }
}

令我恼火的是,我可以将BLUNO连接到我的iPhone,并通过AT命令在串行监视器上获取RSSI。但上面的代码不起作用! 有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

我现在差不多完成了整个项目。

我在上一个代码中的错误是必须在AT命令之前完成的启动部分。正确的方法是

 Serial.begin(115200);    //Initiate the Serial comm
  Serial.print("+"); 
  Serial.print("+"); 
  Serial.print("+");   // Enter the AT mode
  delay(500); // Slow down and wait for connection establishment

而不是

 Serial.print("+++\r\n");

所以是的,剩下的就好了。请记住,在定位信标的准确性方面,这个BLE真的很糟糕。 RSSI读数保持波动,并且在堆栈溢出处使用简化方程计算的距离实际上是不可靠的。

所以,请记住这一点哟!

这是我的完整代码仅供参考。

// while the AT connection is active, the serial port between the pc and the arduino is occuipied.
// You can manipluate the data on arduino, but to display on the serial monitor you need to exit the AT mode
char Data[100];
char RAW[3];
int INDEX;
char Value = '-';
void setup() {
  pinMode(13, OUTPUT); // This the onboard LED
  pinMode(8, OUTPUT); // This is connected to the buzzer
  Serial.begin(115200);    //Initiate the Serial comm
  Serial.print("+"); 
  Serial.print("+"); 
  Serial.print("+");   // Enter the AT mode
  delay(500); // Slow down and wait for connectin establishment
 }


void loop(){
    Serial.println("AT+RSSI=?"); // Ask about the RSSI
 for(int x=0 ; Serial.available() > 0 ; x++ ){    // get the Enter AT mode words
    //delay(20); // Slow down for accuracy
    Data[x] = Serial.read(); // Read and store Data Byte by Byte
    if (Data[x] == Value ) // Look for the elemnt of the array that have "-" that's the start of the RSSI value
      {
        INDEX=x;
      }
  }
    //Serial.println("AT+EXIT");    
    RAW[0] = Data[INDEX]; // Copy the RSSI value to RAW Char array
    RAW[1] = Data[INDEX+1]; 
    RAW[2] = Data[INDEX+2]; 
    RAW[3] = Data[INDEX+3];
    int RSSI = atoi(RAW);  //Convert the Array to an integer
    //Serial.println(RSSI);
    //delay(200); // Give the program time to process. Serial Comm sucks
    double D = exp(((RSSI+60)/-10)); //Calculate the distance but this is VERY inaccurate
    //Serial.println(D);
    if (D>1.00) // If the device gets far, excute the following>>
    {
      digitalWrite(13, HIGH);
      digitalWrite(8, HIGH);
      delay(500);
      digitalWrite(13, LOW);
      digitalWrite(8, LOW);
      delay(500);
    }

}