As the title says... Chat on...

User avatar
By carlhesp
#7570 Hi

I get the error when trying to dofile on a lua script which just has a function defined.
error: stdin:1: attempt to index global 'senddata' (a nil value)
What's the usual cause for this?

code works from inside the function if I copy and paste it.

Code: Select allfunction sendData()
   conn=net.createConnection(net.TCP, 0)
   conn:on("receive", function(conn, payload) print(payload) end)
   conn:connect(5050,'192.168.1.8')
   conn:send("add|"..t.read().."|ceiling\n")
   conn:close()
end
User avatar
By carlhesp
#7595 Of course! Thank you I did not notice. The problem was in init.lua triggering that function.

However now my problem is this.

my function very similar to above

Code: Select allfunction sendData()
   conn=net.createConnection(net.TCP, 0)
   conn:connect(5050,'192.168.1.8')
   conn:send("add|22.22|ceiling\n")
   conn:close()
end


when I run this function I get no TCP connection to my nodejs service. If i copy and paste that chunk of code into LuaLoader I see a TCP connection opened and the correct string come through. Any ideas on this? If i add some print lines into the function I see it output to the console when running the function.

Cheers
User avatar
By ThomasW
#7598
carlhesp wrote:Of course! Thank you I did not notice. The problem was in init.lua triggering that function.

However now my problem is this.

my function very similar to above

Code: Select allfunction sendData()
   conn=net.createConnection(net.TCP, 0)
   conn:connect(5050,'192.168.1.8')
   conn:send("add|22.22|ceiling\n")
   conn:close()
end


when I run this function I get no TCP connection to my nodejs service. If i copy and paste that chunk of code into LuaLoader I see a TCP connection opened and the correct string come through. Any ideas on this? If i add some print lines into the function I see it output to the console when running the function.

Cheers

You are sending without being sure that the connection is made and closing without giving the tcp-stack a chance to actually send the data out. Move send() and close() into callbacks - here is an example:
viewtopic.php?f=24&t=1283#p7500

Thomas