Bluetooth Proximity Sensor



  • Has anyone done a proximity sensor using bluetooth. I have ordered the parts and I am considering this code here and adapting it to something I can use in Vera.. but I really have no idea what I am doing.. I only ordered the parts yesterday so can't really kick this off till next week.

    Just thought i'd ask if anyone else accomplished this already..

    Goal:

    Detect my phone
    Tell Vera I am at home.

    I guess my initial idea is to have it find the phone and set some soft of binary switch in vera to say on/off home/away

    This is not my code... just what I am starting with...

    #include
    // The ID's I found during the set up,
    // change these value's to match your Bluetooth Devices
    char cell[] = "E4EC1064DAE4,5A0204";
    char laptop[] = "2C8158B16919,6E010C";
    unsigned long lastTime;
    unsigned long interval = 10000;
    void setup()
    {
        Serial.begin(115200);  // Begin the serial monitor at 9600bps
        Serial1.begin(115200);
        Serial1.println("---"); // make sure the device is not in command mode
        Serial.println("Started....");
    }
    
    void loop()
    {
        // Get current millis();
        unsigned long currentTime = millis();
        // Check if its already time for our process
        if((currentTime - lastTime) > interval) {
            lastTime = currentTime;
            // Enter command mode
            Serial1.print("$$$");
                    //Wait a bit for the module to respond
            delay(200);
                    // Start the inquiry scan
            Serial1.println("IN,2");
            delay(5000);
            int h = Serial1.available();
            if(h >= 42) { // we know there is a bluetooth device
                char *id = readSerial(h);
                char *str;
                while((str = strtok_r(id, "\n", &id)) != NULL) {
                    if(strncmp(str, cell,19) == 0) {
                        Serial.print("Found Fritz cellphone with id: ");
                        Serial.println(str);
                    } else if(strncmp(str,laptop,19) == 0) {
                        Serial.print("Found Fritz laptop with id: ");
                        Serial.println(str);
                    }
                }
            } else if(h <= 21) { // we know there is no bluetooth device
                Serial.println("No device found");
            }
            Serial1.println("---");
    
        }
        clearAll();
    }
    
    void clearAll() {
        for(int i = 0; i < Serial1.available(); i++) {
            Serial1.read();
        }
    }
    
    // a bit hacked...
    char* readSerial(int h) {
        char input;
        char buffer[100];
        if (Serial1.available() > 0) {
            int j = 0;
            for (int i = 0; i < h; i++) { // the id's start at char 21, se we copy from there
                input = (char)Serial1.read();
                if(i >= 21) {
                    buffer[j] = input;
                    buffer[j+1] = '\0';
                    j++;
                }
            }
            return buffer;
        } else {
            return "No Data";
        }
    }
    

  • Admin

    Welcome!

    Yes, I ordered a bluetooth module like this a couple of weeks ago. Hope to provide an example when it arrives.



  • @hek

    Cool.. i'll race you!!!
    However, I think you might win... 🙂

    PS - How do I do code blocks?


  • Admin

    @Konrad-Walsh

    Surround with ````
    Or indent lines with 4 spaces.



  • @hek
    Thanks



  • @hek
    Did you get anywhere with this? I am trying in vain.. the issue I see is that the BT module and the RF module are both serial outputs/inputs..
    struggling to get off the ground on this


  • Admin

    I accidentally feed the first module with reversed polarity. It did not like that very much. 😞

    The second module arrived this week, but I haven't hooked it up yet. Planned on using software serial and had a test sketch going something like this:

    /* $Id$
    
    Serial terminal for the HC-05 bluetooth module.
    Sets up BT module and allow user interaction with the device via the Arduino Serial Monitor.
    
    Wiring of BT HC-05 device to the Arduino
    RX -> 11
    TX -> 10
    KEY -> 9
    */
    
    #include <SoftwareSerial.h>
    
    // Bluetooth to Arduino pins.
    #define BTkey 9
    #define BTrxPin 10
    #define BTtxPin 11
    
    enum { SYNC, ASYNC };
    
    SoftwareSerial BTSerial(BTrxPin, BTtxPin);
    
    // Send a command to the bluetooth device.
    // mode ASYNC - don't want for the response
    // mode SYNC - wait for the response
    void sendBluetoothCommand(String cmd, uint8_t mode) {
      Serial.print(mode == SYNC ? "SYNC> " : "ASYNC> ");
      Serial.println(cmd);
    
      BTSerial.println(cmd);
      if (mode == ASYNC) return;
      
      char ch = 0;
      while (ch != '\n') {
        if (BTSerial.available()) {
          ch = BTSerial.read();
           Serial.write(ch);
        }
      }
    }
    
    void setup()
    {
       Serial.begin(57600);
       
      /////////////////////////////////////////////////////////
      // SETUP BLUETOOTH  
      // define pin modes for tx, rx:
      pinMode(BTrxPin, INPUT);
      pinMode(BTtxPin, OUTPUT);
      // Switch module to AT mode
      pinMode(BTkey, OUTPUT);
      digitalWrite(BTkey, HIGH);  
      BTSerial.begin(38400);
      sendBluetoothCommand("AT", SYNC);
      sendBluetoothCommand("AT+NAME=blueNode", SYNC);
      // AT+ROLE=p1
      // p1 - 0 Slave Role, 1 Master Role, 2 Slave-Loop role.
      sendBluetoothCommand("AT+ROLE=1", SYNC);
      // AT+INQM=p1,p2,p3
      // p1 - 0 inquire_mode_standad, 1 inquire_mode_rssi
      // p2 - maximum number of bluetooth devices response
      // p3 - the maximum of limited inquiring time. 1~48 (1.28s ~ 61.44s)
      //      15*1.28 = 19sec 
      sendBluetoothCommand("AT+INQM=0,5,15", SYNC);
    }
    
    void loop()
    {
      // Keep reading from HC-05 and send to Arduino Serial Monitor
      if (BTSerial.available())
        Serial.write(BTSerial.read());
    
      // Keep reading from Arduino Serial Monitor and send to HC-05
      if (Serial.available())
        BTSerial.write(Serial.read());
    }
    

    And.. the first module was of the wrong type (I think). Must support master-mode if I understand it correctly. The second one I bought was this type.



  • @hek
    Thanks - Sorry to hear you killed it.. Its why I alway buy two of everything.. one to kill/learn and one to use 🙂

    I have the HC-06
    I noticed you are using the KEY connection... I didn't connect that at all... I will give your code a whirl


  • Admin

    I think KEY must be used to access the AT-commands.



  • ah. that would explain why I cant get the module to return is device info



  • I think something similar can be achieved with Tasker(android) and wifi.

    Tasker detects when it connects to your home network, initiates command telling Vera "Honey, I'm Home!". My home WiFi is much more robust/extensive than any blutooth connection radius would be.

    Thoughts?


  • Admin

    @NotYetRated

    Probably easier but not as fun 🙂



  • @hek haha ohh no denying that 🙂

    The blutooth sensor could definitely come in useful for any number of situations, so it would definitely be good to have! I just think, specifically for "getting home" detection, WiFi may be more robust. Then again, so long as this sensor is placed at entrance to the driveway, or in the garage, distance should be a non-issue. 🙂


  • Hero Member

    The main advantage of BlueTooth would be its shorter range - if you have multiple of them and wanted to detect roughly where the phone was - especially if the modules had RSSI.



  • @Zeph Ahh, shorter range as a benefit, guess I had not considered it like that!



  • @NotYetRated On one side I agree as this is what I have done for the passed few years
    The problem is, this is unreliable..
    but this along with that will make a more robust system..

    See imagine.. I have one at each door...

    I know when I am in the sitting vs the kitchen vs the bedroom..

    now my squeezelite music system follows me.. the lights follow me... and more..- EG - click vol up on my phone adjusts the volume in that room alone....



  • @hek
    I have to wait for you to figure this one out... I have tried many methods but not making progress at all...

    Your code does work by the way.. It sends and receives the data without issue.. but getting it all repackaged for a MySensors plugin isn't happening for me..

    I have taken the binary switch recipe and I am using that so that it will turn on on/off based on the BT id it sees ..

    I might start a fresh tonight.. Ill post any updates if I make progress on it..


  • Admin

    I was thinking of just emulating a door-sensor. When BT detects someone in proximity it sends 1 and 0 when no-one is in range. Just like the binary switch you proposed..

    If you want to send in which device in range you could use VAR1-5 with the BT device identifier as payload (at some interval).



  • I have given up.. I cant get anywhere with this but I think its cause I bought hc-06.. Are you closer?


  • Admin

    Last time I tested with the new HC-05 I couldn't get it to enter AT mode. Will probably make another test when I have some time.



  • ok.. mine enters AT mode.. and I can get it to send and receive data.. I may as well keep plugging at it so



  • @Konrad-Walsh For what I know, the HC-06 is automatically configured to take in AT commands, so that shouldn't be a problem, although mine (that I got today) seems to be defective as I send it the AT command, but I'm getting no answer.



  • @hek When you say you inverted the polarity... are you talking about VCC and GRD? or Rx and TX?


  • Admin

    @PedroC88

    vcc/gnd



  • i am "semi" abandoned it.. I couldn't get it work at all...



  • i am "semi" abandoned it.. I couldn't get it work at all... but I cant say I fully grasp the syntax yet. Hopefully @HEK gets the notion...



  • @Konrad-Walsh At the end I got the HC06 to work, I'm finally understanding all of this, if you tell me what you're trying to accomplish and the code u have I might be able to help. Also the module you're using.



  • I really like this idea, but for it to be effective we need to measure the decibel level of the connection and not just connected/disconnected state. Is this possible?



  • @Øyvind-A.-Espnes I haven't tried it, but with the AT+INQM command you can specify wether you want the RSSI of the device when issuing the AT+INQ command. This document can better explain it.

    After that it should just be a matter of parsing the resulting string and extract the signal strength.



  • I was thinking about going this route, but until last weekend I was using Tasker and having it tell Vera that I am here when my phone connects to my Wifi. I bought a new phone and I can't get it to stay connected when it hasn't been used in a while, so I have to rethink...

    While a Mysensors option could be good, what I am going to do is use my many Android tablets that I have throughout my home and have them always searching my my mobile. With them all throughout my home, I won't have to worry about bluetooth range.

    Sorry it isn't a Mysensors option, but I thought I would share what I am going to do 🙂



  • Any luck with BT?



  • @hek

    Hi,

    Did you get anywhere with this? I would like to build a bluetooth sensor myself but since this is an old thread, I am not really sure if the market has been modernized a little with bluetooth devices..

    Thanks!


  • Admin

    Sorry, no. Realised modern phones BT goes into sleep mode which means they don't announce themselves for longer periods. Kind of made it meaningless for the application.


Log in to reply
 

Suggested Topics

20
Online

11.4k
Users

11.1k
Topics

112.7k
Posts