Controlling a relay from mqtt -> no incoming messages

I wrote a sketch to control a relay from an MQTT-topic. It’s based on this example. I use a wemos D1 mini version 3.0.0.

It publishes to MQTT. All well there. But when i send a “1” into the topic is is subscribed to, nothing happens. Not even the serial monitor. I added several ‘print to serial’ lines to check it all with no effect.

I have putt the code block to top and bottom with no effect.

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

My plan is to control a lighttube in my backyard with it. I even programmed the onboard LED to blink when a connection is lost to either wifi or MQTT. Maybe it’s usefull to someone?

Code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

/*********WiFi settings*******/
#define WLAN_SSID "PrettyFlyForA-WIFI"
#define WLAN_PASS "XXXXXXXXX"
/*********MQTT Settings*******/
#define mqtt_server "XXXXXXX"
#define mqtt_port 1883                   // use 8883 for SSL
#define mqtt_user "XXXXXXXX"
#define mqtt_password "XXXXXXXXX"

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];

// -------------------------------- MQTT CALLBACK --------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(D7, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(D7, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}
// -------------------------------- WIFI SETUP --------------------------------
void setup_wifi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.print("[FUNCTION] Wifi Failed. SSID: ");
    Serial.print(WLAN_SSID);
    Serial.print(" or pass: ");
    Serial.print(WLAN_PASS);
    Serial.println(" incorrect!");
    digitalWrite(D4, LOW);                                    // BLINK LED TO SHOW SOMETHING IS WRONG
    delay(50);
    digitalWrite(D4, HIGH);
    delay(800);
  }
  Serial.print("[SETUP] Wifi connected. IP: ");
  Serial.println(WiFi.localIP());
  digitalWrite(D4, HIGH);
}
// -------------------------------- MQTT RECONNECT --------------------------------
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("[MQTT] Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "NODE-Lichtslang";
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println(" Connected!");
      // Once connected, publish an announcement...
      String ipaddress = WiFi.localIP().toString();
      char ipchar[ipaddress.length() + 1];
      ipaddress.toCharArray(ipchar, ipaddress.length() + 1);
      client.publish("/huis/ip/lichtslang", ipchar);                // PUBLISH (NEW) IP-ADRESS TO TOPIC
      // ... and resubscribe
      client.subscribe("/huis/schakeling/lichtslang");
      Serial.println("[MQTT] reconnect function - subscribe to topic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      setup_wifi();                                               // INCASE THE WIFI HAS DISCONNECTED WE JUMP TO WIFI (RE)CONNECT
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// -------------------------------- BOARD SETUP ON START --------------------------------
void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("Setup EXEC");
  pinMode(D4, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
  delay(10);
  digitalWrite(D4, LOW);
  digitalWrite(D7, HIGH);
  digitalWrite(D8, HIGH);
  WiFi.hostname("NODE-Lichtslang");
#define mac  WiFi.macAddress()

  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

// -------------------------------- LOOP FOREVER --------------------------------
void loop() {
  // WIFI
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("[LOOP] Wifi connected! SSID: ");
    Serial.print(WLAN_SSID);
    Serial.print(" IP-adres: ");
    Serial.println(WiFi.localIP());
    digitalWrite(D4, HIGH);
    delay(15000);
  }

  // MQTT
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 10000) {
    lastMsg = now;
    snprintf (msg, 75, "SOF");                                  // SEND SIGN OF LIFE EVERY 10 SECONDS (AND AVOID TIME_OUTS)
    Serial.print("[MQTT] Publish message: ");
    Serial.println(msg);
    client.publish("/huis/status/lichtslang", msg);
  }
  //  client.setCallback(callback);
}

Serial monitor shows:

Setup EXEC
[MQTT] Attempting MQTT connection... Connected!
[MQTT] reconnect function - subscribe to topic
[LOOP] Wifi connected! SSID: PrettyFlyForA-WIFI IP-adres: 192.168.1.74
[MQTT] Publish message: SOF
[LOOP] Wifi connected! SSID: PrettyFlyForA-WIFI IP-adres: 192.168.1.74

I publish:

/huis/ip/lichtslang 192.168.1.74
/huis/status/lichtslang SOF
/huis/schakeling/lichtslang 1
/huis/schakeling/lichtslang 0
/huis/schakeling/lichtslang 1
/huis/schakeling/lichtslang 0

Serial monitor shows no incoming messages what ever i try…

Although i can program in bash, php, mysql… I consider myself as a beginner in C++ language. :sweat_smile:

All help is welcome! :smiley:

Small addition: I tried several older versions of the PubSubClient.h

client.subscribe() returns true or false. Why are you ignoring that? Maybe you are not actually subscribing, like you think you are.