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?


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

/*********WiFi settings*******/
#define WLAN_SSID "PrettyFlyForA-WIFI"
/*********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("] ");
  for (int i = 0; i < length; i++) {

  // 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() {
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.print("[FUNCTION] Wifi Failed. SSID: ");
    Serial.print(" or pass: ");
    Serial.println(" incorrect!");
    digitalWrite(D4, LOW);                                    // BLINK LED TO SHOW SOMETHING IS WRONG
    digitalWrite(D4, HIGH);
  Serial.print("[SETUP] Wifi connected. IP: ");
  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
      Serial.println("[MQTT] reconnect function - subscribe to topic");
    } else {
      Serial.print("failed, rc=");
      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

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

  client.setServer(mqtt_server, mqtt_port);

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

  // MQTT
  if (!client.connected()) {

  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: ");
    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:
[MQTT] Publish message: SOF
[LOOP] Wifi connected! SSID: PrettyFlyForA-WIFI IP-adres:

I publish:

/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.