-->
Page 1 of 3

ESP8266 cannot connect to local mosquitto server

PostPosted: Mon May 11, 2015 11:58 am
by viktak
HI guys,

I have the following setup for testing my first project with ESP8266 and MQQT:

I have a Mosquito server installed on a Raspberry Pi - this will be the production server (once the project finishes)
I also have an old laptop with Mosquitto server installed - this is only for testing, especially to troubleshoot the problem I am describing here.
Finally, I have an ESP8266 running LUA code on it.

This is what is working so far:
I can publish messages from both the RPi and the laptop to themselves, to each other and to test.mosquitto.org.
I can subscribe to and receive messages on the RPi, on the laptop as well as on the ESP8266.
I can publish messages from the ESP8266 to test.mosquitto.org.

The only thing that's not working is to publish messages from the ESP8266 to any of my local Mosquitto installations, i.e. the RPi and the laptop.

In fact, the problem seems to be with the connection, as my code doesn't get past the point where it tries to connect to the server.

I find this strange, as normally stuff works on the local network and one might have more difficulty getting it right over the internet.

This is the code that connects perfectly to test.mosquitto.org and fails to connect locally:

Code: Select all--MQTTserver="192.168.1.138"  -- when not commented out, this does not work
--MQTTserver="test.mosquitto.org"   -- when not commented out, this works

m:connect(MQTTserver, 1883, 0, function(con2)
                        print("Connected.")
                        m:publish("viktak/test","Sensor online.",0,0, function(con2)
                         m:subscribe("viktak/test",0, function(con2)
                         end)
                        end)
                    end)


Any idea what might be the case? Can it be that the IP address should be specified in a different format? If yes, then what?

Thanks for any pointers!

Re: ESP8266 cannot connect to local mosquitto server

PostPosted: Mon May 11, 2015 4:27 pm
by AcmeUK
If I remember correctly you need to specify port 1883 in your network address ie
MQTTserver="192.168.1.138:1883"

Re: ESP8266 cannot connect to local mosquitto server

PostPosted: Mon May 11, 2015 10:44 pm
by j0hncc
viktak wrote:HI guys,
...
The only thing that's not working is to publish messages from the ESP8266 to any of my local Mosquitto installations, i.e. the RPi and the laptop.
In fact, the problem seems to be with the connection, as my code doesn't get past the point where it tries to connect to the server.
...
This is the code that connects perfectly to test.mosquitto.org and fails to connect locally:
Code: Select all--MQTTserver="192.168.1.138"  -- when not commented out, this does not work
--MQTTserver="test.mosquitto.org"   -- when not commented out, this works

m:connect(MQTTserver, 1883, 0, function(con2)
                        print("Connected.")
                        m:publish("viktak/test","Sensor online.",0,0, function(con2)
                         m:subscribe("viktak/test",0, function(con2)
                         end)
                        end)
                    end)


Any idea what might be the case? Can it be that the IP address should be specified in a different format? If yes, then what?

Thanks for any pointers!


I'm no nodemcu guru, but...
You did not show any call to the mqtt.Client() function, do you have it?
Does your output "Connected." show up on your console?
Are you sure your local mosquitto is listening on 1883?
Does anything show up in your local mosquitto logs? Also , turn debug on in your mosquitto conf file.
Your subscribe is in the callback to your publish, which seems out of place to me.
Namely, with retain=0, if you subscribe to topic AFTER you've published to it you may/should not receive THAT message.
Your subscribe callback function is empty so when you receive a message you ignore it? (Is that "working perfectly on test.mosquitto.org" ?? :) )
Have you tried a structure more like the example in the nodemcu API ref:

Code: Select allm = mqtt.Client("clientid", 120, "user", "password")
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
m:on("message", function(conn, topic, data)
  print(topic .. ":" )
  if data ~= nil then
    print(data)
  end
end)

m:connect("192.168.11.118", 1883, 0, function(conn) print("connected") end)
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();


Cheers,
John

Re: ESP8266 cannot connect to local mosquitto server

PostPosted: Tue May 12, 2015 12:58 am
by viktak
AcmeUK wrote:If I remember correctly you need to specify port 1883 in your network address ie
MQTTserver="192.168.1.138:1883"


Nope, the port is specified separately, see my code in the original post. However, I did try it the way you showed but now it's even worse: I am getting a "DNS Fail!" error.