Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Bluetooth Proximity Sensor

Bluetooth Proximity Sensor

Scheduled Pinned Locked Moved Development
bluetoothpresence
33 Posts 9 Posters 32.0k Views 7 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Konrad Walsh
    wrote on last edited by Konrad Walsh
    #1

    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";
        }
    }
    
    1 Reply Last reply
    0
    • hekH Offline
      hekH Offline
      hek
      Admin
      wrote on last edited by hek
      #2

      Welcome!

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

      K 1 Reply Last reply
      1
      • hekH hek

        Welcome!

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

        K Offline
        K Offline
        Konrad Walsh
        wrote on last edited by
        #3

        @hek

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

        PS - How do I do code blocks?

        hekH 1 Reply Last reply
        0
        • K Konrad Walsh

          @hek

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

          PS - How do I do code blocks?

          hekH Offline
          hekH Offline
          hek
          Admin
          wrote on last edited by
          #4

          @Konrad-Walsh

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

          K 1 Reply Last reply
          1
          • hekH hek

            @Konrad-Walsh

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

            K Offline
            K Offline
            Konrad Walsh
            wrote on last edited by
            #5

            @hek
            Thanks

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Konrad Walsh
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by hek
                #7

                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.

                P 1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Konrad Walsh
                  wrote on last edited by
                  #8

                  @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

                  1 Reply Last reply
                  0
                  • hekH Offline
                    hekH Offline
                    hek
                    Admin
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Konrad Walsh
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NotYetRated
                        wrote on last edited by
                        #11

                        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?

                        hekH K 2 Replies Last reply
                        0
                        • N NotYetRated

                          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?

                          hekH Offline
                          hekH Offline
                          hek
                          Admin
                          wrote on last edited by
                          #12

                          @NotYetRated

                          Probably easier but not as fun :)

                          N 1 Reply Last reply
                          0
                          • hekH hek

                            @NotYetRated

                            Probably easier but not as fun :)

                            N Offline
                            N Offline
                            NotYetRated
                            wrote on last edited by
                            #13

                            @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. :)

                            1 Reply Last reply
                            0
                            • Z Offline
                              Z Offline
                              Zeph
                              Hero Member
                              wrote on last edited by
                              #14

                              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.

                              N 1 Reply Last reply
                              0
                              • Z Zeph

                                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.

                                N Offline
                                N Offline
                                NotYetRated
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • N NotYetRated

                                  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?

                                  K Offline
                                  K Offline
                                  Konrad Walsh
                                  wrote on last edited by Konrad Walsh
                                  #16

                                  @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....

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    Konrad Walsh
                                    wrote on last edited by Konrad Walsh
                                    #17

                                    @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..

                                    1 Reply Last reply
                                    0
                                    • hekH Offline
                                      hekH Offline
                                      hek
                                      Admin
                                      wrote on last edited by
                                      #18

                                      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).

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        Konrad Walsh
                                        wrote on last edited by
                                        #19

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

                                        P 1 Reply Last reply
                                        0
                                        • hekH Offline
                                          hekH Offline
                                          hek
                                          Admin
                                          wrote on last edited by
                                          #20

                                          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.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          11

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular