ESP32 SimpleWiFiServer

源码路径:  C:\Program Files (x86)\Arduino\hardware\espressif\esp32\libraries\WiFi\examples\SimpleWiFiServer

 

#include 

static char* wifi_ssid = "Sudaroot_2.4G";    //WIFI名字
static char* wifi_passwd = "12345678";       //WIFI密码

WiFiServer server(80);                       //WIFI服务器,端口号为80

void setup(void)
{
	Serial.begin(115200);                    //串口初始化
	delay(1000);

	WiFi.begin(wifi_ssid, wifi_passwd);		//连接路由器

	while(WiFi.status() != WL_CONNECTED)	//等待连接成功	
	{
		delay(500);
		Serial.println(".");
	}
	
	Serial.println("WIFI Connected");
	Serial.printf("WIFI IP : ");
	Serial.println(WiFi.localIP());         //打印出wifi的IP

	server.begin();                         //开启WIFI Server
}



void loop(void)
{
	WiFiClient client = server.available(); //如果有client连接成功,返回非0
	
	if(client)
	{
		Serial.printf("New Client IP: ");
		Serial.println(client.remoteIP());  //打印client IP
		Serial.printf("Port: %d\r\n", client.remotePort());  //打印client Prot
		
		while(client.connected())        //判断client是否在线
		{
			if(client.available())       //client是否有数据
			{
				char c = client.read();  //读取一个字节数据
				client.write(c);         //client回显
				Serial.write(c);         //串口打印
				if(c == '\n')            //遇到 回车符号就断开client连接
				{
					client.stop();       //断开client连接
					Serial.println("Client Disconnected.");
				}
			}

			if(Serial.available())
			{
				char b = Serial.read();
				client.write(b);
				Serial.write(b); 
				if(b == '\n')
				{
					client.println("Received \n, Client Close.");
					Serial.println("Received \n, Client Close.");
					client.stop();				
				}				
			}
		}
	}
}

 

下载完代码后,连接家里的路由器,然后电脑肯定也在路由器的局域网内,使用网络调试助手等,连接wifi server的IP和Prot

 

通过板子的串口知道板子IP: WIFI IP : 192.168.0.100

 

用串口调试助手监听板子IP的端口为80的server端,并发送数据

 

 

ESP32 SimpleWiFiServer_第1张图片

ESP32 SimpleWiFiServer_第2张图片

 

如果要监听多个客户端的话,可以创建Freertos多线程。

 

 

你可能感兴趣的:(ESP32)