Stm32l072 push button problem

Hi everyone,
I have an stm32l072 which I already connect to the things network and then I wanted to send information to TTN when ever I press the user button and also the led should blink.
the problem is that push button code works separately and ttn code also works without push button but as soon as I am adding the push button function to the code connection to the things network is getting lost.
can someone help me please?
thank you!

Hello,

you must provide more informations. Like your code or its simplest version where the problem occurs.

BR, Jan

1 Like

thank you for your reply! this is my main function:

int main(void)
{
// setup tracing
setup_trace();

// stores the status of a call to LoRaWAN protocol
lorawan_status_t retcode;
 
while(true){

// Initialize LoRaWAN stack
if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
    printf("\r\n LoRa initialization failed! \r\n");
    printf("No sandwich!\n");
    return -1;
}

printf("\r\n Mbed LoRaWANStack initialized \r\n");
printf("SANDWICH is not here");

// prepare application callbacks
callbacks.events = mbed::callback(lora_event_handler);
lorawan.add_app_callbacks(&callbacks);

// Set number of retries in case of CONFIRMED messages
if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
        != LORAWAN_STATUS_OK) {
    printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
    printf("SANDWICH!!!\n");
    return -1;
}

printf("\r\n CONFIRMED message retries : %d \r\n",
       CONFIRMED_MSG_RETRY_COUNTER);
printf("SANDWICH!\n");

// Enable adaptive data rate
if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
    printf("\r\n enable_adaptive_datarate failed! \r\n");
    return -1;
}

printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
printf("do u have sandwich\n?");

retcode = lorawan.connect();

if (retcode == LORAWAN_STATUS_OK ||
        retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
} else {
    printf("\r\n Connection error, code = %d \r\n", retcode);
    return -1;
}

printf("\r\n Connection - In Progress ...\r\n");


// make your event queue dispatching events forever

printf("press button for led\n");
if (userButton == 1 ) {  // button is pressed
     if  (!buttonDown) {  // a new button press
           led = !led;                 // toogle LED
             buttonDown = true;     // record that the button is now down so we don't count one press lots of times
            // wait_ms(10);              // ignore anything for 10ms, a very basic way to de-bounce the button. 
             
             } 
   }         
      else { // button isn't pressed
              buttonDown = false;
              printf("LED OFF \n ");
   }


   }


ev_queue.dispatch_forever();

return 0;

}

my program stops in" printf(ā€œ\r\n Connection - In Progress ā€¦\r\nā€);" so I dont see anything else in tera term but the push button in the board works.

Hello,

I am not sure but your problem seems to be related to the while loop what you added.
Until your code remain in the while loop, it can not reach the ev_queue.dispatch_forever();, that maybe freeze the program.
Check the documentation for the EventQueue - API references and tutorials | Mbed OS 6 Documentation how work with it.

BR, Jan

1 Like

thank you!