1

尝试构建示例应用程序以连接到 MQTT 服务器遇到 2 种不同的情况

案例 1:如果我使用 Ethernet2 库,_client->connect(this->ip,this->port) 返回 0。已检查所有值。

案例 2:下面详述的编译错误。如果我让它编译

案例 1 - 解释:使用 Ethernet2 我可以编译和上传。我可以使用 setup() 函数中的以下内容连接到 WIFI 并在网络上看到:

EthernetClient ethClient;  
PubSubClient client;
void setup() {
  Serial.begin(115200);
  while (!Serial);
  WiFi.begin(ssid, password);
  delay(1500);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
    delay(500);
  }

  ipIP =  WiFi.localIP() ; // 192.168.8.104 - good
  WiFi.macAddress(mac); // MAC returned - good
  // Connecting to MQTT Server
  client.setClient(ethClient); 
  client.setServer(server, 1883);
  client.setCallback(callback);
  while (!client.connected()) { .   //fails here, always 0
      Serial.print("Attempting MQTT connection...");
      if (client.connect("dev001")) {
            Serial.println("connected");
            // Once connected, publish an announcement...
            client.publish("garden/light","works");
       // and so on
       }

PubSubClient 调用来自基类 Client: public Stream 的 _client.connect。它是一个虚函数,我的 C++ 还不足以知道它背后的代码在哪里进行进一步调试。

案例 2 解释如下。所有依赖项似乎都需要 SPI 版本 1,编译器问题可以在与 w5100.cpp 相关的底部看到。

CONFIGURATION: 
PLATFORM: Espressif 32 > Heltec WIFI LoRa 32
HARDWARE: ESP32 240MHz 320KB RAM (4MB Flash)
Library Dependency Finder -> 
LDF MODES: FINDER(chain) COMPATIBILITY(soft)
Collected 29 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <PubSubClient> 2.7
|-- <Wire> 1.0
|-- <ESP8266_SSD1306> 4.0.0
|   |-- <Wire> 1.0
|   |-- <SPI> 1.0
|-- <SPI> 1.0
|-- <LoRa> 0.5.0
|   |-- <SPI> 1.0
|-- <WiFi> 1.0
|-- <Ethernet> 2.0.0
|   |-- <SPI> 1.0
Compiling .pioenvs/heltec_wifi_lora_32/lib677/Ethernet_ID872/utility/w5100.cpp.o
Compiling .pioenvs/heltec_wifi_lora_32/FrameworkArduino/HardwareSerial.cpp.o
.piolibdeps/Ethernet_ID872/src/utility/w5100.cpp: In static member function 'static uint16_t W5100Class::write(uint16_t, const uint8_t*, uint16_t)':
.piolibdeps/Ethernet_ID872/src/utility/w5100.cpp:315:22: error: no matching function for call to 'SPIClass::transfer(uint8_t [8], int)'
SPI.transfer(cmd, 4);
^

你能给我一些想法,我该如何解决这个问题?非常感谢凯文

4

1 回答 1

2

您实际上使用的是以太网还是 WiFi?您在代码中混合了两者,它们是两个独立的网络。您的代码正在连接到 WiFi,所以我猜您实际上并没有使用以太网。

如果你不使用以太网——几乎没有人在 ESP32 上使用——那么就没有办法EthernetClient为你工作。

在这种情况下,您的代码应该看起来更像:

WiFiClient wifiClient;  
PubSubClient client;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  WiFi.begin(ssid, password);
  delay(1500);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
    delay(500);
  }

  ipIP =  WiFi.localIP() ; // 192.168.8.104 - good
  WiFi.macAddress(mac); // MAC returned - good
  // Connecting to MQTT Server
  client.setClient(wifiClient); 
  client.setServer(server, 1883);

此外,您的输出表明您使用的是 ESP32,而不是 ESP8266(如您的问题所示)。

于 2018-12-03T21:30:26.120 回答