乐鑫技术交流
直播中

张玉珍

7年用户 1041经验值
私信 关注
[问答]

网络服务器卡在新客户端上的原因?

有时,网络服务器会卡在新客户端上,并且不会断开连接。
代码:全选#include
#include
Servo servo1;

int pinservo = D2;
int pulsomin = 1000;
int pulsomax = 2000;

const char* ssid = "Manuela";
const char* password = "manuel34068833";

int ledPin = D4;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  servo1.attach(pinservo,pulsomin,pulsomax);
  servo1.write(50);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(100);
  }

  // Read the first line of the request
  String request = client.readStringUntil('r');
  Serial.println(request);
  client.flush();

  // Match the request

  int value = LOW;
  if (request.indexOf("Abrir") != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
    servo1.write(39);
    delay(400);
    servo1.write(50);
  }
  if (request.indexOf("AbrirYcerrar") != -1){
    digitalWrite(ledPin, LOW);
    value = LOW;
    servo1.write(39);
    delay(400);
    servo1.write(50);
    delay(55000);
    servo1.write(39);
    delay(400);
    servo1.write(50);
  }



  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("");
  client.println("");
  client.println("");


  client.println("

");
  client.println("

Clickea ACA Para abrir el porton

");
  
  client.println("");

  delay(100);
  Serial.println("Client disconnected");
  Serial.println("");
}

更多回帖

发帖
×
20
完善资料,
赚取积分