Connecting multiple devices to mqtt via ethernet with ESP32

Good day, I have installed the following code on my two devices using esp32 and ethernet module. but what is the reason why one of them connects successfully and the other does not?

#include <UIPEthernet.h>
#include <ArduinoJson.h>
#include <MQTT.h>
#include <DHT.h>
#include <WiFi.h>
//////////////////////// ETHERNET CONFİG //////////////////////////
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//////////////////////// ETHERNET CONFİG //////////////////////////

//////////////////////// MQTT CONFİG //////////////////////////
IPAddress mqttServer(xx, xxx, xxx, xxx);
//////////////////////// MQTT CONFİG //////////////////////////

//////////////////////// LED & DHT22 CONFİG //////////////////////////
byte red = 2;
byte green = 0;
byte blue = 4;
byte temp = 15;
#define DHTTYPE DHT11
DHT dht = DHT(temp, DHTTYPE);
//////////////////////// LED & DHT22 CONFİG //////////////////////////

unsigned long lastMillis = 0;

EthernetClient net;
MQTTClient client(256);

void setup_eth() {
  digitalWrite(blue, HIGH);
  Serial.print("connecting ethernet...");

  if (Ethernet.begin(mac) == 0) {
     Serial.println("Failed to configure Ethernet using DHCP");
    for (;;)
  Ethernet.begin(mac, Ethernet.localIP());
  digitalWrite(blue, LOW);


void connect() {
  Serial.print("connecting mqtt...");
  digitalWrite(blue, HIGH);
  byte r = 0;
  while (!client.connected()) {

    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);

    if (client.connect(clientId.c_str())) {
      // Subscribe

    } else {

      if (r == 3) {
        r = 0;
  } Serial.println("\nconnected!");
  digitalWrite(blue, LOW);

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

void setup() {

  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);

  client.begin(mqttServer, 1883, net);

void loop() {

  if (!client.connected()) {


  if (millis() - lastMillis > 5000) {
    lastMillis = millis();
    digitalWrite(green, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(red, LOW);
    StaticJsonBuffer<300> JSON;
    JsonObject& JSONencoder = JSON.createObject();

     Serial.println(Ethernet.maintain ());
    JSONencoder["DEVICE = "] = WiFi.macAddress();
    JSONencoder["USER = "] = "Test";
    JSONencoder["TEMP = "] = dht.readTemperature();
    JSONencoder["HUMD = "] = dht.readHumidity();
    JSONencoder["STATE = "] = true;

    char JSONmessageBuffer[100];
    JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));

    client.publish("/PUB/data", JSONmessageBuffer);


Do you really give both devices the same MAC address ?

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
Ethernet.begin(mac) == 0)

Doesn't the device create its own mac address with this code? Do I have to create?

It looks to me as though it is using DCHP to assign its IP address

I am sceptical as to whether this will work with duplicate MAC addresses for two devices but I freely admit that I know nothing about that Ethernet library

Same MAC is not going to work. Just change the last digit on one of them to F

Thanks for your help.
The address I got using WiFi.macAddress(); byte mac = {}; I make it unique by adding :slight_smile:

Can you please explain what you mean by that as it makes no sense to me

If you really do have byte mac[] = {}; in your code then I foresee problems ahead

Or just use the device MAC.

The mac address of each device must be unique, this may be an issue in the future. I put the mac address of the device in byte mac with WiFi.macAddress ().
Example: 9C: CC: 9C: 13: 10

byte mac = {0x9C,0xCC,0x9C,0x13,0x10};

It's an issue now if you are using DHCP to assign IP addresses.

In general you should always use the MAC address already assigned to the device by the manufacturer. It's really rare that you need to spoof the MAC address.

1 Like

How to query this can you share sample code?

I've never used this library, but try changing
if (Ethernet.begin(mac) == 0) {
if (Ethernet.begin == 0) {

I looked at ehtenet.h and there is no prototype for this construct in the EthernetClass, but there is in the EthernetServer class. It may work.

You are not clear in your first post, but are you using the ESP32-Ethernet-Kit? If yes, then you are using the wrong library. That board uses the eth.h library.

I use these two as hardware. I'm using UIPEthernet.h as I can't run it with ethernet.h.

Probably still the wrong library.

(Ignore the preview warning- this is an artifact of the forum code).

But this is going way off from the original question.

Have you tried simply changing one of the MAC addresses? If this works, then stop there.

yes, i solved.

Did changing the second MAC address fix the problem? Future people searching the same problem would like to know.

byte mac = {}; giving two different mac addresses solved the problem

byte mac[] = {};

To me an array with one element is unlikely to solve anything. Did you mean to allocate more space in the array to be populated later in the sketch or is the sketch writing over memory locations that may or may not contain important data once the MAC address is populated ?

array is not single element, I showed where 6 digit mac code should be written.
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte mac1[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};

Then why did you write