modified: .vscode/arduino.json

modified:   tcp_server/tcp_server.ino
master
YUNLEI 2022-09-04 14:47:05 +08:00
parent b9a97a7e5a
commit 5aad698179
2 changed files with 56 additions and 31 deletions

View File

@ -3,5 +3,5 @@
"board": "esp32:esp32:esp32", "board": "esp32:esp32:esp32",
"port": "COM4", "port": "COM4",
"programmer": "esp32:esptool", "programmer": "esp32:esptool",
"sketch": "tcp_client\\tcp_client.ino" "sketch": "tcp_server\\tcp_server.ino"
} }

View File

@ -1,47 +1,72 @@
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiAP.h> #include <WiFiAP.h>
#define PORT 9100 #define PORT 9100 //端口号
void SerialRe(void); void SerialRe(void);
uint8_t buffer[512]; uint8_t buffer[512]; //串口读取缓冲
const char* ssid = "yourssid"; const char* ssid = "101_lei";
const char* password = "12345678"; const char* password = "leiyun1314";
WiFiServer server(PORT); WiFiServer server(PORT);
WiFiClient client; WiFiClient client;
IPAddress myIP; IPAddress myIP;
void taskUartRe(void *parm);
void setup() { void setup() {
Serial.begin(921600);
Serial.setTimeout(1);
Serial.setRxBufferSize(512);
Serial.onReceive(SerialRe,true);
Serial.println(); Serial.println();
Serial.println("Configuring access point..."); Serial.begin(921600);
Serial.setRxBufferSize(512); //设置接收缓冲
/**WIFI热点AP模式**/
// Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open. // You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password); // WiFi.softAP(ssid, password);
myIP = WiFi.softAPIP(); // myIP = WiFi.softAPIP();
Serial.println("AP IP address: "); // Serial.println("AP IP address: ");
Serial.println(myIP); // Serial.println(myIP);
server.begin(); /**WIFI热点AP模式**/
WiFi.begin(ssid, password); //WIFI STA模式
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
/***查询ipsta模式**/
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/***查询ipsta模式**/
/**查询ipap模式**/
// myIP = WiFi.softAPIP();
// Serial.println("AP IP address: ");
// Serial.println(myIP);
/**查询ipap模式**/
server.begin(); //开启TCP服务端并且监听
xTaskCreate(taskUartRe,"taskUartRe",4096,NULL,1,NULL);
} }
void loop() { void loop() {
if(!client.connected()){ if(!client.connected()){ //查询对象client是否处于连接状态
myIP = WiFi.softAPIP(); // myIP = WiFi.softAPIP();
Serial.println("AP IP address: "); // Serial.println("AP IP address: ");
Serial.println(myIP); // Serial.println(myIP);
client = server.available(); Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client = server.available();
} }
delay(1000); delay(1000);
} }
void SerialRe(){ ///////////////////////////////////////////////
while(Serial.available()>0){ void taskUartRe(void *parm){
int cnt=Serial.available(); Serial.println("taskUartRe create...");
if(cnt<=512){ for(;;){
Serial.readBytes(buffer,cnt); while(Serial.available()>0){
if(client) client.write(buffer,cnt); int cnt=Serial.available();
}else { if(cnt<=512){
Serial.readBytes(buffer,512); Serial.readBytes(buffer,cnt);
if(client) client.write(buffer,512); if(client) client.write(buffer,cnt);
}else {
Serial.readBytes(buffer,512);
if(client) client.write(buffer,512);
}
} }
} }
} }