我正在尝试做一个项目,其中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。但是上面的代码不起作用!有什么帮助吗?
发布于 2014-11-16 20:40:25
到目前为止,我几乎完成了整个项目。
我在最后一段代码中的错误是初始化部分必须在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);
}
}https://stackoverflow.com/questions/26947042
复制相似问题