Search code examples
cazure-iot-hub

mxchip press button send signal to Azure IoT


Can someone share an example or provide some guidance how I can add that when a button in the mxchip IoT device is pressed the signal is sent to Azure IoT? I am using Azure's example code

I see the function in board_init.c that lights up the light:

weak void button_a_callback()
{

WIFI_LED_ON();
AZURE_LED_ON();
USER_LED_ON();
if (BUTTON_A_IS_PRESSED)
{
    val += 32;
    if (val > 2047)
        val = 2047;
    RGB_LED_SET_R(val);
    RGB_LED_SET_G(val);
    RGB_LED_SET_B(val);
}
//set_led_state(true); I'd like to add something like this

}

and in nx_client.c to set the led state (true or false) when the command is received from Azure IoT:

static void set_led_state(bool level)
{
if (level)
{
    printf("\tLED is turned ON\r\n");
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
}
else
{
    printf("\tLED is turned OFF\r\n");
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
}

How can I link these two functions, meaning from button_a_callback to call nx_client.c set_led_state function?

Thanks!


Solution

  • You can reference the functions of nx_clinet.c in board_inti.c program by including the nx_client.h header file in board_inti.h file. To do this add the following line in the board_inti.h file

    #include "nx_client.h"
    

    You would have to remove the key work static before the function set_led_state as it restricts the function from being accessed externally

    To avoid implicit declaration warning, declare the start_led_state function in nx_client.h before endif

    void set_led_state(bool level)
    

    Now you can call the set_led_state function from either button_a_callback or HAL_GPIO_EXTI_Callback code block

    You may want to refer the following links for any issues How to split a C program into multiple files? undefined reference error in VScode How to call the static function from another c file?