Abstract: In this paper, we use xiong Pai development board to access the cloud server through MQTT protocol by using AT instruction mode, and take Cosmic AIR724 as communication module (CAT.1).
This post uses bear Pie development board + Cosmic Air724 (Cat.1 module), access to the EMQ server built by myself
1. Preparation for experiment
1. Experimental environment
- An STM32 development board (bear pie is recommended) and data cable
- STM32CubeMX has been installed
- KeilMDK has been installed and the chip pack corresponding to STM32 development board has been imported (STM32L431RCT6 is used by Little Bear)
- A Cat.1 module (Uart interface, AT instruction) and dupont line
2. Target effect
- Create the project and configure the parameters through CubeMX
- Communication module is controlled by AT instruction through serial port 2
- Cat.1 Send the CORRESPONDING AT command to access the cloud server
- Complete data subscription and publication through MQTT protocol
Two, through CubeMX production MDK project
A. Chip selection
- Open CubeMX and enter the chip selection:
- Choose your own STM32 chip (i.e. STM32L431RCT6) :
B. Set the RCC of the clock source
- Change the system clock source
By default, the system clock uses the internal high speed clock (HSI). If you choose HSE, the clock is more accurate
- Set the port corresponding to the external clock
- Configuring the Clock Tree
The maximum system clock of STM32L431RCT6 can be 80MHz, we can configure it to the maximum
C. Parameter Settings (Port Settings)
1) Configure USART1
USART is used, the mode is asynchronous, the baud rate is 115200, and there is no hardware flow control
2) Configure UART2 and connect to Cat.1
Cat.1 module burning AT firmware, of course, also support Luat development, we in order to facilitate learning, is to AT instruction development
We use Uart2 for the bear. The lead pins for the bear are PA2->USART_TX and PA3->USART_RX
Other options, baud rate set to 9600, other default
3) Turn on the Uart2 interrupt and turn on the receive DMA
Open the interrupt
Open receive DMA
Finally, the generated code is OK
D. Engineering Settings
Some basic Settings, including project name, storage location, project environment, composition of various files in the project
E. Generate code
Third, write the corresponding code
1. Output redirection of serial port 1
We know that printf is a printing function that formats the output to stdout based on the string arguments passed in. We need to print printf to the serial port, just write an output function that mimics printf in the usart.c file
- Add header file
/* USER CODE BEGIN 0 / #include
#include
#include
/ USER CODE END 0 / write output function/USER CODE BEGIN 1 */ void UsartPrintf(UART_HandleTypeDef *huart, char *fmt,…) {
unsigned char UsartPrintfBuf[296]; va_list ap; unsigned char *pStr = UsartPrintfBuf;
va_start(ap, fmt); vsprintf((char *)UsartPrintfBuf, fmt, ap); // Format va_end(ap); while(*pStr ! = 0) { USART1->TDR = *pStr++; while((USART1->ISR & 0x40) == 0); Void UsartPrintf(UART_HandleTypeDef *huart, char FMT,…) ; // UsartPrintf(&huart1; ,”hello worldrn”); / USER CODE END 1 */
Note: Add your own code between begin and end
2. Main code of communication
We create two files, cat1.h and cat. c. Meanwhile, we improve the sending and receiving function of serial port 2 and add serial port callback function to ensure data integrity
A). Serial port callback function
Because after sending AT instruction, the received data needs to be processed; One side is not processing fast enough, so add a serial port callback function
Void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {if(huart->Instance == USART2) {if(huart->Instance == USART2) { If (usart2type.usartreclen >0) memcpy(&Usart2type.Usart2RecBuffer[Usart2type.UsartRecLen],Usart2type.Usart2DMARecBuffer,Usart2type.UsartDMARecLen); / / archived to the pending area Usart2type UsartRecLen + = Usart2type. UsartDMARecLen; } else { memcpy(Usart2type.Usart2RecBuffer,Usart2type.Usart2DMARecBuffer,Usart2type.UsartDMARecLen); / / archived to the pending area Usart2type UsartRecLen = Usart2type. UsartDMARecLen; } memset(Usart2type.Usart2DMARecBuffer, 0x00, sizeof(Usart2type.Usart2DMARecBuffer)); Usart2type.UsartRecFlag = 1; }}
B). Serial port 2 is idle
An idle interrupt occurs when a set of data is sent or received
Function as follows
void USART2_IRQHandler(void) { /* USER CODE BEGIN USART2_IRQn 0 / if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE) ==SET) // Trigger idle interrupt {uint16_t temp = 0; __HAL_UART_CLEAR_IDLEFLAG(&huart2); // Clear serial port 2 idle interrupt flag HAL_UART_DMAStop(&huart2); // disable DMA temp = huart2.Instance->ISR; F0 ISR temp = huart2.Instance->RDR; Temp = hdma_usart2_rx.Instance->CNDTR; temp = hdma_usart2_rx. Not the number of data transmission in / / get the DMA Usart2type. UsartDMARecLen = USART2_DMA_REC_SIE – temp; HAL_UART_RxCpltCallback(&huart2); // USER CODE END USART2_IRQn 0 / HAL_UART_IRQHandler(&huart2); / USER CODE BEGIN USART2_IRQn 1 */ HAL_UART_Receive_DMA(&huart2,Usart2type.Usart2DMARecBuffer,USART2_DMA_REC_SIE); // re-open DMA reception
/* USER CODE END USART2_IRQn 1 */ }
C).at instruction communication process
The main AT instructions are as follows
TsATCmds ATCmds [] = {{” ATrn “, “OK”, 200, NO_REC, 10}, / / test {” AT + CIMIrn “, “4600”, 200, NO_REC, 10}, / / mobile Numbers {” AT + CSQrn “, “+ CSQ”, 200, NO_REC, 10}, {/ / signal AT + CEREG?” Rn, “” + CEREG: 0, 1″, 200, NO_REC, 10}, {/ / network “AT + CGATT? Rn “,”+CGATT:1”,200,NO_REC,10}, // AT {“AT+CCLK? Rn, “” + CCLK”, 200, NO_REC, 10}, / / time {” AT + MCONFIG = 0001, feel, 2020 rn) “, “OK”, 200, NO_REC, 10}, / / / / the MQTT parameters {” device1 AT + MCONFIG = 111, 123456 rn) “, “OK”, 200, NO_REC, 100}, {” AT + MDISCONNECTrn “, “”, 200, NO_REC, 5}, {“AT+MIPSTART=123.56.117.8, 1883RN “,”CONNECT OK”,200,NO_REC,5}, TCP connection to / / {” AT + MCONNECT = 1, 60 rn) “, “CONNACK OK”, 200, NO_REC, 5}, an MQTT connection to / / {” AT + MSUB = “, “SUBACK”, 200, NO_REC, 5}, {/ / sub theme “AT + MPUB =”, “OK”, 200, NO_REC, 5}, Posting AT / / {” AT + MQTTSTATUrn “, “+ MQTTSTATU”, 200, NO_REC, 5}, MCONNECT {“AT+RESETrn”,””,200,NO_REC,5}// restart};
Compile + download
Click compile,0 error,0 warning
Bear pie is connected to the computer and the code is downloaded to the development board
5. Connect hardware
1. Connect Cat.1 to Uart1_tx and Cat.1 to uart2_rx. Gnd connect Gnd;
Note: be sure to connect to GND, common ground is required.
2. The debugging
At this point, we debug for the last time; Use bear serial port 1 as serial port printing, open serial port debugging assistant, and check the running status (printf() prints corresponding information after each instruction is sent).
The attachment includes CubeMX project and MDK project
The next step is to connect the device to Huawei cloud IoTDA for better device management
Click to follow, the first time to learn about Huawei cloud fresh technology ~