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.