Configuring onConnected and onDisconnected via Bluetooth + Argon

Greetings,

I’m connecting two Argons (one as central and one as peripheral). I know the devices are connected because I can see the peripheral device responding. However, I’m having issues using onConnected and onDisconnected (basically the LED doesn’t turn on and the Log statements don’t appear).

I looked through the docs and the forum, and the relevant code is below.

void setup() {
    Serial.begin();
    BLE.on();

#if SYSTEM_VERSION == SYSTEM_VERSION_v310
    // This is required with 3.1.0 only
    BLE.setScanPhy(BlePhy::BLE_PHYS_AUTO);
#endif

    pinMode(PIN_LED, OUTPUT);
    BLE.onConnected(onConnected, NULL);
    BLE.onDisconnected(onDisconnected, NULL);
}

void onConnected(const BlePeerDevice& peer, void* context) {
    Log.info("peer %s connected", (const char*)peer.address().toString());
    digitalWrite(PIN_LED, HIGH);
}

void onDisconnected(const BlePeerDevice& peer, void* context) {
    Log.info("peer %s disconnected", (const char*)peer.address().toString());
    digitalWrite(PIN_LED, LOW);
}

Am I missing something obvious?

Thanks
Rob

When I tested your peripheral code with nRF Connect the LED behaved as expected (when defining my own const int PIN_LED = D7 - are you missing that in your running code or have you just not copied it over to your post?).
However, the log statement doesn’t, because you are missing a log handler.
Add this to your definitions

SerialLogHandler logger(LOG_LEVEL_WARN, {{"app", LOG_LEVEL_ALL}});

If these tips don’t help:
Can you try to advertise your device and then check whether a it works with some other “central” device (e.g. via nRF Connect app).
When that works it may also be related to your central code.

Thanks @ScruffR for your response. I did have the const int PIN_LED and SerialLogHandler declarations but I had forgotten to paste them in.

I connected to the peripheral device with the Bluefruit app, and the LED did turn on and off when the app connects and disconnects. Even though the peripheral LED connect/disconnected worked with Bluefruit, I wondered if a PWM output I was using on pin D7 could interfere. When I moved that PWM output to another pin, the peripheral device LED connect/disconnect now works with both Bluefruit and the Argon central device! Thank you for your suggestion!

It does lead me to a related question. On the central device, I’d like to have the same connect/disconnect functionality with the on board LED–meaning when the central device connects to a peripheral device, the central device LED should turn on also.

When I use the put the above code in my central device, the on board LED doesn’t turn on when connected. However, the following code does work though

void loop() {
...
    if (peer.connected()) {
        digitalWrite(PIN_LED, HIGH);
    } else {
        digitalWrite(PIN_LED, LOW);
    }

Is this the best way to handle this in the central device? Is there not a callback approach like with the peripheral code?

Thanks
Rob

Can you elaborate on that or provide some code we could test that with?
When I set PIN_LED = D7 and use analogWrite() rather than digitalWrite() in the callback it works just the same.
So I cannot see a direct correlation between PWM on D7 and the callback not firing.

That's no surprise given the reference documents :wink:


and

The docs would suggest so

Given that, on a central device I'd put the digitalWrite(PIN_LED, HIGH) after the BLE.connect() call and only if (!peer.connected()) digitalWrite(PIN_LED, LOW) into loop(). Since a connection won't automagically establish without the central initiating it, I'd see little use in forcing the LED on over and over while the connection persists.

Thanks @ScruffR for all your help. You cleared about how BLE connections work.

As to the PWM issue on D7 I was having, I’m afraid I can’t really elaborate. After I applied your fixes, I can’t see find the replicate exact bug.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.