cancel
Showing results for 
Search instead for 
Did you mean: 

How to Control GPIO Pins with AWS Shadow

Alnur ELBERIER
Associate II
The following document will provide you with a brief overview of the basics of activating GPIO pins on the STM32U5 dev board to interact with a relay. This document will focus on the steps to configure the STM32U5 boards software to enable GPIO pins as well as the process for how to utilize AWS to test these GPIO pins.

1. User provided items

  • 5V relay.

  • Different devices that can be connected to the relay.


2. General requirements


3. Introduction

The STM32U5 IoT discovery kit is equipped with multiple GPIO ports, of which can be connected to and controlled by AWS Core through published JSON commands by utilization of MQTT tests. These ports can also be configured to be set to an active low or an active high manually through the AWS console.


4. Configuring the Project on the STM32CubeIDE for GPIO manipulation


To allow for GPIO modification for the U5 board that the certain files need to be configured/changed under the b_u585i_iot2a_ntz project.
Note: Errors can typically be ignored, as they will not affect the final build nor the capabilities of enabling and disabling GPIO pins on AWS.
Under Project Explorer, click on b_u585i_iot02a_ntzm, then click on Common->config->hw_defs.h to open the respective header file within the STM32CubeIDE.
1415.png
Once opened, the file within the project should look similar to the one depicted below.
1416.png
Note: The preconfigured GPIO pins are associated with certain buttons and LED’s on the U5 board and should not be manipulated without prior knowledge of their functionalities.
Then using the following pdf/link:
https://www.st.com/resource/en/user_manual/um2237-stm32cubeprogrammer-software-description-stmicroelectronics.pdf
decide on which GPIO pin(s)/port(s) that you wish to use. Taking note specifically of the STM32U585 pin assignment.
1417.png
Note: The GPIO pins and their respective ports are listed on pages 33-44, and that the following code sample makes utilization of the CN14 pins located on the back of the U5 board.
Using one of the listed ports, add in a define for the pin itself using the numerical of the STM32U585 pin and then another define for the specific port, which should be the letter just before the numerical of the STM32U585 pin. For example, if we wanted to use the GPIO pin associated with STM32LU585 pin PB6 we would write the following 2 defines as shown below.
1418.png
Note: If you wish to enable more than 1 GPIO pin, repeat the above process for each specific pin you wish to use, being careful not to overwrite any reserved pins, which are specific on their respective tables in the pdf.
Then after this, under the Project Explorer, click on b_u585i_iot02a_ntzm, then click on Common->sys->hal_init.c to open the respective compiler file within the STM32CubeIDE.
1419.png
Scroll down until you see a function defined as static void hw_gpio_init( void ), similar to what is shown below.
1420.png
Make sure that the proper GPIO_CLK (s) are enabled otherwise the GPIO will be unable to send the proper signaling. Then to initialize the GPIO follow the following steps:

  1. Copy the LED Outputs section, and paste a copy of it below the original section of code.

  2. Change .Pin to the respective pin you defined for previously in hw_def.h.

  3. Change any instance of GPIOH to the respective GPIO port as previously defined in hw_def.h

  4. Change both the LED_RED_PIN | LED_GREEN_Pin in HAL_GPIO_WritePin to the respective pin you defined in hw_def.h


1421.png
After defining the GPIO pins in hal_init.c, under the Project Explorer, click on b_u585i_iot02a_ntzm, then click on Common->app->shadow_device_task.c to open the respective compiler file within the STM32CubeIDE.
1422.png
Change the #define shadowexampleSHADOW_REPORTED_JSON to the desired JSON state that you wish to be reported to AWS.
1423.png
Note: #define shadowexampleSHADOW_REPORTED_JSON_LENGTH (sizeof(shadowexampleSHADOW_REPORTED_JSON) - 6) will need to have it is numerical value [shown in red] changed depending on how it is reformatted.
Add in the respective current and reported power on states similar to below for each of the respective GPIO pins that you defined.
1424.png
Scroll down until you see:
 static void prvIncomingPublishUpdateDeltaCallback(void *pvCtx, MQTTPublishInfo_t *pxPublishInfo)
And add the respective log warning as well as the log information and pin writes for the respective GPIO pins, which were defined previously in hw_def.h. Note that if more than 1 pin is added that lines 508 to 537 will be repeated for each respective pin.
1425.png
Then scroll down until you see:
static void prvIncomingPublishUpdateAcceptedCallback(void *pvCtx, MQTTPublishInfo_t *pxPublishInfo)
And add what is shown on lines 723 to 748 repeating them one after another for each respective GPIO pin, which was previously defined in hw_def.h.
1426.png
Finally, scroll down until you see void vShadowDeviceTask(void *pvParameters).
Add an HAL_GPIO_WritePin for each GPIO pin defined, under the xStatus = prvInitializeCtx(&xShadowCtx); code as shown below.
1427.png
After this update the snprinf command for each of the pins, which was defined according to their CurrentPowerOnState, similar to how it is shown below
1428.png
Finally, underneath the for(;;) make sure to add to the if statement a check to see whether the CurrentPowerOnState is not equal to the ReportedPowerOnState for each pin, which was added, adding an || statement between each pin, which is being checked. Then add the respective CurrentPowerOnState for each pin using LogInfo. Then format the snprintf command so that it can report to AWS similar to what is shown below.
1429.png
Build and run the project with the U5 board connected to reflash the board with the updated GPIO modification code, making sure that it is configured and provisioned correctly.
Note: The software should be fully formatted for GPIO modification by this point.


5. Enabling GPIO pins/activity on AWS


Log in to aws.amazon.com with the IAM user created before. On the AWS management console, click on Iot Core. Under Things shown below, click on the name of the thing you entered in the terminal prompt.
1430.png
Under the Things tab with the respective Thing selected, move to Device Shadow and click on the shadow listed for the Thing.
1431.png
Click on the tab titled “MQTT topics” and under this tab, click on the MQTT test client.
1433.png
In the MQTT test client, click on the tab titled “Publish to a Topic” and under the topic name type or select $aws/things/YOUR_DEVICE_NAME/shadow/update. Then in the Message payload type similar to what is shown below.
1434.png
To set a certain pin to on change the “0” string to a “1” and hit publish,
Note: This message payload needs to match the format of shadowexampleSHADOW_REPORTED_JSON with the main difference being to replace reported with desired.
Once published the respective GPIO pins are toggled off or on accordingly.
 


5. Next Steps

Control GPIO pins using Alexa Here
Version history
Last update:
‎2022-11-02 05:13 AM
Updated by: