Problem with devices connected by mqtt

Hi. How are they?
My English is not very good
I need help with a problem that I cannot solve.
I have two devices:

  • Wemos: connected through a mqtt link, with which I control 7 lights.
  • Nodemecu: you have a DHT22 sensor connected through the MQTT link.
    If only one of the devices is connected, it works perfect. But if I connect the two, it stops working immediately.
    In the log viwer if it shows me the temperature and the change of the switches, but does not perform the actions on the devices. What will be the problem?
    The server is mounted on a raspberry pi 3 b +, I am not using the latest update from openhab, but the previous one.
    If you can give me an orientation of the problem, I would greatly appreciate it.

A few questions which may help us to help you:

What software/firmware is running on your Wemos and your NodeMCU?

Connected to what? Your MQTT broker? And what stops working?

Is this the problem you describe above, or a different problem? Is the problem that you cannot actuate your relays through openHAB? But you can actuate if only one device is attached?

Can you show us your Thing and Item configurations?

In general we are going to need a complete description of exactly what is not working, how your system is setup (configurations), and what you have tried already!

When sharing text configurations, please use code fences: sandwich your text between three backticks

```
like this
example
```

which will look like this:

like this
example

That would point to a possible duplicate MQTT device ID.

Without knowing all your settings we can’t tell.

1 Like

Thanks for answering so quickly. I have not updated the firmware of any of the devices, I am using them with the factory settings.
I am using mosquito-linked mqtt link.
The items are:

Number Tempcasa "Indoor temperature [% .0f ° C]" <temperature> {channel = "mqtt: topic: Temperature: Temperature"}
Number Humcasa "Relative humidity [% .0f %%]" <humidity> {channel = "mqtt: topic: Humidity: Humidity"} 


EXTERIOR LIGHT switch "Front patio light" <light> {channel = "mqtt: topic: 0ce450f9: LuzPatio"}
Switch LUZ4 "Luz 4" <light> {channel = "mqtt: topic: 5cb896d7: LUZ4"}
Switch LUZ5 "Luz 5" <light> {channel = "mqtt: topic: 1d6e677f: LUZ5"}
Switch LUZ6 "Luz 6" <light> {channel = "mqtt: topic: 79b7657b: LUZ6"}
Switch LUZ7 "Luz 7" <light> {channel = "mqtt: topic: f93aefd9: LUZ7"}

SITEMAPS

sitemap Casa label="Main Menu" {

   Frame 
	{
	Default item=Datos_NTP label="Fecha"
	Default item=Datos_NTP1 label="Hora"
	}

   Frame
        {
        Text label="Casa" icon="group"
                        {
			             Text item=Tempcasa 
                         Text item=Humcasa 
						 Switch item=SENSORdeLUZ
                         Switch item=LUZEXTERIOR
						 Switch item=LUZ4
						 Switch item=LUZ5
						 Switch item=LUZ6
						 Switch item=LUZ7
                        }
        }

   Frame
        {
        Text label="Taller" icon="group"
                        {
                        Switch item=LUZCHURRASQUERA
                        Switch item= SENSORDEMOVIMIENTOTALLER
                        }
        }

    Frame 
	{
	Text label="Clima" icon="sun"
                        {
                        Text item=Temperature label= "Temperatura" icon= "temperature" 
                        }
	}



   Frame
	{
  	Text label="Datos astronómicos" icon="sun"
			{
			Text item=AMANECER
                        Text item=ANOCHECER
                        Text item= ESTACIONES
                        Text item= SIGNOS
	                }
        }

The file of things I modify it from the paper ui

Please edit your post to use code fences. As shown, code fences are three back ticks ```, not speech or quote marks "

We need to see this, as this is the link to outside of openHAB.

Do you really have spaces after the colons? Does that work? This would normally be

mqtt:topic:5cb896d7:LUZ4

Additionally, the Temperature and Humidity Items point to slightly unusually named Thing Channels, especially if using PaperUI. Again, let’s see your Thing configurations to check.

Great! So forget about openHAB for a minute, and prove that your esp devices are properly connected to Mosquitto, and you’re receiving the correct data on the correct topics. If you paste those logs here (using code fences!), we can compare them to your Thing configurations.

Can you also prove that your esp devices themselves are identifying to Mosquitto with unique names?

What exactly is not working?

1 Like

Hello how are you?
I’m trying to solve the problem but I still can’t find the solution, I pass my complete configuration:









ITEMS

Switch LUZ3 "Luz 3"<light>{mqtt=">[mosquitto:CASA/LUZ3:command:ON:ON3],>[mosquitto:CASA/LUZ3:command:OFF:OFF3]"}
Switch LUZ4 "Luz 4"<light>{mqtt=">[mosquitto:CASA/LUZ4:command:ON:ON4],>[mosquitto:CASA/LUZ4:command:OFF:OFF4]"}

Number Tempcasa "Temperatura interior [%.0f °C]" <temperature> {mqtt="<[mosquitto:CASA/TEMPERATURA:state:default]"}
Number Humcasa "Humedad relativa [%.0f %%]" <humidity> {mqtt="<[mosquitto:CASA/HUMEDAD:state:default]"}

SITEMAPS

sitemap Casa label="Main Menu" {


   Frame 
	{
	Default item=Datos_NTP label="Fecha"
	Default item=Datos_NTP1 label="Hora"
	}

   Frame
        {
        Text label="Casa" icon="group"
                        {
			             Text item=Tempcasa 
                         Text item=Humcasa 
						 Switch item=SENSORdeLUZ
                         Switch item=LUZ3
						 Switch item=LUZ4
						 Switch item=LUZ5
						 Switch item=LUZ6
						 Switch item=LUZ7
                        }
        }

   Frame
        {
        Text label="Taller" icon="group"
                        {
                        Switch item=LUZCHURRASQUERA
                        Switch item= SENSORDEMOVIMIENTOTALLER
                        }
        }

    Frame 
	{
	Text label="Clima" icon="sun"
                        {
                        Text item=Temperature label= "Temperatura" icon= "temperature" 
                        }
	}



   Frame
	{
  	Text label="Datos astronómicos" icon="sun"
			{
			Text item=AMANECER
                        Text item=ANOCHECER
                        Text item= ESTACIONES
                        Text item= SIGNOS
	                }
        }

CODE WEMOS

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


#define relay3 4     //D2  // LUZ4
#define relay4 0     //D3  // LUZ5
#define mqtt_user "openhabian"
#define mqtt_password "leo"

#define wifi_ssid "Speedy-C8A471"     // "Nombre de tu WiFi"
#define wifi_password "3015289896"    // "Password de tu WiFi"
#define mqtt_server "192.168.1.43"    // "IP de tu Raspberry"
#define mqtt_port 1883
String deviceID = "esp-relay"; 


#define intopic3 "CASA/LUZ3"
#define intopic4 "CASA/LUZ4"


WiFiClient espClient;

PubSubClient client;

void setup() {
    

    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);
        
    Serial.begin(115200);
    setup_wifi();
    client.setClient(espClient);
    client.setServer(mqtt_server, mqtt_port);
    client.setCallback(callback);
    
}

void setup_wifi() {
    
    delay(10);
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(wifi_ssid);

    WiFi.begin(wifi_ssid, wifi_password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}


void reconnect() {
  
    
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        if (client.connect(deviceID.c_str(), mqtt_user, mqtt_password)) {
            Serial.println("connected");
            client.subscribe(intopic3);
            client.subscribe(intopic4);
           
            
        
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            
            
            delay(5000);
        }
    }
}


void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Mensaje recibido [");
    Serial.print(topic);
    Serial.print("] ");
    String(receivedChar) = "";
    for (int i = 0; i < length; i++) {
        receivedChar += (char)payload[i];
    }
    Serial.println(receivedChar);
 

    if (receivedChar == "OFF3")
        {
        digitalWrite(relay3, LOW);
        }
    if (receivedChar == "ON3"){
        digitalWrite(relay3, HIGH);     
        }

/////////////////////////////

    if (receivedChar == "OFF4")
        {
        digitalWrite(relay4, LOW);
        }
    if (receivedChar == "ON4")
        {
        digitalWrite(relay4, HIGH);     
        }

Serial.println();
}



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

CODE NODEMUECU

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

#define DHTPIN D5     

#define wifi_ssid "Speedy-C8A471"     
#define wifi_password "3015289896"    

#define mqtt_server "192.168.1.43"    
#define mqtt_port 1883
#define mqtt_user "openhabian"
#define mqtt_password "leo"     
String deviceID = "esp-sensor"; 

#define humidity_topic "CASA/HUMEDAD"         
#define temperature_topic "CASA/TEMPERATURA"

#define DHTTYPE DHT22   // DHT 22  (AM2302)


WiFiClient espClient;
PubSubClient client;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    setup_wifi();
    client.setClient(espClient);
    client.setServer(mqtt_server, mqtt_port);
    client.setCallback(callback);
    dht.begin();
}

void setup_wifi() {
    
    delay(10);
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(wifi_ssid);

    WiFi.begin(wifi_ssid, wifi_password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void reconnect() {
    
    while (!client.connected()) {
            Serial.print("Attempting MQTT connection...");
                if (client.connect(deviceID.c_str(), mqtt_user, mqtt_password)) {
            Serial.println("connected");
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            delay(5000);
        }
    }
}


long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;


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++) {
        char receivedChar = (char)payload[i];
        Serial.print(receivedChar);
    }
    Serial.println();
}

void loop() {
    if (!client.connected()) {
        reconnect();
    }
    client.loop();
    long now = millis();


    if (now - lastMsg > 50000) {

            float hum = dht.readHumidity();
            float temp = dht.readTemperature(false);
            Serial.println(String(temp).c_str());
            client.publish(temperature_topic, String(temp).c_str(), true);
            Serial.println(String(hum).c_str());
            client.publish(humidity_topic, String(hum).c_str(), true);
    }}

These are not the Items you showed before.
For some reason, you have now configured them for MQTT binding version 1 syntax.
This will never work with the MQTT binding version 2, which uses channels instead, the way you had it before.

Hello
If I had changed them, I already placed it as I had it before but it still does not work.

Switch LUZ3 "Luz 3"<light>{channel="mqtt:topic:c5f64c21:LUZ3"}
Switch LUZ4 "Luz 4"<light>{channel="mqtt:topic:c5f64c21:LUZ4"}

Number Tempcasa "Temperatura interior [%.0f °C]" <temperature> {channel="mqtt:topic:f70842be:TEMPERATURA"}
Number Humcasa "Humedad relativa [%.0f %%]" <humidity> {channel="mqtt:topic:f70842be:HUMEDAD"}

You have shown several screnshots of your esp relay and sensor, but not the one showing the config of the thing where the bridge gets selected. That would be the bl6e pencil ABOVE the channels.

I think Jürgen was right. @Leandro_Villarreal are you using the exact same code on all of the devices? If so, you will need to change the deviceID to be different for each one.

When a second device tries to connect to your MQTT broker, the first will be disconnected. When the first one gets disconnected it will try to reconnect and kick the second one off. They will continue to disconnect each other until your turn one of them off.

Hello
I also feel that it may be a duplicate deviceID problem, I am looking at how to solve it.
In the images I show you the configuration that you asked me.


The deviceID is assigned in your code that you upload to the ESP device. Why don’t you use something like Tasmota instead? It would make things easier I think.

The deviceID is something set in each of your Wemos and NodeMCU devices, and used by them to communicate with your MQTT broker.

You will not see any of that looking in openHAB. Although openHAB does have its own deviceID, part of the Bridge thing settings. That does need to be unique and different from Wemos etc. By default, it makes up a random one.

Thank you very much I will try that technique

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