[mw_shl_code=cpp,true]/**********************************************************
* 文件: tcp-server.ino by 零知实验室(
www.lingzhilab.com)
* -^^- 零知开源,让电子制作变得更简单! -^^-
* 时间: 2019/06/17 14:12
* 说明:
************************************************************/
#include <ESP8266WiFi.h>
int port = 8888; //Port number
WiFiServer server(port);
//Server connect to WiFi Network
const char *ssid = "xx"; //Enter your wifi SSID
const char *password = "xx"; //Enter your wifi Password
int count=0;
// 复位或上电后运行一次:
void setup() {
//在这里加入初始化相关代码,只运行一次:
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //Connect to wifi
// Wait for connection
Serial.println("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("port:");
Serial.println(port);
server.begin();
}
//一直循环执行:
void loop() {
// 在这里加入主要程序代码,重复执行:
WiFiClient client = server.available();
if (client) {
if(client.connected())
{
Serial.println("Client Connected");
}
while(client.connected()){
while(client.available()>0){
// read data from the connected client
Serial.write(client.read());
}
//Send Data to connected client
while(Serial.available()>0)
{
client.write(Serial.read());
}
}
client.stop();
Serial.println("Client disconnected");
}
}[/mw_shl_code]