I can successfully get it to boot up, get on wifi, and run my script to send the temp on the first try. That works, phant receives the temp, all is well.
HOWEVER, if I throw all of that code into any type of a loop or try and put any delay in there, either a) the connection won't get created and the data won't send, and/or b) I end up running out of memory.
Help?
init.lua
function startup()
print('in startup')
dofile('sendtemp.lua')
end
wifi.setmode(wifi.STATION)
wifi.sta.config("mywifi","mypassword")
wifi.sta.connect()
tmr.delay(1000000) -- wait 1,000,000 us = 1 second
tmr.alarm(1,1000, 1, function() if wifi.sta.getip()==nil then print("Waiting for IP address!") else print(wifi.sta.status()) print(wifi.sta.getip()) tmr.alarm(0,5000,0,startup) tmr.stop(1) end end)
sentemp.lua
require("phant")
hostName = "192.168.0.108"
port = 8087
publicKey = "key"
privateKey = "key"
phant.init(hostName .. ":" .. port, publicKey, privateKey)
print("Reading Temp")
voltage = adc.read(0)
mV = voltage * (3300/1024)
tempC = (mV - 500) / 10
tempF = (tempC * 9 / 5) + 32
phant.add("temp", tempF)
conn = net.createConnection(net.TCP, 0)
conn:on("connection", function(conn, payload)
print("Connected")
conn:send(phant.post())
end)
conn:on("sent", function()
print("Sent")
end)
conn:connect(port, hostName)
On the first run, I will get this result:
Reading Temp
> Connected
Sent
If I change my init.lua to this, though:
function startup()
print('in startup')
while ( true)
do
dofile('sendtemp.lua')
tmr.delay(1000000)
end
end
wifi.setmode(wifi.STATION)
wifi.sta.config("mywifi","mypassword")
wifi.sta.connect()
tmr.delay(1000000) -- wait 1,000,000 us = 1 second
tmr.alarm(1,1000, 1, function() if wifi.sta.getip()==nil then print("Waiting for IP address!") else print(wifi.sta.status()) print(wifi.sta.getip()) tmr.alarm(0,5000,0,startup) tmr.stop(1) end end)
I just get this result:
Reading Temp
Reading Temp
Reading Temp
Reading Temp
Reading Temp
Reading Temp
PANIC: unprotected error in call to Lua API (not enough memory)
PANIC: unprotected error in call to Lua API (attempt to call a string value)
and my temps are never sent to phant. Similarly if I try and put the temp read/send code in my sendtemp.lua in any kind of a loop, I get the same issue.
Any help is appreciated.
Thanks,
Andy