tcp/tcp_server/tcp_server.ino

48 lines
1.1 KiB
C++

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define PORT 9100
void SerialRe(void);
uint8_t buffer[512];
const char* ssid = "yourssid";
const char* password = "12345678";
WiFiServer server(PORT);
WiFiClient client;
IPAddress myIP;
void setup() {
Serial.begin(921600);
Serial.setTimeout(1);
Serial.setRxBufferSize(512);
Serial.onReceive(SerialRe,true);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
myIP = WiFi.softAPIP();
Serial.println("AP IP address: ");
Serial.println(myIP);
server.begin();
}
void loop() {
if(!client.connected()){
myIP = WiFi.softAPIP();
Serial.println("AP IP address: ");
Serial.println(myIP);
client = server.available();
}
delay(1000);
}
void SerialRe(){
while(Serial.available()>0){
int cnt=Serial.available();
if(cnt<=512){
Serial.readBytes(buffer,cnt);
if(client) client.write(buffer,cnt);
}else {
Serial.readBytes(buffer,512);
if(client) client.write(buffer,512);
}
}
}