MQTT Reconnection Loop

So i got to a point where everything was working. to a certain point.

now it looks like i altered something in Openhab (possible the .cfg file) that the esp01 module connects and disconnects. and goes on an endless loop. it was doing nothing like this before.

so in my troubleshooting i have notice that it stops trying to reconnect and stays connected when i stop the openhab server.

i subscribed to the topic, and with openhab server not running, i received fine with no problems, but ofcourse my sitemap was not up.

then i restarted the openhab server and it starts the loop again.

i installed using the apt get method.

any insights?

Is your esp and openHAB trying to connect to the MQTT broker with the same client id? This would cause the other to disconnect each time a connection attempt was made.

not sure what you are asking.

here is my openhab.cfg

mqtt:garage.url=tcp:// mqtt:garage.clientId=MyMQ
mqtt:garage.usr=dwayne mqtt:garage.pwd=raspberry
mqtt:mqttbroker.url=tcp:// mqtt:mqttbroker.clientId=MyMQTT
mqtt:mqttbroker.usr=dwayne mqtt:mqttbroker.pwd=raspberry
mqtt:localbroker.url=tcp:// mqtt:localbroker.clientId=MyMQT
mqtt:localbroker.usr=dwayne mqtt:localbroker.pwd=raspberry
weather:location.home.woeid=2450022 weather:location.home.provider=Yahoo

# PATH TO openhab
OPENHABPATH = /etc/openhab/

# Set ports for HTTP (S) server
HTTP_PORT = 8080
https_port = 8443

and the init.lua program

-- Garage Door controller version 2/15/15
-- GPIO0 is connected to switch with internal pulldown enabled  
--GPIO2 is connected to Relay

print("Program Start")

-- Start up mqtt
m = mqtt.Client("ESP1", 120, "dwayne", "raspberry")
m:lwt("/lwt", "offline", 0, 0)
m:connect("", 1883, 0, function(conn) print("mqtt connected")
   m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1") 

-- Reconnect to mqtt server if needed
m:on("offline", function(con) print ("reconnecting...")
   tmr.alarm(1, 10000, 0, function()
      m:connect("", 1883, 0, function(conn) print("mqtt connected")
         m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1") 

 -- Switch Trigger
gpio.trig(3, "both",function (level)
   state =
   print("Sent openhab/garage/switch1 " .. state )    

-- MQTT Message Processor
m:on("message", function(conn, topic, msg)   
   print("Recieved:" .. topic .. ":" .. msg)   
   if (msg=="GO") then  -- Activate Door Button
      print("Activating Door")   
      tmr.delay(1000000) -- wait 1 second
      print("Invalid - Ignoring")   

Each client (for example your esp module is one client, openhab is a different client) must connect to the mqtt broker with a unique client id.

In the screen dump in your initial post, you have set the clientId to ESP1 in one of your connections, which seems to match the ESP1 in the init.lua program (I think this is the clientId, but I don’t have the api to hand). This would cause an error.

The simplest solution is to remove the clientId line from the as by default openhab generates a fairly unique client id to connect with.

I’m not sure why you have defined several connections to the same broker (IP address and port). You only need one.

Also, I seem to remember generating the client id from the chip identifier on the ESP module, to make sure that is unique, for when you get more ESP modules connected.

And another thought: the broker should produce an error log of what went wrong. You don’t say which broker you are using but mosquitto produces a log in /var/log/mosquitto (on debian type systems)

Thank you for your help. The reason is, I didn’t know which one worked. And I figured the client I’d need to match for them to communicate.

I’m going to try eliminating the client id.