Turn on LED when authorized phone is in range


#1

Hi,
I just got my Bluz DK and having a little hard time to get started.

I would like to turn on an LED when a - previously paired or somehow authorized phone - is in range and successfully auto-connects to bluz.

Hope you guys can point me in the right direction.

Thank you!
James


#2

Programming bluz is pretty simple. What you want to do is basically set it up in local mode (that way it won’t try and connect to the cloud when it connects to the smartphone) and then poll for the BLE state.

You can load the code from this example for bluz: http://docs.bluz.io/tutorials/local_communication/#programming-bluz

Then, in the loop, you can delete the code that checks the sendData flag and do something like this:

if (BLE.getState() == BLEState.BLE_CONNECTED && previousState == BLEState.BLE_ADVERTISING) {
  // turn on something
  previousState = BLEState.BLE_CONNECTED;
}

You just need to set the previousState in the setup or config.

From the app side, you now just need to connect and it will turn on. In the bluz app, you can turn on Automatic Reconnect from the iOS Settings app, then scroll down to bluz. This should allow the phone to reconnect automatically.

Note that in the example app I sent you to, you can get the bluz board back into cloud mode by holding D6 HIGH during boot. This can be used if you need to get the board back into automatic mode so you can reconnect to the cloud for reprogramming.


#3

thanks! I get the following error:

bluz.cpp:22:35: error: expected primary-expression before '.' token
         SYSTEM_MODE(AUTOMATIC);
                                   ^

bluz.cpp:22:78: error: expected primary-expression before '.' token
         SYSTEM_MODE(AUTOMATIC);
                                                                              ^

bluz.cpp:24:29: error: expected primary-expression before '.' token
 }
                             ^

my code:

#include "application.h"
SYSTEM_MODE(MANUAL);

bool sendData = false;
bool previousState;

void dataCallbackHandler(uint8_t *data, uint16_t length) {
    sendData = true;
}

void setup() {
    BLE.registerDataCallback(dataCallbackHandler);

    pinMode(D6, INPUT_PULLDOWN);
    if (digitalRead(D6) == HIGH) {
        SYSTEM_MODE(AUTOMATIC);
    }
}

void loop() {
    System.sleep(SLEEP_MODE_CPU);
    if (BLE.getState() == BLEState.BLE_CONNECTED && previousState == BLEState.BLE_ADVERTISING) {
  // turn on something 
    previousState = BLEState.BLE_CONNECTED;
}
}

#4

got the code to compile and flash, but the LED won’t light up

#include "application.h"
SYSTEM_MODE(MANUAL);

bool sendData = false;
bool previousState;
BLEState state = BLE.getState();

int LED = D7;


void dataCallbackHandler(uint8_t *data, uint16_t length) {
    sendData = true;
}

void setup() {
    BLE.registerDataCallback(dataCallbackHandler);
    
    pinMode(LED, OUTPUT);

    pinMode(D6, INPUT_PULLDOWN);
    if (digitalRead(D6) == HIGH) {
        SYSTEM_MODE(AUTOMATIC);
    }
}

void loop() {
    System.sleep(SLEEP_MODE_CPU);
    if (state == BLE_CONNECTED && previousState == BLE_ADVERTISING) {
        digitalWrite(LED, HIGH);
    previousState = BLE_CONNECTED;
}
}

#5

You need to move the BLE.getState() function into the if statement, right now it wouldn’t check it each iteration of the loop it is only checking the previously saved state. So it should look like this:

if (BLE.getState() == BLE_CONNECTED && previousState == BLE_ADVERTISING)

Also, you should set previousState, right now the initial value would be 0. So change the definition of previousState to this:

BLEState previousState = BLE_ADVERTISING;


#6

Thank you Eric! got it to work!


#7

@conman
Would you be willing to show your code?