http - Arduino 集成开发环境/ESP8266 : Why does a button have to be pressed twice to get the correct output?

标签 http arduino esp8266

我一直在自学 ESP8226 编程,特别是 ESP8226-12F。我有这个基于在线示例的脚本:

#include <ESP8266WiFi.h>

WiFiServer server(80); //Initialize the server on Port 80

void setup() {
  WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
  WiFi.begin("***","***"); // Provide the (SSID, password);
  Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
  IPAddress HTTPS_ServerIP= WiFi.localIP(); // Obtain the IP of the Server
  Serial.print("Server IP is: "); // Print the IP to the monitor window
  Serial.println(HTTPS_ServerIP);
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(WiFi.localIP());
  server.begin(); // Start the HTTP Server
}

void loop() {
  String status;
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("Somebody has connected :)");
  //Read what the browser has sent into a String class and print the request to the monitor
  String request = client.readString();
  Serial.println(request);
  // Handle the Request
  if (request.indexOf("/CLOSE") != -1) {
    Serial.println(request.indexOf("/CLOSE"));
    status = "CLOSED";
  }
  if (request.indexOf("/OPEN") != -1) {
    Serial.println(request.indexOf("/OPEN"));
    status = "OPEN";
  }
  Serial.println(status);
  // The HTML
  String s = "HTTP/1.1 200 OK";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE html><html><head><meta http-equiv=\"Content-Security-Policy\"   content=\"default-src 'self'\"> <meta name=\"referrer\" content=\"no-referrer\" /></head><body>";
  s += "<br><input type=\"button\" name=\"b1\" value=\"OPEN DOOR\" onclick=\"javascript:location.href='/OPEN'\">";
  s += "<br><input type=\"button\" name=\"b2\" value=\"CLOSE DOOR\" onclick=\"javascript:location.href='/CLOSE'\">";
  if(status == "OPEN") {
    s+= "<br><br><p>OPEN</p>";
  } else {
    s+= "<br><br><p>CLOSED</p>";
  }
  s += "</body></html>\n";
  client.flush(); //clear previous info in the stream
  client.print(s); // Send the response to the client
  delay(1);
  Serial.println("Client disonnected");

您会看到初始 HTTP 响应创建了两个按钮:OPEN DOOR 和 CLOSE DOOR。

我遇到的问题是必须按两次关闭按钮才能使草图输出“关闭”。

这是串行监视器的输出。

1:第一次浏览到http://10.0.1.92 :

Somebody has connected :)
GET / HTTP/1.1
Host: 10.0.1.92
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive

Client disonnected

2:按下打开按钮 http://10.0.1.92/OPEN并显示“OPEN”

Somebody has connected :)
GET /OPEN HTTP/1.1
Host: 10.0.1.92
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://10.0.1.92/


4  // This is value of request.indexOf("/CLOSE")
OPEN
Client disonnected

3: 按关闭 http://10.0.1.92/CLOSE并且仍然显示“OPEN”

Somebody has connected :)
GET /CLOSE HTTP/1.1
Host: 10.0.1.92
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://10.0.1.92/OPEN


4 
371 // No idea where this comes from
OPEN
Client disonnected

4:第二次按CLOSE,显示“CLOSED”:

Somebody has connected :)
GET /CLOSE HTTP/1.1
Host: 10.0.1.92
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://10.0.1.92/CLOSE


4
CLOSED
Client disonnected

为什么 CLOSE 按钮必须按两次才能显示“CLOSED”?这是逻辑问题,还是 HTTP 问题,还是两者的某种组合?

最佳答案

这是可疑的:

if(status == "OPEN"){
   s+= "<br><br><p>"+status+"</p>";
} else {
   s+= "<br><br><p>"+status+"</p>";
}

如果两个备选方案相同,if 语句是什么?

此外,Referer 显示三个不同的值,而根据您的设计,一个人会期望两个值。我想知道您最初在哪里设置这个二进制状态机的状态?

编辑:

所以 OP 已经更新了源代码。

if(status == "OPEN") {
    s+= "<br><br><p>OPEN</p>";
  } else {
    s+= "<br><br><p>CLOSED</p>";
  }

对比:

 if (request.indexOf("/CLOSE") != -1) {
    Serial.println(request.indexOf("/CLOSE"));
    status = "CLOSED";
  }
  if (request.indexOf("/OPEN") != -1) {
    Serial.println(request.indexOf("/OPEN"));
    status = "OPEN";
  }

您现在看到它们在逻辑上有何不同了吗?

这可能意味着当状态机启动时,由于它没有打开,所以它在顶部 block 中关闭但在底部 block 中没有关闭。按下下一个按钮将其更改为打开,然后第二次将其更改为关闭?这可能是正在发生的事情吗?

关于http - Arduino 集成开发环境/ESP8266 : Why does a button have to be pressed twice to get the correct output?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44655184/

相关文章:

http - 如何在nifi中使用invoke http来执行GET请求?

android - 在android应用程序的Onclick监听器方法之外调用方法

arduino - 尝试使用 Arduino Adafruit CC3000 Webclient 循环 http 请求

mysql - 使用 DNS 和 WiFi 将 Arduino 连接到远程 MySQL 数据库

c - 如何生成唯一的文件名?

linker - ESP8266:我该如何克服 "section ` .text' 不适合区域 `iram1_0_seg' 的问题?

使用 HTTP header 上传 PHP 文件

php - ERR_CONNECTION_RESET 与 PHP 脚本

google-chrome - HTTP PATCH 谓词支持状态

javascript - 在 Chrome 上的 Web Serial API 中获取连接设备的名称