STA 模式:此时ESP8266模块是终端,可连接室内路由、手机热点或者AP热点
模块连接AP热点,可与其进行双向数据通信
模块连接手机热点,可与手机上的网络调试助手通信
模块连接路由器,可以在当前网络下的PC或者手机的网络调试助手通信
也可以通过路由接入互联网,从而手机或电脑通过互联网实现对设备的远程控制
AP 模式: ESP8266的默认模式,此时模块作为热点,实现手机的直接连接、STA模块的直接连接或电脑直接与模块通信,实现局域网无线通信。
STA+AP 模式:两种模式的共存模式,( STA 模式) 即可以通过路由器连接到互联网,并通过互联网控制设备;( AP 模式)即作为 wifi 热点,其他 wifi 设备连接到模块。这样实现局域网和广域网的无缝切换,方便操作。
STA模式下进行TCP Client通信
1 #include <ESP8266WiFi.h> 2 3 char* ssid = "your_wifi"; 4 char* passwd = "your_passwd"; 5 const uint16_t port = 8089; 6 const char * host = "192.168.1.7"; // your_Serverip 7 WiFiClient client; 8 9 void setup() { 10 Serial.begin(115200); 11 WiFi.mode(WIFI_STA); 12 WiFi.begin(ssid, passwd); 13 14 Serial.println("connecting to router... "); 15 //等待wifi连接成功 16 while (WiFi.status() != WL_CONNECTED) { 17 Serial.print("."); 18 delay(500); 19 } 20 Serial.println(""); 21 Serial.print("WiFi connected, local IP address:"); 22 Serial.println(WiFi.localIP()); 23 24 delay(500); 25 Serial.print("connecting to "); 26 Serial.println(host); 27 if (!client.connect(host, port)) { 28 Serial.println("connection failed"); 29 Serial.println("wait 5 sec..."); 30 delay(5000); 31 return; 32 }else 33 { 34 Serial.println("connect to tcp server success."); 35 Serial.println("Send this data to tcp server"); 36 client.println(String("hello tcp server")); 37 } 38 } 39 40 void loop() { 41 //读取从tcp server返回到响应数据 42 String recv_data = client.readStringUntil(‘\r‘); 43 Serial.println(recv_data); 44 45 if (0 == recv_data.compareTo("exit")) 46 { 47 Serial.println("closing connection"); 48 client.stop(); 49 } 50 delay(200); 51 }
STA模式下进行TCP Server通信
1 #include <ESP8266WiFi.h> 2 #include <WiFiClient.h> 3 #include <WiFiServer.h> 4 #define CLIENTS_MAX_NUMS 4 5 /* Set these to your desired credentials. */ 6 const char *ssid = "your_wifi"; 7 const char *password = "your_passwd"; 8 WiFiServer server(8089); 9 WiFiClient serverClients[CLIENTS_MAX_NUMS]; 10 void setup() { 11 Serial.begin(115200); 12 WiFi.begin(ssid, password); 13 Serial.println("\r\nconnecting to router... "); 14 //等待wifi连接成功 15 while (WiFi.status() != WL_CONNECTED) { 16 Serial.print("."); 17 delay(500); 18 } 19 Serial.println(""); 20 Serial.print("WiFi connected, local IP address:"); 21 Serial.println(WiFi.localIP()); 22 23 delay(500); 24 Serial.println("Start tcp server..."); 25 server.begin(); 26 server.setNoDelay(true); 27 } 28 void loop() { 29 int i = 0; 30 if (server.hasClient()) 31 { 32 for (i = 0; i < CLIENTS_MAX_NUMS; i++) 33 { 34 if (!serverClients[i] || !serverClients[i].connected()) 35 { 36 if (serverClients[i]) 37 { 38 serverClients[i].stop(); 39 } 40 serverClients[i] = server.available(); 41 continue; 42 } 43 } 44 } 45 for (i = 0; i < CLIENTS_MAX_NUMS; i++) 46 { 47 if (serverClients[i] && serverClients[i].connected()) 48 { 49 if (serverClients[i].available()) 50 { 51 while (serverClients[i].available()) 52 { 53 String recv_data = serverClients[i].readStringUntil(‘\r‘); 54 Serial.println("recv data from tcp server:"); 55 Serial.println(recv_data); 56 // send back 57 serverClients[i].println(recv_data); 58 } 59 } 60 } 61 } 62 delay(50); 63 }
1 #include <ESP8266WiFi.h> 2 const char *ssid = "ESP8266 Website"; 3 const char *password = "12345678"; 4 WiFiServer server(80); 5 void setup() 6 { 7 Serial.begin(115200); 8 Serial.println(); 9 Serial.print("Setting soft-AP ... "); 10 11 IPAddress softLocal(192,168,1,1); 12 IPAddress softGateway(192,168,1,1); 13 IPAddress softSubnet(255,255,255,0); 14 15 WiFi.softAPConfig(softLocal, softGateway, softSubnet); 16 WiFi.softAP(ssid, password); 17 18 IPAddress myIP = WiFi.softAPIP(); 19 Serial.print("AP IP address: "); 20 Serial.println(myIP); 21 server.begin(); 22 Serial.printf("Web server started, open %s in a web browser\n", WiFi.softAPIP().toString().c_str()); 23 } 24 void loop() 25 { 26 WiFiClient client = server.available(); 27 if (client) 28 { 29 Serial.println("\n[Client connected]"); 30 while (client.connected()) 31 { 32 if (client.available()) 33 { 34 // String line = client.readStringUntil(13);// arduino换行符号 ascll码 13 35 String line = client.readStringUntil(‘\r‘); 36 Serial.println(line); 37 // wait for end of client‘s request, that is marked with an empty line 38 if (line.length() == 1 && line[0] == ‘\n‘){ 39 //返回响应内容 40 String htmlPage = String("HTTP/1.1 200 OK\r\n") + 41 "Content-Type: text/html\r\n" + 42 "Connection: close\r\n" + // the connection will be closed after completion of the response 43 "Refresh: 5\r\n" + // refresh the page automatically every 5 sec 44 "\r\n" + 45 "<!DOCTYPE HTML>" + 46 "<html><head><title>ESP8066 Website</title><link rel=‘icon‘ href=‘https://imuncle.github.io/images/avatar.jpg‘><meta charset=‘utf-8‘></head>" + 47 "<body><h1>Hello World!</h1><p>本网页由ESP8266模块展示</p><p>Designed by big_uncle</p></body>" + 48 "</html>" + 49 "\r\n"; 50 client.println(htmlPage); 51 break; 52 } 53 } 54 } 55 delay(500); 56 57 client.stop(); 58 Serial.println("[Client disonnected]"); 59 } 60 }