Site icon Renzo Mischianti

Node32-W5500-MQTT Problem

Ciao everyone, I have a problem with my prototype. I'd like to implement MQTT over LAN using an ESP32 and a standard W5500 Nano. The prototype successfully acquires an IP address via DHCP, but MQTT doesn't work. The code is very simple and minimal. I tested the same Ethernet cable with my laptop and I can send MQTT messages without issues. Any ideas?

#include <SPI.h>
#include <Ethernet.h>
#include <MQTT.h>
 
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA};
 
EthernetClient net;
MQTTClient client;
 
unsigned long lastMillis = 0;
 
void connect() {
  Serial.print("connecting...");
  while (!client.connect("TestEsp")) {
    Serial.print(".");
    delay(1000);
  }
 
  Serial.println("connected!");
 
  client.subscribe("/Test");
}
 
void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
 
}
 
void setup() {
  Serial.begin(115200);
  Ethernet.init(5); //CS pin 
  Ethernet.begin(mac);
  Serial.print("Local IP : ");
  Serial.println(Ethernet.localIP());
 

  client.begin("192.168.1.66", net);
  client.onMessage(messageReceived);
 
  connect();
}
 
void loop() {
  client.loop();
 
  if (!client.connected()) {
    connect();
  }
 

  if (millis() - lastMillis > 5000) {
    lastMillis = millis();
    client.publish("Node32s", "Test");
  }
}
Grazie mille
Exit mobile version