MQTT fails over home network but works direct connecting.

Hi there all.

Hope everyone is well, If anyone could help me on this Id really appreciate it.
Ill Paste the code at the end of the post. Its altered from the original code I was
using after the "Byt" mac address it was using IPADDRESS which the W5100 Ethernet
page didn't have much about .hence i commented out the address and added the other
Ethernet byte fields.

So the problem I am having is that it worked fine I programmed and tested it from the
device to my laptop. but moving it to the home lan it doesn't seem to work. I can Ping
the device with no trouble, good constant ping. There's no firewall on my lan and it only
filters out WAN traffic . Could someone point me in the direction of the problem?

Physical devices are arduino mega 2560 R3 original. Arduino ethernet W5100 shield original.

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ARDUINO_CLIENT_ID "Sense_1"                       // Client ID for Arduino pub/sub
#define PUB_TEMP1 "Sense_1/sensor/DHT221Temp"             // MTTQ topic for Temp1
#define PUB_HUMI1 "Sense_1/sensor/DHT221Humi"             // MTTQ topic for Humi1
#define PUB_TEMP2 "Sense_1/sensor/DHT222Temp"             // MTTQ topic for Temp2
#define PUB_HUMI2 "Sense_1/sensor/DHT222Humi"             // MTTQ topic for Humi2
#define PUB_DS18B1 "Sense_1/sensor/DS18B1"                // MTTQ topic for DS18B201
#define PUB_DS18B2 "Sense_1/sensor/DS18B2"                // MTTQ topic for DS18B202
#define PUB_DS18B3 "Sense_1/sensor/DS18B3"                // MTTQ topic for DS18B203
#define PUB_DS18B4 "Sense_1/sensor/DS18B4"                // MTTQ topic for DS18B204
#define SUB_LED "arduino_1/led"                           // MTTQ topic for LED
#define PUBLISH_DELAY 3000                                // Publishing delay [ms]

// Hardware setup details
#define ledPin  36
#define DHTPIN1 28        // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN2 29
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define ONE_WIRE_BUS 38

// Networking details
byte mac[]    = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };  // Ethernet shield (W5100) MAC address
byte ip[] = { 10, 118, 3, 20 };
byte subnet[] = { 255, 0, 0, 0 };
byte gateway[] = { 10, 0, 0, 1 };
//IPAddress ip(10, 118, 3, 20);                           // Ethernet shield (W5100) IP address
IPAddress server(10, 10, 11, 99);                       // MTTQ server IP address

DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
EthernetClient ethClient;
PubSubClient client(ethClient);

// Addresses of 4 DS18B20s
DeviceAddress sensor1 = { 0x28, 0xAA, 0x92, 0x56, 0x1A, 0x13, 0x02, 0x4D };
DeviceAddress sensor2 = { 0x28, 0xAA, 0xDA, 0x13, 0x3D, 0x14, 0x01, 0x99 };
DeviceAddress sensor3 = { 0x28, 0xAA, 0x37, 0xE9, 0x3C, 0x14, 0x01, 0x6E };
DeviceAddress sensor4 = { 0x28, 0xD5, 0x4C, 0xA3, 0x2F, 0x14, 0x01, 0x9A };

//float temp01 = 0;
//float temp02 = 0;
//float temp03 = 0;
//float temp04 = 0;

long previousMillis;

void setup()
{
  Serial.begin(9600);

  // LED off
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // MTTQ parameters
  client.setServer(server, 1883);
  client.setCallback(callback);

  // Ethernet shield configuration
  Ethernet.begin(mac, ip);

  dht1.begin();
  dht2.begin();
  sensors.begin();
  
  delay(1500); // Allow hardware to stabilize

  previousMillis = millis();
}

void loop()
{
  if (!client.connected())
    reconnect();

  if (millis() - previousMillis > PUBLISH_DELAY)
  {
    previousMillis = millis();
    float humi1 = dht1.readHumidity(); // humidity
    float temp1 = dht1.readTemperature(); // temperature [C]
    float humi2 = dht2.readHumidity(); // humidity
    float temp2 = dht2.readTemperature(); // temperature [C]
    
    char tmpBuffer[30];

    // check if any reads failed and exit early (to try again).
    if (isnan(humi1) || isnan(temp1))
    {
      Serial.println("error reading sensor data");
      return;
    }
    else
    {
      Serial.print("[DHT221] temperature[C]: ");
      Serial.print(temp1);
      Serial.print(", humidity: ");
      Serial.println(humi1);
      Serial.print("[DHT222] temperature[C]: ");
      Serial.print(temp2);
      Serial.print(", humidity: ");
      Serial.println(humi2);
      
      Serial.println("Requesting DS18 Temperatures...");
      sensors.requestTemperatures(); // Send the command to get temperatures
      
      int Se1 = sensors.getTempC(sensor1);
      delay(800);
      Serial.print("Sensor1 Temp is: ");
      Serial.print(Se1);
      Serial.print("C: ");
      Serial.print("\n\r");
      int Se2 = sensors.getTempC(sensor2);
      delay(800);
      Serial.print("Sensor2 Temp is: ");
      Serial.print(Se2);
      Serial.print("C: ");
      Serial.print("\n\r");
      int Se3 = sensors.getTempC(sensor3);
      delay(800);
      Serial.print("Sensor3 Temp is: ");
      Serial.print(Se3);
      Serial.print("C: ");
      Serial.print("\n\r");
      int Se4 = sensors.getTempC(sensor4);
      delay(800);
      Serial.print("Sensor4 Temp is: ");
      Serial.print(Se4);
      Serial.print("C: ");
      Serial.print("\n\r");
      
      //temp01 = sensors.getTempCByIndex(0);
      //temp02 = sensors.getTempCByIndex(1);
      //temp03 = sensors.getTempCByIndex(2);
      //temp04 = sensors.getTempCByIndex(3);
      
      Serial.println("DONE");

      //Serial.println(temp01);
      //Serial.println(temp02);
      //Serial.println(temp03);
      //Serial.println(temp04);
      
      //Serial.print("Sensor 1(*C): ");
      //Serial.println(sensors.getTempC(sensor1));
      //Serial.print("Sensor 2(*C): ");
      //Serial.println(sensors.getTempC(sensor2));
      //Serial.print("Sensor 3(*C): ");
      //Serial.println(sensors.getTempC(sensor3));
      //Serial.print("Sensor 4(*C): ");
      //Serial.println(sensors.getTempC(sensor4));

      
      client.publish(PUB_TEMP1, dtostrf(temp1, 6, 2, tmpBuffer));
      client.publish(PUB_HUMI1, dtostrf(humi1, 6, 2, tmpBuffer));
      client.publish(PUB_TEMP2, dtostrf(temp2, 6, 2, tmpBuffer));
      client.publish(PUB_HUMI2, dtostrf(humi2, 6, 2, tmpBuffer));
      client.publish(PUB_DS18B1, dtostrf(Se1, 6, 2, tmpBuffer));
      client.publish(PUB_DS18B2, dtostrf(Se2, 6, 2, tmpBuffer));
      client.publish(PUB_DS18B3, dtostrf(Se3, 6, 2, tmpBuffer));
      client.publish(PUB_DS18B4, dtostrf(Se4, 6, 2, tmpBuffer));
      
      
    }
  }

  client.loop();
}

void reconnect()
{
  // Loop until reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection ... ");
    // Attempt to connect
    if (client.connect(ARDUINO_CLIENT_ID)) {
      Serial.println("connected");
      // (re)subscribe
      client.subscribe(SUB_LED);
    } else {
      Serial.print("Connection failed, state: ");
      Serial.print(client.state());
      Serial.println(", retrying in 5 seconds");
      delay(5000); // Wait 5 seconds before retrying
    }
  }
}

// sub callback function
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("[sub: ");
  Serial.print(topic);
  Serial.print("] ");
  char message[length + 1] = "";
  for (int i = 0; i < length; i++)
    message[i] = (char)payload[i];
  message[length] = '\0';
  Serial.println(message);

  // SUB_LED topic section
  if (strcmp(topic, SUB_LED) == 0)
  {
    if (strcmp(message, "on") == 0)
      digitalWrite(ledPin, HIGH);
    if (strcmp(message, "off") == 0)
      digitalWrite(ledPin, LOW);
  }
}

Thanks again everyone

MQTT_and_Node_Red_v2.ino (6.84 KB)

So you actually have a home LAN that uses a complete A network (10.0.0.0/8)? Although it isn't impossible that you did such a rather unusual configuration my guess is that this configuration isn't correct for your home LAN.

If you need help from us you have to tell us much more details about your network setup. I doubt that the rest of the sketch is relevant for your problem, but with the information you provided we cannot check the IP configuration you made.