【发布时间】: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