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

使用API获取天气信息时HTTPClient.h库编译报错求助

问题

尝试编写ESP32代码通过API获取数据,但使用HTTPClient.h库时出现编译错误。以下是我的代码:

/* https://www.youtube.com/watch?v=j0bhm-0Z1Sw */
#include <WiFi.h>
#include <HTTPClient.h>
#include <credentials.h>

String serverName = "https://api.publicapis.org/entries/";

unsigned long lastTime = 0;
unsigned long timerDelay = 50000;

void setup() {
  Serial.begin(115200);
  Serial.println(__FILE__);
}

void setupwifi() {

  WiFi.mode(WIFI_STA);  //optional
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }

  Serial.println("\nConnected to the wiFi network ");
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());
  callApi();

}  

void callApi() {
  Serial.println("test2");
  Serial.println("test");
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String serverPath = serverName;
    http.begin(serverPath.c_str());

    int httpResponseCode = http.GET();
    Serial.print(httpResponseCode);
    Serial.print("httpResponseCode");

    if (httpResponseCode > 0) {}
    Serial.print("HTTP Response Code: ");
    Serial.println(httpResponseCode);
    String payload = http.getString();
    Serial.println(payload);
  } else {
    Serial.print(" Error Code: ");
    Serial.println(httpResponseCode);
  }
  http.end

 else {
  Serial.println("WiFi Disconnected ");
}

lastTime = millis();
}
void loop() {}

编译时的错误信息:

C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino: In function 'void callApi()':
C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino:36:5: error: 'HTTPClient' was not declared in this scope
     HTTPClient http;
     ^~~~~~~~~~
C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino:36:5: note: suggested alternative: 'HttpClient'
     HTTPClient http;
     ^~~~~~~~~~
     HttpClient
C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino:38:5: error: 'http' was not declared in this scope
     http.begin(serverPath.c_str());
     ^~~~
C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino:51:20: error: 'httpResponseCode' was not declared in this scope
     Serial.println(httpResponseCode);
                    ^~~~~~~~~~~~~~~~
C:\Users\Peter\AppData\Local\Temp\.arduinoIDE-unsaved2023111-33236-9hag6p.ncf8h\sketch_dec1c\wifiModule.ino:53:3: error: 'http' was not declared in this scope
   http.end
   ^~~~
Using library WiFi at version 2.0.0 in folder: C:\Users\Peter\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\libraries\WiFi 
Using library HTTPClient at version 2.0.0 in folder: C:\Users\Peter\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\libraries\HTTPClient 
Using library credentials in folder: D:\Documents\Arduino\libraries\credentials (legacy)
exit status 1

Compilation error: 'HTTPClient' was not declared in this scope

错误原因分析

  • 代码语法结构混乱:callApi()函数存在多处语法错误:
    1. if (httpResponseCode > 0) {} 后直接跟随未被包裹的代码,破坏了代码块结构
    2. http.end 缺少括号和分号,且位置错误,超出了HTTPClient http的作用域(http定义在if块内,块外无法访问)
    3. 存在无对应if的多余else分支,导致函数结构完全错乱
  • 变量作用域错误:httpResponseCode定义在if(WiFi.status() == WL_CONNECTED)块内,在else分支中无法访问,触发编译报错

解决办法与修复后的代码

修复所有语法错误,调整变量作用域,补充缺失的逻辑:

/* https://www.youtube.com/watch?v=j0bhm-0Z1Sw */
#include <WiFi.h>
#include <HTTPClient.h>
#include <credentials.h>

String serverName = "https://api.publicapis.org/entries/";

unsigned long lastTime = 0;
unsigned long timerDelay = 50000;

void setup() {
  Serial.begin(115200);
  Serial.println(__FILE__);
  setupwifi(); // 补充调用WiFi初始化函数
}

void setupwifi() {
  WiFi.mode(WIFI_STA);  //optional
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }

  Serial.println("\nConnected to the WiFi network ");
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());
}  

void callApi() {
  Serial.println("test2");
  Serial.println("test");
  
  // 提前定义变量,确保所有分支可访问
  int httpResponseCode = -1;
  
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String serverPath = serverName;
    
    http.begin(serverPath.c_str());
    httpResponseCode = http.GET();
    Serial.print("httpResponseCode: ");
    Serial.println(httpResponseCode);

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response Code: ");
      Serial.println(httpResponseCode);
      String payload = http.getString();
      Serial.println(payload);
    }
    
    // 在http实例作用域内调用end()释放资源
    http.end();
  } else {
    Serial.print("WiFi Disconnected, Error Code: ");
    Serial.println(httpResponseCode);
  }

  lastTime = millis();
}

void loop() {
  // 按设定间隔定时调用API
  if (millis() - lastTime >= timerDelay) {
    callApi();
  }
}

额外说明:

  • 原代码setup()未调用setupwifi(),导致WiFi永远不会初始化,已补充该调用
  • 在loop()中添加定时逻辑,匹配原代码中定义的timerDelay变量用途
  • 调整http.end()位置,确保在HTTPClient实例的作用域内执行,避免资源泄漏

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 20:23:11