如何在同一代码中使用基于 Web 的代码和真实按钮?

Posted

技术标签:

【中文标题】如何在同一代码中使用基于 Web 的代码和真实按钮?【英文标题】:How can i use web based code and real button in the same code? 【发布时间】:2019-05-01 20:06:27 【问题描述】:

我在 Arduino IDE 中编写了代码。使用此代码,我可以在网页上控制 4 个 LED 的开关。在网页中的 9 个按钮。

2 个用于 LED-1 开关的按钮。 2 个用于 LED-2 开和关的按钮。 2 个用于 LED-3 开和关的按钮。 2 个用于 LED-4 开和关的按钮。 1 个按钮用于打开和关闭所有 LED。

我还想在电路中添加一个真正的按钮。当我按下这个按钮时,我想运行那个切换代码。我该怎么做?

这是我的代码;

#include <ESP8266WiFi.h>

const char* ssid = "UnderSpeed";
const char* password = "q1w2e3r4t5__";
const char* host = "192.168.1.185"; //it will tell you the IP once it starts up
                                        //just write it here afterwards and upload
int r0 = D0;
int r1 = D1;
int r2 = D2;
int r3 = D3;
int btn0 = D5;

WiFiServer server(301); //just pick any port number you like

void setup() 
  Serial.begin(115200);
  delay(10);
Serial.println(WiFi.localIP());
  // prepare GPIO2
  pinMode(r0, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(btn0, INPUT_PULLUP);
  digitalWrite(r0, HIGH);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);

  // 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.println(WiFi.localIP());


void loop() 
  // Check if a client has connected

  WiFiClient client = server.available();
  if (!client) 
    return;
  

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

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

  // Match the request
  if (req.indexOf("") != -10)   //checks if you're on the main page

    if (req.indexOf("/r0-OFF") != -1)  //checks if you clicked OFF
      digitalWrite(r0, HIGH);
      Serial.println("Relay-0 OFF");
    
    if (req.indexOf("/r0-ON") != -1)  //checks if you clicked ON
      digitalWrite(r0, LOW);
      Serial.println("Relay-0 ON");
    

    if (req.indexOf("/r1-OFF") != -1)  //checks if you clicked OFF
      digitalWrite(r1, HIGH);
      Serial.println("Relay-1 OFF");
    
    if (req.indexOf("/r1-ON") != -1)  //checks if you clicked ON
      digitalWrite(r1, LOW);
      Serial.println("Relay-1 ON");
    

    if (req.indexOf("/r2-OFF") != -1)  //checks if you clicked OFF
      digitalWrite(r2, HIGH);
      Serial.println("Relay-2 OFF");
    
    if (req.indexOf("/r2-ON") != -1)  //checks if you clicked ON
      digitalWrite(r2, LOW);
      Serial.println("Relay-2 ON");
    

    if (req.indexOf("/r3-OFF") != -1)  //checks if you clicked OFF
      digitalWrite(r3, HIGH);
      Serial.println("Relay-3 OFF");
    
    if (req.indexOf("/r3-ON") != -1)  //checks if you clicked ON
      digitalWrite(r3, LOW);
      Serial.println("Relay-3 ON");
    

    if (req.indexOf("/btn0") != -1)  //checks if you clicked OFF
      buton_islemi();
    

  

  else 
    Serial.println("invalid request");
   client.stop();
    return;
  

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";

  s += "<br><input type=\"button\" name=\"rly0-on\" value=\"Relay-0 ON \" onclick=\"location.href='/r0-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly0-off\" value=\"Relay-0 OFF\" onclick=\"location.href='/r0-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly1-on\" value=\"Relay-1 ON \" onclick=\"location.href='/r1-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly1-off\" value=\"Relay-1 OFF\" onclick=\"location.href='/r1-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly2-on\" value=\"Relay-2 ON \" onclick=\"location.href='/r2-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly2-off\" value=\"Relay-2 OFF\" onclick=\"location.href='/r2-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly3-on\" value=\"Relay-3 ON \" onclick=\"location.href='/r3-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly3-off\" value=\"Relay-3 OFF\" onclick=\"location.href='/r3-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"btn0\" value=\"Button-0 \" onclick=\"location.href='/btn0'\">";
  s += "<br>";

  s += "</html>\n";

  client.flush();


  // Send the response to the client
  client.print(s);
  delay(1);  


void buton_islemi() 
  digitalWrite(r0, !digitalRead(r0));
  delay(500);
  digitalWrite(r1, !digitalRead(r1));
  delay(500);
  digitalWrite(r2, !digitalRead(r2));
  delay(500);
  digitalWrite(r3, !digitalRead(r3));
  Serial.println("Button Pressed");

【问题讨论】:

【参考方案1】:

试试这个:

int button = 2;
int d = 0;
void setup() 
  pinMode(button, INPUT);

void loop() 
d = digitalRead(button);  // read input value
  if (d == LOW) 
 // your web server code

else 
Serial.print("Press the button to togggle led's");


【讨论】:

以上是关于如何在同一代码中使用基于 Web 的代码和真实按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何获取web客户端IP地址

如何在具有灵活高度的同一部分中的集合视图中加载有条件的 Web 视图和图像视图

如何在弹出窗口中打开新选项卡?

PayPal - 使用 REST API 从沙盒切换到真实账户

web3.0设计区块链如何解决真实交易的信用问题?

如何在同一个活动中使用两种 Web 服务方法?