Chat freely about anything...

User avatar
By bgowland
#45608 I have an MQTT broker using mosquitto on a Raspberry Pi but I'm have real trouble getting my ESP8266 (GizWits model) to talk to it.

The broker works fine - I can publish / subscribe using apps on my Android phone and tablet so that side of things is working OK.

Trying to send some basic test info from the ESP8266 however, simply isn't working - I don't even get to the point of connecting to be able to publish anything.

If anyone can give me an idea of what's wrong I'd really appreciate it. Code as follows...

config.lua file
Code: Select all-- config.lua file

--- Placeholders ---
temperature = 0
humidity = 0

--- MQTT ---
mqtt_broker_ip = "192.168.0.50"
mqtt_broker_port = 1883
mqtt_username = ""
mqtt_password = ""
mqtt_client_id = "esptest"

--- WIFI ---
wifi_SSID = "MySSID"
wifi_password = "MyPassword"
wifi_signal_mode = wifi.PHYMODE_G


user.lua file
Code: Select all-- user.lua
dofile("config.lua")

function publishData(humidity,temperature)
    -- Setup MQTT client and events
    print("publishData entered")
    payload = "Temp: " .. temperature .. " Humidity: " .. humidity
    print("Payload: " .. payload)

    print("Setting up mqtt.Client...")
    m = mqtt.Client(mqtt_client_id, 120, mqtt_username, mqtt_password)

    print("Attempting client connect...")
    m:connect(mqtt_broker_ip, mqtt_broker_port, 0, function(client) print("connected") end,
                                     function(client, reason) print("failed reason: "..reason) end)
    print("Exiting publishData...")
end

-- Connect to network
wifi.setmode(wifi.STATION)
wifi.setphymode(wifi_signal_mode)
wifi.sta.config(wifi_SSID, wifi_password)
wifi.sta.connect()

function Failure()
    print("In Failure function")
    tmr.stop(0)
    return 0
end

-- Watchdog Loop   
tmr.alarm(0, 10000, 0, function() Failure() end)
print()
tmr.alarm(1, 1000, 1, function()
    print("Attempting to connect...")
    ip = wifi.sta.getip()
    if ip ~= nil then --wait for proper IP
        print("Got IP: " .. ip)
        tmr.stop(0)                   
        tmr.stop(1)
        print("About to call publishData...")
        publishData(humidity, temperature)
        print("Returned from publishData...")
    end
end)


When I run user.lua I see the following logged in ESPlorer...

Code: Select allAttempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Got IP: 192.168.0.39
About to call publishData...
publishData entered
Payload: Temp: 0 Humidity: 0
Setting up mqtt.Client...
Attempting client connect...
Exiting publishData...
Returned from publishData...


Basically it seems the result of m:connect(...) is calling neither of the callbacks for a successful or unsuccessful connection and I don't understand why.

The code was more complicated than what I've posted but I trimmed it right back for testing and it still wont connect - more accurately neither of the callbacks are triggered which is very odd. Any ideas?

Cheers,
Brian
User avatar
By lord_alan
#45626 Hi Brian,

In your code for the user.lua file you define a payload but you never actually use it ;-)
I can't see a mqtt.publish() call and also I think you probably need to define a topic path for MQTT as well (even if it's just "/".

See my function here:

http://paste.ubuntu.com/15888129/

If this still fails, can you also post your mosquito configuration.

Try enabling the debug log for mosquito and see if you are getting any traffic at all published.
User avatar
By bgowland
#45636 Hi Alan,

lord_alan wrote:In your code for the user.lua file you define a payload but you never actually use it ;-)
I can't see a mqtt.publish() call and also I think you probably need to define a topic path for MQTT as well (even if it's just "/".

See my function here:

http://paste.ubuntu.com/15888129/
Thanks - the code I posted is the minimalist version just to see if I could get a "connected" or "failed - reason" type of log message to the ESPlorer terminal window. I've tried publishing with valid topic too but it never gets to that code.

lord_alan wrote:Try enabling the debug log for mosquito and see if you are getting any traffic at all published.
Thanks again, I worked out how to enable mosquitto logging and I see the following...

Apr 17 14:05:00 raspberrypi mosquitto[2272]: New connection from 192.168.0.39.
Apr 17 14:05:00 raspberrypi mosquitto[2272]: Invalid protocol "MQTT" in CONNECT from 192.168.0.39.
Apr 17 14:05:00 raspberrypi mosquitto[2272]: Socket read error on client (null), disconnecting.


Searching for that error message, it seems there can be mqtt version mismatches between clients and brokers. In one post I saw that v0.15 of the linux mosquitto was described as a "bad" version...guess what I see in my syslog after a reboot...
Apr 17 14:01:03 raspberrypi mosquitto[2272]: mosquitto version 0.15 (build date 2013-01-09 05:15:05+0000) starting


Not sure if that's the answer yet. The version of Raspian on my RPi is quite old and I got mosquitto using apt-get. I'll download a new version of Raspian and see if I can get a newer version of mosquitto.

Cheers,
Brian
User avatar
By bgowland
#46630 Just to close this issue...

It did turn out to be a problem with the mosquitto version running on the RPi. I had to download a new version of Raspian at which point I was able to use apt-get to pull a version of mosquitto compatible with MQTT v3.1.1. Nothing wrong with my code after all. Oh well.

Cheers,
Brian