This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How with NRF52840 to send a request on power-on

Greetings!

Сould you help me to solve the difficulty with your DK? I have an idea to connect two boards with Mesh Light Control and Mesh Light Switch, and have the Client board send a request to turn on the LED on the Server board when power is applied to the Client board.

In your example this is done with buttons 1 and 2, I was able to connect them using an app nrf Mesh on my phone. But then, as I understand it, I need to emulate a button press when the board is turned on. 

I see that this is done through the Generic OnOff Client, and there through the bt_mesh_onoff_cli_set_unack function, but I can't figure out the parameters. When i debugged, and when the button was pressed, execution stopped at model_handler.c 76 line, but then I can't do anything with these parameters.

  • Hi Denisov, 

    Have you got a look at the description of the function in gen_onoff.h : 

    /** @brief Set the OnOff state in the srv without requesting a response.
     *
     * @param[in] cli Client model to send on.
     * @param[in] ctx Message context, or NULL to use the configured publish
     * parameters.
     * @param[in] set New OnOff parameters to set. @p set::transition can either
     * point to a transition structure, or be left to NULL to use the default
     * transition parameters on the server.
     *
     * @retval 0 Successfully sent the message.
     * @retval -ENOTSUP A message context was not provided and publishing is not
     * supported.
     * @retval -EADDRNOTAVAIL A message context was not provided and publishing is
     * not configured.
     * @retval -EAGAIN The device has not been provisioned.
     */
    int bt_mesh_onoff_cli_set_unack(struct bt_mesh_onoff_cli *cli,
    				struct bt_mesh_msg_ctx *ctx,
    				const struct bt_mesh_onoff_set *set);

    So what you need to provide to the function is the client that you want to use to send the command. And the third parameter is the Generic on off command. 
    If you want to call this function when it's turned on, you can add an extra function to call it in side model_handler.c and then call the function inside bt_ready() for example , right before the printing of "Mesh initialized" . You may need to check if the device is provisioned or not, similar to what inside the button_handler_cb(). 

  • Hello!

    I tried to do it according to your advice. In model_handler.c I added code like this:

    if (!bt_mesh_is_provisioned()) {
    struct bt_mesh_onoff_set set = {
    .on_off = !buttons[0].status,
    };
    int err;
    
    err = bt_mesh_onoff_cli_set_unack(&buttons[0].client,
    NULL, &set.on_off);
    }
    
    for (int i = 0; i < 4; ++i) {
    if (!(pressed & changed & BIT(i))) {
    continue;
    }
    
    struct bt_mesh_onoff_set set = {
    .on_off = !buttons[i].status,
    };
    int err;


    After that I called that function in main.c, inside bt_ready(), and nothing has changed. I understand that there may be an error somewhere here, but I also had problems with the boards, which sometimes, without changes in the code, stop connecting to each other and the phone. So I would like to ask you what could be the reason for the boards to disconnect spontaneously, and how I can fix the code?

  • Hi, 

    Which hardware are you using to test ? Please try to test using on our DK. It's preferred to test mesh with nRF52 board as the nRF53 may not fully supported with mesh. 

    Please show how you called it inside bt_ready(). You should call it after bt_mesh_prov_enable() function. 
    Please try to send a fix dummy data first , you don't need to read the button inside that. 

  • Good afternoon!

    I apologize for the long answer, it was a business trip. I using a standard nrf52840DK, and version nrf connect SDK 1.4.2.

    So far I have not been able to solve the problem, so I tried to attach files with projects for both boards, perhaps you could help me.

     mesh.zip 

  • Hi,

    When you call an API please always check for the return error value. 
    Note that the provisioning setting and the model configuration is loaded via settings_load() call. By doing that the value of buttons[] array inside model_handler.c is restored. You should use the buttons[] array that declared in model_handler.c instead of declare your own buttons array in main.c which has no value at all. 

    What you can do is to make a function inside model_handler.c that call the function bt_mesh_onoff_cli_set_unack() and use the buttons[].clients inside that. Then in main.c you call that function after settings_load(). 

Related