fatal error: ESP8266WiFi.h: No such file or directory

fatal error: ESP8266WiFi.h: No such file or directory

我正在尝试用我的 ESP32 制作一个“家庭重量”并使用 IBMCloud 显示该值,但是我 运行 遇到了 Arduino IDE 和我的代码的一些问题。

我收到这个错误:

Arduino:1.8.5 (Windows 10), Tarjeta:"ESP32 Dev Module, QIO, 80MHz, 4MB (32Mb), 921600, None"

C:\Users\XX\Documents\Arduino\IBM_Watson_Connect\IBM_Watson_Connect.ino:8:25: fatal error: ESP8266WiFi.h: No such file or directory

compilation terminated.

exit status 1 Compiling error for the ESP32 Dev Module card.

我使用的是 ESP32 开发板。我的代码是这样的:

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/releases/tag/v2.3

    #include "HX711.h" //Load Cell Amplifier
    HX711 cell(D2, D4); //Amplifier is connected to these pins on the NodeMCU ESP8266 Board

    #define WLAN_SSID       "XXXXX"  
    #define WLAN_PASS       "XXXXX"  

    #define ORG "XXXXX"
    #define DEVICE_TYPE "XXXXXX"
    #define DEVICE_ID "XXXXX"
    #define TOKEN "XXXXXXXX"

    char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
    char topic[] = "iot-2/evt/status/fmt/json";
    char authMethod[] = "use-token-auth";
    char token[] = TOKEN;
    char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

    WiFiClient wifiClient;
    PubSubClient client(server, 1883, NULL, wifiClient);

    void setup() {


     Serial.begin(115200);
     Serial.println();

      // Connect to WiFi access point.
      Serial.println(); Serial.println();
      Serial.print("Connecting to ");
      Serial.println(WLAN_SSID);

      WiFi.begin(WLAN_SSID, WLAN_PASS);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println();

      Serial.println("WiFi connected");
      Serial.println("IP address: "); Serial.println(WiFi.localIP());

    }

    int counter = 0;

    void loop() {

     if (!!!client.connected()) {
     Serial.print("Reconnecting client to ");
     Serial.println(server);
     while (!!!client.connect(clientId, authMethod, token)) {
     Serial.print(".");
     delay(500);
     }
     Serial.println();
     }


    //----------Get data from load cell and amplifier

      long valCalibrated = 0;
      long val = 0;
      float count = 0;


      count = count + 1;
      val = 0.5 * val    +   0.5 * cell.read();
      valCalibrated =  (val - 4137240) / 234.20;

    //----------Send data to IBM Waton IoT Service

     String payload = "{\"d\":{\"weight\":";
     payload += valCalibrated;
     payload += "}}";

     Serial.print("Sending payload: ");
     Serial.println(payload);

     if (client.publish(topic, (char*) payload.c_str())) {
     Serial.println("Publish ok");
     } else {
     Serial.println("Publish failed");
     }

     ++counter;
     delay(100); //adjust delay to send more or less reads per unit time
    }

有些地方提到库丢失,板子没有正确选择,库没有更新。我检查了它们。Arduino 已更新,库已安装和更新,正确的板子是选中(我实际上已经尝试了所有其他 Esp32 相关的板,结果相同)

您正在为 ESP32 而不是 ESP8266 构建程序。有很多相似之处,但它们是完全不同的芯片和不同的软件。

所以您不对 ESP32 使用 ESP8266WiFi.h。在 ESP32 上,头文件名为 WiFi.h(与 Arduinos 上的 WiFi 支持保持一致——ESP32 Arduino Core 旨在比 ESP8266 版本更兼容普通的 Arduino Core)。

你需要

#include <WiFi.h>

而不是ESP8266WiFi.h

您可以在 the official repository for the Arduino SDK for the ESP32 中找到这些文件的代码。

WiFi.h for the ESP32 在自己的评论中将自己标识为 ESP8266.h 也无济于事...)