You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.2 KiB
120 lines
3.2 KiB
#include <Arduino.h> |
|
#line 1 "f:\\研发项目\\魔方还原机器人\\WIFI端\\WiFiTelnetToSerial\\WiFiTelnetToSerial.ino" |
|
#line 1 "f:\\研发项目\\魔方还原机器人\\WIFI端\\WiFiTelnetToSerial\\WiFiTelnetToSerial.ino" |
|
#include <ESP8266WiFi.h> |
|
#include <algorithm> |
|
#include <ESP8266WiFiMulti.h> |
|
|
|
#ifndef STASSID |
|
#define STASSID "111222333444" //WIFI名 |
|
#define STAPSK "1223334444" //WIFI密码 |
|
#endif |
|
|
|
#define BAUD_SERIAL 115200 // 串口波特率115200 |
|
|
|
#define RXBUFFERSIZE 1024 //设置串口接收的字节大小 |
|
|
|
|
|
//最多的连接个数 |
|
#define MAX_SRV_CLIENTS 2 |
|
|
|
const char* ssid = STASSID; //wifi名字 |
|
const char* password = STAPSK;//wifi密码 |
|
|
|
const int port = 30; //服务器端口30 |
|
|
|
WiFiServer server(port); //创建服务器对象 |
|
WiFiClient serverClients[MAX_SRV_CLIENTS]; //连接对象数组 |
|
|
|
ESP8266WiFiMulti WiFiMulti; |
|
|
|
|
|
// 初始化函数 |
|
#line 30 "f:\\研发项目\\魔方还原机器人\\WIFI端\\WiFiTelnetToSerial\\WiFiTelnetToSerial.ino" |
|
void setup(); |
|
#line 59 "f:\\研发项目\\魔方还原机器人\\WIFI端\\WiFiTelnetToSerial\\WiFiTelnetToSerial.ino" |
|
void loop(); |
|
#line 30 "f:\\研发项目\\魔方还原机器人\\WIFI端\\WiFiTelnetToSerial\\WiFiTelnetToSerial.ino" |
|
void setup() { |
|
Serial.begin(BAUD_SERIAL); // 串口初始化 |
|
Serial.setRxBufferSize(RXBUFFERSIZE); //串口接收的初始化 |
|
|
|
pinMode(LED_BUILTIN, OUTPUT); //配置LED灯 |
|
digitalWrite(LED_BUILTIN, HIGH); //LED置高 |
|
|
|
//设置客户端模式 |
|
WiFi.mode(WIFI_STA); |
|
//连接网络 |
|
WiFiMulti.addAP(ssid, password); |
|
|
|
// 等待连接WIFI |
|
while (WiFiMulti.run() != WL_CONNECTED) { |
|
Serial.print("."); |
|
delay(500); |
|
} |
|
Serial.println(""); |
|
Serial.println("WiFi connected"); |
|
Serial.println("IP address: "); |
|
Serial.println(WiFi.localIP()); //获取自身IP地址 |
|
delay(500); |
|
//启动服务器 |
|
server.begin(); |
|
server.setNoDelay(true); //关闭小包合并包模式并去除延时 |
|
|
|
} |
|
|
|
// loop函数 |
|
void loop() { |
|
//如果新的连接请求 |
|
if (server.hasClient()) |
|
{ |
|
int i = 0; |
|
for (i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
if (!serverClients[i]) //遍历连接 |
|
{ |
|
serverClients[i] = server.available();//建立新的连接 |
|
Serial.print("New:"); |
|
Serial.print(i+1); |
|
break; |
|
} |
|
} |
|
//如果连接数量已经达到最大 |
|
if (i == MAX_SRV_CLIENTS) |
|
{ |
|
WiFiClient client = server.available(); |
|
client.stop(); |
|
Serial.print("stop\n"); |
|
} |
|
} |
|
//检测客户端发送上来的信息 |
|
for (int i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
//如果该客户端处于连接状态 |
|
if(serverClients[i] && serverClients[i].connected()) |
|
{ |
|
while(serverClients[i].available()) |
|
{ |
|
uint8_t c = serverClients[i].read(); |
|
Serial.write(c); |
|
} |
|
} |
|
} |
|
// 如果串口接收到数据 |
|
if(Serial.available()) |
|
{ |
|
delay(10); |
|
size_t counti = Serial.available(); |
|
uint8_t sbuf[counti]; |
|
Serial.readBytes(sbuf, counti); |
|
for (int i = 0; i < MAX_SRV_CLIENTS; i++) |
|
{ |
|
// 如果客户端还处于连接状态 |
|
if(serverClients[i] && serverClients[i].connected()) |
|
{ |
|
serverClients[i].write(sbuf,counti); //将串口接收到的数据发送到各个客户端 |
|
} |
|
} |
|
} |
|
} |
|
|
|
|