Arduino Mega MQTT problems

I am having an issue with controlling some relays on an Arduino Mega with an Ethernet shield to home assistant using MQTT.

I have a DHT11 sensor connected and it is reporting temperature and humidity so the Mega is connected and talking but whenever I try to turn on one of the relays I get no response. I was wondering if someone here could look at the c code for the Arduino and the YAML in Home Assistant and see where I went wrong? I’ve been at this a while making slow progress, but I’m almost ready to pack it in. Another thing to note is that I can ping the Arduino successfully but it does not show up in my router's GUI.

I have this topic posted in the HA forum as well but it is not gaining any interest.

Thank you in advance!

#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>
#include <DHT.h>

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

DHT dht(2, DHT11, 23);  // DHT 22 on pin 9 - get power from pin 8 (cant use 10-13 as used for SPI by the ethernet card)
long previousMillis = 25000;
long interval = 30000;
    // Update these with values suitable for your network.
const char* mqtt_server = "192.168.0.135";
const char* client_name = "zone1";

const int Zone1Pin = 25;
const int Zone2Pin = 27;
char const* switchTopic1 = "/boiler/zone1/";
char const* switchTopic2 = "/boiler/zone2/";

byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0xA9, 0xCE };  //physical mac address
byte ip[] = { 192, 168, 0, 101 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);                             //server port
String readString;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(mqtt_server, 1883, callback, ethClient);

void setup() {

  pinMode(Zone1Pin, OUTPUT);
  digitalWrite(Zone1Pin, HIGH);

  pinMode(Zone2Pin, OUTPUT);
  digitalWrite(Zone2Pin, HIGH);

  Serial.begin(57600);
  delay(100);

  Ethernet.begin(mac, ip);

    //client.connect("Zone1", mqtt_user, mqtt_password);
  client.connect("zone1", "boiler", "123186");

  reconnect();

  // start the Ethernet connection and the server:

  Serial.println("about to begin ethernet");

  delay(1500);  // Allow the hardware to sort itself out
                //  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  if (!ethClient.connected()) {
    Serial.println("about to retry ethernet");
    ethClient.stop();
    Ethernet.begin(mac, ip);
    Serial.print("server is now at ");
    Serial.println(Ethernet.localIP());
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  
  String topicStr = topic;
  
  Serial.println("Callback update.");
  Serial.print("Topic: ");
  Serial.println(topicStr); //debug if we got a topic

  if (topicStr == "/boiler/zone1/") 
   {

    if(payload[0] == '1'){
      digitalWrite(Zone1Pin, LOW);
      client.publish("/boiler/zone1/", "1");
    }

    else if (payload[0] == '0'){
      digitalWrite(Zone1Pin, HIGH);
      client.publish("/boiler/zone1/", "0");
    }
   } 

  if (topicStr == "/boiler/zone2/") 
   {    
    
    if(payload[0] == '1'){
      digitalWrite(Zone2Pin, LOW);
      client.publish("/boiler/zone2/", "1");
    }

    else if (payload[0] == '0'){
      digitalWrite(Zone2Pin, HIGH);
      client.publish("/boiler/zone2/", "0");
    }      
   }
}


void reconnect() {

  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(client_name)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.subscribe(switchTopic1);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(2000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    Serial.println(" not connected - will try agin - calling reconect");
    reconnect();
  }
  client.loop();

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    float h = dht.readHumidity();
    float f = dht.readTemperature(false);
    if (isnan(h) || isnan(f)) {
      client.publish("boiler/supply/temperature", "DHT problem");
      previousMillis = currentMillis;  // add on and try again next loop
      return;
    }
    previousMillis = currentMillis;


    Serial.println(String(f));
    client.publish("boiler/supply/temperature", String(f).c_str());
    Serial.println(String(h));
    client.publish("boiler/supply/humidity", String(h).c_str());
  }
}
mqtt:  
  switch:
    - name: "Zone1"
      state_topic: "/boiler/zone1/"
      command_topic: "/boiler/zone1/"
      payload_on: "1"
      payload_off: "0"
      qos: 0
      retain: true
      unique_id: "zone1"
    
    - name: "Zone2"
      state_topic: "/boiler/zone2/"
      command_topic: "/boiler/zone2/"
      payload_on: "1"
      payload_off: "0"
      qos: 0
      retain: true
      unique_id: "zone2"
      
  sensor:
    - name: "Supply Temp"
      state_topic: "boiler/supply/temperature"
      unit_of_measurement: "F"
      value_template: '{{ value | round(0) }}'
      unique_id: "Supply Temp"

    - name: "Supply Humidity"
      state_topic: "boiler/supply/humidity"
      unit_of_measurement: "%"
      value_template: '{{ value | round(0) }}'
      unique_id: "Supply Humidity"

What do all those Serial.print() statements tell you?

One thing I did notice, is that your topics begin with "/" but the temp sensor does not. Could that be an issue?

Try install this MQTT client and watch the communication between broker and client.

Open the MQTT client and subscribe to all topics using # wildcard.

Might be some info to be had by looking at the router logs.

Here is some of the info from the serial monitor, this is all that I get unless it doesn't "connect". I also removed the "/" in the code and YAML but results are the same.

21:46:21.651 -> about to begin ethernet
21:46:23.176 -> server is at 192.168.0.101
21:47:23.265 -> 11.10
21:47:23.265 -> 17.10
21:55:23.923 -> 11.40
21:55:23.923 -> 16.20
21:57:24.101 -> 11.30
21:57:24.101 -> 16.20

I have MQTT explorer installed on HA, I can see the temp and humidity messages come from the Arduino and the messages to turn on the relay come from the broker, but nothing is published from the Arduino about the state of the relays.

I have wire shark on my pc, I can see the messages coming through for the relays from the broker, but what is curious to me is that I would think I would see the temp and humidity messages from the Arduino but have nothing coming from its IP. I will admit that my experience with wire shark is limited.

Show us the log with this messages coming from Arduino and broker.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.