I am playing with mqtt and have some problems. I assume that I do not properly understand the mqtt module or how lua runs from a file.
I uploaded a file ‘t’:
m = mqtt.Client("esp-07", 600);
m:connect("192.168.2.7", 1883, 0, function(conn) print("connected") end);
m:publish("stats/temperature/esp-07","30C",0,1,function(conn) print("sent 30C") end);
m:close();
I first ran the above commands individually from a terminal:
> m = mqtt.Client("esp-07", 600);
> m:connect("192.168.2.7", 1883, 0, function(conn) print("connected") end);
> connected
m:publish("stats/temperature/esp-07","30C",0,1,function(conn) print("sent 30C") end);
> sent 30C
m:close();
> m=nil
As one can see, it worked and the correct messages were logged.
In another terminal I was running a ‘sub’ and got the expected value:
$ mosquitto_sub -i e4 -h 192.168.2.7 -v -t 'stats/temperature/esp-07'
stats/temperature/esp-07 30C
I now ran the same commands from the file:
> dofile("t")
>
No messages here and none showing on the ‘sub’ terminal.
I now manually did:
> m:connect("192.168.2.7", 1883, 0, function(conn) print("connected") end);
> sent 30C
connected
Interestingly, not only did I get the expected ‘connected’, but the older missing 'sent’
message now showed up. The mqtt server did not show a new published value.
I proceeded and it still works OK from the command line:
m:publish("stats/temperature/esp-07","30C",0,1,function(conn) print("sent 30C") end);
> sent 30C
m:close();
> m=nil
>
This time a new published value showed up as expected in the ‘sub’ terminal.
Inserting delays between the commands in the file made no difference.
Inserting 'print ("…")'s in the file does show, so I conclude the statements are executed.