I am using a STM32F411RE NUCLEO board to create a Zigbee mesh-network. I purchased a Zigbee Dev. Kit to understand the devices.
At the beginning i used the XCTU software from Digi to configure the devices. After all that worked fine for me, i tried to communicate with my NUCLEO board over UART with the Zigbee device.
To enter the AT command mode i have to send "+++" over the UART.
This is how CUBEMX generated my UART initialisation
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
I Wrote the first function to enter the AT command mode. In main.c i included my Header where my function is stored.
#include <XBEE_AT_CMD.h>
Inside the while loop i call my function like this, passing huart1.
while (1)
{
if( AT_enter_CMDmode(&huart1) == 1 )
{
printf("OK \n");
}
else
{
printf("failed entering CMD mode \n");
}
HAL_Delay(1000);
}
The Function it self looks like this. It simply sends "+++" to the Zigbee. After that i am receiving the answer.
int AT_enter_CMDmode(UART_HandleTypeDef *huart)
{
char receive[3];
HAL_UART_Transmit(huart,(uint8_t*)"+++", 3, 100);
HAL_UART_Receive(huart, (uint8_t*)receive, 3, 100);
if( strcmp(receive,OK) == 0 )
{
return 1;
}
else
{
return 0;
}
}
After i enter the AT command mode by sending "+++" the device is answering with an "OK". But this just works sometimes. I tried to apply some delays because in the Datasheet it says you should implement a guard time before and after entering the command mode. Sometimes i get an answer which is similar to the expectet "OK" but without an Carriage return at the end or two "O" and one "K". When i put a Breakpoint after HAL_UART_Receive(huart, (uint8_t*)receive, 3, 100);
I see different answers from the device.
Answer 1: "\rOK"
Answer 2: "\r"
Answer 3: "O"
Answer 4: "OOK"
the answer i am looking for is "OK\r"
It looks like there is an issue with the UART timing maybe? Did i passed the UART handler in the right way to the function? It feels like i am not reading the UART properly, but not sure.