gpt4 book ai didi

c++ - esp8266网络开关问题

转载 作者:行者123 更新时间:2023-12-01 14:58:04 26 4
gpt4 key购买 nike

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

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

我使代码略有不同,以便在将继电器置于高状态五秒钟后重置 esp8266。一切正常,但是当我通过多个设备访问端口时,esp 什么也没做,只是在重置后再次工作。

我尝试在连接的客户端 en 在 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 是通用的 TCP 服务器,不支持 HTTP 协议(protocol)。现代浏览器使用默认启用 Keep-Alive 选项的 HTTP/1.1 协议(protocol),因此浏览器不会自行关闭连接。您需要通过发出 client.close(); 手动关闭 TCP 连接。在 client.println("</html>"); 行之后这样您就可以处理来自其他客户端的请求。

但我建议使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com