我为Arduino(Node MCU)编写wifi服务器。
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char *ssid = "mywifi"; // You will connect your phone to this Access Point
const char *pw = "qwerty123"; // and this is the password
IPAddress ip(192, 168, 0, 1); // From RoboRemo app, connect to this IP
IPAddress netmask(255, 255, 255, 0);
const int port = 9876; // and this port
WiFiServer server(port);
WiFiClient client;
char cmd[100]; // stores the command chars received from RoboRemo
int cmdIndex;
unsigned long lastCmdTime = 60000;
unsigned long aliveSentTime = 0;
void setup() {
delay(1000);
Serial.begin(115200);
WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
WiFi.softAP(ssid, pw); // configure ssid and password for softAP
server.begin(); // start TCP server
Serial.println("ESP8266 RC receiver 1.1 powered by RoboRemo");
Serial.println((String)"SSID: " + ssid + " PASS: " + pw);
Serial.println((String)"RoboRemo app must connect to " + ip.toString() + ":" + port);
}
void loop() {
if(!client.connected()) {
client = server.available();
return;
}
Serial.println("new client");
if(client.available()) {
char c = (char)client.read(); // read char from client (RoboRemo app)
Serial.println((String)c);
}
}
当我启动这台服务器时,它的工作很费劲。我从我的android normaly连接到它。然后我在我的android上打开telnet并尝试连接到服务器。但是在服务器端创建了许多客户端,并在创建连接丢失后。
当我启动服务器时,我有这个日志
SID: mywifi PASS: qwerty123
RoboRemo app must connect to 192.168.0.1:9876
当我尝试从telnet
连接到服务器时,我有这个日志:
SSID: mywifi PASS: qwerty123
RoboRemo app must connect to 192.168.0.1:9876
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
new client
.....
在android telnet
方面我有错误:
Error while receive from server: recvfron filed: ECONNRESET (Connection reset by peer)
答案 0 :(得分:0)
new client
跟踪不在正确的位置,当连接客户端时,loop
的每次执行都会打印出来。那么&#34;新客户&#34;痕迹印刷很多。
为了仅在服务器获取时打印new client
,您可以这样开始:
void loop() {
if(!client.connected()) {
client = server.available();
if(client.connected()) {
Serial.println("new client");
}
return;
}
if(client.available()) {
char c = (char)client.read(); // read char from client (RoboRemo app)
Serial.println((String)c);
}
}