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

The way for ssing buttons for creating menu and using mqtt for sending adc value

Hi dear community, 

I am currently using nRF52 DK which has 52832 on it. I am using it for my bachelor thesis project and want to create simple sensor network. I am using RPi as a base station and 2 nRFs as nodes. Up to now, I have made progress using Mqtt example of the last Sdk, then eliminate button press so node can handle connection and publishing without pressing button. Later on, I made my publisher, subscriber at the same time since it receives some message from the base station. I am publishing temperature value read by adc from my node. My problem came after that point, I would like to use onboard buttons to create menu on my node. For instance, menu has 4 options like show the temperature data, show how much time does the node is on etc. I have done successfully the menu but when I use it with my other features, the button interrupt is not triggered or if it is triggered the publishing activity halts until button interrupt is completed. As far as I have understood from the previous posts, it is because of the priority of interrupts. I try to change priorities but could not yield any result,  I want from my system to respond to my button while publishing without any blocking in each activity. Actually, I am so newbie, that is why I chose this project as my thesis project but now, I could not figure out to what idea I am supposed to use? Maybe Rtos etc. Could anyone direct me to an idea so I can search and learn by myself. For this question, I think that sharing my code is not necessary but if anyone needs, I will share it. 

Best regards,

Halil

Parents
  • Hello,

    I don't know exactly what your menu looks like? Is it a screen, or do you send some text strings over UART? Or over Mqtt?

    However, it does sound like you are on the right track. It sounds like one of your interrupts keeps blocking the other interrupts. 

    One way to go is using an RTOS, but to be honest, I don't think this is necessary. 

    Since all your actions are interrupt based, managing them by flags seems like a possible way to go.

    I believe the trick would be to have the button presses set a flag. Then you have to leave the button event handler, to allow other interrupts to happen. What you can do is to check this flag in your main loop, and then perform some action / function call from the main-priority. Since the main-priority is the lowest priority, it would still allow for new interrupts, such as those who were blocked.

    So in pseudo, it could be something like this:

    volatile uint8_t button_press;
    
    void my_button_handler(...)
    {
        button_press = button_pressed;
    }
    
    int main(void)
    {
        ...
        buttons_init(my_button_handler)
        ...
        while (1)
        {
            if (button_press)
            {
                uint8_t temp = button_pressed;
                button_pressed = 0; //prepare for new button interrupt before entering the menu thing.
                my_menu_function_call(temp);
            }
            pwr_mgmt(); // or whatever waiting function your project uses.
        }
    }

    Best regards,

    Edvin

Reply
  • Hello,

    I don't know exactly what your menu looks like? Is it a screen, or do you send some text strings over UART? Or over Mqtt?

    However, it does sound like you are on the right track. It sounds like one of your interrupts keeps blocking the other interrupts. 

    One way to go is using an RTOS, but to be honest, I don't think this is necessary. 

    Since all your actions are interrupt based, managing them by flags seems like a possible way to go.

    I believe the trick would be to have the button presses set a flag. Then you have to leave the button event handler, to allow other interrupts to happen. What you can do is to check this flag in your main loop, and then perform some action / function call from the main-priority. Since the main-priority is the lowest priority, it would still allow for new interrupts, such as those who were blocked.

    So in pseudo, it could be something like this:

    volatile uint8_t button_press;
    
    void my_button_handler(...)
    {
        button_press = button_pressed;
    }
    
    int main(void)
    {
        ...
        buttons_init(my_button_handler)
        ...
        while (1)
        {
            if (button_press)
            {
                uint8_t temp = button_pressed;
                button_pressed = 0; //prepare for new button interrupt before entering the menu thing.
                my_menu_function_call(temp);
            }
            pwr_mgmt(); // or whatever waiting function your project uses.
        }
    }

    Best regards,

    Edvin

Children
  • Dear Edvin, 

    Thank you for this great answer. My menu will be simple. It is 20x4 character lcd and will be 4 options at first. Then, the user will determine to choose one. Actually, Rtos would be a great advancement for my knowledge but it increases the complexity of my code and this is not a thing I desired for now. I will reconsider your great answer to integrate my system. 

    Best regards,

    Halil

Related