AP
建立WIFI,接受STA连接,串口数据和TCP互传
1#include <ESP8266WiFi.h> 2 3const char *ssid = "esp8266_666"; 4const char *password = "12345678"; 5WiFiServer server(8266); 6void setup() 7{ 8 Serial.begin(115200); 9 Serial.println(); 10 Serial.print("Setting soft-AP ... "); 11 12IPAddress softLocal(192,168,1,1); 13IPAddress softGateway(192,168,1,1); 14IPAddress softSubnet(255,255,255,0); 15 16WiFi.softAPConfig(softLocal, softGateway, softSubnet); 17WiFi.softAP(ssid, password); 18 19IPAddress myIP = WiFi.softAPIP(); 20 Serial.print("AP IP address: "); 21 Serial.println(myIP); 22 server.begin(); 23 Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); 24 25} 26 float roll, pitch, heading; 27void loop() 28{ 29 WiFiClient client = server.available(); 30 if (client) 31 { 32 Serial.println("\n[Client connected]"); 33 while (client.connected()) 34 { 35 36 // 将串口数据打印给TCP 37 if(Serial.available()){ 38 size_t len = Serial.available(); 39 uint8_t sbuf[len]; 40 Serial.readBytes(sbuf, len); 41 42 client.write(sbuf, len); 43 delay(1); 44 45 } 46 47 48 roll=roll+10; pitch=pitch+10; heading=heading+10; 49 Serial.print("Orientation: "); 50 Serial.print(heading); 51 Serial.print(" "); 52 Serial.print(pitch); 53 Serial.print(" "); 54 Serial.println(roll); 55 56 delay(1000); 57 58 // 将TCP数据打印给串口 59 if (client.available()) 60 { 61 // String line = client.readStringUntil(13);// arduino换行符号 ascll码 13 62 String line = client.readStringUntil('\r'); 63 Serial.println(line); 64 65 } 66 } 67 delay(1); 68 69 // client.stop(); 70 Serial.println("[Client disonnected]"); 71 } 72 73 }
STA
连接WIFI,连接AP,串口数据互传
1#include <ESP8266WiFi.h> 2 3const char* ssid = "esp8266_666"; 4const char* password = "12345678"; 5 6const char* host = "192.168.1.1"; 7 const int httpPort = 8266; 8 9IPAddress staticIP(192,168,1,22); 10IPAddress gateway(192,168,1,1); 11IPAddress subnet(255,255,255,0); 12 13WiFiClient client; 14 15 16void setup(void) 17{ 18 Serial.begin(115200); 19 Serial.println(); 20 21 Serial.printf("Connecting to %s\n", ssid); 22 WiFi.config(staticIP, gateway, subnet); 23 WiFi.begin(ssid, password); 24 while (WiFi.status() != WL_CONNECTED) 25 { 26 delay(500); 27 Serial.print("."); 28 } 29 Serial.println(); 30 Serial.print("Connected, IP address: "); 31 Serial.println(WiFi.localIP()); 32 33 34 35 if (!client.connect(host, httpPort)) { 36 Serial.println("connection failed"); 37 return; 38 } 39 40 41} 42 43void loop() { 44 45 // client.print("abc\r"); 46 // delay(1000); 47 while(client.available()){ 48 //int newLine = 13; 49 // String line = client.readStringUntil(13); 50 String line = client.readStringUntil('\r'); 51 Serial.println(line); 52 } 53 54 }
测试实例 发送 abc/秒,串口打印输出
1#include <ESP8266WiFi.h> 2 3const char* ssid = "esp8266_666"; 4const char* password = "12345678"; 5 6const char* host = "192.168.1.1"; 7 const int httpPort = 8266; 8 9IPAddress staticIP(192,168,1,22); 10IPAddress gateway(192,168,1,1); 11IPAddress subnet(255,255,255,0); 12 13WiFiClient client; 14 15 16void setup(void) 17{ 18 Serial.begin(115200); 19 Serial.println(); 20 21 Serial.printf("Connecting to %s\n", ssid); 22 WiFi.config(staticIP, gateway, subnet); 23 WiFi.begin(ssid, password); 24 while (WiFi.status() != WL_CONNECTED) 25 { 26 delay(500); 27 Serial.print("."); 28 } 29 Serial.println(); 30 Serial.print("Connected, IP address: "); 31 Serial.println(WiFi.localIP()); 32 33 34 35 if (!client.connect(host, httpPort)) { 36 Serial.println("connection failed"); 37 return; 38 } 39 40 41} 42 43void loop() { 44 45 46 47 48 client.print("abc\r"); 49 delay(1000); 50 while(client.available()){ 51 String line = client.readStringUntil('\r'); 52 Serial.print(line); 53 } 54 55 }