默认情况下,ESP8266 的 IP 地址在 AP 模式下是 192.168.4.1,以便其他设备连接到该 AP 时可以进行通信。
代码
#include"main.h"#include"stdio.h"#include"string.h"#include"wifi-ops.h"UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;voidSystemClock_Config(void);staticvoidMX_GPIO_Init(void);staticvoidMX_USART1_UART_Init(void);staticvoidMX_USART3_UART_Init(void);#ifdefESP_STATION/*当stm32收到 esp/服务器发来消息, 会自动执行该函数* data,len 表示收到的具体数据和长度*** 该函数是在 中断执行,不好执行耗时/睡眠工作* */int log_success_flags =0;//0-uninit 1-log 2-failedintesp_station_callback(char*data,int len){printf("recv from serv: %s\r\n",data);//登陆成功失败标志位if(strstr(data,"log success")){log_success_flags =1;}elseif(strstr( data,"log failed")){log_success_flags =2;}}#endif/* 希望手机发来消息,该函数 被调用, data len分别是对方发来的消息和长度*/intesp_ap_callback(char*data,int len){printf("recv from phone: %s\r\n",data);//cmd:wifiname=hyx,wifipasswd=8888888,usrname=xiaowang,passwd=123456;}voidHAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart,uint16_t Size){//other thing to dowifi_uart_prepare_idle(huart);//after recv success, wo must prepare uart for next time}int__io_putchar(int ch){HAL_UART_Transmit(&huart1,(unsignedchar*)&ch,1,1);return ch;}intmain(void){HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_USART1_UART_Init();MX_USART3_UART_Init();/* USER CODE BEGIN 2 */int ret;int linkid;//每个手机都可以连接ap,都会安排一个idchar sendbuf[128]={0};#ifdefESP_STATION//预处理uart3 for esp ...wifi_uart_prepare_idle(&huart3);ret =wifi_station_init(&huart3, esp_station_callback );if(ret <0){printf("%s-%d wifi_station_init err\r\n",__func__,__LINE__);return-35;}ret =wifi_station_join_ap(&huart3,"WANGQINGFA","1234567890");if(ret <0){printf("%s-%d wifi_station_join_ap err\r\n",__func__,__LINE__);return-35;}ret =wifi_station_tcp_connect(&huart3,"107.148.201.156",10001);if(ret <0){printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__);return-35;}/*登陆服务器 toServ:action=log,usrname=xiaowang,passwd=123456,devname=sml001; */snprintf(sendbuf,sizeof(sendbuf),"toServ:action=log,usrname=%s,passwd=%s,devname=sml001;","xiaowang","123456");ret =wifi_station_tcp_send_data(&huart3,sendbuf,strlen(sendbuf));if(ret <0){printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__);}for(int i=0;i<50;i++){if(log_success_flags ==2){printf("%s-%d log failed \r\n",__func__,__LINE__);return-23;}elseif(log_success_flags ==1){printf("%s-%d log success \r\n",__func__,__LINE__);break;}HAL_Delay(10);}#endifwifi_uart_prepare_idle(&huart3);ret =wifi_ap_init(&huart3, esp_ap_callback);if(ret <0){printf("%s-%d wifi_ap_init err\r\n",__func__,__LINE__);return-35;}ret =wifi_ap_set_args(&huart3,"zhangsan","12345678");if(ret <0){printf("%s-%d wifi_ap_set_args err\r\n",__func__,__LINE__);return-35;}linkid =wifi_ap_tcp_listen_and_wait_connect_timeout(&huart3,10001,5*60*1000);if(linkid <0){printf("%s-%d wifi_ap_tcp_listen_and_wait_connect_timeout err\r\n",__func__,__LINE__);return-35;}/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */strcpy(sendbuf,"cmd:hello phone,I have got you messgae,please reset it;");while(1){wifi_ap_tcp_send_data(&huart3, linkid , sendbuf,strlen(sendbuf));#ifdefESP_STATIONret =wifi_station_tcp_send_data(&huart3,sendbuf,strlen(sendbuf));if(ret <0){printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__);}#endifHAL_Delay(500);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */}/*** @brief System Clock Configuration* @retval None*/voidSystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct ={0};RCC_ClkInitTypeDef RCC_ClkInitStruct ={0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;if(HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|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;if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2)!= HAL_OK){Error_Handler();}}/*** @brief USART1 Initialization Function* @param None* @retval None*/staticvoidMX_USART1_UART_Init(void){/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate =115200;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();}}staticvoidMX_USART3_UART_Init(void){huart3.Instance = USART3;huart3.Init.BaudRate =115200;huart3.Init.WordLength = UART_WORDLENGTH_8B;huart3.Init.StopBits = UART_STOPBITS_1;huart3.Init.Parity = UART_PARITY_NONE;huart3.Init.Mode = UART_MODE_TX_RX;huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart3.Init.OverSampling = UART_OVERSAMPLING_16;if(HAL_UART_Init(&huart3)!= HAL_OK){Error_Handler();}/* USER CODE BEGIN USART3_Init 2 *//* USER CODE END USART3_Init 2 */}/*** @brief GPIO Initialization Function* @param None* @retval None*/staticvoidMX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct ={0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_RESET);/*Configure GPIO pins : PC6 PC7 PC8 */GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOC,&GPIO_InitStruct);}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/voidError_Handler(void){__disable_irq();while(1){}/* USER CODE END Error_Handler_Debug */}#ifdefUSE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/voidassert_failed(uint8_t*file,uint32_t line){/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */}#endif/* USE_FULL_ASSERT */
先看代码:
using UnityEngine;
using UnityEngine.UI;public class TestInput : MonoBehaviour
{[SerializeField]InputField inputField;void Start(){Debug.Log(inputField.text);Debug.Log(inputField.text.Length);Debug.Log(inputField.preferredWidth);Debug…
织物缺陷检测检测系统源码分享
[一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示]
1.研究背景与意义
项目参考AAAI Association for the Advancement of Artificial Intelligence
项目来源AACV Association for the Advancement of Computer Vis…