Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ esp8266互联网交换机问题_C++_Switch Statement_Esp8266_Portforwarding - Fatal编程技术网

C++ esp8266互联网交换机问题

C++ esp8266互联网交换机问题,c++,switch-statement,esp8266,portforwarding,C++,Switch Statement,Esp8266,Portforwarding,我试图使门中继开关系统,我可以从任何地方通过端口转发操作 我找到了一个非常有用的指南和代码,我的程序基于此: 在将继电器置于高状态五秒钟后,我使代码稍微不同,以重置esp8266。这一切都很好,但当我通过多个设备访问端口时,esp什么也没做,只是在重置后再次工作 我试图在一个连接到en的客户端10秒后没有发送消息但它不工作后,发出if语句重置esp。我可能错过了一些非常简单的事情,如果有人能帮我,我将不胜感激 提前谢谢 亲切问候, 山丘 带有if语句的代码: #include <ESP8

我试图使门中继开关系统,我可以从任何地方通过端口转发操作

我找到了一个非常有用的指南和代码,我的程序基于此:

在将继电器置于高状态五秒钟后,我使代码稍微不同,以重置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(); 

  }
}
#包括
常量字符*ssid=“------”//键入您的ssid
const char*password=“----”//键入您的密码
无符号long-previousMillis=0;
常数长间隔=10000;
int-ledPin=2;//ESP8266的GPIO2
WiFiServer服务器(80)//服务端口
无效设置(){
序列号开始(115200);
延迟(10);
引脚模式(LED引脚,输出);
数字写入(ledPin,低电平);
//连接到WiFi网络
Serial.println();
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
//启动服务器
server.begin();
Serial.println(“服务器已启动”);
//打印IP地址
打印(“使用此URL连接:”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.println(“/”);
}
void循环()
{
无符号长电流毫秒=毫秒();
//检查客户端是否已连接
WiFiClient=server.available();
如果(!客户端)
{
返回;
}

如果(currentMillis-previousMillisWiFiServer是不知道HTTP协议的通用TCP服务器。 现代浏览器使用HTTP/1.1协议,默认情况下启用“保持活动”选项,所以浏览器本身不会关闭连接。 您需要手动关闭TCP连接,方法是在
client.println(“”;
这样,您就可以处理来自其他客户端的请求

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

下面是一个例子,我将如何解决切换继电器的任务

#包括
#包括
const char*ssid=“--------------”;
const char*password=“-----------------------”;
const short int LED_引脚=2;
bool led_为_开=假;
ESP8266Web服务器(80);
void flipLed(){
led灯亮起=!led灯亮起;
//这是用于led的,即集电极开路
数字写入(LED_引脚,LED_开启?低:高);
//当您需要在高输出水平下打开继电器时,这适用于继电器
//数字写入(LED_引脚,LED_开启?高:低);
sendHeader(“Location”、“/”、true);
server.send(302,“文本/普通”和“”);
}
void handleNotFound(){
String message=“找不到文件\n\n”;
消息+=“URI:”;
message+=server.uri();
消息+=“\n方法:”;
message+=(server.method()==HTTP\u GET)?“GET”:“POST”;
消息+=“\n消息:”;
message+=server.args();
消息+=“\n”;
对于(uint8_t i=0;i\
wifi信号电平:%d
\ 灯光为:%s
\ \ ", 小时,分钟%60,秒%60, WiFi.RSSI(), led灯是否亮?“亮”:“关” ); 发送(200,“文本/html”,响应); } 无效设置(){ 序列号开始(115200); WiFi.模式(WiFi_STA); WiFi.begin(ssid,密码); while(WiFi.waitForConnectResult()!=WL_已连接){ Serial.println(“连接失败!重新启动…”); 特别是重新启动(); } WiFi.hostname(“led ctrl”);//esp将使用它进行dhcp请求,WiFi路由器将在客户端列表中显示该名称 Serial.println(“就绪”); 串行打印(“IP地址:”); Serial.println(WiFi.localIP()); 引脚模式(LED_引脚,输出); 数字写入(LED_引脚,高); onNotFound服务器(handleNotFound); 服务器上(“/flip-led”,flipLed); server.on(“/”,handleRoot); server.begin(); } void循环(){ server.handleClient(); }
#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("");

}