Need help rebuilding home automation raspberry pi

Here's what part of my Home Network looks like (as I think this part is very similar to yours)...


My local private MQTT broker is running on port 1883 on the RPi at 192.168.1.156
Node-RED is running on port 1880 also on 192.168.1.156
My Sonoff switch is running Tasmota firmware on port 80 at 192.168.1.50

As mentioned before, the best place to locate YOUR Mosquitto configuration file is in conf.d


The reason for this is... the mosquitto.conf file could get over-written when Mosquitto is updated - which means YOUR settings would be lost and would need to be inserted again.

Mosquitto will automatically look inside the folder named conf.d to see if there are any files in there that end with the suffix .conf If it finds any then it/they are loaded.

As you can see from my diagram I have my own file there called... myconfig.conf
You can give it any name you like as long as it ends with .conf

Here is what I have in my myconfig.conf file...

per_listener_settings true
user mosquitto

# Global limits
max_queued_messages 200
max_packet_size 65536  # 64Kb

# MQTT listener
listener 1883 192.168.1.156
allow_anonymous true

# Persistence
persistence true
persistence_file mosquitto.db
autosave_interval 900
autosave_on_changes false

# Logging
log_timestamp true

And to complete the picture, here is what the Tasmota settings look like in my SonOff switch...

Note-A: The above IP addresses are for my system - YOURS will be different.

Note-B: My home network is NOT exposed to the Internet (I use Tailscale for that).

Note-C: I use MQTT 3.1.1 protocol (as Tasmota does not support v5.0 protocol)

Note-D: My RPi runs headless - I just access Node-RED via a browser on my PC.

I hope this helps in getting your new system working.

2 Likes

for "dynamicdave"
Thanks so much for the reply. I learn something from everyone's help. I just haven't hit the magic button yet. I implemented your positioning of the .conf file. As much fooling around as I have been doing, I am amazed that I haven't erased it before now.
What is confusing to me is the fact that I have the old system sitting here working fine. It was built almost three years ago and my thought was to update and I never considered this much trouble. I used the latest revision to the course that produced the first system thinking it would function as before. I spent a good deal of time trying to wrap my head around this MQTT Explorer that everyone say I should be using. I finally managed to get it connected to the old system and it did show some results when connected to the old system. But I still don't see how it will point out the reasons for not working. I have enclosed a screenshot of some of the data from the old system.


The fact that something displayed there that I recognized was amazing.
And, Yes, your home network is identical to mine

Regards

Well not exactly everyone.

Not saying it's not a valuable tool, its just one more added complication.

I think that's about as far as i can go with this problem, sorry.

OK thanks for the extra info as that has helped to clarify some of this.

I think based on what you have described the old version of the custom firmware on your switches does not support MQTT 5.0

It would be an interesting exercise to make sure the MQTT in and out Nodes are configured for MQTT 3 or lower and that you have enabled the anonymous login on the Mosquitto broker.

It might be an idea to post a screenshot of the login to one of the Sonoff devices with the alternate firmware.

Craig

Questions...

  • Have you tried connecting the MQTT Explorer to your new system ?
  • Are the new and old systems on two different Raspberry Pi devices?
  • If so, have the two Raspberry Pi devices got different fixed IP addresses?
  • Which method are you using to give the devices a fixed IP address?
  • Are the Sonoff switches for your new system *new ones or are you using the ones from your old system?

As Craig said... it would be useful to post a screenshot of the login/settings page for the SonOff.

Earlier in this thread you posted a back-to-back simple test for your MQTT.
monday_mqtt_problem
I assume the MQTT-In and -OUT nodes are using your new broker.
Can you verify when you click the Inject node you see the result on the Debug ?
You should be able to also see this activity on the MQTT Explorer as well.

I assume your old system has a fixed IP of 192.168.1.151
What is the fixed IP address of your new system?

Note: Getting a MQTT broker up & running on a system doesn't normally cause this much grief.

Have you changed the IP address to match the fixed IP address of your new Raspberry Pi ?

I believe that he has said they have the same ip, and are not both on at the same time.

Let me answer some questions here this morning:

  1. both new and old have the same i/p addresses. I just do not run them at the same time. and I disconnect the ethernet cable of the one not in use.
  2. The simple test does not work on the new system. In fact , I don't see any activity in MQTTEX with new system.
  3. The sonoff devices are "old" from when the system was 2+yrs ago. code to follow.
  4. In the MQTT nodes configuration you have the choice of using "MQTT 3. (legacy) ,
    MQTT 3.1.1 or MQTT 5. I think that mine are set to 3.1.1
  5. I use the "nmtui" system to set the static i/p on the RPi.
https://rntlab.com/
This is the code to flash sonoff basic. This version of firmware
is for the devices used in home. They will be labeled 4004_xx with two digits.
They will return a state message to nodered. 
*****/

// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "XXXXXX";
const char* password = "XXXXXXXXX";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.1.151";

// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);

// GPIOs of your ESP8266 on your 4004_05
int gpio13Led = 13;
int gpio12Relay = 12;

// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
 delay(10);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.print("WiFi connected - ESP IP address: ");
 Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
 Serial.print("Message arrived on topic: ");
 Serial.print(topic);
 Serial.print(". Message: ");
 String messageTemp;
 
 for (int i = 0; i < length; i++) {
   Serial.print((char)message[i]);
   messageTemp += (char)message[i];
 }
 Serial.println();

 // Feel free to add more if statements to control more GPIOs with MQTT

`QUOTE of the DAY

I never imagined it would take all this to get this working . I have a bad feeling that I will find a misspell or number out of place.

Thanks again for all your help and ideas.

Can you connect to the new mqtt broker via MQTT Explorer or not?

The script you posted is incomplete โ€” it's missing:

  • The rest of callback() โ€” the logic that actually switches the relay based on the message
  • setup() โ€” where you initialize pins, serial, and call setup_wifi()
  • loop() โ€” where client.loop() is called and reconnection is handled
  • The MQTT subscription topic (e.g., home/4004_xx/command)
  • The state publish back to Node-RED that you mentioned

Show us a screenshot of MQTTExplorer connected to the new system please.

Although it's years since I have used Arduino code (as I currently use Micro-Python or Tasmota) - I managed to find an old script for a Sonoff S20 using an ESP8266 so you can compare YOUR script against it and hopefully find where the discrepancy is located.

Apart from the above and this listing - I'm out of ideas for now - sorry.

/*
 * Sonoff Basic / S20 โ€” MQTT + Node-RED Controller
 * Board : Generic ESP8266 (or "ITEAD Sonoff" if installed)
 * Flash  : 1 MB, DOUT mode
 *
 * Wiring / Pins (Sonoff Basic):
 *   GPIO0  โ€“ Button (active-LOW, with internal pull-up)
 *   GPIO12 โ€“ Relay  (HIGH = ON)
 *   GPIO13 โ€“ LED    (active-LOW)
 *
 * Dependencies (install via Library Manager):
 *   - PubSubClient  by Nick O'Leary
 *   - ArduinoJson   v6.x  (optional, for richer payloads)
 *
 * MQTT Topics:
 *   Subscribe  : cmnd/<DEVICE_ID>/power   โ†’ payload "ON" / "OFF" / "TOGGLE"
 *   Publish    : stat/<DEVICE_ID>/power   โ†’ "ON" / "OFF"
 *   Publish    : tele/<DEVICE_ID>/state   โ†’ JSON heartbeat every 30 s
 *
 * Node-RED flow hint:
 *   [inject "ON"] โ†’ [mqtt out: cmnd/sonoff01/power]
 *   [mqtt in: stat/sonoff01/power] โ†’ [debug / dashboard switch]
 */

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

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  USER CONFIG โ€” edit these
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
const char* WIFI_SSID     = "YourSSID";
const char* WIFI_PASSWORD = "YourWiFiPassword";

const char* MQTT_SERVER   = "192.168.1.100";   // broker IP or hostname
const int   MQTT_PORT     = 1883;
const char* MQTT_USER     = "";                // leave blank if not required
const char* MQTT_PASS     = "";
const char* DEVICE_ID     = "sonoff01";        // unique per device

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  PIN DEFINITIONS
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#define PIN_RELAY   12
#define PIN_LED     13
#define PIN_BUTTON   0

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  TOPIC HELPERS
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
String topicCommand;   // cmnd/<id>/power
String topicStat;      // stat/<id>/power
String topicTele;      // tele/<id>/state

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  GLOBALS
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
WiFiClient   wifiClient;
PubSubClient mqtt(wifiClient);

bool  relayState      = false;
bool  lastButtonState = HIGH;
unsigned long lastTele = 0;
const unsigned long TELE_INTERVAL = 30000; // ms

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  HELPERS
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void setRelay(bool on) {
  relayState = on;
  digitalWrite(PIN_RELAY, on ? HIGH : LOW);
  digitalWrite(PIN_LED,   on ? LOW  : HIGH);  // LED active-LOW
  mqtt.publish(topicStat.c_str(), on ? "ON" : "OFF", true); // retained
  Serial.printf("[RELAY] %s\n", on ? "ON" : "OFF");
}

void publishTelemetry() {
  char buf[128];
  snprintf(buf, sizeof(buf),
    "{\"device\":\"%s\",\"power\":\"%s\",\"rssi\":%d,\"uptime\":%lu}",
    DEVICE_ID,
    relayState ? "ON" : "OFF",
    WiFi.RSSI(),
    millis() / 1000
  );
  mqtt.publish(topicTele.c_str(), buf);
  Serial.printf("[TELE] %s\n", buf);
}

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  MQTT CALLBACK
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  String msg;
  for (unsigned int i = 0; i < length; i++) msg += (char)payload[i];
  msg.trim();
  Serial.printf("[MQTT IN] %s โ†’ %s\n", topic, msg.c_str());

  if (String(topic) == topicCommand) {
    if      (msg.equalsIgnoreCase("ON"))     setRelay(true);
    else if (msg.equalsIgnoreCase("OFF"))    setRelay(false);
    else if (msg.equalsIgnoreCase("TOGGLE")) setRelay(!relayState);
  }
}

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  Wi-Fi
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void connectWiFi() {
  Serial.printf("\n[WiFi] Connecting to %s", WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    // blink LED while connecting
    digitalWrite(PIN_LED, !digitalRead(PIN_LED));
  }
  digitalWrite(PIN_LED, HIGH); // LED off (active-LOW)
  Serial.printf("\n[WiFi] Connected โ€” IP: %s\n", WiFi.localIP().toString().c_str());
}

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  MQTT
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void connectMQTT() {
  while (!mqtt.connected()) {
    Serial.printf("[MQTT] Connecting to %s:%d โ€ฆ ", MQTT_SERVER, MQTT_PORT);

    String clientId = String("sonoff-") + DEVICE_ID;

    // Last-will message so Node-RED knows if the device drops off
    bool ok = mqtt.connect(
      clientId.c_str(),
      MQTT_USER, MQTT_PASS,
      topicStat.c_str(), 1, true, "OFFLINE"
    );

    if (ok) {
      Serial.println("connected");
      mqtt.subscribe(topicCommand.c_str());
      // Announce current state
      mqtt.publish(topicStat.c_str(), relayState ? "ON" : "OFF", true);
    } else {
      Serial.printf("failed (rc=%d) โ€” retry in 5 s\n", mqtt.state());
      delay(5000);
    }
  }
}

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  SETUP
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void setup() {
  Serial.begin(115200);

  // Build topic strings once
  topicCommand = String("cmnd/") + DEVICE_ID + "/power";
  topicStat    = String("stat/") + DEVICE_ID + "/power";
  topicTele    = String("tele/") + DEVICE_ID + "/state";

  pinMode(PIN_RELAY,  OUTPUT);
  pinMode(PIN_LED,    OUTPUT);
  pinMode(PIN_BUTTON, INPUT_PULLUP);

  digitalWrite(PIN_RELAY, LOW);
  digitalWrite(PIN_LED,   HIGH); // off

  connectWiFi();

  mqtt.setServer(MQTT_SERVER, MQTT_PORT);
  mqtt.setCallback(mqttCallback);
  mqtt.setKeepAlive(60);

  connectMQTT();
  publishTelemetry();
}

// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
//  LOOP
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
void loop() {
  // Maintain connections
  if (WiFi.status() != WL_CONNECTED) connectWiFi();
  if (!mqtt.connected())             connectMQTT();
  mqtt.loop();

  // Physical button โ€” toggle on falling edge
  bool buttonNow = digitalRead(PIN_BUTTON);
  if (lastButtonState == HIGH && buttonNow == LOW) {
    delay(50); // debounce
    if (digitalRead(PIN_BUTTON) == LOW) {
      setRelay(!relayState);
    }
  }
  lastButtonState = buttonNow;

  // Periodic telemetry
  if (millis() - lastTele >= TELE_INTERVAL) {
    lastTele = millis();
    publishTelemetry();
  }
}

Then either your broker is misconfigured OR the new device has a firewall preventing access.

I would note that just today, I had Mosquitto fail on me after an update. There were 2 issues I've not seen before. First was a missing folder, no idea how it disappeared. Second was due to an old certificate reference that is no longer included in Linux. So do check your config. In my case, even a config check using sudo -u mosquitto /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -v didn't find the issue though the mosquitto log did. The first issue was harder to track down because there was no clear log entry.

As for a firewall, I wouldn't normally expect to see one on a Pi but it is possible I suppose. There are 2 possibilities iptables and ufw.

More replies. Using identical settings in MQTTEX, I am not able to connect to the broker of the new system. When I click on "Connect" I get a big red error msg in lower left corner saying disconnected. when I switch to the old system, I get results shown in a post a few back. There is no firewall connected to the RPi. Here is the code for the sonoff device

/*****
 
 All the resources for this project:
 https://rntlab.com/
 This is the code to flash sonoff basic. This version of firmware
 is for the devices used in home. They will be labeled 4004_xx with two digits.
 They will return a state message to nodered. 
*****/

// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "XXXXX";
const char* password = "XXXXXXXX";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.1.151";

// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);

// GPIOs of your ESP8266 on your 4004_05
int gpio13Led = 13;
int gpio12Relay = 12;

// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic home/office/xxxxx, you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
  if(topic=="home/office/4004_05"){
      Serial.print("Changing 4004_05 to ");
      if(messageTemp == "on"){
        digitalWrite(gpio13Led, LOW);
        digitalWrite(gpio12Relay, HIGH);
        Serial.print("On");
        client.publish("home/office/4004_05/state","on");
      }
      else if(messageTemp == "off"){
        digitalWrite(gpio13Led, HIGH);
        digitalWrite(gpio12Relay, LOW);
        Serial.print("Off");
       client.publish("home/office/4004_05/state","off");
      }
  }
  Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    /*
     YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
     To change the ESP device ID, you will have to give a new name to the ESP8266.
     Here's how it looks:
       if (client.connect("ESP8266Client")) {
     You can do it like this:
       if (client.connect("ESP1_Office")) {
     Then, for the other ESP:
       if (client.connect("ESP2_Garage")) {
      That should solve your MQTT multiple connections problem
    */
    if (client.connect("4004_05")) {
      Serial.println("connected");  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more 4004_05s)
      client.subscribe("home/office/4004_05");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
  // preparing GPIOs
  pinMode(gpio13Led, OUTPUT);
  digitalWrite(gpio13Led, HIGH);
  
  pinMode(gpio12Relay, OUTPUT);
  digitalWrite(gpio12Relay, HIGH);
  
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

// For this project, you don't need to change anything in the loop function. 
// Basically it ensures that you ESP is connected to your broker
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("4004_05");
}

Thanks again for looking

Please post the file (from the new system) /etc/mosquitto/mosquitto.conf and any files in the folder /etc/mosquitto/conf.d (apart from README), and tell us what the file(s) are called. Copy/paste the text please, not screenshot

Also show us what this command shows
sudo systemctl status mosquitto

1 Like

As someone said it just shouldn't be this hard. Maybe it's time to consider wiping the system and starting over. That way the forum could look over his shoulder as he installed stuff.

Clearly the issue isn't the sonoff device since you've already said that you cant connect from MQTTEX on your PC either.

As well as what Colin said above, it might be useful to have look at Mosquitto's log file to see if there are any issues.

On a Raspberry Pi it will probably be located at /var/log/mosquitto
It's owned by root so you will have to use sudo to inspect it.

e.g. sudo cat /var/log/mosquitto/mosquitto.log

Just as a suggestion maybe remove and reinstall mosquitto, there just isn't much to it so maybe worth a try, here are my notes on how to install that I use (a lot)

remove mosquitto
sudo apt-get purge --remove mosquitto
then reboot just because

install mosquitto
sudo apt-get install mosquitto mosquitto-clients -y

then you need to modify for anonymous access,

I would not put in a use and password until it works, and I never use a password as before anyone can get in range of my wifi the dogs will eat them.

sudo nano /etc/mosquitto/mosquitto.conf
add the following to the bottom of the file
allow_anonymous true
listener 1883
make sure you save when exiting

sudo service mosquitto start

then
sudo systemctl status mosquitto
should say mosquitto running

do
hostname -I
will give you IP for pi

open MQTT explorer and point at that IP
should connect even if you won't get messages

I would also ping the PI from the box running MQTT explorer just to confirm connectivity

give it a try

1 Like

The morning is gone as well as half of the afternoon and I am no further along than yesterday. There may be some advancements as seen in some activity explained later. I started with "Gerry's" advice and started with a new image on the Pi using the 64bit OS called OS (lite). And then following the instructions of the RNTlab course v1.7, I installed NodeRed v4.1.8. I added the required nodes per the instructions using the deprecated version of the ui nodes and couple more such as "timerswitch " and "bigtimer". And then proceeded with Mosquitto. Everything went as advertised.



I finally managed to fumble around with MQTTEX and got this shot.

First of all I took the shot at the wrong time but what I proved to myself was that I could change the result on the line "4004_05 = on/off" by clicking the buttons on the two inject nodes in the flow above. But the only way I can change the "state= line" is to publish that message right down there in the lower right corner.

In response to the latest posts above I checked the log suggested. I don't that was the log you meant because here is a sampling of those results.


This appears to be the log of the broker listening to that i/p and recording all the devices around the house. I'm not sure why that is happening but I am not going to mess with that as that system still works.

I think my next attack is going to be taking another switch and flashing it with different i/p so that I can run both systems at the same time.

Thanks again for the help