2016-06-27 4 views
0

Я пишу wifi-сервер для Arduino (Node MCU).Arduino wifi server создал много клиентов и потерял соединение

#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); 
    } 

} 

Когда я запускаю этот сервер, он работает без проблем. Я подключаюсь к нему со своей обычной андроиды. Затем я открываю 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

Трассировка new client не в правильном месте, когда клиент подключен, каждое исполнение loop распечатает его. Затем трассировка «новый клиент» печатается много.
Для того, чтобы напечатать new client только тогда, когда сервер получить его, вы можете procced так:

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); 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^