First of all I'm using the following:
- ESP8266 (ESP12E)
- Default firmware from Git (built with linux with all pertinent configs, according with nodemcu documentation)
- ESPlorer IDE
My specific problem (I think) is that I can't connect my MQTT client to a adafruit.io broker. I think I've successfully connected to WiFi (confimed by two separate SSIDs). The issue comes when creating the MQTT client AND connecting it to its broker.
-- INITIAL DEFINES
-- defines
station_cfg={}
station_cfg.ssid = "<my wifi>" -- my personal ssid
station_cfg.pwd = "<wifi pwd>" -- my wifi pwd
--station_cfg.ssid = "IoT" -- campus' ssid
--station_cfg.pwd = "<wifi pwd>" -- campus' wifi pwd, I tried this first, then my home's. It
worked as it should
station_cfg.auto = false
station_cfg.save = false
mqtt_client_cfg = {}
mqtt_client_cfg.clientid = "alro" -- any ID
mqtt_client_cfg.keepalive = 120 -- went for the example's value
mqtt_client_cfg.username = "AlvaRocha" -- obviously a paranoic Ctrl+C/V from adafruit
mqtt_client_cfg.password = "aio_KO<safety edit>sXwbgtWCboCal" -- obviously a paranoic Ctrl+C/V
-- from adafruit
wifi.setmode(wifi.STATION)
wifi.sta.config(station_cfg)
wifi.sta.connect(connect) -- so far so good
iot_test = mqtt.Client(mqtt_client_cfg) -- open client, apparently OK
--iot_test:lwt("/lwt", "offline", 0, 0) -- this line is on examples, doesn't affect outputs
iot_test:on("connect", function(client) print("client connected") end)
iot_test:on("offline", function(client) print("client offline") end) -- this event is called
-- always (line 27)
function connect(params)
print('Connected to:', params.SSID) -- I understant from documentation that this is called IF
-- successfull wifi connection. Double checked with
-- campus' and home wifi
end
function disconnect(params)
print('Disconnected from:', params.SSID) -- ignore this function
end
function get_broker(mqtt_client)
mqtt_client:connect("io.adafruit.com", 1883, false, false, --Found this double 'false' on
--https://www.electronicwings.com/nodemcu/nodemcu-
--mqtt-client-with-esplorer-ide
function(client) -- CONNECTED CALLBACK
print('connected to broker')
--break
-- continue
client:subscribe("/Test", 0, function(client) print("Succesfull sub.") end) -- (LINE 42)
--break
--- continue
end,
function(_reason) -- OFFLINE CALLBACK not called (called if I don't write the 'double false'
-- from the example)
print('connection failed', reason)
end)
end
get_broker(iot_test) -- connect to broker
ISSUE: even though line 27 is called, CONNECTED CALLBACK is called. No consistency there.
ISSUE: Line 42 output : PANIC: unprotected error in call to Lua API (init.lua:42: not connected)
Hoping the comments are helpfull, I want to state the obvious:
- This is my first time using ESP8266, LUA and MQTT
- I read as much as I felt comfortable with (you can never read enough)
- I don't like Arduino IDE (but I'll try it as this gets answered)
I suspect the following:
- I messed up somewhere at firmware config.
- WiFi is not really connected.
- I'm missing something obvious.
Best regards, Alvaro R.