4

I am working on esp8266 and trying to connect to test.mosquitto.org. here is what I got from net

m = mqtt.Client("clientid", 60, "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("http://test.mosquitto.org/", 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();

I am not sure from where to get clientId ,user and pass,

here what I am getting : DNS retry 1! DNS retry 2! DNS retry 3! DNS retry 4! DNS Fail!

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Ankit jhunjhunwala
  • 155
  • 1
  • 1
  • 11
  • The answer was given by @hardillb. Also have a look at the example at https://github.com/nodemcu/nodemcu-firmware#connect-to-mqtt-broker – Marcel Stör Jan 06 '16 at 11:34

3 Answers3

7

The problem is the http:// at the start of the connect string and the / at the end

The connect command wants just a hostname not a URL and even if it did you would want to pass tcp://test.mosquitto.org or mqtt://test.mosquitto.org

...
m:connect("test.mosquitto.org", 1883, 0, function(conn) print("connected") end)
...

Also as an aside, your topics should not start with a /, this just adds an extra unnecessary null to the start of the topic tree.

hardillb
  • 54,545
  • 11
  • 67
  • 105
3

Try with the IP instead of the name:

m:connect("85.119.83.194", 1883, 0, function(conn) print("connected") end)

if that works that means that you are having a problem resolving the name of the website (for whatever reason).

ProgrammerV5
  • 1,915
  • 2
  • 12
  • 22
  • This works for me. I am running my client on RPi. Could you please guide me how can resolve the issue for resolving the name? – Ashish Sharma Mar 01 '20 at 08:10
0

RE the original question: I use it without the last arguments as

m = mqtt.Client(clientID, 60)

and clientID is any name you want, to distinguish yourself from other clients (e.g. "Ankit").

The topic should be more descriptive of the payload (e.g. "message") and not a generic /topic.

Also note the earlier answers, for the connection use an IP (e.g. "85.119.83.194") or a hostname (e.g. "test.mosquitto.org") of the server.

HTH

Eyal
  • 61
  • 4