KevinY
KevinY

Reputation: 1239

ESP32 network connect failure

Trying to build an example application to connect to a MQTT server Encountering 2 different situations

Case 1: _client->connect(this->ip,this->port) returns 0 if I use the Ethernet2 library. All the values have been checked.

Case 2: the compile error detailed below. If I get it to compile

Case 1 - explained: Using Ethernet2 I can compile and upload. I can connect to the WIFI and be seen on the network using the following in the setup() function:

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 calls _client.connect which is from a base class, Client: public Stream. It is a virtual function and my C++ isn't good enough to know where the code is behind it to debug further.

Case 2 is explained below. All the dependencies seem to require SPI version 1 and the compiler problem can be seen at the bottom relating to w5100.cpp I've seen online a few very similar situations but don't know enough about this stuff yet to fix it.

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);
^

Can you give me some idea how I can resolve this please? Many thanks Kevin

Upvotes: 1

Views: 567

Answers (1)

romkey
romkey

Reputation: 7069

Are you actually using Ethernet or WiFi? You're mixing the two in your code, and they're two separate networks. Your code is connecting to WiFi, so I'm guessing you're not actually using ethernet.

If you're not using ethernet - and almost nobody on an ESP32 is - then there's no way EthernetClient is going to work for you.

In that case your code should look more like:

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);

Also your output says you're using an ESP32, not an ESP8266 (as in your question).

Upvotes: 2

Related Questions