Chat freely about anything...

User avatar
By viktak
#17225 @John: Yes, I have all the necessary parts - the code I am showing in this post is just the relevant part, I cut out the non relevant bits. The code, in general, must be good, because it works on a public server (test.mosquitto.org).

Nope, "Connected" doesn't get shown on the console.
Yes, the IP address and port settings are correct, verified, and working from other boxes (RPi, laptop).
I tried your code example, it cannot connect either to the local mosquito servers, but connects fine to tes.mosquitto.org.

This is what I find in the logs:
Message from my laptop to the RPi:
Code: Select allMay 12 12:04:04 dev mosquitto[2316]: New connection from 192.168.1.138.
May 12 12:04:04 dev mosquitto[2316]: New client connected from 192.168.1.138 as mosq_pub_4037_dell-lat.

Message from ESP8266 to RPi:
Code: Select allMay 12 12:06:07 dev mosquitto[2316]: New connection from 192.168.1.141.
May 12 12:06:07 dev mosquitto[2316]: Invalid protocol "MQTT" in CONNECT from 192.168.1.141.
May 12 12:06:07 dev mosquitto[2316]: Socket read error on client (null), disconnecting.


j0hncc wrote:
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
User avatar
By viktak
#17228 Actually, using the logs I found out (after some searching on the internet, that the mosquito version on RPi is not compatible with the one I am using on the ESP. Following instructions on http://mosquitto.org/2013/01/mosquitto- ... epository/ solved the problem. Now my code works on my private MQTT broker too.

Thanks for everyone chipping in!