Post your best Lua script examples here

User avatar
By fahimhaider
#49022 Dear all
I write a code for reading the adc value and send it to the web. but i am facing below mention issue
"PANIC: unprotected error in call to Lua API (main.lua:39: attempt to concatenate a function value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a userdata value)
PANIC: unprotected error in call to Lua API (attempt to call a string value)"


Below are my code
function adc_avg()
val=0
i=0
while(i<20)do
val=val+adc.read(0) -- for averaging add the prvious value in val
-- print("Non average adc:",val)
i = i+1
end
val = val/20
-- print("Avrage ADC Value:",val)
return adc_avg
end

tmr.alarm(0, 5000, 1, function() -- 5sec for get the ip address for router
add = wifi.sta.getip()
if(add == nil) then
print("Connecting to AP...\n")
else
ip, nm, gw=wifi.sta.getip()
print("IP Info: \nIP Address: ",ip)
print("Netmask: ",nm)
print("Gateway Addr: ",gw,'\n')
tmr.stop(0)
end
end)


-- Start a simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
-- adc_avg()
-- value=adc_avg()
conn:send("<h1>HELLO.PVT!!! </h1>")
conn:send('<h2>(ESP8266-12 ADC)</h2>')
conn:send('<p>ADC Value = '..adc_avg()..'</p><br>')
end)


Regards
Fahim Haider
User avatar
By 8n1
#49025 You are returning the function itself instead of the averaged adc value in adc_avg().
User avatar
By fahimhaider
#49058
8n1 wrote:You are returning the function itself instead of the averaged adc value in adc_avg().

Please give me the solution what should i do to resolve this issue