1st post here, thank u for helping...
I have been playing a while with the tiny beast and I am having some strange behaviour with sockets. Maybe it's something trivial but I am not able to sort it out.
I am trying to set up a client (esp) to server (pc) connection to log some data.
I wrote a couple of files, a simple init.lua and a mycode.lua which contains a couple of functions.
If I run the code interactively from ESPlorer everything works fine, and I am able to connect to the server, send & receive data etc.
But if I run the same code in "run mode" e.g. as it would do by itself, the client does not connect: the "connect" event is never fired, and I guess that this is the reason why data are not sent to server. If at the end of code execution i run the same TcpSend function interactively it works just fine...
Btw, I am using functions here because otherwise IP address is not assigned to the esp, again only if running in run mode; again, interactively everything is just fine.
Any implicit but obvious bit I am losing?
thanks a lot for helping... code is posted below.
>>> init.lua:
--Startup file--
NextFile="RemoteSensor.lua"
l = file.list();
for k,v in pairs(l) do
print("name:"..k..", size:"..v)
if k == NextFile then
print("Wait 5 seconds please")
tmr.alarm(0, 5000, 0, function() dofile(NextFile) end)
print("Started file ".. NextFile)
else
-- do nothing
end
end
>>> remotesensor.lua
-- remote unit
function SendTcp(srv_port,srv_ip,msg)
sk=net.createConnection(net.TCP,0)
sk:on("receive", function(sck, c) print("server said: " .. c) end )
sk:on("connection", function(conn, payload)
print("connected ")
end)
sk:on("disconnection", function(conn, payload) end)
sk:connect(srv_port,srv_ip)
sk:send(msg)
print("payload: ".. msg)
--sk:close()
end
function RemoteSensorFunction ()
second=1000000
-- at wakeup wait & check for network to be available
print("wake up, Neo...")
wifi_status=0
tmr.delay(5*second)
wifi_status=wifi.sta.status()
print("status: " .. wifi_status)
-- check for STATION_GOT_IP
if wifi_status ~= 5 then
print("no network - no party...")
-- add action to turn to standby
else
print ("network available.")
myip,mymask,mygw=wifi.sta.getip()
print("ip address: " .. myip)
end
-- prepare message; clever format...
msg = "prova"
-- open socket and send data
SendTcp(10000,"192.168.0.101",msg)
-- now go to deep sleep
print("back to sleep")
--node.dsleep(dsleep_time*second)
end
RemoteSensorFunction ()