0

I'm trying to test programs with a concept of Internet of Things. I got this simple mqtt lua's client. It works fine after flashing my devkit nodemcu v2, but when i restart it or i save it again ESPlorer says:

test.lua:2: attempt to call field 'Client' (a nil value) 

Code: Source: https://www.cloudmqtt.com/docs-nodemcu.html

-- initiate the mqtt client and set keepalive timer to 120sec
mqtt = mqtt.Client("client_id", 120) -- "username", "password")

mqtt:on("connect", function(con) print ("connected") end)
mqtt:on("offline", function(con) print ("offline") end)

-- on receive message
mqtt:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)

mqtt:connect("IP", 1883, 0, function(conn) 
print("connected")
-- subscribe topic with qos = 0
mqtt:subscribe("/topic",0, function(conn) 
-- publish a message with data = my_message, QoS = 0, retain = 0
 mqtt:publish("/topic","hello",0,0, function(conn) 
  print("sent") 
end)
end)
end)

Is it possible that this client can work anytime? What am i missing? Right now i can only communicate with broker after saving script on flashed device.

  • 1
    Line 2 replaces `mqtt` with whatever `mutt.Client()` returns, so it cannot be run a second time. – lhf Apr 14 '17 at 11:58
  • Did you go through the module selection process documented [here](https://nodemcu.readthedocs.io/en/latest/en/build/#select-modules) when you built your firmware? You need to make sure the MQTT module is enabled. While it's poor practice to overwrite a library in the way this code does, I don't think it will cause the error you're seeing. – Perry Apr 14 '17 at 23:06
  • I tried with custom firmware but it did not help in my case. Anyway i found another mqtt client. The code is similar and it works without any problems. Thanks for comments guys. – AidanSalvatore Apr 18 '17 at 14:21

0 Answers0