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

ESP32S NodeMCU HTTP页面无法连接问题排查求助

项目基础信息
  • 硬件:ESP32S NodeMCU
  • 开发环境:VSCode with PlatformIO
问题描述

本人首次接触ESP32与Web服务器开发,若术语使用有误敬请谅解。当前正在开发业余ESP32项目,核心功能涉及HTTP页面交互:已编写if-else逻辑读取Web服务器URL后缀,以此设置waterState变量。目前ESP32可正常连接WiFi网络,但电脑端始终无法访问对应URL地址。预期效果为页面返回所选浇水时段(如morning),同时串口终端输出函数执行完成的提示。

已尝试调整WiFiClient与WiFiServer实例的位置,但未解决问题。此前将ScheduleProt相关代码放在WiFi连接逻辑内时程序可正常运行,现在复现该写法仍无法生效。

项目代码

以下为项目各模块代码(代码、技术术语、标识符均保持原样):

WiFiConnect.h

#ifndef WIFICONNECT_H
#define WIFICONNECT_H

#include <WiFiClient.h>
#include "Wifi.h"


class WiFiConnect
{
    private: 
    #define WIFI_NETWORK "NetworkName"
    #define WIFI_PASSWORD "Password"
    #define WIFI_TIMEOUT 20000
       
    public:
    void wifiConnect();
    void wifiDisconnect();

};

#endif

WiFiConnect.cpp - 负责WiFi连接逻辑

#include <WiFi.h>
#include <WiFiConnect.h>


void WiFiConnect::wifiConnect() {

    WiFiServer server(80);

    Serial.print("Connecting to WiFi");
    WiFi.mode(WIFI_AP);
    WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

    unsigned long startAttemptTime = millis();

    //Connection Protocol
    while(WiFi.status() != WL_CONNECTED && (millis() - startAttemptTime) < WIFI_TIMEOUT) {
        Serial.println("...");
        delay(100);
    }
    if(WiFi.status() != WL_CONNECTED) {
        Serial.println("Failed, attempting again...");
        delay(5000); 
        }
    else{
        Serial.print("Connected: ");
        Serial.println(WiFi.localIP());
        server.begin();
        }
    

}

void WiFiConnect::wifiDisconnect() {
  Serial.println("Disconnecting from WiFi...");
  WiFi.disconnect();
}

ScheduleProt.h

#ifndef SCHEDULEPROT_H
#define SCHEDULEPROT_H


class ScheduleProt {
    public:
    int waterState = -1;
    void scheduleWatering();
};
#endif

ScheduleProt.cpp - 读取HTTP服务器URL请求

#include <WiFi.h>
#include <Arduino.h>
#include "WiFiConnect.h"
#include "ScheduleProt.h"


void ScheduleProt::scheduleWatering() {

  WiFiServer server(80);

  WiFiClient client = server.available();

  WiFiConnect newConnection;

    newConnection.wifiConnect();
    while(waterState = -1){
      if (client) {

        String req = client.readStringUntil('\r');
        Serial.println("Waiting for user input...");

        //Watering Times
          if(req.indexOf("/morning/") != -1){
            client.print("Morning");
            waterState = 0;
          } 
          else if(req.indexOf("/noon/") != -1){
            waterState = 1;
            client.print("Noon");
          }  
          else if(req.indexOf("/evening/") != -1){
            waterState = 2;
            client.print("Evening");
          }      
          else if(req.indexOf("/midnight/") != -1){
            waterState = 3;
            client.print("Midnight");
          }   
      }
  }
  

  Serial.println("User input recieved, Huzzah!" + waterState);

  newConnection.wifiDisconnect();

}

main.cpp

#include <Arduino.h>
#include <time.h>
#include "Wifi.h"
#include "ScheduleProt.h"
#include "WaterProt.h"
#include "CurrentTime.h"

#define DEEPSLEEPTIME 86400000
#define WATER_DURATION 10000
#define MOTORPIN 0

WaterProt waterProtocol;
CurrentTime currentTime;
ScheduleProt newSchedule;

void setup() {
    Serial.begin(9600);
    newSchedule.scheduleWatering();
}

void loop() {

    if (waterProtocol.getWateringHour(newSchedule.waterState) == currentTime.getCurrentHour()){
            waterProtocol.waterPlant(MOTORPIN, WATER_DURATION);    
        }
    else {
        esp_sleep_enable_timer_wakeup(1800000);
        esp_deep_sleep_start();
    }

    esp_sleep_enable_timer_wakeup(DEEPSLEEPTIME);
    esp_deep_sleep_start();

}
运行日志与报错

串口终端输出日志

�Connecting to WiFi...
...
...
...
...
...
...
...
...
Connected: 192.168.1.100

网页访问报错

网页访问报错截图

恳请各位提供解决方案,非常感谢!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 11:06:29