【STM32F413H Discovery Kit 试用】WiFi 连接
本文介绍了 STM32F413H Discovery Kit 开发套件关于 WiFi 通信的测试流程。
介绍
开发板板载 Inventek Systems 公司的 eS-WiFi Rev B 模组
模组官网:ISM43362-M3G-L44-E/U Serial-to-WiFi Module – Inventek Systems .
ISM43362-M3G-L44-E/U 是一款嵌入式 2.4 GHz Wi-Fi 模块。
Wi-Fi 模块硬件包括一个 Cypress CYW43362、一个集成天线或可选的外部天线,以及一个具有 SPI 或 UART 接口功能的 STM32F205 主机处理器。
Wi-Fi 模块不需要操作系统,并且具有集成的 TCP/IP 堆栈,只需要 AT 命令集即可为无线产品建立连接。该模块具有 14.5mm x 30mm 表面贴装基底面,并通过了 FCC、IC、CE 模块认证。
该产品面向低成本嵌入式无线应用,可实现快速、简单且经济高效的方法,为平台添加 Wi-Fi 连接。
模组功能
- 802.11 b/g/n 兼容 Broadcom MAC/基带/无线电设备
- 完全包含 TCP/IP 堆栈,最大限度地减少主机 CPU 需求
- 可通过 Cypress WICED SDK 的 Inventek IWIN AT 命令进行配置
- 主机接口:UART、SPI 高达 25 MHz
- 网络功能 ICMP ( Ping)、ARP、DHCP、TCP、UDP
- 低功耗运行(3.3V 电源),内置低功耗模式
- 安全 Wi-Fi 身份验证 WEP-128、WPA-PSK (TKIP)、WPA2-PSK
模组架构
WiFi 模组
模组对应的原理图如下
工程测试
使用 STM32CubeIDE 软件打开官方示例工程 STM32Cube_FW_F4_V1.28.0\Projects\STM32F413H-Discovery\Applications\WIFI\WiFi_Client_Server
;
打开 Application
- User
- main.c
文件,输入关键信息后构建工程并运行。
代码
1.输入可检索到的 WiFi 信息,包括 SSID 和 PASSWORD;
2.输入远程 TCP 服务端 IP 地址,如 192.168.253.4
#include "main.h"
#define SSID "xxx"
#define PASSWORD "xxxxxxx"
#define LCD_USE
#define CONNECTION_TRIAL_MAX 10
/* Private variables ---------------------------------------------------------*/
const uint8_t RemoteIP[] = {192,168,253, 4};
uint8_t RxData [500];
uint8_t TxData[] = "STM32 : Hello!\n";
uint16_t RxLen;
uint8_t MAC_Addr[6];
uint8_t IP_Addr[4];
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
int main(void)
{
int32_t Socket = -1;
uint16_t Datalen;
uint16_t Trials = CONNECTION_TRIAL_MAX;
#if defined (LCD_USE)
uint8_t modulestr[20] = {0};
#endif /* LCD_USE */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED4);
#if defined (LCD_USE)
BSP_LCD_InitEx(LCD_ORIENTATION_LANDSCAPE_ROT180);
BSP_LCD_DisplayOn();
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), (BSP_LCD_GetYSize() -200));
BSP_LCD_SetFont(&Font12);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_DisplayStringAt(10, 10, (uint8_t *)"ES-WIFI Module in TCP", CENTER_MODE );
BSP_LCD_DisplayStringAt(10, 23, (uint8_t *)"Client mode demonstration", CENTER_MODE );
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(4, (uint8_t *)"TCP Client Instructions:");
BSP_LCD_DisplayStringAtLine(6, (uint8_t *)"1-Make sure your Phone connected ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t *)"to the network that you configured");
BSP_LCD_DisplayStringAtLine(8, (uint8_t *)"using Configuration AccessPoint.");
BSP_LCD_DisplayStringAtLine(10,(uint8_t *)"2-Create a server by using the ");
BSP_LCD_DisplayStringAtLine(11,(uint8_t *)"android application TCP Server");
BSP_LCD_DisplayStringAtLine(12,(uint8_t *)"with port(8002).");
BSP_LCD_DisplayStringAtLine(14,(uint8_t *)"3-Get the Network Name or IP Address");
BSP_LCD_DisplayStringAtLine(15,(uint8_t *)"of your Android from the step 2.");
HAL_Delay(2000);
#endif /* LCD_USE */
/*Initialize WIFI module */
if(WIFI_Init() == WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), (BSP_LCD_GetYSize() -200));
BSP_LCD_SetFont(&Font12);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_DisplayStringAt(10, 10, (uint8_t *)"ES-WIFI Module in TCP", CENTER_MODE );
BSP_LCD_DisplayStringAt(10, 23, (uint8_t *)"Client mode demonstration", CENTER_MODE );
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(7, (uint8_t *)"ES-WIFI Module Initialized.");
#endif /* LCD_USE */
if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(9, (uint8_t *)"Es-wifi module MAC Address:");
sprintf((char*)modulestr,"%X:%X:%X:%X:%X:%X.", MAC_Addr[0],
MAC_Addr[1],
MAC_Addr[2],
MAC_Addr[3],
MAC_Addr[4],
MAC_Addr[5]);
BSP_LCD_DisplayStringAtLine(10, (uint8_t *) modulestr);
#endif /* LCD_USE */
}
else
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(9, (uint8_t *)"ERROR : CANNOT get MAC address");
#endif /* LCD_USE */
BSP_LED_On(LED4);
}
if( WIFI_Connect(SSID, PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(11, (uint8_t *)"Es-wifi module connected");
#endif /* LCD_USE */
if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_DisplayStringAtLine(12, (uint8_t *)"es-wifi module got IP Address :");
sprintf((char*)modulestr,"%d.%d.%d.%d", IP_Addr[0],
IP_Addr[1],
IP_Addr[2],
IP_Addr[3]);
BSP_LCD_DisplayStringAtLine(13, (uint8_t *) modulestr);
BSP_LCD_DisplayStringAtLine(14, (uint8_t *)"Trying to connect to Server:");
sprintf((char*)modulestr,"%d.%d.%d.%d:8002", RemoteIP[0],
RemoteIP[1],
RemoteIP[2],
RemoteIP[3]);
BSP_LCD_DisplayStringAtLine(15, (uint8_t *) modulestr);
#endif /* LCD_USE */
while (Trials--)
{
if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", (char *)RemoteIP, 8002, 0) == WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_DisplayStringAtLine(16, (uint8_t *)"TCP Connection opened successfully.");
#endif /* LCD_USE */
Socket = 0;
}
}
if(!Trials)
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(14, (uint8_t *)"ERROR : Cannot open Connection");
#endif /* LCD_USE */
BSP_LED_On(LED4);
}
}
else
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(12, (uint8_t *)"ERROR: es-wifi module CANNOT get IP address");
#endif /* LCD_USE */
BSP_LED_On(LED4);
}
}
else
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(11, (uint8_t *)"ERROR:es-wifi module NOT connected\n");
#endif /* LCD_USE */
BSP_LED_On(LED4);
}
}
else
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(7, (uint8_t *)"ERROR : WIFI Module cannot be initialized.\n");
#endif /* LCD_USE */
BSP_LED_On(LED4);
}
while(1)
{
if(Socket != -1)
{
if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen) == WIFI_STATUS_OK)
{
if(Datalen > 0)
{
if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen) != WIFI_STATUS_OK)
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(8, (uint8_t *)"ERROR : Failed to send Data.");
#endif /* LCD_USE */
}
}
}
else
{
#if defined (LCD_USE)
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(9, (uint8_t *)"ERROR : Failed to Receive Data.");
#endif /* LCD_USE */
}
}
}
}
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 200;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1);
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
if(ret != HAL_OK)
{
while(1);
}
}
测试
Android 应用可采用 WiFi 模组官方提供的软件 TCP Client and TCP Server Android Demo Application .
1.使用 LCD 屏
保持 #define LCD_USE
,打开 Android 客户端 TCP Socket 软件,在 TCP Server 中输入监听端口号 8002
;
编译工程并运行,将固件上传至开发板芯片,自动复位并运行程序,效果如下
若 WiFi 连接失败,则板载 LED4 红灯常亮。
2.不使用 LCD 屏
注释代码 //#define LCD_USE
,同样在 Android 客户端软件使用 TCP Server 服务器监听 8002
端口,实现网络通信。
总结
本文介绍了 STM32F413H Discovery kit 开发板实现 WiFi 网络通信的相关测试,分析了关键工程代码,并对测试方法和测试结果进行了必要的说明。