ESP32网络连接失败
ESP32 network connect failure
正在尝试构建示例应用程序以连接到 MQTT 服务器
遇到2种不同的情况[=12=]
案例 1:_client->connect(this->ip,this->port) returns 0 如果我使用 Ethernet2 库。所有值都已检查。
情况二:编译错误详述如下。
如果我让它编译
案例 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 调用来自基础 class 的 _client.connect,客户端:public 流。
它是一个虚函数,我的 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);
^
你能告诉我如何解决这个问题吗?
非常感谢
凯文
您实际上使用的是以太网还是 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(如您的问题)。
正在尝试构建示例应用程序以连接到 MQTT 服务器 遇到2种不同的情况[=12=]
案例 1:_client->connect(this->ip,this->port) returns 0 如果我使用 Ethernet2 库。所有值都已检查。
情况二:编译错误详述如下。 如果我让它编译
案例 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 调用来自基础 class 的 _client.connect,客户端:public 流。 它是一个虚函数,我的 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);
^
你能告诉我如何解决这个问题吗? 非常感谢 凯文
您实际上使用的是以太网还是 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(如您的问题)。