【问题标题】:change an if / else if statement to a switch case in Arduino将 if / else if 语句更改为 Arduino 中的 switch case
【发布时间】:2022-08-23 19:47:00
【问题描述】:

我正在使用 ESP32D 制作 Web 服务器,我正在从 Arduino IDE 对其进行编程,并且我有一个带有意大利面条代码的功能,我想用 switch case 重构它, 这是我的函数,我在 void loop() 中调用它

void control(){
              if (header.indexOf(\"GET /2/on\") >= 0) {
                Serial.println(\"GPIO 2 on\");
                output2State = \"on\";
                digitalWrite(output2, HIGH);
              } else if (header.indexOf(\"GET /2/off\") >= 0) {
                Serial.println(\"GPIO 2 off\");
                output2State = \"off\";
                digitalWrite(output2, LOW);
              } else if (header.indexOf(\"GET /15/on\") >= 0) {
                Serial.println(\"GPIO 15 on\");
                output15State = \"on\";
                digitalWrite(output15, HIGH);
              } else if (header.indexOf(\"GET /15/off\") >= 0) {
                Serial.println(\"GPIO 15 off\");
                output15State = \"off\";
                digitalWrite(output15, LOW);
              }
} 




void function_loop(){
  
  WiFiClient client = server.available();   // Listen for incoming client
  // Serial.println(typeOf(client));

  

  if (client) {                             // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    Serial.println(\"New Client.\");          // print a message out in the serial port
    String currentLine = \"\";                // make a String to hold incoming data from the client
    while (client.connected() && currentTime - previousTime <= timeoutTime) {  // loop while the client\'s connected
      currentTime = millis();
      if (client.available()) {             // if there\'s bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == \'\\n\') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that\'s the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what\'s coming, then a blank line:
            client.println(\"HTTP/1.1 200 OK\");
            client.println(\"Content-type:text/html\");
            client.println(\"Connection: close\");
            client.println(); 


            control();
            
}

我该怎么做才能将它传递给 switch case?我不知道如何传递要修改的变量(在这种情况下是 header.indexOf)

    标签: server arduino arduino-esp32


    【解决方案1】:

    从您的示例中不清楚您的 header 是什么类型,但我猜它是一个字符串。

    像这样发送header到control()

    control(header);
    

    并像这样声明 control()

    void control(String& header) {
    

    【讨论】:

    • 是的。是一个我忘记的字符串,在我声明的 void setup() 之前:String header;
    • 我已经进行了您提到的更改,但它不断向我抛出错误:constantes.cpp:62:36: error: call to non-'constexpr' function 'int String::indexOf(const String&amp;) const' case header.indexOf("GET /2/on") &gt;= 0:
    • 这是我现在的功能:
    • void control(const String&amp; header) { switch (header){ case header.indexOf("GET /2/on") &gt;= 0: Serial.println("GPIO 2 on"); output2State = "on"; digitalWrite(output2, HIGH); break; case header.indexOf("GET /2/off") &gt;= 0: Serial.println("GPIO 2 off"); output2State = "off"; digitalWrite(output2, LOW); break; } }
    • switch (header) 不起作用。但是你可以分别搜索GET /2/GET /15/onoff的索引来“简化”。
    最近更新 更多