You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32开发板点对点通信场景下无法修改晶体管状态问题求助

代码存在的核心问题及修复方案

1. 服务端请求处理逻辑缺失

你在setup()里定义的GET请求回调,仅返回了参数,没有实际修改本地LED的状态,自然无法响应另一块开发板发来的状态同步请求。
修复:在回调函数中增加状态更新逻辑,代码如下:

server.on("^\/led\/([0-9]+)\/state\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) {
  String ledNumber = request->pathArg(0);
  String state = request->pathArg(1);
  // 新增状态更新逻辑
  int newState = state.toInt();
  transistorState = newState ? HIGH : LOW;
  transistorStateInt = newState;
  digitalWrite(transistor, transistorState);
  request->send(200, "text/plain", "LED: " + ledNumber + ", with state: " + state);
});

2. 状态变量赋值错误

你在本地触控切换状态的逻辑里,把transistorStateInt的赋值写错了:

// 原错误代码
else{
  transistorState = 1; // 此处错误赋值了状态变量,同步用的transistorStateInt永远不会更新
}

修复为:

else{
  transistorStateInt = 1;
}

3. AsyncClient未触发连接逻辑

你仅定义了TCP连接成功的回调函数,但没有调用connect方法连接对端开发板的HTTP端口,请求永远不会发送。同时每次循环都新建AsyncClient对象会造成内存泄漏,且不需要在状态无变化时重复发送请求。
修复逻辑:

  • 仅在本地LED状态发生变化时才向对端发送同步请求
  • 补全AsyncClient的连接逻辑,用完释放资源
    修改后的loop代码参考:
// 新增全局变量,记录上次同步的状态,避免重复发送请求
int lastSyncedState = -1;

void loop() {
  // 删掉原loop开头的while(1),Arduino的loop本身就是循环,加死循环会阻塞底层网络栈调度
  touchValue = touchRead(touchPin);

  if (millis() - lastTimeButtonStateChanged > debounceDuration) {
    buttonState = (touchValue < threshold) ? HIGH: LOW;
    if (buttonState != lastButtonState) {
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
      if (buttonState == LOW) {
        transistorState = (transistorState == HIGH) ? LOW: HIGH;
        digitalWrite(transistor, transistorState);
        if(transistorState == LOW){
          transistorStateInt = 0;
        } else{
          transistorStateInt = 1;
        }
      }
    }
  }
  // 仅状态变化时才发同步请求
  if(transistorStateInt != lastSyncedState) {
    for (auto const &host : Husarnet.listPeers()) {
      IPv6Address peerAddr = host.first;
      if(host.second != "master") {
        AsyncClient* client_tcp = new AsyncClient;
        client_tcp->onConnect([](void *arg, AsyncClient *client) {
          String requestURL = "/led/1/state/" + String(transistorStateInt);
          String GETreq = String("GET ") + requestURL + " HTTP/1.1\r\n" + "Host: esp32\r\n" + "Connection: close\r\n\r\n";
          if ( client->canSend() && (client->space() > GETreq.length())){
            client->add(GETreq.c_str(), GETreq.length());
            client->send();
          } else {
            Serial.printf("\r\nSENDING ERROR!\r\n");
          }
          // 连接关闭后释放内存避免泄漏
          client->onDisconnect([](void *arg, AsyncClient *c) {
            c->free();
            delete c;
          }, nullptr);
        }, client_tcp);
        // 补全连接逻辑,默认HTTP端口为80
        if(!client_tcp->connect(peerAddr, 80)) {
          Serial.println("连接对端失败");
          client_tcp->free();
          delete client_tcp;
        }
      }
    }
    lastSyncedState = transistorStateInt;
  }
  // 加小延迟避免CPU占用过高
  delay(10);
}

4. 其他小问题

  • 串口波特率写为1155200,标准波特率为115200,会导致串口打印乱码,可自行修正。

内容的提问来源于stack exchange,提问作者CatiaV5

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 18:48:00