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
-- 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
-- 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...
Attempting 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