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

User avatar
By Toshi Bass
#9266 Hi I am new to lua and I have the following script which successfully controls a couple of relays, I am trying to add a delay to make it like pulse the relays, so On delay Off ok as it is the script works fine for a small delay but I want a much longer delay no problem there just increase the tmr .delay() but I need some help because I want to send the callback - conn:send(output) which is the new state of vars sw5 & sw6 without having to wait until the delay is complete or in fact even started but I cannot figure out were to put the delay code I tried in positions -- x -- y -- z but either the delay still completes before the callback is sent or the delay is never activated

note* if the payload is nothing then the current script just sends the current state of vars sw5 & sw6 which is exactly what I want, but at the moment the delay still slows up the callback.

Any help would be appreciated, thanks

pin=0
value=0
sw5,sw6=0,0
srv=net.createServer(net.TCP)
srv:listen(8080,function(conn)
conn:on("receive",function(conn,pl)
--add gpio's pin numbers like {3,4,5} to switch more pins
for v,i in pairs{5,6} do
gpio.mode(i,gpio.OUTPUT)
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_write = gpio.write
if string.find(pl,"gpio"..i.."=0") then gpio_write(i,1) pin=i value=0 end
if string.find(pl,"gpio"..i.."=1") then gpio_write(i,1) pin=i value=1 end
if pin==5 then sw5=value end
if pin==6 then sw6=value end

-- pulse delay
tmr.delay(3000000)
gpio_write(pin,0)

end
output = (sw5..","..sw6)
print("Send-Responce: ",output)
conn:send(output)
conn:on("sent",function(conn) conn:close() end)
-- x
end)
-- y
end)
User avatar
By dpwhittaker
#10460 Tmr.delay does not continue unril the time is up. Instead of:

Code: Select alltmr.delay(3000000)
gpio_write(pin,0)


You want:

Code: Select alltmr.alarm(0,3000,0,function() gpio_write(pin,0) end)


This sets an alarm to come back 3 seconds from now and execute the function, but the rest of the code around it continues immediately.