-->
Page 11 of 17

Re: MQTT for NodeMCU Lua working NOW

PostPosted: Tue Jan 27, 2015 10:22 am
by bicli
Hello guys,

I am having a problem, where I hope somebody can help me out, since I am not an expert on Lua.
I put the following code into "init.lua" file

Code: Select allmqtt.Client("clientid", 120, "user", "password")
m:lwt("/lwt", "offline", 0, 0)
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.4.2", 1984, 0, function(conn) print("connected") end)
m:subscribe("hello_world",0, function(conn) print("subscribe success") end)
m:publish("test_publish","hello",0,0, function(conn) print("sent") end)


But when I restart the ESP8266, console says:
lua: init.lua:2: attempt to index global 'mqtt' (a nil value)

What am I doing wrong here?

PS: I flashed the device with ESP8266Flasher.exe from https://github.com/nodemcu/nodemcu-flasher
Thank you

Re: MQTT for NodeMCU Lua working NOW

PostPosted: Tue Jan 27, 2015 10:28 am
by Suxsem
You forgot the m= in first line :)
Code: Select allm = mqtt.Client("

Re: MQTT for NodeMCU Lua working NOW

PostPosted: Tue Jan 27, 2015 10:37 am
by bicli
Oh that is a copy paste mistake of mine. It should have been:
Code: Select allm = mqtt.Client("clientid", 120, "user", "password")
m:lwt("/lwt", "offline", 0, 0)
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.4.2", 1984, 0, function(conn) print("connected") end)
m:subscribe("hello_world",0, function(conn) print("subscribe success") end)
m:publish("temperature","hello",0,0, function(conn) print("sent") end)

And the error is about 'mqtt', but what actually doesn't he like about it?

Re: MQTT for NodeMCU Lua working NOW

PostPosted: Tue Jan 27, 2015 11:38 am
by cdrwolfe
Double check your version is the latest one or is the one with the mqtt module.

I had the same problem and the result was my firmware didn't have the module.

Regards

Cdr