Disabling the advertising and scanning by a short press of a button and re-enabling it by pressing the same button again.

Hello Team, I hope you will be fine.

We are working on a project where we integrated multiple libraries from different projects. We are using soft devices too.  Now I want to put the device in sleep mode(all the other events should turn off except the RTC) by short pressing the button and returning back to normal operation by pressing again the same button. The problem is that we cannot use a simple system on sleep mode because we have the GPS Data which is 10 samples per second and will wake the device almost all the time and consume a lot of power. Similarly, we cannot use the system off mode as the device gets reset after that which we don't want. So I want to disable all the events like advertising, scanning, GPS etc. manually by short pressing a button and returning back to normal operation by pressing the same button again. I don't know how will I do it like disabling the advertising, scanning, GPS etc. manually and re-enabling by pressing the same button again. Simply I want the device to be OFF (Except the RTC) by pressing a button and ON by pressing the same button again. Any help regarding this will be highly appreciated. 

Kind Regards,

Sami Ul Haq

  • Hi,

    Which SDK are you using?

    The problem is that we cannot use a simple system on sleep mode because we have the GPS Data which is 10 samples per second and will wake the device almost all the time and consume a lot of power.

    I believe this is wrong. System ON is typically used for exactly these kinds of use cases, where the device sleeps for most of the time (in system ON mode) consuming  a few micro amps, and wakes up whenever there is work to do and goes back to sleep. For this reason, virtually all nRF applications use system ON low power mode as much as possible.

    we cannot use the system off mode as the device gets reset after that which we don't want.

    I agree. System OFF mode is not suitable in this case.

    Basically, system ON mode just means that the CPU enters idle mode. And that is done by most SDK examples, and it wakes up on RTC when there is an event that has been scheduled (for instance a radio event of some sort, or some other application event, like reading data from a GPS). A key point here is that the device is sleeping also while advertising, in between advertising events. It is not so that the CPU is active all the time doing nothing (this would be fundamentally the wrong approach for a low power design).

    Generally, it is your responsibility to stop whatever you are doing that consumes power before entering sleep mode. There are generally API calls for things, like stopping advertising. This depends on SDK, but if you are for instance using the nRF5 SDK you typically call sd_ble_gap_adv_stop() to stop advertising. And similar for other activities. Then entering sleep mode is done "automatically", by calling __WFE() in the main loop or idle thread. If you are using BLE in the nRF5 SDK, you typically call sd_app_evt_wait(), either directly or via a library. If you are not using it, you may call it directly yourself (refer to an example). If using the nRF Connect SDK, the idle thread will call __WFE() for you.

    If you want to wake up on a button press, you configure interrupt on a GPIO pin (see relevant example for the SDK you are using).

  • Hello Einar,

    Which SDK are you using?

    nRF5_SDK_17.0.2

    I believe this is wrong. System ON is typically used for exactly these kinds of use cases, where the device sleeps for most of the time (in system ON mode) consuming  a few micro amps, and wakes up whenever there is work to do and goes back to sleep. For this reason, virtually all nRF applications use system ON low power mode as much as possible.

    But I read in the power management tutorial that in System ON mode the device wakes on every event like advertising, scanning, RTC etc. which we don't want. We want that when we press a button and the device closes all its activities like scanning, advertising, GPS etc. (only RTC is running as we don't want that the device time to be disturbed). In short that we want that by pressing a button the device got shutdown artificially( like turning OFF a television from remote not from the power switch) and only RTC is running, So we have to manually close all the activities except RTC. 

    A key point here is that the device is sleeping also while advertising, in between advertising events. It is not so that the CPU is active all the time doing nothing (this would be fundamentally the wrong approach for a low power design).

    As you said the device will wake on the mentioned events and it will then perform events like Advertising, Scanning etc. and will consume a lot of power as the device is scanning continuously all the time.

    A key point here is that the device is sleeping also while advertising, in between advertising events. It is not so that the CPU is active all the time doing nothing (this would be fundamentally the wrong approach for a low power design).

    Yes, that's true that the device goes to sleep in between the advertising, but the advertising interval is short and more important there is scanning all the time which cannot be avoided and it will consume a lot of power.

    Generally, it is your responsibility to stop whatever you are doing that consumes power before entering sleep mode. There are generally API calls for things, like stopping advertising. This depends on SDK, but if you are for instance using the nRF5 SDK you typically call sd_ble_gap_adv_stop() to stop advertising. And similar for other activities. Then entering sleep mode is done "automatically", by calling __WFE() in the main loop or idle thread.

    Yes, sir, I used that API and it is stopping advertising, and nrf_ble_scan_stop(); stop scanning but once I connected to the peripheral like Polar H10 or central like mobile App then I was unable to disconnect from it by pressing a button. I want that when I press a button and the NRF52 got disconnected from peripheral and central and stop scanning and advertising as well. And if for example if I disconnect from the peripheral then I am unable to re-connect again by pressing the same button again for waking of the device. 

    Thanks and Regards,

    Sami Ul Haq

  • Sami Ul Haq said:
    But I read in the power management tutorial that in System ON mode the device wakes on every event like advertising, scanning, RTC etc. which we don't want. We want that when we press a button and the device closes all its activities like scanning, advertising, GPS etc. (only RTC is running as we don't want that the device time to be disturbed). In short that we want that by pressing a button the device got shutdown artificially( like turning OFF a television from remote not from the power switch) and only RTC is running, So we have to manually close all the activities except RTC. 

    What you are describing here is exactly what system ON mode is for. But you must yourself disable advertising, turn off the GPS, etc. When you stop everything, but RAM is still powered and RTC is running, that is system on low power mode.

    Sami Ul Haq said:
    Yes, that's true that the device goes to sleep in between the advertising, but the advertising interval is short and more important there is scanning all the time which cannot be avoided and it will consume a lot of power.

    The SoftDevice BLE will do what it is configured to do. Scanning and advertising can be avoided. If you don't want do do it for a period after a button press, then stop doing theseactivities when you get the button press.

    Sami Ul Haq said:
    Yes, sir, I used that API and it is stopping advertising, and nrf_ble_scan_stop(); stop scanning but once I connected to the peripheral like Polar H10 or central like mobile App then I was unable to disconnect from it by pressing a button.

    Stopping to scan is separate from stopping to advertise, which in turn is separate from disconnecting a connection. If you also want to disconnect an active connection upon a button press, you need to do that. For that, you use sd_ble_gap_disconnect().

    Sami Ul Haq said:
    And if for example if I disconnect from the peripheral then I am unable to re-connect again by pressing the same button again for waking of the device. 

    If you disconnected and stopped advertising etc. on the first button press and want to be able to connect again on the second button press that means you need to start connectable advertising on the second button press. So in this case, if you are using something similar to SDK examples which use the advertising library, you call ble_advertising_start() when you want to start advertising again. That way a central can connect. If you also implement central features and want to start scanning and connect, you do that. There is nothing special in this case, just that incidentally you use a button press to trigger this instead of some other event (or simply doing it at startup).

  • Hello Einar, Thank you for your precious reply.

    What you are describing here is exactly what system ON mode is for. But you must yourself disable advertising, turn off the GPS, etc. When you stop everything, but RAM is still powered and RTC is running, that is system on low power mode.

    Yes, sir, I knew that and the RTC is not a problem for us it should be ON.

    Stopping to scan is separate from stopping to advertise, which in turn is separate from disconnecting a connection. If you also want to disconnect an active connection upon a button press, you need to do that. For that, you use sd_ble_gap_disconnect().

    Yes sir, I just studied that today and understand the difference between  sd_ble_gap_adv_stop() and

    sd_ble_gap_disconnect(). I can now successfully stop scanning and disconnect the peripheral like the Polar H10 sensor by pressing a button but I am unable to reconnect it by long-pressing the same button. I am calling scan_init() and scan_start() after a long press but it does not re-connect the peripheral (Polar H10). I don't know why?

    One more problem is that I am unable to even disconnect the central like Mobile app by calling sd_ble_gap_disconnect() after a short pressing of a button. 

    Can you please help me with these problems.?

    If you disconnected and stopped advertising etc. on the first button press and want to be able to connect again on the second button press that means you need to start connectable advertising on the second button press. So in this case, if you are using something similar to SDK examples which use the advertising library, you call ble_advertising_start() when you want to start advertising again. That way a central can connect. If you also implement central features and want to start scanning and connect, you do that. There is nothing special in this case, just that incidentally you use a button press to trigger this instead of some other event (or simply doing it at startup).

    I am doing exactly the same but unable to re-connect the peripheral(Polar H10). And the mobile app is even not disconnected after calling the disconnect function.

    Kind Regards,

    Sami Ul Haq

  • Hi,

    Sami Ul Haq said:
    after a long press but it does not re-connect the peripheral (Polar H10). I don't know why?

    I suggest you debug / perhaps add some logging to se what is happening.

    Sami Ul Haq said:
    One more problem is that I am unable to even disconnect the central like Mobile app by calling sd_ble_gap_disconnect() after a short pressing of a button. 

    As long as you call sd_ble_gap_disconnect() with the correct connection handle, the connection will be disconnected. However, if you are using the advertising module it will automatically start advertise again upon disconnecting (see implementation of on_disconnected() in components\ble\ble_advertising\ble_advertising.c). So you you need to change the configuration to prevent that, by setting config.ble_adv_on_disconnect_disabled to true  in the ble_advertising_init_t instance you provide to the call to ble_advertising_init().

    Sami Ul Haq said:
    I am doing exactly the same but unable to re-connect the peripheral(Polar H10). And the mobile app is even not disconnected after calling the disconnect function.

    I see. In that case you need to debug to understand what is actually happening first.

Related