This is the API for utilizing the ESP8266 WiFi module on STM32F103VET6. Referenced from this CSDN article, modified for using it on STM32F103VET6.
The folder original
stores the original version of the code.
I am from Hong Kong University of Science and Technology and Iam currently working on a group project in the course "Introduction to Embedded Systems" (course code: ELEC3300) that requires using STM32F103VET6, and my group would like to utilize the ESP8266 on it. However it is so hard to find how to start the module.
- USART
- DMA
- Timer
- Connect B8 to 3.3V, and the USART pin to RX and TX (depends on which USART you are using, for example USART2 is using A2 as TX and A3 as RX, then connect A2 to RX and A3 to TX)
- Create a new project with CubeIDE (or other)
- Set High Speed Clock to "Crystal/Ceramic Resonator"
- Set the USART mode to "Asynchronous"
- Enable NVIC global interrupt for USART
- Enable DMA for USART RX
- Enable a timer
- Set the timer's clock source to "Internal Clock", prescalar to "72-1", counter period to "10000-1" (depends on your clock configuration, the formula is 1ms = ((1+PRESCALER)/72M)*(1+PERIOD))
- Under "Project Manager", enable "Generate peripheral initialization as a pair of '.c/.h' files per peripheral"
- Copy
wifi.h
andwifi.c
to folderInc
andSrc
respectively - Add the following code to USARTx_IRHandler, where x is your USART number
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
USER_UART_Handler();
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
HAL_UART_Receive_DMA(&huart2, Espdatatype.DMARecBuffer, DMA_REC_SIZE);
/* USER CODE END USART2_IRQn 1 */
}
- Include
wifi.h
tostm32f1xx.h
andmain.h
- Add the following function (you can place it in
main.c
):
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
static uint8_t count = 0;
if(htim == (&htim3)){
count++;
if(count >= 10){
recDataHandle();
count = 0;
}
}
}
- Add
wifiInit(CLIENT)
orwifiInit(SERVER)
tomain.cpp
, CLIENT means starting ESP8266 as client (station mode) and SERVER means starting ESP8266 as server (AP) - Modify
wifi.c
, change the UART/USART/timer to your configuration - Modify
wifi.h
according to your preferences - Done!
void sendData(char *userdata, uint16_t userLength);
sendData()
will send the data out after connecting to a server, userLength
is equivilent to strlen(userdata)
.
void onReceiveData();
onReceiveData()
will be called when data is received, the data is stored in Espdatatype.UserBuffer
, and the length of the data is stored in Espdatatype.UserRecLen