我正在尝试制作一个门继电器开关系统,该系统可以通过端口转发从任何地方进行操作。

我发现了一个非常有用的指南和代码,我在此程序中基于该指南和代码:
https://openhomeautomation.net/control-a-lamp-remotely-using-the-esp8266-wifi-chip
https://github.com/openhomeautomation/esp8266-relay

在将继电器置于高电平状态五秒钟后,我使代码稍有不同以重置esp8266。一切正常,但是当我通过多个设备访问该端口时,esp不再执行任何操作,仅在重置后才能工作。

我尝试在连接客户端10秒钟后未发送消息但无法正常工作后,通过if语句重置esp。我可能缺少了一些非常简单的内容,如果有人可以帮助我,我将不胜感激。

提前致谢!

亲切的问候,

希勒

带if语句的代码:

#include <ESP8266WiFi.h>

const char* ssid = "------";//type your ssid
const char* password = "-------";//type your password
unsigned long previousMillis = 0;
const long interval = 10000;

int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port

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

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 to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}

void loop()
{
unsigned long currentMillis = millis();

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


if (currentMillis - previousMillis <= interval)
{

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



// 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("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
Serial.println("door opening");
Serial.println("5");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("1");
delay(1000);
ESP.restart();
}

//Set ledPin according to the request
//digitalWrite(ledPin, value);

// 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("<!DOCTYPE HTML>");
client.println("<html>");

client.print("<p style='font-size:500%'>He ouwe boef!");


client.println("<br><br>");
client.println("<p style='font-size:500%'>click <a href=\"/LED=ON\">here</a> to open door.<br>");
client.println("</html>");

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}
else
{
 Serial.println("connection too long, disconnected");
 ESP.restart();

  }
}

没有代码
#include <ESP8266WiFi.h>

const char* ssid = "--------";//type your ssid
const char* password = "------------";//type your password



int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port

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

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 to connect: ");
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(1);



// 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("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
Serial.println("door open now");
Serial.println("5");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("1");
delay(1000);
ESP.restart();
}

//Set ledPin according to the request
//digitalWrite(ledPin, value);

// 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("<!DOCTYPE HTML>");
client.println("<html>");

client.print("<p style='font-size:500%'>He ouwe boef!");


client.println("<br><br>");
client.println("<p style='font-size:500%'>click <a href=\"/LED=ON\">here</a> to open door.<br>");
client.println("</html>");

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}

最佳答案

WiFiServer是不知道HTTP协议(protocol)的通用TCP服务器。
现代浏览器使用HTTP / 1.1协议(protocol),默认情况下启用了Keep-Alive选项,因此浏览器本身不会关闭连接。
您需要通过在client.close();行之后发出client.println("</html>");来手动关闭TCP连接
这样,您便可以处理来自其他客户端的请求。

但是我建议使用HTTP感知服务器(例如ESP8266WebServer)来简化代码。

这是一个示例,我将如何解决切换继电器的任务。

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "--------------";
const char* password = "----------------";

const short int LED_PIN = 2;

bool led_is_on = false;

ESP8266WebServer server(80);


void flipLed() {
  led_is_on = !led_is_on;

//this is for led , i.e. open collector
  digitalWrite(LED_PIN, led_is_on ? LOW : HIGH);

//this is for relay when you need to turn relay ON with High level of output
//  digitalWrite(LED_PIN, led_is_on ? HIGH : LOW);

  server.sendHeader("Location", "/", true);
  server.send( 302, "text/plain", "");
}


void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);
}

void handleRoot() {
  char response[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  snprintf(response, sizeof(response),
  "<html><body>\
    uptime: %02d:%02d:%02d<br>\
    wifi signal level: %d<br>\
    Light is : %s<br>\
    <a href=\"/flip-led\">flip</a>\
  </body></html>",
  hr, min % 60, sec % 60,
  WiFi.RSSI(),
  led_is_on?"ON":"OFF"
  );
  server.send(200, "text/html", response);
}


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

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    ESP.restart();
  }

  WiFi.hostname("led-ctrl"); //esp will use it for dhcp request and wifi router will display the name in clients list

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

  server.onNotFound(handleNotFound);
  server.on("/flip-led", flipLed);
  server.on("/", handleRoot);

  server.begin();

}


void loop() {

  server.handleClient();

}

关于c++ - esp8266互联网交换机问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60340632/

10-12 00:26