使用 RSSI 与 HC05 模块和 Arduino 进行室内定位的源代码-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1677|回复: 2

使用 RSSI 与 HC05 模块和 Arduino 进行室内定位的源代码

[复制链接]
发表于 2021-9-13 13:16 | 显示全部楼层 |阅读模式
本帖最后由 z01228 于 2021-9-17 13:23 编辑

使用 RSSI 与 HC05 模块和 Arduino 进行室内定位的源代码
HC05_bluetooth_RSSI_localization-master.zip (16.9 KB, 下载次数: 9)

https://github.com/pasks87/HC05_bluetooth_RSSI_localization
  1. #include <SoftwareSerial.h>
  2. #include <time.h>
  3. //Define Pins
  4. #define RxD 11  //Receive Serial pin
  5. #define TxD 10  //Transmit Serial pin
  6. SoftwareSerial bt(RxD, TxD) ;
  7. //Define for Data
  8. char btData;
  9. String splitString(String str, String devider, int arrayNumber);
  10. String deviceAddress;
  11. String deviceClass;
  12. String deviceRSSI;
  13. String message;

  14. bool RSSIVisible = true;
  15. String atQuestEnd = "?\r\n";
  16. String atSetEnd = "\r\n";
  17. String atStart = "AT+";
  18. String Initialize = "INIT";
  19. String accesCode = "IAC=9E8B33";
  20. String deviceType = "CLASS=0";
  21. String searchRSSI = "INQM=1,1,20";
  22. //INQM=<Par1>,<Par2>,<Par3>   Par1: 0--inquiry mode standard/1--inquiry mode rssi
  23. //                            Par2: Numero max di device Bluetooth che rispondono
  24. //                            Par3: Tempo (1-48 : 1.28s to 61.44s)
  25. String receiveRSSI = "INQ";
  26. String roleIsMaster = "ROLE=1";
  27. String roleIsSlave = "ROLE=0";
  28. float n;
  29. #define BTkey 9
  30. void ATCmdSetup() {
  31.   bt.listen();
  32.   bt.print(atStart + Initialize + atSetEnd);    //initialize device
  33.   bt.print(atStart + accesCode + atSetEnd);    //defines the accesCode this device shares with the others
  34.   bt.print(atStart + deviceType + atSetEnd);    //defines device type
  35.   bt.print(atStart + searchRSSI + atSetEnd);    //Search Bluetooth with RSSI value
  36. }

  37. void filterCode() {
  38.   if (message.substring(0, 5) == "+INQ:") {
  39.     deviceAddress = splitString(message, ",", 0).substring(5);
  40.     deviceClass = splitString(message, ",", 1);
  41.     deviceRSSI = splitString(message, ",", 2);
  42.   }
  43.   message = "";
  44. }

  45. String splitString(String str, String devider, int arrayNumber) {
  46.   int previousDevider = 0;
  47.   int deviders = 1;
  48.   for (int i = 0; i < str.length(); i++) {
  49.     if (str.substring(i, i + devider.length()) == devider) {
  50.       deviders++;
  51.     }
  52.   }
  53.   int devideCounter = 0;
  54.   String devidedString[deviders + 1];
  55.   for (int i = 0; i < str.length(); i++) {
  56.     if (str.substring(i, i + devider.length()) == devider) {
  57.       if (devideCounter) {
  58.         devidedString[devideCounter] = str.substring(devidedString[devideCounter   - 1].length() + 1, i);
  59.       } else {
  60.         devidedString[0] = str.substring(0, i);
  61.       }
  62.       devideCounter++;
  63.       previousDevider = i + devider.length();
  64.     }
  65.   }
  66.   devidedString[devideCounter] = str.substring(previousDevider, str.length());
  67.   if (arrayNumber > deviders) {
  68.     return "ERROR; array number not found. Array number is to big.";
  69.   } else {
  70.     return devidedString[arrayNumber];
  71.   }
  72. }

  73. //Funzione che converte un numero esadecimale in decimale
  74. unsigned int hexToDec(String hexString) {
  75.   unsigned int decValue = 0;
  76.   int nextInt;
  77.   for (int i = 0; i < hexString.length() - 2; i++) {
  78.     nextInt = int(hexString.charAt(i));
  79.     if (nextInt >= 48 && nextInt <= 57)
  80.       nextInt = map(nextInt, 48, 57, 0, 9);
  81.     if (nextInt >= 65 && nextInt <= 70)
  82.       nextInt = map(nextInt, 65, 70, 10, 15);
  83.     if (nextInt >= 97 && nextInt <= 102)
  84.       nextInt = map(nextInt, 97, 102, 10, 15);

  85.     nextInt = constrain(nextInt, 0, 15);
  86.     decValue = (decValue * 16) + nextInt;
  87.   }
  88.   return decValue;
  89. }

  90. //Funzione per il calcolo della distanza
  91. void distanceRSSI() {
  92.   float tempo;
  93.   float rssi_dbm; //RSSI scansionato
  94.   float rssi_ref = -61; // RSSI ad 1 metro di distanza
  95.   float distanza; //Variabile usata per memorizzare i valori della distanza di ogni singolo beacon rispetto al nodo target
  96.   if (deviceAddress == "98D3:36:80EF77") {
  97.     rssi_dbm = hexToDec(deviceRSSI) - 65535; //complemento a due per determinare il valore RSSI in dBm
  98.     Serial.print(rssi_dbm);
  99.     Serial.print("  ");
  100.     n = (rssi_ref - rssi_dbm) / (10 * 1.73);
  101.     //Calcolo della distanza
  102.     distanza = (100) * (pow(10, n));
  103.     Serial.print(distanza);
  104.     tempo = millis();
  105.     Serial.print("  ");
  106.     Serial.println(tempo / 1000);
  107.   }
  108. }


  109. void setup() {
  110.   // define pin modes for tx, rx:
  111.   pinMode(RxD, INPUT);
  112.   pinMode(TxD, OUTPUT);
  113.   // Switch module to AT mode
  114.   pinMode(BTkey, OUTPUT);
  115.   digitalWrite(BTkey, HIGH);
  116.   Serial.begin(9600);
  117.   // Start the software serial - baud rate for AT mode is 38400
  118.   bt.begin(38400); // HC-05 default speed in AT command mode
  119.   ATCmdSetup();
  120.   Serial.flush();
  121. }

  122. void loop() {
  123.   // Keep reading from the HC-05 and send to the Arduino Serial Monitor
  124.   if (bt.available()) {
  125.     btData = bt.read();
  126.     message.concat(btData); // la concatenazione avviene in 0,654 secondi
  127.     if (btData == '\n') {
  128.       bt.print(atStart + receiveRSSI + atSetEnd);
  129.       filterCode();
  130.       if (RSSIVisible) {
  131.         distanceRSSI();
  132.       }
  133.     }
  134.   }
  135. }
复制代码


发表于 2022-2-24 03:18 | 显示全部楼层
实现这个程序需要几个蓝牙模块呀楼主
 楼主| 发表于 2022-2-28 16:03 | 显示全部楼层
这个准确性怎么样?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 09:26 , Processed in 0.174565 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表