pubsubclient wont publish

Hi !

I’am an absolute beginner in Arduino programming, so please exuse me if my question is silly.

I’am looking at the code since days, and can not figure out where the problem is.

I put together a code from various example’s, and already did a lot of experiments with it, that’s why it is a mess, and full of commented lines.

The code should connect to a broker (mosquitto, running on my local network on a raspbery). - OK
Publish an “ONLINE” message in a status topic. - OK
Leave a last will to publish “OFFLINE” in the same topic. - OK
Both messages retained - OK

Than it should subscribe to a topic (AKAC/POND/IN/#) and listen to messages - OK

Depending in what subtopic a message arrives (D1-D8), it should control relay outputs. - OK
1 = switch to high, 0 = switch to low, 2 = switch state - OK
print on serial = OK
publish to a topic - NOT OK

nothing happens, no publishing is done. I can not figure out why?

see lines 72-100

if(strTopic == “AKAC/POND/IN/D1”)
// Switch on the RELAY1 if an 1 was received as first character

if ((char)payload[0] == ‘0’) {

digitalWrite(relay1_pin, LOW); // Turn the LED on (Note that LOW is the voltage level
Serial.println(“relay1_pin → LOW”);
client.publish(“AKAC/POND/OUT/D1”,“0”, 1);
relayState = LOW;
// EEPROM.write(0, relayState); // Write state to EEPROM
// EEPROM.commit();
} else if ((char)payload[0] == ‘1’) {
digitalWrite(relay1_pin, HIGH); // Turn the LED off by making the voltage HIGH
Serial.println(“relay1_pin → HIGH”);
client.publish(“AKAC/POND/OUT/D1”,“1”, 1);
relayState = HIGH;
//EEPROM.write(0, relayState); // Write state to EEPROM
} else if ((char)payload[0] == ‘2’) {
relayState = !relayState;
digitalWrite(relay1_pin, relayState); // Turn the LED off by making the voltage HIGH
Serial.print("relay1_pin → switched to ");
//EEPROM.write(0, relayState); // Write state to EEPROM

What am I not seeing ???

mqtt_basic.ino (12.5 KB)

void callback(char* topic, byte* payload, unsigned int length) {

  EthernetClient ethClient;
  PubSubClient client(ethClient);

Your client is not connected! At least the client you're trying to use for the publishing inside the callback (which isn't the best idea anyway).