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://10.0.0.84:1883 mqtt:garage.clientId=MyMQ
mqtt:garage.usr=dwayne mqtt:garage.pwd=raspberry
mqtt:mqttbroker.url=tcp://10.0.0.84:1883 mqtt:mqttbroker.clientId=MyMQTT
mqtt:mqttbroker.usr=dwayne mqtt:mqttbroker.pwd=raspberry
mqtt:localbroker.url=tcp://10.0.0.84:1883 mqtt:localbroker.clientId=MyMQT
mqtt:localbroker.usr=dwayne mqtt:localbroker.pwd=raspberry
weather:location.home.woeid=2450022 weather:location.home.provider=Yahoo
weather:location.home.language=de
weather:location.home.updateInterval=10
mqttitude:home.lat=-80.385855
mqttitude:home.lon=025.610323
mqttitude:geofence=100
mqtt:mosquitto.url=tcp://localhost:1883

# 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 pete@hoffswell.com
-- GPIO0 is connected to switch with internal pulldown enabled  
gpio.write(3,gpio.LOW)
gpio.mode(3,gpio.INPUT,gpio.PULLDOWN)  
--GPIO2 is connected to Relay
gpio.mode(4,gpio.OUTPUT)  
gpio.write(4,gpio.HIGH)  

print("Program Start")

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

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

 -- Switch Trigger
gpio.trig(3, "both",function (level)
   state = gpio.read(3)
   m:publish("openhab/garage/switch1",state,0,0)
   print("Sent openhab/garage/switch1 " .. state )    
end)

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

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 openhab.org 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.

Edit:
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.

Thanks